train_keras_svd.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # main imports
  2. import sys, os
  3. import argparse
  4. import json
  5. import numpy as np
  6. import pandas as pd
  7. # models imports
  8. from keras.preprocessing.image import ImageDataGenerator
  9. from keras.models import Sequential
  10. from keras.layers import Conv1D, MaxPooling1D
  11. from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
  12. from keras.wrappers.scikit_learn import KerasClassifier
  13. from keras import backend as K
  14. from sklearn.utils import shuffle
  15. from sklearn.metrics import roc_auc_score
  16. # modules and config imports
  17. import custom_config as cfg
  18. def f1(y_true, y_pred):
  19. def recall(y_true, y_pred):
  20. """Recall metric.
  21. Only computes a batch-wise average of recall.
  22. Computes the recall, a metric for multi-label classification of
  23. how many relevant items are selected.
  24. """
  25. true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
  26. possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
  27. recall = true_positives / (possible_positives + K.epsilon())
  28. return recall
  29. def precision(y_true, y_pred):
  30. """Precision metric.
  31. Only computes a batch-wise average of precision.
  32. Computes the precision, a metric for multi-label classification of
  33. how many selected items are relevant.
  34. """
  35. true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
  36. predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
  37. precision = true_positives / (predicted_positives + K.epsilon())
  38. return precision
  39. precision = precision(y_true, y_pred)
  40. recall = recall(y_true, y_pred)
  41. return 2*((precision*recall)/(precision+recall+K.epsilon()))
  42. def generate_model(input_shape):
  43. model = Sequential()
  44. #model.add(Conv1D(128, (10), input_shape=input_shape))
  45. #model.add(Activation('relu'))
  46. #model.add(Conv1D(128, (10)))
  47. #model.add(Activation('relu'))
  48. #model.add(Conv1D(128, (10)))
  49. #model.add(Activation('relu'))
  50. #model.add(MaxPooling1D(pool_size=(2)))
  51. #model.add(Conv1D(64, (10)))
  52. #model.add(Activation('relu'))
  53. #model.add(Conv1D(64, (10)))
  54. #model.add(Activation('relu'))
  55. #model.add(Conv1D(64, (10)))
  56. #model.add(Activation('relu'))
  57. #model.add(MaxPooling1D(pool_size=(2)))
  58. #model.add(Conv1D(32, (10)))
  59. #model.add(Activation('relu'))
  60. #model.add(Conv1D(32, (10)))
  61. #model.add(Activation('relu'))
  62. #model.add(Conv1D(32, (10)))
  63. #model.add(Activation('relu'))
  64. #model.add(MaxPooling1D(pool_size=(2)))
  65. model.add(Flatten(input_shape=input_shape))
  66. model.add(Dense(2048))
  67. model.add(Activation('relu'))
  68. model.add(BatchNormalization())
  69. model.add(Dropout(0.2))
  70. model.add(Dense(1024))
  71. model.add(Activation('relu'))
  72. model.add(BatchNormalization())
  73. model.add(Dropout(0.2))
  74. model.add(Dense(512))
  75. model.add(Activation('relu'))
  76. model.add(BatchNormalization())
  77. model.add(Dropout(0.3))
  78. model.add(Dense(256))
  79. model.add(Activation('relu'))
  80. model.add(BatchNormalization())
  81. model.add(Dropout(0.3))
  82. model.add(Dense(128))
  83. model.add(Activation('relu'))
  84. model.add(BatchNormalization())
  85. model.add(Dropout(0.3))
  86. model.add(Dense(20))
  87. model.add(Activation('relu'))
  88. model.add(BatchNormalization())
  89. model.add(Dropout(0.3))
  90. model.add(Dense(1))
  91. model.add(Activation('sigmoid'))
  92. model.compile(loss='binary_crossentropy',
  93. optimizer='adam',
  94. metrics=['accuracy', f1])
  95. return model
  96. def main():
  97. parser = argparse.ArgumentParser(description="Process deep_network_keras_svd.py parameters")
  98. parser.add_argument('--data', type=str, help='Data filename prefix to access train and test dataset')
  99. parser.add_argument('--output', type=str, help='Name of filename to save model into')
  100. parser.add_argument('--size', type=int, help='Size of input data vector')
  101. args = parser.parse_args()
  102. p_datafile = args.data
  103. p_output_filename = args.output
  104. p_vector_size = args.size
  105. epochs = 10
  106. batch_size = cfg.keras_batch
  107. input_shape = (p_vector_size, 1)
  108. ###########################
  109. # 1. Get and prepare data
  110. ###########################
  111. dataset_train = pd.read_csv(p_datafile + '.train', header=None, sep=";")
  112. dataset_test = pd.read_csv(p_datafile + '.test', header=None, sep=";")
  113. # default first shuffle of data
  114. dataset_train = shuffle(dataset_train)
  115. dataset_test = shuffle(dataset_test)
  116. # get dataset with equal number of classes occurences
  117. noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 1]
  118. not_noisy_df_train = dataset_train[dataset_train.ix[:, 0] == 0]
  119. nb_noisy_train = len(noisy_df_train.index)
  120. noisy_df_test = dataset_test[dataset_test.ix[:, 0] == 1]
  121. not_noisy_df_test = dataset_test[dataset_test.ix[:, 0] == 0]
  122. nb_noisy_test = len(noisy_df_test.index)
  123. final_df_train = pd.concat([not_noisy_df_train[0:nb_noisy_train], noisy_df_train])
  124. final_df_test = pd.concat([not_noisy_df_test[0:nb_noisy_test], noisy_df_test])
  125. # shuffle data another time
  126. final_df_train = shuffle(final_df_train)
  127. final_df_test = shuffle(final_df_test)
  128. final_df_train_size = len(final_df_train.index)
  129. final_df_test_size = len(final_df_test.index)
  130. # use of the whole data set for training
  131. x_dataset_train = final_df_train.ix[:,1:]
  132. x_dataset_test = final_df_test.ix[:,1:]
  133. y_dataset_train = final_df_train.ix[:,0]
  134. y_dataset_test = final_df_test.ix[:,0]
  135. #######################
  136. # 2. Getting model
  137. #######################
  138. model = generate_model(input_shape)
  139. model.summary()
  140. #model = KerasClassifier(build_fn=model, epochs=cfg.keras_epochs, batch_size=cfg.keras_batch, verbose=0)
  141. #######################
  142. # 3. Fit model : use of cross validation to fit model
  143. #######################
  144. # reshape input data
  145. x_dataset_train = np.array(x_dataset_train).reshape(len(x_dataset_train), p_vector_size, 1)
  146. x_dataset_test = np.array(x_dataset_test).reshape(len(x_dataset_test), p_vector_size, 1)
  147. model.fit(x_dataset_train, y_dataset_train, validation_split=0.20, epochs=cfg.keras_epochs, batch_size=cfg.keras_batch)
  148. score = model.evaluate(x_dataset_test, y_dataset_test, batch_size=batch_size)
  149. if not os.path.exists(cfg.saved_models_folder):
  150. os.makedirs(cfg.saved_models_folder)
  151. # save the model into HDF5 file
  152. model_output_path = os.path.join(cfg.saved_models_folder, p_output_filename + '.json')
  153. json_model_content = model.to_json()
  154. with open(model_output_path, 'w') as f:
  155. print("Model saved into ", model_output_path)
  156. json.dump(json_model_content, f, indent=4)
  157. model.save_weights(model_output_path.replace('.json', '.h5'))
  158. # Save results obtained from model
  159. y_test_prediction = model.predict(x_dataset_test)
  160. print("Metrics : ", model.metrics_names)
  161. print("Prediction : ", score)
  162. print("ROC AUC : ", roc_auc_score(y_dataset_test, y_test_prediction))
  163. if __name__== "__main__":
  164. main()