-
Notifications
You must be signed in to change notification settings - Fork 3
/
multi_application_train.py
177 lines (112 loc) · 5.45 KB
/
multi_application_train.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
from keras.preprocessing.image import ImageDataGenerator
import keras.applications as keras_applications
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd
import sys
from timeit import default_timer as timer
from keras import backend as K
from keras.callbacks import CSVLogger
import json
from keras.callbacks import ModelCheckpoint
start=timer()
input_shape=(200,200,1)
rootoutput='outputs/'
rootdataset='dataset/'
ModelNumber=int(sys.argv[1])-1
#classes=int(sys.argv[2])
expprefix=sys.argv[2]
datapath=sys.argv[3]
classes_name=sys.argv[4].split(",")
classes=len(classes_name)
'''
if classes ==2:
expprefix="binary"
datapath="esample"
classes_name=["nonsym","sym"]
if classes==3:
# expprefix="multi"
# classes_name=["H","R","T","V"]
# datapath='hvttraindata'
expprefix="multirefrottarAll"
classes_name=["ref","rot","tar"]
datapath='multi_ref_rot_tra'
# expprefix="binarydense"
# datapath="binary_data"
# classes_name=["nonsym","sym"]
# expprefix="binaryhv"
# datapath="hvdata"
# classes_name=["H","V"]
# expprefix="binaryvnonsym"
# datapath="binaryvnonsymdata"
# classes_name=["nonsym","V"]
# expprefix="binaryhnonsym"
# datapath="binaryhnonsymdata"
# classes_name=["nonsym","H"]
'''
timerfile= rootoutput+ expprefix+'/timer.csv'
outdir=rootoutput + expprefix+"/output/"
checkpoint_dir = rootoutput+ expprefix+ "/models/"
validation_data_dir = rootdataset+ datapath+'/valid'
train_path = rootdataset+datapath+'/train'
test_path = rootdataset+datapath+'/test'
print("timerfile:", timerfile)
print("outdir:", outdir)
print("checkpoint_dir:", checkpoint_dir)
print("validation_data_dir:", validation_data_dir)
print("train_path:", train_path)
print("test_path:", test_path)
img_width, img_height = 200, 200
train_data_dir = train_path
nb_train_samples = 12000#14000
nb_validation_samples = 1600
epochs = 20
batch_size = 16
def recall_m(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision_m(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def f1_m(y_true, y_pred):
precision = precision_m(y_true, y_pred)
recall = recall_m(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
names=['ResNet50', 'MobileNet', 'MobileNetV2', 'NASNetMobile', 'NASNetLarge', 'VGG16', 'VGG19', 'Xception', 'InceptionResNetV2', 'DenseNet121', 'DenseNet201']
m=[keras_applications.ResNet50, keras_applications.MobileNet, keras_applications.MobileNetV2, keras_applications.NASNetMobile, keras_applications.NASNetLarge, keras_applications.VGG16, keras_applications.VGG19, keras_applications.Xception, keras_applications.InceptionResNetV2,
keras_applications.DenseNet121,keras_applications.DenseNet201]
modelname= str(ModelNumber)+"_"+ names[ModelNumber]
model=m[ModelNumber](weights=None, input_shape=input_shape,classes=classes)
csv_logger = CSVLogger(outdir+ modelname+ 'log.csv', append=True, separator=';')
checkpoint = ModelCheckpoint(checkpoint_dir+modelname+"_checkpoint.best.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy',f1_m,precision_m, recall_m])
#model.summary()
train_datagen = ImageDataGenerator(rescale = 1. / 255)
test_datagen = ImageDataGenerator(rescale = 1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_dir, classes= classes_name, target_size =(img_width, img_height), batch_size = batch_size, class_mode='categorical',color_mode="grayscale")
validation_generator = test_datagen.flow_from_directory( validation_data_dir, classes= classes_name, target_size =(img_width, img_height), batch_size = batch_size, class_mode='categorical',color_mode="grayscale")
model.fit_generator(train_generator, verbose=1, steps_per_epoch = nb_train_samples // batch_size, epochs = epochs, validation_data = validation_generator, validation_steps = nb_validation_samples // batch_size, callbacks=[csv_logger,checkpoint])
#model.save_weights(checkpoint_dir+modelname+'_model_saved_weight.h5')
history=model.history
with open( outdir+ "/history_" + modelname+ '.json', 'w') as f:
json.dump(history.history, f)
steps = np.ceil(validation_generator.samples/batch_size)
Y_pred = model.predict_generator(validation_generator, steps=steps)
y_pred = np.argmax(Y_pred, axis=1)
#print('Confusion Matrix')
target_names = [k for k in validation_generator.class_indices]
#print(target_names)
cm=confusion_matrix(validation_generator.classes, y_pred)
np.savetxt(outdir+ "/conf_" + modelname+".csv", cm, fmt="%d", delimiter=",")
f=open(outdir+ "/conf_" + modelname+".csv", "a")
txt= str(target_names).replace("[","").replace("]","")
with open(outdir+ "/conf_" + modelname+".csv", 'r') as original: data = original.read()
with open(outdir+ "/conf_" + modelname+".csv", 'w') as modified: modified.write(txt + "\n" + data)
with open( timerfile, 'a+') as modified: modified.write(modelname + ", " + str( round( (timer()- start) /(60*60) ,2)) + "\n")