from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Conv3D, MaxPooling3D, AveragePooling3D from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization from keras import backend as K import tensorflow as tf # trick to enable import of config import sys sys.path.insert(0, '..') from . import metrics from config import cnn_config as cfg def generate_model_2D(_input_shape): model = Sequential() model.add(Conv2D(60, (2, 2), input_shape=_input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(40, (2, 2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(20, (2, 2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(140)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(120)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(80)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(40)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(20)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy', metrics.auc]) return model def generate_model_3D(_input_shape): model = Sequential() print(_input_shape) model.add(Conv3D(60, (1, 2, 2), input_shape=_input_shape)) model.add(Activation('relu')) model.add(MaxPooling3D(pool_size=(1, 2, 2))) model.add(Conv3D(40, (1, 2, 2))) model.add(Activation('relu')) model.add(MaxPooling3D(pool_size=(1, 2, 2))) model.add(Conv3D(20, (1, 2, 2))) model.add(Activation('relu')) model.add(MaxPooling3D(pool_size=(1, 2, 2))) model.add(Flatten()) model.add(Dense(140)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(120)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(80)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(40)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(20)) model.add(Activation('relu')) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy', metrics.auc]) return model def get_model(n_channels, _input_shape): if n_channels == 1: return generate_model_2D(_input_shape) if n_channels == 3: return generate_model_3D(_input_shape)