31
31
from keras .callbacks import EarlyStopping , ModelCheckpoint , CSVLogger , Callback
32
32
from keras .applications .resnet50 import preprocess_input
33
33
import h5py
34
-
34
+ import argparse
35
35
36
36
37
37
def get_im_cv2 (path ,dim = 224 ):
@@ -48,7 +48,7 @@ def pre_process(img):
48
48
49
49
# Function to build X, y in numpy format based on the train/validation datasets
50
50
def read_data (class_folders ,path ,num_class ,dim ,train_val = 'train' ):
51
- print train_val
51
+ print ( train_val )
52
52
train_X ,train_y = [],[]
53
53
for c in class_folders :
54
54
path_class = path + str (train_val ) + '/' + str (c )
@@ -73,7 +73,7 @@ def inception_pseudo(dim=224,freeze_layers=30,full_freeze='N'):
73
73
model_final = Model (input = model .input ,outputs = out )
74
74
if full_freeze != 'N' :
75
75
for layer in model .layers [0 :freeze_layers ]:
76
- layer .trainable = False
76
+ layer .trainable = False
77
77
return model_final
78
78
79
79
# ResNet50 Model for transfer Learning
@@ -89,7 +89,7 @@ def resnet_pseudo(dim=224,freeze_layers=10,full_freeze='N'):
89
89
model_final = Model (input = model .input ,outputs = out )
90
90
if full_freeze != 'N' :
91
91
for layer in model .layers [0 :freeze_layers ]:
92
- layer .trainable = False
92
+ layer .trainable = False
93
93
return model_final
94
94
95
95
# VGG16 Model for transfer Learning
@@ -106,7 +106,7 @@ def VGG16_pseudo(dim=224,freeze_layers=10,full_freeze='N'):
106
106
model_final = Model (input = model .input ,outputs = out )
107
107
if full_freeze != 'N' :
108
108
for layer in model .layers [0 :freeze_layers ]:
109
- layer .trainable = False
109
+ layer .trainable = False
110
110
return model_final
111
111
112
112
@@ -148,7 +148,7 @@ def train_model(train_X,train_y,n_fold=5,batch_size=16,dim=224,lr=1e-5,model='Re
148
148
CSVLogger ('keras-5fold-run-01-v1-epochs_ib.log' , separator = ',' , append = False ),reduce_lr ,
149
149
ModelCheckpoint (
150
150
'kera1-5fold-run-01-v1-fold-' + str ('%02d' % (k + 1 )) + '-run-' + str ('%02d' % (1 + 1 )) + '.check' ,
151
- monitor = 'val_loss' , mode = 'min' , # mode must be set to max or Keras will be confused
151
+ monitor = 'val_loss' , mode = 'min' ,
152
152
save_best_only = True ,
153
153
verbose = 1 )
154
154
]
@@ -173,7 +173,7 @@ def train_model(train_X,train_y,n_fold=5,batch_size=16,dim=224,lr=1e-5,model='Re
173
173
def inference_validation (test_X ,test_y ,model_save_dest ,n_class = 5 ,folds = 5 ):
174
174
pred = np .zeros ((len (test_X ),n_class ))
175
175
176
- for k in xrange (1 ,folds + 1 ):
176
+ for k in range (1 ,folds + 1 ):
177
177
model = keras .models .load_model (model_save_dest [k ])
178
178
pred = pred + model .predict (test_X )
179
179
pred = pred / (1.0 * folds )
@@ -186,22 +186,23 @@ def inference_validation(test_X,test_y,model_save_dest,n_class=5,folds=5):
186
186
187
187
if __name__ == "__main__" :
188
188
start_time = time .time ()
189
- path = '/home /santanu/Downloads /Diabetic Retinopathy/New/'
189
+ path = '/media /santanu/9eb9b6dc-b380-486e-b4fd-c424a325b976/book AI /Diabetic Retinopathy/New/'
190
190
class_folders = ['0' ,'1' ,'2' ,'3' ,'4' ]
191
191
num_class = len (class_folders )
192
192
dim = 224
193
193
lr = 1e-5
194
- print 'Starting time:' ,start_time
194
+ print ('Starting time:' ,start_time )
195
+ print ("Starting Data processing" )
195
196
train_X ,train_y = read_data (class_folders ,path ,num_class ,dim ,train_val = 'train' )
196
197
model_save_dest = train_model (train_X ,train_y ,n_fold = 5 ,batch_size = 16 ,dim = 224 ,lr = 1e-5 ,model = 'InceptionV3' )
197
198
#model_save_dest = {1:'InceptionV3__1'}
198
199
test_X ,test_y = read_data (class_folders ,path ,num_class ,dim ,train_val = 'validation' )
199
200
pred_class ,accuracy ,kappa = inference_validation (test_X ,test_y ,model_save_dest ,n_class = 5 ,folds = 5 )
200
201
np .save (path + "dict_model" ,model_save_dest )
201
- print "-----------------------------------------------------"
202
- print "Kappa score:" , kappa
203
- print "accuracy:" , accuracy
204
- print "End of training"
205
- print "-----------------------------------------------------"
202
+ print ( "-----------------------------------------------------" )
203
+ print ( "Kappa score:" , kappa )
204
+ print ( "accuracy:" , accuracy )
205
+ print ( "End of training" )
206
+ print ( "-----------------------------------------------------" )
206
207
207
208
0 commit comments