-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClassificationExperiment.py
307 lines (247 loc) · 12.4 KB
/
ClassificationExperiment.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
import sys,os,argparse
# Parse the Arguments
execfile("LArTPCDNN/ClassificationArguments.py")
# Process the ConfigFile
execfile(ConfigFile)
# Now put config in the current scope. Must find a prettier way.
if "Config" in dir():
for a in Config:
exec(a+"="+str(Config[a]))
# Use "--Test" to run on less events and epochs.
OutputBase="TrainedModels"
if TestMode:
MaxEvents=int(20e3)
NTestSamples=int(20e2)
Epochs=10
OutputBase+=".Test"
print "Test Mode: Set MaxEvents to",MaxEvents,"and Epochs to", Epochs
if LowMemMode:
n_threads=1
multiplier=1
# Calculate how many events will be used for training/validation.
NSamples=MaxEvents-NTestSamples
# Function to help manage optional configurations. Checks and returns
# if an object is in current scope. Return default value if not.
def TestDefaultParam(Config):
def TestParamPrime(param,default=False):
if param in Config:
return eval(param)
else:
return default
return TestParamPrime
TestDefaultParam=TestDefaultParam(dir())
# Load the Data
from LArTPCDNN.LoadData import *
TrainSampleList,TestSampleList=DivideFiles(FileSearch,[float(NSamples)/MaxEvents,float(NTestSamples)/MaxEvents],
datasetnames=[u'images'],
Particles=Particles)
# Figure out the output shape... This is not necessary. But the automatic mechanism is inefficient.
if ScanWindowSize>0:
# shapes=[(BatchSize*multiplier, 2, 240, ScanWindowSize), (BatchSize*multiplier, NClasses)]
shapes=[(BatchSize*multiplier, 240, ScanWindowSize),
(BatchSize*multiplier, 240, ScanWindowSize),
(BatchSize*multiplier, NClasses)]
viewshape=(None, 240, ScanWindowSize)
else:
shapes=[(BatchSize*multiplier, 240, 4096/DownSampleSize),
(BatchSize*multiplier, 240, 4096/DownSampleSize),
(BatchSize*multiplier, NClasses)]
viewshape=(None, 240, 4096/DownSampleSize)
def MakeGenerator(SampleList,NSamples,
cachefile="LArIAT-LoadDataTest-Cache.h5",**kwargs):
return DLMultiClassFilterGenerator(SampleList, FilterEnergy(EnergyCut), max=NSamples,
preprocessfunction=ProcessWireData(DownSampleSize,ScanWindowSize,Normalize),
postprocessfunction=MergeInputsInception(),
batchsize=BatchSize,
shapes=shapes,
n_threads=n_threads,
multiplier=multiplier,
cachefile=cachefile,
**kwargs)
# Use DLGenerators to read data
Train_genC = MakeGenerator(TrainSampleList, NSamples,
cachefile="/tmp/LArTPCDNN-LArIAT-TrainEvent-Cache.h5")
Test_genC = MakeGenerator(TestSampleList, NTestSamples,
cachefile="/tmp/LArTPCDNN-LArIAT-TestEvent-Cache.h5")
print "Train Class Index Map:", Train_genC.ClassIndexMap
#print "Test Class Index Map:", Test_genC.ClassIndexMap
Cache=True
if Preload:
print "Caching data in memory for faster processing after first epoch. Hope you have enough memory."
Train_gen=Train_genC.PreloadGenerator()
Test_gen=Test_genC.PreloadGenerator()
elif Cache:
print "Caching data on disk for faster processing after first epoch. Hope you have enough disk space."
Train_gen=Train_genC.DiskCacheGenerator(n_threads_cache)
Test_gen=Test_genC.DiskCacheGenerator(n_threads_cache)
else:
Train_gen=Train_genC.Generator()
Test_gen=Test_genC.Generator()
# Build/Load the Model
from DLTools.ModelWrapper import ModelWrapper
from LArTPCDNN.Models import *
# You can automatically load the latest previous training of this model.
if TestDefaultParam("LoadPreviousModel") and not LoadModel:
print "Looking for Previous Model to load."
ModelName=Name
if View1 and View2:
ModelName+="_Merged"
MyModel=ModelWrapper(Name=ModelName, LoadPrevious=True,OutputBase=OutputBase)
# You can load a previous model using "-L" option with the model directory.
if LoadModel:
print "Loading Model From:",LoadModel
if LoadModel[-1]=="/": LoadModel=LoadModel[:-1]
MyModel=ModelWrapper(Name=os.path.basename(LoadModel),InDir=os.path.dirname(LoadModel),
OutputBase=OutputBase)
MyModel.Load(LoadModel)
if not MyModel.Model:
FailedLoad=True
else:
FailedLoad=False
OldModel=False
# Or Build the model from scratch
if not MyModel.Model and OldModel:
import keras
print "Building Model...",
if View1:
View1Model=FullyConnectedClassification(Name+"View1", viewshape, Width, Depth,
BatchSize, NClasses,
init=TestDefaultParam("WeightInitialization",'normal'),
activation=TestDefaultParam("activation","relu"),
Dropout=TestDefaultParam("DropoutLayers",0.5),
BatchNormalization=TestDefaultParam("BatchNormLayers",False),
NoClassificationLayer=View1 and View2,
OutputBase=OutputBase)
View1Model.Build()
MyModel=View1Model
if View2:
View2Model=FullyConnectedClassification(Name+"View2", viewshape, Width, Depth,
BatchSize, NClasses,
init=TestDefaultParam("WeightInitialization",'normal'),
activation=TestDefaultParam("activation","relu"),
Dropout=TestDefaultParam("DropoutLayers",0.5),
BatchNormalization=TestDefaultParam("BatchNormLayers",False),
NoClassificationLayer=View1 and View2,
OutputBase=OutputBase)
View2Model.Build()
MyModel=View2Model
if View1 and View2:
MyModel=MergerModel(Name+"_Merged",[View1Model,View2Model], NClasses, WeightInitialization,
OutputBase=OutputBase)
# Configure the Optimizer, using optimizer configuration parameter.
MyModel.Loss=loss
# Build it
MyModel.Build()
print " Done."
if not MyModel.Model and not OldModel:
import keras
print "Building Model...",
### ^^^^^ I change here (2,240,256) --> (1,240,256)
####^^^^^change output_shape=2 --> output_shape = (128, 10)
MyModel=SiameseInceptionClassification( Name, (1,240,256), width=0, depth=0, BatchSize=2048,
N_classes=100, kernel_initializer=0, BatchNormalization=False, Dropout=False,
NoClassificationLayer=False,
activation='relu',nb_filter=np.array([32]),nb_row=np.array([4]),
nb_column=np.array([4]),subsample=(1,1),output_shape=10,output_act='linear')
# Configure the Optimizer, using optimizer configuration parameter.
MyModel.Loss=loss
# Build it
MyModel.Build()
print " Done."
print "Output Directory:",MyModel.OutDir
# Store the Configuration Dictionary
MyModel.MetaData["Configuration"]=Config
if "HyperParamSet" in dir():
MyModel.MetaData["HyperParamSet"]=HyperParamSet
# Print out the Model Summary
MyModel.Model.summary()
# Compile The Model
print "Compiling Model."
MyModel.BuildOptimizer(optimizer,Config)
MyModel.Compile(Metrics=["accuracy"])
# Train
if Train or (RecoverMode and FailedLoad):
print "Training."
# Setup Callbacks
# These are all optional.
from DLTools.CallBacks import TimeStopping, GracefulExit
from keras.callbacks import *
callbacks=[ ]
# Still testing this...
if TestDefaultParam("UseGracefulExit",0):
print "Adding GracefulExit Callback."
callbacks.append( GracefulExit() )
if TestDefaultParam("ModelCheckpoint",False):
MyModel.MakeOutputDir()
callbacks.append(ModelCheckpoint(MyModel.OutDir+"/Checkpoint.Weights.h5",
monitor=TestDefaultParam("monitor","val_loss"),
save_best_only=TestDefaultParam("ModelCheckpoint_save_best_only"),
save_weights_only=TestDefaultParam("ModelCheckpoint_save_weights_only"),
mode=TestDefaultParam("ModelCheckpoint_mode","auto"),
period=TestDefaultParam("ModelCheckpoint_period",1),
verbose=0))
if TestDefaultParam("EarlyStopping"):
callbacks.append(keras.callbacks.EarlyStopping(monitor=TestDefaultParam("monitor","val_loss"),
min_delta=TestDefaultParam("EarlyStopping_min_delta",0.01),
patience=TestDefaultParam("EarlyStopping_patience"),
mode=TestDefaultParam("EarlyStopping_mode",'auto'),
verbose=0))
if TestDefaultParam("RunningTime"):
print "Setting Runningtime to",RunningTime,"."
TSCB=TimeStopping(TestDefaultParam("RunningTime",3600*6),verbose=False)
callbacks.append(TSCB)
# Don't fill the log files with progress bar.
if sys.flags.interactive:
verbose=1
else:
verbose=1 # Set to 2
print "Evaluating score on test sample..."
score = MyModel.Model.evaluate_generator(Test_gen, NTestSamples/BatchSize) #steps=NTestSamples/BatchSize)
print "Initial Score:", score
MyModel.MetaData["InitialScore"]=score
MyModel.History = MyModel.Model.fit_generator(Train_gen,
samples_per_epoch= (NSamples),###^^^steps_per_epoch=(NSamples/BatchSize),
###^^^The steps_per_epoch is typically samples_per_epoch / batch_size.
nb_epoch = Epochs,###^^^epochs=Epochs,
verbose=verbose,
validation_data=Test_gen,
nb_val_samples = NTestSamples,###^^^validation_steps=NTestSamples/BatchSize,
callbacks=callbacks)
score = MyModel.Model.evaluate_generator(Test_gen, NTestSamples/BatchSize) #steps=NTestSamples/BatchSize)
print "Evaluating score on test sample..."
print "Final Score:", score
MyModel.MetaData["FinalScore"]=score
if TestDefaultParam("RunningTime"):
MyModel.MetaData["EpochTime"]=TSCB.history
# Store the parameters used for scanning for easier tables later:
for k in Params:
MyModel.MetaData[k]=Config[k]
# Save Model
MyModel.Save()
else:
print "Skipping Training."
# Analysis
if Analyze:
Test_genC = MakeGenerator(TestSampleList, NTestSamples,
cachefile="/tmp/LArTPCDNN-LArIAT-TestEvent-Cache-2000-PID947.h5") ###^^^Test_genC.cachefilename) #"/tmp/LArTPCDNN-LArIAT-TestEvent-Cache.h5")
Test_genC.PreloadData(n_threads_cache)
[Test_X_View1, Test_X_View2], Test_Y = MergeInputs()(tuple(Test_genC.D))
from DLAnalysis.Classification import MultiClassificationAnalysis
result,NewMetaData=MultiClassificationAnalysis(MyModel,[Test_X_View1,Test_X_View2],
Test_Y,BatchSize,PDFFileName="ROC",
IndexMap=Test_genC.ClassIndexMap)
MyModel.MetaData.update(NewMetaData)
# Save again, in case Analysis put anything into the Model MetaData
if not sys.flags.interactive:
MyModel.Save()
else:
print "Warning: Interactive Mode. Use MyModel.Save() to save Analysis Results."
# Make sure all of the Generators processes and threads are dead.
# Not necessary... but ensures a graceful exit.
# if not sys.flags.interactive:
# for g in GeneratorClasses:
# try:
# g.StopFiller()
# g.StopWorkers()
# except:
# pass