123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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)
|