| 161 | |
| 162 | |
| 163 | def run_model(): |
| 164 | train_steps = len(train_generator.labels)/ batch_size |
| 165 | val_steps = len(validation_generator.labels) / batch_size |
| 166 | |
| 167 | filepath= "models/covid19-"+args.structure+ "-" + database + "-" + args.model_name + "-" + str(image_size) + "-" + str(batch_size) + "-"+str(args.lr)+ ".h5" #### set the path to save models having lowest validation loss during training |
| 168 | checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min') |
| 169 | tensorboard = TensorBoard(log_dir="logs/{}".format(time())) |
| 170 | |
| 171 | |
| 172 | history = model.fit_generator( |
| 173 | train_generator, |
| 174 | epochs=num_epoches, |
| 175 | steps_per_epoch=train_steps, |
| 176 | validation_data=validation_generator, |
| 177 | validation_steps=val_steps, |
| 178 | use_multiprocessing=True, |
| 179 | workers=20, |
| 180 | callbacks=[checkpoint,tensorboard]) |
| 181 | |
| 182 | |
| 183 | ### Save training loss |
| 184 | train_auc = history.history['auc'] |
| 185 | val_auc = history.history['val_auc'] |
| 186 | train_loss = history.history['loss'] |
| 187 | val_loss = history.history['val_loss'] |
| 188 | d_loss = pd.DataFrame({'train_auc':train_auc, 'val_auc':val_auc, 'train_loss':train_loss, 'val_loss':val_loss}) |
| 189 | d_loss.to_excel("loss/covid19-" +args.structure+ "-" + database + "-" + args.model_name + "-" + str(image_size) + "-" + str(batch_size) + "-"+str(args.lr)+ ".csv", index=False) |
| 190 | |
| 191 | |
| 192 | |