cnn_models.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # main imports
  2. import sys
  3. # model imports
  4. from keras.preprocessing.image import ImageDataGenerator
  5. from keras.models import Sequential, Model
  6. from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Conv3D, MaxPooling3D, AveragePooling3D
  7. from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
  8. from keras.applications.vgg19 import VGG19
  9. from keras import backend as K
  10. import tensorflow as tf
  11. # configuration and modules imports
  12. sys.path.insert(0, '') # trick to enable import of main folder module
  13. import custom_config as cfg
  14. #from models import metrics
  15. def generate_model_2D(_input_shape, _weights_file=None):
  16. model = Sequential()
  17. model.add(Conv2D(60, (2, 2), input_shape=_input_shape))
  18. model.add(Activation('relu'))
  19. model.add(MaxPooling2D(pool_size=(2, 2)))
  20. model.add(Conv2D(40, (2, 2)))
  21. model.add(Activation('relu'))
  22. model.add(MaxPooling2D(pool_size=(2, 2)))
  23. model.add(Conv2D(20, (2, 2)))
  24. model.add(Activation('relu'))
  25. model.add(MaxPooling2D(pool_size=(2, 2)))
  26. model.add(Flatten())
  27. model.add(Dense(140))
  28. model.add(Activation('relu'))
  29. model.add(BatchNormalization())
  30. model.add(Dropout(0.5))
  31. # model.add(Dense(120))
  32. # model.add(Activation('sigmoid'))
  33. # model.add(BatchNormalization())
  34. # model.add(Dropout(0.5))
  35. model.add(Dense(80))
  36. model.add(Activation('relu'))
  37. model.add(BatchNormalization())
  38. model.add(Dropout(0.5))
  39. model.add(Dense(40))
  40. model.add(Activation('relu'))
  41. model.add(BatchNormalization())
  42. model.add(Dropout(0.5))
  43. model.add(Dense(20))
  44. model.add(Activation('relu'))
  45. model.add(BatchNormalization())
  46. model.add(Dropout(0.5))
  47. model.add(Dense(2))
  48. model.add(Activation('softmax'))
  49. # reload weights if exists
  50. if _weights_file is not None:
  51. model.load_weights(_weights_file)
  52. model.compile(loss='categorical_crossentropy',
  53. optimizer='adam',
  54. #metrics=['accuracy', metrics.auc])
  55. metrics=['accuracy'])
  56. return model
  57. def generate_model_3D(_input_shape, _weights_file=None):
  58. model = Sequential()
  59. print(_input_shape)
  60. model.add(Conv3D(60, (1, 2, 2), input_shape=_input_shape))
  61. model.add(Activation('relu'))
  62. model.add(MaxPooling3D(pool_size=(1, 2, 2)))
  63. model.add(Conv3D(40, (1, 2, 2)))
  64. model.add(Activation('relu'))
  65. model.add(MaxPooling3D(pool_size=(1, 2, 2)))
  66. model.add(Conv3D(20, (1, 2, 2)))
  67. model.add(Activation('relu'))
  68. model.add(MaxPooling3D(pool_size=(1, 2, 2)))
  69. model.add(Flatten())
  70. model.add(Dense(140))
  71. model.add(Activation('relu'))
  72. model.add(BatchNormalization())
  73. model.add(Dropout(0.5))
  74. model.add(Dense(120))
  75. model.add(Activation('relu'))
  76. model.add(BatchNormalization())
  77. model.add(Dropout(0.5))
  78. model.add(Dense(80))
  79. model.add(Activation('relu'))
  80. model.add(BatchNormalization())
  81. model.add(Dropout(0.5))
  82. model.add(Dense(40))
  83. model.add(Activation('relu'))
  84. model.add(BatchNormalization())
  85. model.add(Dropout(0.5))
  86. model.add(Dense(20))
  87. model.add(Activation('relu'))
  88. model.add(BatchNormalization())
  89. model.add(Dropout(0.5))
  90. model.add(Dense(2))
  91. model.add(Activation('sigmoid'))
  92. # reload weights if exists
  93. if _weights_file is not None:
  94. model.load_weights(_weights_file)
  95. model.compile(loss='categorical_crossentropy',
  96. optimizer='rmsprop',
  97. #metrics=['accuracy', metrics.auc])
  98. metrics=['accuracy'])
  99. return model
  100. # using transfer learning (VGG19)
  101. def generate_model_3D_TL(_input_shape, _weights_file=None):
  102. # load pre-trained model
  103. model = VGG19(weights='imagenet', include_top=False, input_shape=_input_shape)
  104. # display model layers
  105. model.summary()
  106. # do not train convolutional layers
  107. for layer in model.layers[:5]:
  108. layer.trainable = False
  109. '''predictions_model = Sequential(model)
  110. predictions_model.add(Flatten(model.output))
  111. predictions_model.add(Dense(1024))
  112. predictions_model.add(Activation('relu'))
  113. predictions_model.add(BatchNormalization())
  114. predictions_model.add(Dropout(0.5))
  115. predictions_model.add(Dense(512))
  116. predictions_model.add(Activation('relu'))
  117. predictions_model.add(BatchNormalization())
  118. predictions_model.add(Dropout(0.5))
  119. predictions_model.add(Dense(256))
  120. predictions_model.add(Activation('relu'))
  121. predictions_model.add(BatchNormalization())
  122. model.add(Dropout(0.5))
  123. predictions_model.add(Dense(100))
  124. predictions_model.add(Activation('relu'))
  125. predictions_model.add(BatchNormalization())
  126. predictions_model.add(Dropout(0.5))
  127. predictions_model.add(Dense(20))
  128. predictions_model.add(Activation('relu'))
  129. predictions_model.add(BatchNormalization())
  130. predictions_model.add(Dropout(0.5))
  131. predictions_model.add(Dense(1))
  132. predictions_model.add(Activation('sigmoid'))'''
  133. # adding custom Layers
  134. x = model.output
  135. x = Flatten()(x)
  136. x = Dense(1024, activation="relu")(x)
  137. x = BatchNormalization()(x)
  138. x = Dropout(0.5)(x)
  139. x = Dense(256, activation="relu")(x)
  140. x = BatchNormalization()(x)
  141. x = Dropout(0.5)(x)
  142. x = Dense(64, activation="relu")(x)
  143. x = BatchNormalization()(x)
  144. x = Dropout(0.5)(x)
  145. x = Dense(16, activation="relu")(x)
  146. predictions = Dense(1, activation="softmax")(x)
  147. # creating the final model
  148. model_final = Model(input=model.input, output=predictions)
  149. model_final.summary()
  150. # reload weights if exists
  151. if _weights_file is not None:
  152. model.load_weights(_weights_file)
  153. model_final.compile(loss='binary_crossentropy',
  154. optimizer='rmsprop',
  155. # metrics=['accuracy', metrics.auc])
  156. metrics=['accuracy'])
  157. return model_final
  158. def get_model(n_channels, _input_shape, _tl=False, _weights_file=None):
  159. if _tl:
  160. if n_channels == 3:
  161. return generate_model_3D_TL(_input_shape, _weights_file)
  162. else:
  163. print("Can't use transfer learning with only 1 channel")
  164. if n_channels == 1:
  165. return generate_model_2D(_input_shape, _weights_file)
  166. if n_channels >= 2:
  167. return generate_model_3D(_input_shape, _weights_file)