train_model.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # main imports
  2. import numpy as np
  3. import pandas as pd
  4. import sys, os, argparse
  5. import json
  6. # model imports
  7. import cnn_models as models
  8. import tensorflow as tf
  9. import keras
  10. from keras import backend as K
  11. from keras.callbacks import ModelCheckpoint
  12. from sklearn.metrics import roc_auc_score, accuracy_score, precision_score, recall_score, f1_score
  13. # image processing imports
  14. import cv2
  15. from sklearn.utils import shuffle
  16. # config imports
  17. sys.path.insert(0, '') # trick to enable import of main folder module
  18. import custom_config as cfg
  19. def main():
  20. parser = argparse.ArgumentParser(description="Train Keras model and save it into .json file")
  21. parser.add_argument('--data', type=str, help='dataset filename prefix (without .train and .val)', required=True)
  22. parser.add_argument('--output', type=str, help='output file name desired for model (without .json extension)', required=True)
  23. parser.add_argument('--tl', type=int, help='use or not of transfer learning (`VGG network`)', default=0, choices=[0, 1])
  24. parser.add_argument('--batch_size', type=int, help='batch size used as model input', default=cfg.keras_batch)
  25. parser.add_argument('--epochs', type=int, help='number of epochs used for training model', default=cfg.keras_epochs)
  26. #parser.add_argument('--val_size', type=float, help='percent of validation data during training process', default=cfg.val_dataset_size)
  27. args = parser.parse_args()
  28. p_data_file = args.data
  29. p_output = args.output
  30. p_tl = args.tl
  31. p_batch_size = args.batch_size
  32. p_epochs = args.epochs
  33. #p_val_size = args.val_size
  34. initial_epoch = 0
  35. ########################
  36. # 1. Get and prepare data
  37. ########################
  38. print("Preparing data...")
  39. dataset_train = pd.read_csv(p_data_file + '.train', header=None, sep=";")
  40. dataset_val = pd.read_csv(p_data_file + '.val', header=None, sep=";")
  41. print("Train set size : ", len(dataset_train))
  42. print("val set size : ", len(dataset_val))
  43. # default first shuffle of data
  44. dataset_train = shuffle(dataset_train)
  45. dataset_val = shuffle(dataset_val)
  46. print("Reading all images data...")
  47. # getting number of chanel
  48. n_channels = len(dataset_train[1][1].split('::'))
  49. print("Number of channels : ", n_channels)
  50. img_width, img_height = cfg.keras_img_size
  51. # specify the number of dimensions
  52. if K.image_data_format() == 'channels_first':
  53. if n_channels > 1:
  54. input_shape = (1, n_channels, img_width, img_height)
  55. else:
  56. input_shape = (n_channels, img_width, img_height)
  57. else:
  58. if n_channels > 1:
  59. input_shape = (1, img_width, img_height, n_channels)
  60. else:
  61. input_shape = (img_width, img_height, n_channels)
  62. # `:` is the separator used for getting each img path
  63. if n_channels > 1:
  64. dataset_train[1] = dataset_train[1].apply(lambda x: [cv2.imread(path, cv2.IMREAD_GRAYSCALE) for path in x.split('::')])
  65. dataset_val[1] = dataset_val[1].apply(lambda x: [cv2.imread(path, cv2.IMREAD_GRAYSCALE) for path in x.split('::')])
  66. else:
  67. dataset_train[1] = dataset_train[1].apply(lambda x: cv2.imread(x, cv2.IMREAD_GRAYSCALE))
  68. dataset_val[1] = dataset_val[1].apply(lambda x: cv2.imread(x, cv2.IMREAD_GRAYSCALE))
  69. # reshape array data
  70. dataset_train[1] = dataset_train[1].apply(lambda x: np.array(x).reshape(input_shape))
  71. dataset_val[1] = dataset_val[1].apply(lambda x: np.array(x).reshape(input_shape))
  72. # get dataset with equal number of classes occurences
  73. noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 1]
  74. not_noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 0]
  75. nb_noisy_train = len(noisy_df_train.index)
  76. noisy_df_val = dataset_val[dataset_val.ix[:, 0] == 1]
  77. not_noisy_df_val = dataset_val[dataset_val.ix[:, 0] == 0]
  78. nb_noisy_val = len(noisy_df_val.index)
  79. final_df_train = pd.concat([not_noisy_df_train[0:nb_noisy_train], noisy_df_train])
  80. final_df_val = pd.concat([not_noisy_df_val[0:nb_noisy_val], noisy_df_val])
  81. # shuffle data another time
  82. final_df_train = shuffle(final_df_train)
  83. final_df_val = shuffle(final_df_val)
  84. final_df_train_size = len(final_df_train.index)
  85. final_df_val_size = len(final_df_val.index)
  86. validation_split = final_df_val_size / (final_df_train_size + final_df_val_size)
  87. print("----------------------------------------------------------")
  88. print("Validation size is based of `.val` content")
  89. print("Validation split is now set at", validation_split)
  90. print("----------------------------------------------------------")
  91. # use of the whole data set for training
  92. x_dataset_train = final_df_train.ix[:,1:]
  93. x_dataset_val = final_df_val.ix[:,1:]
  94. y_dataset_train = final_df_train.ix[:,0]
  95. y_dataset_val = final_df_val.ix[:,0]
  96. x_data_train = []
  97. for item in x_dataset_train.values:
  98. #print("Item is here", item)
  99. x_data_train.append(item[0])
  100. x_data_train = np.array(x_data_train)
  101. x_data_val = []
  102. for item in x_dataset_val.values:
  103. #print("Item is here", item)
  104. x_data_val.append(item[0])
  105. x_data_val = np.array(x_data_val)
  106. print("End of loading data..")
  107. print("Train set size (after balancing) : ", final_df_train_size)
  108. print("val set size (after balancing) : ", final_df_val_size)
  109. #######################
  110. # 2. Getting model
  111. #######################
  112. # create backup folder for current model
  113. model_backup_folder = os.path.join(cfg.backup_model_folder, p_output)
  114. if not os.path.exists(model_backup_folder):
  115. os.makedirs(model_backup_folder)
  116. # add of callback models
  117. filepath = os.path.join(cfg.backup_model_folder, p_output, p_output + "__{epoch:02d}.hdf5")
  118. checkpoint = ModelCheckpoint(filepath, monitor='val_auc', verbose=1, save_best_only=True, mode='max')
  119. callbacks_list = [checkpoint]
  120. model = models.get_model(n_channels, input_shape, p_tl)
  121. model.summary()
  122. # check if backup already exists
  123. backups = sorted(os.listdir(model_backup_folder))
  124. if len(backups) > 0:
  125. # TODO : check of initial epoch
  126. last_backup = backups[-1]
  127. last_epoch = int(last_backup.split('__')[1].replace('.hdf5', ''))
  128. initial_epoch = last_epoch
  129. print("-------------------------------------------------")
  130. print("Previous backup model found with already", last_epoch, "done...")
  131. print("Resuming from epoch", str(last_epoch + 1))
  132. print("-------------------------------------------------")
  133. # concatenate train and validation data (`validation_split` param will do the separation into keras model)
  134. y_data = np.concatenate([y_dataset_train.values, y_dataset_val.values])
  135. x_data = np.concatenate([x_data_train, x_data_val])
  136. # validation split parameter will use the last `%` data, so here, data will really validate our model
  137. model.fit(x_data, y_data, validation_split=validation_split, initial_epoch=initial_epoch, epochs=p_epochs, batch_size=p_batch_size, callbacks=callbacks_list)
  138. score = model.evaluate(x_data_val, y_dataset_val, batch_size=p_batch_size)
  139. print("Accuracy score on val dataset ", score)
  140. if not os.path.exists(cfg.saved_models_folder):
  141. os.makedirs(cfg.saved_models_folder)
  142. # save the model into HDF5 file
  143. model_output_path = os.path.join(cfg.saved_models_folder, p_output + '.json')
  144. json_model_content = model.to_json()
  145. with open(model_output_path, 'w') as f:
  146. print("Model saved into ", model_output_path)
  147. json.dump(json_model_content, f, indent=4)
  148. model.save_weights(model_output_path.replace('.json', '.h5'))
  149. # Get results obtained from model
  150. y_train_prediction = model.predict(x_data_train)
  151. y_val_prediction = model.predict(x_data_val)
  152. y_train_prediction = [1 if x > 0.5 else 0 for x in y_train_prediction]
  153. y_val_prediction = [1 if x > 0.5 else 0 for x in y_val_prediction]
  154. acc_train_score = accuracy_score(y_dataset_train, y_train_prediction)
  155. acc_val_score = accuracy_score(y_dataset_val, y_val_prediction)
  156. f1_train_score = f1_score(y_dataset_train, y_train_prediction)
  157. f1_val_score = f1_score(y_dataset_val, y_val_prediction)
  158. recall_train_score = recall_score(y_dataset_train, y_train_prediction)
  159. recall_val_score = recall_score(y_dataset_val, y_val_prediction)
  160. pres_train_score = precision_score(y_dataset_train, y_train_prediction)
  161. pres_val_score = precision_score(y_dataset_val, y_val_prediction)
  162. roc_train_score = roc_auc_score(y_dataset_train, y_train_prediction)
  163. roc_val_score = roc_auc_score(y_dataset_val, y_val_prediction)
  164. # save model performance
  165. if not os.path.exists(cfg.results_information_folder):
  166. os.makedirs(cfg.results_information_folder)
  167. perf_file_path = os.path.join(cfg.results_information_folder, cfg.csv_model_comparisons_filename)
  168. # write header if necessary
  169. if not os.path.exists(perf_file_path):
  170. with open(perf_file_path, 'w') as f:
  171. f.write(cfg.perf_train_header_file)
  172. # add information into file
  173. with open(perf_file_path, 'a') as f:
  174. line = p_output + ';' + str(len(dataset_train)) + ';' + str(len(dataset_val)) + ';' \
  175. + str(final_df_train_size) + ';' + str(final_df_val_size) + ';' \
  176. + str(acc_train_score) + ';' + str(acc_val_score) + ';' \
  177. + str(f1_train_score) + ';' + str(f1_val_score) + ';' \
  178. + str(recall_train_score) + ';' + str(recall_val_score) + ';' \
  179. + str(pres_train_score) + ';' + str(pres_val_score) + ';' \
  180. + str(roc_train_score) + ';' + str(roc_val_score) + '\n'
  181. f.write(line)
  182. print("You can now run your model with your own `test` dataset")
  183. if __name__== "__main__":
  184. main()