cnn_models.py 6.1 KB

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