-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseNNArgs.py
831 lines (625 loc) · 26.1 KB
/
parseNNArgs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
import sys
import os
import json
import getopt
# parse command line arguments
def parseArgs():
params = parse(sys.argv[1:])
try:
if os.environ['IS_INTERACTIVE'] == 'true':
params['version'] = 'tmp'
except KeyError:
pass
print(params, params['version'])
return params
def parse(argv):
params = dict()
#___________________________________________________________
# General Parameters for Classifier and Agent
#___________________________________________________________
# root dir for output
params['root'] = None
# maximum number of training steps
params['numTrainSteps'] = 1000
# size of mini batch
params['miniBatchSize'] = 16
# specify a seed for random number generator, uses random seed if None
params['seed'] = None
# a version string , for reference
params['version'] = '1'
# use dropout in networks
params['dropout'] = False
# DATA PRE-PROCESSING
# Randomly flips images horizontally and vertically
params['distorted'] = False
params['distortBrightnessRelative'] = 0.0
params['distortContrast'] = 0.0
params['distortGaussian'] = 0.0
params['distortSaltPepper'] = 0.0
# normalization factor used to scale images before they are fed into networks
# applies to training data for classifier and to images recorded by agent
params['normFactorTrain'] = None
# TRAINING PARAMETERS
# which optimizer to use: 'sgd', 'momentum', or 'adam'
params['optimizer'] = 'momentum'
params['learning-rate'] = 0.1
params['lr-decay'] = False
params['momentum'] = 0.9
params['mom-decay'] = False
params['weight-decay'] = 0.0
# threshold for classification into good or bad probe
params['threshold'] = 0.5
# there will be a pooling operation after each of the first X levels of the cnn
params['numPool'] = None
# parameter for batchnorm
params['batchnorm-decay'] = 0.9
# size of images to be processed by the networks in pixels
params['pxRes'] = 64
# path to a classifier network you want to load
params['classNN'] = None
# controls how much info is printed
params['verbose'] = False
params['veryverbose'] = False
params['veryveryverbose'] = False
# use CPU only, very slow
params['noGPU'] = False
# ___________________________________________________________
# Parameters for the Classifier
# ___________________________________________________________
# path to training data
params['in_dir'] = None
# don't split the dataset into train and val, the validation set is specified
# separately using --valInDirX (and normFactorValX)
params['allForTrain'] = False
# paths to validation datasets
params['valInDir1'] = None
params['valInDir2'] = None
params['valInDir3'] = None
params['valInDir4'] = None
params['valInDir5'] = None
params['valInDir6'] = None
# factors used to normalize the validation datasets
params['normFactorVal1'] = None
params['normFactorVal2'] = None
params['normFactorVal3'] = None
params['normFactorVal4'] = None
params['normFactorVal5'] = None
params['normFactorVal6'] = None
# raw input data is supplied as one image per file
# for faster resuming/evaluation it is, after loading it the first time,
# stored as one numpy binary blob.
# you can copy these blobs to a separate location and use them as input
# for subsequent experiments by setting this parameter to the path
# to the containing directory
params['loadBlob'] = None
# use batchnorm in classifier network
params['batchnorm'] = False
# defines the vgg-style network,
# for the cnn part specifies the number of feature maps in each level,
# each convolution is repeated three times
# for the fully connected part specifies the number of neurons
params['cnnSetSizes'] = "32,64,96"
params['fcSizes'] = "2048,2048"
# extra factor to penalize false positives harder
params['penalizeFP'] = False
# extra factor to increase the weight of positive/good samples in the loss
params['relWeightPosSamples'] = None
# path to validation data used by the evaluation scripts
# by setting validationData the files are loaded from scratch
# by setting loadValBlob precomputed numpy binary blobs are used
# (analogue to loadBlob (see above))
params['validationData'] = None
params['loadValBlob'] = None
# preload all data into gpu memory
# (recommendation: True (no augmentation if set to False))
params['preload'] = True
# use 'area under the curve' instead of cross entropy EXPERIMENTAL
params['aucLoss'] = False
# read only image file names that end in this suffix
# if 'None' this is set to the string "Default"
params['suffix'] = None
# ___________________________________________________________
# Parameters for the Agent
# ___________________________________________________________
# CONNECTION TO SERVER
# the address of the SPM server you want to connect to
params['host'] = "localhost"
# the port at which you want to connect
params['port'] = 50008
# We start a dummy server pretending to be an STM.
# This will automatically activate the 'dummyServer' option.
params['startServer'] = False
# A dummy server will give random syntactically correct answers.
# automatically activated by 'startServer'
params['dummyServer'] = False
# use together with 'startServer' and 'dummyServer'
# the dummy server will read images in this directory and return
# them when asked to scan an area
params['dummyImageDir'] = None
# SELECTION OF NEXT SCANNING REGION
# determines how the next scanning region is selected
# 'closest' will find a compromise between staying close to the center and the last region
# 'simple' will use a fixed spiral
params['spiral'] = "closest" # "simple"
# regularization factor for selecting the next region
# large value leads to more regular path
params['spiralReg'] = 1.0
# Maximum allowed movement between to images in nm.
# If the required movement is larger, the approach area is changed
params['maxMovement'] = 500.0
# Size of the gridcells used for motion planing in nm
params['cellSize'] = 10.0
# size of scanning region in nanometer
params['sizeNano'] = 10
# threshold for the difference between max and min values in the image.
# If this is exceeded, the region is considered to contain excessive roughness.
params['debrisThreshold'] = 6
# Safety margin to subtract from approach area in nm
params['margin'] = 0.0
# PROBE CONDITIONING
# path to .csv describing possible conditioning actions
params['actionFile'] = None
# Threshold for finding an empty area in nm
# determines how close a pixel value has to be to the plane to be considered empty
params['ThEmptyArea'] = 0.01
# Use difference of Gaussians blob detector to find empty spot. EXPERIMENTAL
params['emptySpotDOG'] = False
# A parameter for when using 'emptySpotDOG'
params['emptySpotPlaneMinSz'] = 0
# Should we reset (destroy) the tip at the beginning?
params['initDestroy'] = False
# CNN PARAMETERS
# defines the vgg-style network,
# for the cnn part specifies the number of feature maps in each level,
# each convolution is repeated three times
# for the fully connected part specifies the number of neurons
params['cnnSetSizesDQN'] = "32,64,96"
params['fcSizesDQN'] = "2048,2048"
# use batchnorm in agent network
params['batchnormDQN'] = False
# Smartly initialize output neurons
params['outputInitialization'] = False
# REINFORCEMENT LEARNING
# hard or soft update of target dqn
params['noHardResetDQN'] = False
# factor for soft update
params['tau'] = None
# frequency of hard update
params['resetFreq'] = None
# RL discount factor
params['gamma'] = 0.99
# get [q]values of target network for actions where main network is max
# (recommendation: True)
params['doubleDQN'] = False
# use DQN with the dueling network extension
# (recommendation: False)
params['duelingDQN'] = False
# Should target network update (batchnorm) moving_mean/variance using tau
# or use batch statistics
params['updateTargetBNStatsWithTau'] = False
# choose a reward scheme
# 'stepF': 'rewardPos' after each step, 'rewardFinal' when episode is finished
# 'step': 'rewardPos' after each step
# 'cl': 'rewardPos' if classifier output improved, 'rewardNeg' else, 'rewardFinal' when episode is finished
# 'sCl': soft version of 'cl'
# 'sClSuM': another soft version of 'cl'
params['reward'] = None
params['rewardPos'] = None
params['rewardNeg'] = None
params['rewardFinal'] = None
# EXPLORATION AND LEARNING
# the number of conseq. times a bad probe has to be detected by classifier,
# before a probe shaping episode is initiated
# IMPORTANT: set this to 'None' for training the agent.
# if 'None' the probe will be reset (destroyed) after each successful episode.
# Set it to small positive number during deepSPM operation.
params['maxBadImgCount'] = None
# number of initial completely random episodes
params['randomEps'] = 100
# start learning if at least x elements in buffer
params['startLearning'] = 200
# max number of steps in episode
params['stepsTillTerm'] = 100
# anneal epsilon linearly in X steps
params['annealSteps'] = 20000
# start linear decay of epsilon at
params['epsilonStart'] = 0.95
# stop linear decay at this; epsilon is constant afterwards
params['epsilonStop'] = 0.0
# only learn, no exploration
params['onlyLearn'] = False
# size of replay buffer
params['replaySz'] = None
# stop if buffer full
params['termAtFull'] = False
# maximum number of probe conditioning episodes
params['numEpisodes'] = 1000
# use Huber loss instead of mean square error
params['huberLoss'] = False
# this option is for evaluating the performance of the agent
# if activated, it will alternate between using the agent and a random selection of tip shaping actions
params['interEval'] = False
# EXPERIMENTAL
# Use these two only together and together with 'interEval'.
# This will alternate between a 'fixedAction' (provide index)
# and 'agentB' (provide path)
params['fixedAction'] = 0
params['agentB'] = None
# perform pure evaluation episodes every 25 episodes
# DEPRECATED
params['evaluation'] = False
# LOADING AND STORING
# path to an action network we want to load
params['dqnNN'] = None
# use x network for agent init
params['useClassNN'] = False
# load old replay buffer
params['loadReplay'] = None
# load an old model
params['loadModel'] = None
# periodically store tensorflow action network model
params['storeModel'] = False
# periodically store replay buffer
params['storeBuffer'] = False
params['restoreApproachAreaMask'] = None
# we are resuming a run that was previoulsy stoped
params['resume'] = False
# keep only newest n saved dqn models to save disc space; delete older ones
params['keepNewestModels'] = 200
# learning/exploration is performed in different threads, RECOmMENDED
params['async'] = False
# Make the exploration thread ssleep from time to time to simulated a slow microscope
params['sleep'] = False
# use with 'sleep'. specify a time one iteration should take
params['sleepA'] = None
# We pause exploring when the buffer is larger than this value, DEPRECATED
params['limitExploring'] = None
# IMAGE PRE-PROCESSING
# Apply RANSAC plane fitting and subtraction
params['RANSAC'] = True
#clip value to [-1.5,1.5] after normalization and plane subtraction
params['clip'] = True
# each experience is inserted 16 times into buffer
# (rotating and mirroring)
params['fullAugmentation'] = False
try:
print(argv)
opts, args = getopt.getopt(argv, "i:",
['seed=',
'resume=',
'root=',
'version=',
'miniBatchSize=',
'numEpisodes=',
'numTrainSteps=',
'distorted',
'distortBrightnessRelative=',
'distortContrast=',
'distortGaussian=',
'distortSaltPepper=',
'dropout=',
'learning-rate=',
'lr-decay',
'momentum=',
'mom-decay',
'optimizer=',
'penalizeFP',
'relWeightPosSamples=',
'weight-decay=',
'noPreload',
'suffix=',
'batchnorm',
'batchnormDQN',
'batchnorm-decay=',
'threshold=',
'cnnSetSizes=',
'fcSizes=',
'cnnSetSizesDQN=',
'fcSizesDQN=',
'numPool=',
'allForTrain',
'valInDir1=',
'valInDir2=',
'valInDir3=',
'valInDir4=',
'normFactorTrain=',
'normFactorVal1=',
'normFactorVal2=',
'normFactorVal3=',
'normFactorVal4=',
'validationData=',
'doubleDQN',
'duelingDQN',
'noHardResetDQN',
'tau=',
'resetFreq=',
'loadLevel=',
'reward=',
'rewardPos=',
'rewardNeg=',
'rewardFinal=',
'gamma=',
'randomEps=',
'stepsTillTerm=',
'startLearning=',
'async',
'sleep',
'sleepA=',
'limitExploring=',
'annealSteps=',
'classNN=',
'useClassNN',
'dqnNN=',
'blob=',
'valBlob=',
'replaySz=',
'termAtFull',
'loadReplay=',
'loadModel=',
'storeModel',
'model=',
'storeBuffer',
'onlyLearn',
'sizeNano=',
'pxRes=',
'evaluation',
'verbose',
'veryverbose',
'veryveryverbose',
'noGPU',
'startServer',
'host=',
'port=',
'spiral=',
'restoreApproachAreaMask=',
'actionFile=',
'noRANSAC',
'noClip',
'emptySpotDOG',
'emptySpotPlaneMinSz=',
'updateTargetBNStatsWithTau',
'fullAugmentation',
'outputInitialization',
'epsilonStart=',
'epsilonStop=',
'huberLoss',
'aucLoss',
'keepNewestModels=',
'interEval',
'fixedAction=',
'agentB=',
'maxBadImgCount=',
'dummyServer',
'debrisThreshold=',
'dummyImageDir=',
])
print(opts, args)
except getopt.GetoptError as err:
print('args parse error')
print('args: ', argv)
print(err)
exit()
for opt, arg in opts:
print("+++++++")
print(opt, arg)
if opt == '--resume':
params['resume'] = bool(int(arg))
elif opt == '--root':
params['root'] = arg
elif opt == '--seed':
params['seed'] = int(arg)
elif opt == '--version':
params['version'] = arg
elif opt == '--numTrainSteps':
params['numTrainSteps'] = int(arg)
elif opt == '--numEpisodes':
params['numEpisodes'] = int(arg)
elif opt == '--miniBatchSize':
params['miniBatchSize'] = int(arg)
elif opt == '--noPreload':
params['preload'] = False
elif opt == '--distorted':
params['distorted'] = True
elif opt == '--distortBrightnessRelative':
params['distortBrightnessRelative'] = float(arg)
elif opt == '--distortContrast':
params['distortContrast'] = float(arg)
elif opt == '--distortGaussian':
params['distortGaussian'] = float(arg)
elif opt == '--distortSaltPepper':
params['distortSaltPepper'] = float(arg)
elif opt == '--dropout':
params['dropout'] = float(arg)
elif opt == '-i':
params['in_dir'] = arg
elif opt == '--valInDir1':
params['valInDir1'] = arg
elif opt == '--valInDir2':
params['valInDir2'] = arg
elif opt == '--valInDir3':
params['valInDir3'] = arg
elif opt == '--valInDir4':
params['valInDir4'] = arg
elif opt == '--normFactorTrain':
params['normFactorTrain'] = arg
elif opt == '--normFactorVal1':
params['normFactorVal1'] = arg
elif opt == '--normFactorVal2':
params['normFactorVal2'] = arg
elif opt == '--normFactorVal3':
params['normFactorVal3'] = arg
elif opt == '--normFactorVal4':
params['normFactorVal4'] = arg
elif opt == '--suffix':
params['suffix'] = arg
elif opt == '--batchnorm':
params['batchnorm'] = True
elif opt == '--batchnormDQN':
params['batchnormDQN'] = True
elif opt == '--batchnorm-decay':
params['batchnorm-decay'] = float(arg)
elif opt == '--threshold':
params['threshold'] = float(arg)
elif opt == '--cnnSetSizes':
params['cnnSetSizes'] = arg
elif opt == '--fcSizes':
params['fcSizes'] = arg
elif opt == '--cnnSetSizesDQN':
params['cnnSetSizesDQN'] = arg
elif opt == '--fcSizesDQN':
params['fcSizesDQN'] = arg
elif opt == '--numPool':
params['numPool'] = int(arg)
elif opt == '--allForTrain':
params['allForTrain'] = True
elif opt == '--learning-rate':
params['learning-rate'] = float(arg)
elif opt == '--lr-decay':
params['lr-decay'] = True
elif opt == '--momentum':
params['momentum'] = float(arg)
elif opt == '--mom-decay':
params['mom-decay'] = True
elif opt == '--weight-decay':
params['weight-decay'] = float(arg)
elif opt == '--optimizer':
params['optimizer'] = arg
elif opt == '--penalizeFP':
params['penalizeFP'] = True
elif opt == '--relWeightPosSamples':
params['relWeightPosSamples'] = float(arg)
elif opt == '--doubleDQN':
params['doubleDQN'] = True
elif opt == '--duelingDQN':
params['duelingDQN'] = True
elif opt == '--noHardResetDQN':
params['noHardResetDQN'] = True
elif opt == '--tau':
params['tau'] = float(arg)
elif opt == '--resetFreq':
params['resetFreq'] = int(arg)
elif opt == '--validationData':
params['validationData'] = arg
elif opt == '--reward':
params['reward'] = arg
elif opt == '--rewardPos':
params['rewardPos'] = float(arg)
elif opt == '--rewardNeg':
params['rewardNeg'] = float(arg)
elif opt == '--rewardFinal':
params['rewardFinal'] = float(arg)
elif opt == '--gamma':
params['gamma'] = float(arg)
elif opt == '--randomEps':
params['randomEps'] = int(arg)
elif opt == '--stepsTillTerm':
params['stepsTillTerm'] = int(arg)
elif opt == '--startLearning':
params['startLearning'] = int(arg)
elif opt == '--async':
params['async'] = True
elif opt == '--sleepA':
params['sleepA'] = float(arg)
elif opt == '--sleep':
params['sleep'] = True
elif opt == '--limitExploring':
params['limitExploring'] = int(arg)
elif opt == '--annealSteps':
params['annealSteps'] = float(arg)
elif opt == '--classNN':
params['classNN'] = arg
elif opt == '--useClassNN':
params['useClassNN'] = True
elif opt == '--dqnNN':
params['dqnNN'] = arg
elif opt == '--blob':
params['loadBlob'] = arg
elif opt == '--valBlob':
params['loadValBlob'] = arg
elif opt == '--replaySz':
params['replaySz'] = int(arg)
elif opt == '--termAtFull':
params['termAtFull'] = True
elif opt == '--loadReplay':
params['loadReplay'] = arg
elif opt == '--loadModel':
params['loadModel'] = arg
elif opt == '--storeModel':
params['storeModel'] = True
elif opt == '--storeBuffer':
params['storeBuffer'] = True
elif opt == '--onlyLearn':
params['onlyLearn'] = True
elif opt == '--sizeNano':
params['sizeNano'] = int(arg)
elif opt == '--pxRes':
params['pxRes'] = int(arg)
elif opt == '--evaluation':
params['evaluation'] = True
elif opt == '--verbose':
params['verbose'] = True
elif opt == '--veryverbose':
params['veryverbose'] = True
elif opt == '--veryveryverbose':
params['veryveryverbose'] = True
elif opt == '--noGPU':
params['noGPU'] = True
elif opt == '--startServer':
params['startServer'] = True
params['dummyServer']= True
elif opt == '--host':
params['host'] = arg
elif opt == '--port':
params['port'] = int(arg)
elif opt == '--spiral':
params['spiral'] = arg
elif opt == '--restoreApproachAreaMask':
params['restoreApproachAreaMask'] = arg
elif opt == '--actionFile':
params['actionFile']= (arg)
elif opt == '--noRANSAC':
params['RANSAC'] = False
elif opt == '--noClip':
params['clip'] = False
elif opt == '--emptySpotDOG':
params['emptySpotDOG'] = True
elif opt == '--emptySpotPlaneMinSz':
params['emptySpotPlaneMinSz'] = int(arg)
elif opt == '--updateTargetBNStatsWithTau':
params['updateTargetBNStatsWithTau'] = True
elif opt == '--fullAugmentation':
params['fullAugmentation'] = True
elif opt == '--outputInitialization':
params['outputInitialization'] = True
elif opt == '--epsilonStart':
params['epsilonStart'] = float(arg)
elif opt == '--epsilonStop':
params['epsilonStop'] = float(arg)
elif opt == '--huberLoss':
params['huberLoss'] = True
elif opt == '--aucLoss':
params['aucLoss'] = True
elif opt == '--keepNewestModels':
params['keepNewestModels'] = int(arg)
elif opt == '--interEval':
params['interEval'] = True
params['initDestroy'] = True
params['dummyServer'] = True
elif opt == '--fixedAction':
params['fixedAction'] = int(arg)
elif opt == '--agentB':
params['agentB'] = arg
elif opt == '--maxBadImgCount':
params['maxBadImgCount'] = int(arg)
elif opt == '--dummyServer':
params['dummyServer']= True
elif opt == '--debrisThreshold':
params['debrisThreshold'] = float(arg)
elif opt == '--dummyImageDir':
params['dummyImageDir'] = arg
if params['veryveryverbose'] == True:
params['veryverbose'] = True
params['verbose'] = True
elif params['veryverbose'] == True:
params['verbose'] = True
return params