def upward_layer( skip_connection, inp, n_conv, channels, kernel_initializer, activation, dropout_rate, ): out = merged = concatenate([skip_connection, inp]) for _ in range(n_conv): out = Conv3D( channels * 4, (5, 5, 5), padding="same", kernel_initializer=kernel_initializer, activation=activation, )(out) if dropout_rate: out = SpatialDropout3D(dropout_rate)(out, training=True) out = add([merged, out]) out = keras.layers.Conv3DTranspose( channels, (2, 2, 2), strides=(2, 2, 2), kernel_initializer=kernel_initializer, activation=activation, )(out) if dropout_rate: out = SpatialDropout3D(dropout_rate)(out, training=True) return out
def PatchGanDiscriminator(output_dim, patch_size, padding='same', strides=(2,2,2), kernel_size=(4,4,4), batch_norm=True, dropout= True): inputs = Input(shape=[patch_size[0], patch_size[1], patch_size[2], output_dim[4]]) filter_list = [64, 128, 256, 512, 512, 512] # Layer1 without Batch Normalization disc_out = Conv3D(filters=filter_list[0], kernel_size=kernel_size,kernel_initializer=RandomNormal(mean=0.0, stddev=0.02), bias_initializer=Zeros(), padding=padding, strides=strides)(inputs) disc_out = LeakyReLU(alpha=0.2)(disc_out) # disc_out = BatchNormalization(axis=4)(disc_out) # Original one with Batch normalization # build the rest Layers # Conv -> BN -> LeakyReLU for i, filter_size in enumerate(filter_list[1:]): name = 'disc_conv_{}'.format(i+1) disc_out = Conv3D(name=name, filters=filter_list[i+1],kernel_initializer=RandomNormal(mean=0.0, stddev=0.02), bias_initializer=Zeros(), kernel_size=kernel_size, padding=padding, strides=strides)(disc_out) if batch_norm: disc_out = BatchNormalization(axis=4)(disc_out) # channel_last convention if dropout: disc_out = SpatialDropout3D(rate=0.5)(disc_out) disc_out = LeakyReLU(alpha=0.2)(disc_out) x_flat = Flatten()(disc_out) x = Dense(2, activation='sigmoid',name="disc_dense")(x_flat) patch_GAN_discriminator = Model(input=inputs, output=x, name="patch_gan") return patch_GAN_discriminator
def create_convolution_block(input_layer, n_filters, batch_normalization=False, kernel=(3, 3, 3), activation=None, padding='same', strides=(1, 1, 1), instance_normalization=False, dropout=False): """ :param strides: :param input_layer: :param n_filters: :param batch_normalization: :param kernel: :param activation: Keras activation layer to use. (default is 'relu') :param padding: :return: """ layer = Conv3D(n_filters, kernel, padding=padding, strides=strides)(input_layer) if batch_normalization: layer = BatchNormalization(axis=4)(layer) elif instance_normalization: try: from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization except ImportError: raise ImportError("Install keras_contrib in order to use instance normalization." "\nTry: pip install git+https://www.github.com/farizrahman4u/keras-contrib.git") layer = InstanceNormalization(axis=4)(layer) if dropout: layer = SpatialDropout3D(rate=0.5)(layer) if activation is None: return Activation('relu')(layer) else: return activation()(layer)
def Block(model_in, filters=settings.options.filters, add=True, drop=False): kreg = None wreg = None if settings.options.l1reg: kreg = l1(settings.options.l1reg) # model = DepthwiseConv3D( \ # kernel_size=(3,3,3), # padding='same', # depth_multiplier=1, # activation=settings.options.activation, # kernel_regularizer=kreg )(model_in) # model = Conv3D( \ # filters=filters, # kernel_size=(1,1,1), # padding='same', # activation='linear', # kernel_regularizer=wreg, # use_bias=True)(model) model = Conv3D( \ filters=filters, kernel_size=(3,3,3), padding='same', activation=settings.options.activation, kernel_regularizer=kreg, use_bias=True)(model_in) if drop: model = SpatialDropout3D(settings.options.dropout)(model) if add: model = Add()([model_in, model]) return model
def get_unet(_num_classes=1): _depth = settings.options.depth _filters = settings.options.filters indim = (settings._ny, settings._nx, settings.options.thickness) layer_in = Input(shape=(*indim, 1)) layer_mid = Conv3D( \ filters=_filters, kernel_size=(3,3,3), padding='same', activation=settings.options.activation )(layer_in) layer_mid = Add()([layer_in, layer_mid]) layer_mid = module_mid(layer_mid, depth=_depth) if settings.options.dropout > 0.0: layer_mid = SpatialDropout3D(settings.options.dropout)(layer_mid) layer_out = Conv3D(\ filters=_num_classes, kernel_size=(1,1,1), padding='same', # activation='softmax', activation='sigmoid', use_bias=True)(layer_mid) model = Model(inputs=layer_in, outputs=layer_out) if settings.options.gpu > 1: return multi_gpu_model(model, gpus=settings.options.gpu) else: return model
def addConvBNSequential(model_in, filters=32): print(model_in.shape) if settings.options.batchnorm: model_in = BatchNormalization()(model_in) if settings.options.dropout > 0.0: if settings.options.D3: model_in = SpatialDropout3D(settings.options.dropout)(model_in) else: model_in = SpatialDropout2D(settings.options.dropout)(model_in) if settings.options.D3: model = Conv3D(filters=filters, kernel_size=(3, 3, 3), padding='same', activation=settings.options.activation)(model_in) else: model = Conv2D(filters=filters, kernel_size=(3, 3), padding='same', activation=settings.options.activation)(model_in) if settings.options.fanout: model = concatenate([model_in, model]) else: model = Add()([model_in, model]) return model
def _conv_block(x, filters): bn_scale = PARAMS['bn_scale'] activation = PARAMS['activation'] kernel_initializer = PARAMS['kernel_initializer'] weight_decay = PARAMS['weight_decay'] bottleneck = PARAMS['bottleneck'] dropout_rate = PARAMS['dropout_rate'] x = BatchNormalization(scale=bn_scale, axis=-1)(x) x = activation()(x) x = Conv3D(filters * bottleneck, kernel_size=(1, 1, 1), padding='same', use_bias=False, kernel_initializer=kernel_initializer, kernel_regularizer=l2_penalty(weight_decay))(x) if dropout_rate is not None: x = SpatialDropout3D(dropout_rate)(x) x = BatchNormalization(scale=bn_scale, axis=-1)(x) x = activation()(x) x = Conv3D(filters, kernel_size=(3, 3, 3), padding='same', use_bias=True, kernel_initializer=kernel_initializer, kernel_regularizer=l2_penalty(weight_decay))(x) return x
def create_convolution_block_up(input_layer,skip_conn, n_filters, batch_normalization=True, kernel_size=(4, 4, 4), activation='relu', padding='same', strides=(2, 2, 2), instance_normalization=False, dropout=True): # 3DConv + Normalization + Activation # Instance Normalization is said to perform better than Batch Normalization init = RandomNormal(mean=0.0, stddev=0.02) # new layer = Conv3DTranspose(n_filters, kernel_size, padding=padding, kernel_initializer=init, strides=strides)(input_layer) if batch_normalization: layer = BatchNormalization(axis=4)(layer) # channel_last convention # elif instance_normalization: elif instance_normalization: try: from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization except ImportError: raise ImportError("Install keras_contrib in order to use instance normalization." "\nTry: pip install git+https://www.github.com/farizrahman4u/keras-contrib.git") layer = InstanceNormalization(axis=4)(layer) if dropout: layer = SpatialDropout3D(rate=0.5)(layer) layer = concatenate([layer, skip_conn], axis=4) layer = Activation(activation)(layer) return layer
def make_keras_functions(self): """ Make all Keras functions for this layer, including its own, dropout, etc. """ from keras.layers import (TimeDistributed, Bidirectional, Dropout, SpatialDropout1D, SpatialDropout2D, SpatialDropout3D) k = self.make_keras_function() # can override ### wrap layer: if self.bidirectional: if self.bidirectional is True: k = Bidirectional(k, name=self.name) else: k = Bidirectional(k, merge_mode=self.bidirectional, name=self.name) if self.time_distributed: k = TimeDistributed(k, name=self.name) ### sequence: k = [k] if self.dropout > 0: if self.dropout_dim == 0: k += [Dropout(self.dropout)] elif self.dropout_dim == 1: k += [SpatialDropout1D(self.dropout)] elif self.dropout_dim == 2: k += [SpatialDropout2D(self.dropout)] elif self.dropout_dim == 3: k += [SpatialDropout3D(self.dropout)] return k
def create_context_module(input_layer, n_level_filters, dropout_rate=0.3): convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters) dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters) return convolution2
def create_regularized_context_module(input_layer, n_level_filters, ewc, index, dropout_rate=0.3, data_format="channels_first"): convolution1 = ewc.create_regularized_convolution_block(input_layer=input_layer, n_filters=n_level_filters, index=index) index += 1 dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) convolution2 = ewc.create_regularized_convolution_block(input_layer=dropout, n_filters=n_level_filters, index=index) return convolution2
def dense_block(filters, D1): channel_axis = -1 DB2 = BatchNormalization(axis=channel_axis)(D1) DB2 = PReLU()(DB2) DB2 = Conv3D(filters, 3, padding='same', kernel_initializer='he_normal')(DB2) DB2 = SpatialDropout3D(rate=0.2, data_format=data_format)(DB2) DB2 = concatenate([D1, DB2], channel_axis) for i in range(3): #block1 4*12=48 DB1 = BatchNormalization(axis=channel_axis)(DB2) DB1 = PReLU()(DB1) DB1 = Conv3D(filters, 3, padding='same', kernel_initializer='he_normal')(DB1) DB1 = SpatialDropout3D(rate=0.2, data_format=data_format)(DB1) DB2 = concatenate([DB2, DB1], channel_axis) return DB2
def HyperDenseNet(kernelshapes, numkernelsperlayer, input_shape, activation_name="sigmoid", dropout_rate=0.3, n_labels=2, optimizer=Adam, initial_learning_rate=5e-4, loss_function="categorical_crossentropy"): n_conv_layer = 0 for kernel in kernelshapes: if len(kernel) == 3: n_conv_layer += 1 layers = [] inputs = Input(input_shape) current_layer = inputs layers.append(current_layer) for i in range(n_conv_layer): current_layer = Conv3D(numkernelsperlayer[i], kernelshapes[i], strides=(1, 1, 1), padding='valid', activation=activation_name, data_format='channels_first')(current_layer) layers.append(current_layer) cropped_layers = [] n_layers = len(layers) for count, layer in enumerate(layers): cropped_layer = Cropping3D(cropping=(n_layers - 1 - count), data_format="channels_first")(layer) cropped_layers.append(cropped_layer) current_layer = Concatenate(axis=1)(cropped_layers) for i in range(n_conv_layer, len(kernelshapes)): current_layer = Conv3D(numkernelsperlayer[i], [1, 1, 1], strides=(1, 1, 1), padding='valid', activation=activation_name, data_format='channels_first')(current_layer) current_layer = SpatialDropout3D( rate=dropout_rate, data_format='channels_first')(current_layer) current_layer = Conv3D(n_labels, [1, 1, 1], strides=(1, 1, 1), padding="valid", activation=None, data_format='channels_first')(current_layer) current_layer = Softmax(axis=1)(current_layer) model = Model(inputs=inputs, outputs=current_layer) model.compile(optimizer=optimizer(lr=initial_learning_rate), loss=loss_function) return model
def SpatialDropoutND(x, **kwargs): """Choose a function based on input size.""" dim = K.ndim(x) if dim == 3: return SpatialDropout1D(**kwargs) elif dim == 4: return SpatialDropout2D(**kwargs) elif dim == 5: return SpatialDropout3D(**kwargs) else: raise Exception('Unsupported input size.')
def create_context_module(input_layer, n_level_filters, dropout_rate=0.3, data_format="channels_first"): # 残差单元:conv_block->dropout->conv_block convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters) dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters) return convolution2
def create_context_module(input_layer, n_level_filters, dropout_rate=0.3, data_format="channels_last"): convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters, kernel=(3, 3, 3)) dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters, kernel=(3, 3, 3)) return convolution2
def conv_block(input_layer, level, n_base_filters, kernel_size, padding, strides, dropout_rate=0.3): n_filters = min(128, (2**level) * n_base_filters) conv = mini_conv_block(input_layer, n_filters, kernel_size, padding, strides) dropout = SpatialDropout3D(rate=dropout_rate, data_format='channels_first')(conv) conv = mini_conv_block(dropout, n_filters, kernel_size, padding) conv = AveragePooling3D()(conv) return conv
def create_context_module(input_layer, n_level_filters, dropout_rate=0.3, data_format="channels_last"): # creates convolutional block to process image features convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters) # dropouts some weight to reduce overfitting of features dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) # Additional convolutional block to process more features and manage feature maps convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters) return convolution2
def add_layer(x, num_filters, d_rate, batch_norm): x = Conv3D(num_filters, kernel_size=(3, 3, 3), padding='same', activation='relu')(x) if d_rate > 0: x = SpatialDropout3D(d_rate)(x) x = Conv3D(num_filters, kernel_size=(3, 3, 3), padding='same')(x) if batch_norm: x = BatchNormalization(axis=-1)(x) x = Activation('relu')(x) return MaxPooling3D(pool_size=(2, 2, 2), strides=2, padding='valid')(x)
def FPM(self, x): x1 = MaxPooling3D((2, 2), strides=(1, 1), padding='same')(x) x1 = Conv3D(64, 1, padding='same')(x1) x2 = Conv3D(64, 3, padding='same')(x) x3 = Conv3D(64, 3, padding='same', dilation_rate=4)(x) x4 = Conv3D(64, 3, padding='same', dilation_rate=8)(x) x5 = Conv3D(64, 3, padding='same', dilation_rate=16)(x) x = keras.layers.concatenate([x1, x2, x3, x4, x5], axis=-1) x = BatchNormalization()(x) x = Activation('relu')(x) x = SpatialDropout3D(0.25)(x) return x
def get_unet(_num_classes=1): _depth = settings.options.depth _filters = settings.options.filters _v = settings.options.v if settings.options.D3: shape_in = (settings._ny, settings._nx, settings.options.thickness, 1) elif settings.options.D25: shape_in = (settings._ny, settings._nx, settings.options.thickness) else: shape_in = (settings._ny, settings._nx, 1) layer_in = Input(shape=shape_in) layer_mid = Activation('linear')(layer_in) layer_mid = ConvBlock(layer_mid, filters=_filters, add=False) for j in range(_v): layer_mid = module_mid(layer_mid, depth=_depth, filters=_filters) if settings.options.D3: if settings.options.dropout > 0.0: layer_mid = SpatialDropout3D(settings.options.dropout)(layer_mid) layer_out = Conv3D(filters=_num_classes, kernel_size=(1, 1, 1), padding='same', activation='sigmoid', use_bias=True)(layer_mid) else: if settings.options.dropout > 0.0: layer_mid = SpatialDropout2D(settings.options.dropout)(layer_mid) layer_out = Conv2D(filters=_num_classes, kernel_size=(1, 1), padding='same', activation='sigmoid', use_bias=True)(layer_mid) #layer_out = Dense(_num_classes, activation='sigmoid', use_bias=True)(layer_mid) model = Model(inputs=layer_in, outputs=layer_out) model.summary() if settings.options.gpu > 1: return multi_gpu_model(model, gpus=settings.options.gpu) else: return model
def addConvBNSequential(model, filters=32): if settings.options.batchnorm: model = BatchNormalization()(model) if settings.options.dropout > 0.0: if settings.options.D3: model = SpatialDropout3D(settings.options.dropout)(model) else: model = SpatialDropout2D(settings.options.dropout)(model) if settings.options.D3: model = Conv3D(filters=filters, kernel_size=(3, 3, 3), padding='same', activation=settings.options.activation)(model) else: model = Conv2D(filters=filters, kernel_size=(3, 3), padding='same', activation=settings.options.activation)(model) return model
def gt_loss_wrapper(e=1e-8): # Wrapper allows more information to be passed def gt_loss(y_true, y_pred): return dice_score(y_true, y_pred) return gt_loss def vae_loss_wrapper(input_shape, z_mean, z_var, L2w=0.1, KLw=0.1): # Wrapper allows more information to be passed def vae_loss(y_true, y_pred): (c, H, W, D) = input_shape num_dims = c * H * W * D L2l = K.mean(K.square(y_true - y_pred), axis=(1, 2, 3, 4)) KLl = (1 / num_dims) * K.sum(K.exp(z_var) + K.square(z_mean) - 1 - z_var, axis=-1) return L2w * KLw * L2l * KLl return vae_loss def block_a(layers, num_filters, format='channels_first'): base = Conv3D(filters=filters, kernel_size=(1, 1, 1), strides=1, data_format=format)(layers) x = Sequential() x.add(Activation('relu')) x.add(Conv3D(filters=filters, kernel_size=(3, 3, 3), strides=1, padding='same', data_format=format)) x.add(Activation('relu')) x.add(Conv3D(filters=filters, kernel_size=(3, 3, 3), strides=1, padding='same', data_format=format)) output = Add()([x, base]) return output def build_model(input_shape=(4, 64, 64, 64), out_channels=3, L2w=0.1, KLw=0.1, exp=1e-8): (channels, H, W, D) = input_shape '''Input''' base = Input(input_shape) x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first')(base) x = SpatialDropout3D(0.2, data_format='channels_first')(x) x_1 = block_a(x, 32, name='x1') x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first')(x_1) x = block_a(x, 64) x_2 = block_a(x, 64) x = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first')(x_2) x = block_a(x, 128) x_3 = block_a(x, 128) x = Conv3D(filters=128, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first')(x_3) x = block_a(x, 256) x = block_a(x, 256) x = block_a(x, 256) x4 = block_a(x, 256) '''Decoder''' x = Conv3D(filters=128, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x4) x = UpSampling3D(size=2, data_format='channels_first')(x) x = Add()([x, x_3]) x = block_a(x, 128) x = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = Add()([x, x_2]) x = block_a(x, 64) x = Conv3D(filters=32, kernel_size=(1, 1, 1),strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = Add()([x, x_1]) x = block_a(x, 32) x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first')(x) '''Output''' ground_truth_out = Conv3D(filters=out_channels, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', activation='sigmoid')(x) '''VAE''' x = Activation('relu')(x) x = Conv3D(filters=16, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first')(x) z_mean = Dense(128)(x) z_var = Dense(128)(x) x = Lambda(sampling)([z_mean, z_var]) x = Dense((channels//4) * (H//16) * (W//16) * (D//16))(x) x = Activation('relu')(x) x = Reshape(((channels//4), (H//16), (W//16), (D//16)))(x) x = Conv3D(filters=256, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = Conv3D( filters=128, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = block_a(x, 128) x = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = block_a(x, 64) x = Conv3D(filters=32, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) x = UpSampling3D(size=2, data_format='channels_first')(x) x = block_a(x, 32) x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first')(x) '''Output''' VAE_out = Conv3D(filters=4, kernel_size=(1, 1, 1), strides=1, data_format='channels_first')(x) # Build and Compile the model model = Model(inp, outputs=[ground_truth_out, VAE_out]) # Create the model model.compile(adam(lr=1e-4), [gt_loss(exp), vae_loss(input_shape, z_mean, z_var, L2w=L2w, KLw=KLw)], metrics=[dice_score]) return model
def model3d_layers(sz=48, alpha=1.5, do_features=False): layers = [] def conv3dparams(**replace_params): params = { 'activation': ELU(), 'border_mode': 'valid', 'init': 'he_normal' } params.update(replace_params) return params layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") sz = int(sz * alpha) # if vsize == (32,32,32): # layers.append( Convolution3D(sz, 3, 3, 3, subsample=(2,2,2), **conv3dparams()) ) # else: layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") # if vsize == (32,32,32): # layers.append( Convolution3D(sz, 3, 3, 3, **conv3dparams()) ) # else: layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(SpatialDropout3D(0.2)) sz = int(sz * alpha) layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(SpatialDropout3D(0.2)) sz = int(sz * alpha) layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 3, 3, 3, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(SpatialDropout3D(0.5)) sz = int(sz * alpha) layers.append(Convolution3D(sz, 2, 2, 2, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append(Convolution3D(sz, 1, 1, 1, **conv3dparams())) layers.append("BatchNormalization") layers.append( Convolution3D(1, 1, 1, 1, **conv3dparams(activation='linear', border_mode='same'))) layers.append(GlobalMaxPooling3D()) layers.append(Activation('sigmoid')) return layers
def build_model(self, input_dim, output_dim): input = Input(shape=input_dim) labels = Input(name='the_labels', shape=[output_dim], dtype='float32') input_length = Input(name='input_length', shape=[1], dtype='int64') label_length = Input(name='label_length', shape=[1], dtype='int64') #STCNN-1 stcnn1_padding = ZeroPadding3D(padding=(1, 2, 2), input_shape=input_dim)(input) stcnn1_convolution = Convolution3D(32, 3, 5, 5, subsample=(1, 2, 2))(stcnn1_padding) stcnn1_bn = BatchNormalization()(stcnn1_convolution) stcnn1_acti = Activation('relu')(stcnn1_bn) #SPATIAL-DROPOUT stcnn1_dp = SpatialDropout3D(0.5)(stcnn1_acti) #MAXPOOLING-1 stcnn1_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn1_dp) #STCNN-2 stcnn2_padding = ZeroPadding3D(padding=(1, 2, 2), input_shape=input_dim)(stcnn1_maxpool) stcnn2_convolution = Convolution3D(64, 3, 5, 5, subsample=(1, 2, 2))(stcnn2_padding) stcnn2_bn = BatchNormalization()(stcnn2_convolution) stcnn2_acti = Activation('relu')(stcnn2_bn) #SPATIAL-DROPOUT stcnn2_dp = SpatialDropout3D(0.5)(stcnn2_acti) #MAXPOOLING-2 stcnn2_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn2_dp) #STCNN-3 stcnn3_padding = ZeroPadding3D(padding=(1, 2, 2), input_shape=input_dim)(stcnn2_maxpool) stcnn3_convolution = Convolution3D(64, 3, 3, 3, subsample=(1, 2, 2))(stcnn2_padding) stcnn3_bn = BatchNormalization()(stcnn2_convolution) stcnn3_acti = Activation('relu')(stcnn2_bn) #SPATIAL-DROPOUT stcnn3_dp = SpatialDropout3D(0.5)(stcnn2_acti) #MAXPOOLING-3 stcnn3_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn2_dp) stcnn3_maxpool_flatten = TimeDistributed(Flatten())(stcnn3_maxpool) # remove lstm layer for word recognition # #Bi-GRU-1 # gru_1 = GRU(256, return_sequences=True, name='gru1')(stcnn3_maxpool_flatten) # gru_1b = GRU(256, return_sequences=True, go_backwards=True, name='gru1_b')(stcnn3_maxpool_flatten) # gru1_merged = merge([gru_1, gru_1b], mode='concat', concat_axis=2) # #Bi-GRU-2 # gru_2 = GRU(256, return_sequences=True, name='gru2')(gru1_merged) # gru_2b = GRU(256, return_sequences=True, go_backwards=True, name='gru2_b')(gru1_merged) # gru2_merged = merge([gru_2, gru_2b], mode='concat', concat_axis=2) # li = TimeDistributed(Dense(28))(gru2_merged) #fc linear layer li = TimeDistributed(Dense(28))(stcnn3_maxpool_flatten) #flatten and to 0-9 li_flatten = Flatten()(li) fc_clf = Dense(output_dim)(li_flatten) #normal loss y_pred = Activation('softmax', name='y_pred')(fc_clf) #ctc loss y_pred_1 = TimeDistributed(Activation('softmax', name='y_pred_1'))(li) loss_out = Lambda(self.ctc_lambda_func, output_shape=(1, ), name='ctc')( [y_pred_1, labels, input_length, label_length]) self.model = Model(input=[input, labels, input_length, label_length], output=[loss_out, y_pred]) if self._complie_on_build: optimizer = Adam(lr=0.0001) self.model.compile(loss={ 'ctc': lambda y_true, y_pred: y_pred, 'y_pred': 'categorical_crossentropy' }, optimizer=optimizer, loss_weights={ 'ctc': 1., 'y_pred': 1. }, metrics={'y_pred': 'accuracy'})
def build_model(input_shape=(4, 160, 192, 128), output_channels=3, weight_L2=0.1, weight_KL=0.1, dice_e=1e-8): """ build_model(input_shape=(4, 160, 192, 128), output_channels=3, weight_L2=0.1, weight_KL=0.1) ------------------------------------------- Creates the model used in the BRATS2018 winning solution by Myronenko A. (https://arxiv.org/pdf/1810.11654.pdf) Parameters ---------- `input_shape`: A 4-tuple, optional. Shape of the input image. Must be a 4D image of shape (c, H, W, D), where, each of H, W and D are divisible by 2^4, and c is divisible by 4. Defaults to the crop size used in the paper, i.e., (4, 160, 192, 128). `output_channels`: An integer, optional. The no. of channels in the output. Defaults to 3 (BraTS 2018 format). `weight_L2`: A real number, optional The weight to be given to the L2 loss term in the loss function. Adjust to get best results for your task. Defaults to 0.1. `weight_KL`: A real number, optional The weight to be given to the KL loss term in the loss function. Adjust to get best results for your task. Defaults to 0.1. `dice_e`: Float, optional A small epsilon term to add in the denominator of dice loss to avoid dividing by zero and possible gradient explosion. This argument will be passed to loss_gt function. Returns ------- `model`: A keras.models.Model instance The created model. """ c, H, W, D = input_shape assert len(input_shape) == 4, "Input shape must be a 4-tuple" assert (c % 4) == 0, "The no. of channels must be divisible by 4" assert (H % 16) == 0 and (W % 16) == 0 and (D % 16) == 0, \ "All the input dimensions must be divisible by 16" # ------------------------------------------------------------------------- # Encoder # ------------------------------------------------------------------------- ## Input Layer inp = Input(input_shape) ## The Initial Block x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first', name='Input_x1')(inp) ## Dropout (0.2) x = SpatialDropout3D(0.2, data_format='channels_first')(x) ## Green Block x1 (output filters = 32) x1 = green_block(x, 32, name='x1') x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first', name='Enc_DownSample_32')(x1) ## Green Block x2 (output filters = 64) x = green_block(x, 64, name='Enc_64_1') x2 = green_block(x, 64, name='x2') x = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first', name='Enc_DownSample_64')(x2) ## Green Blocks x2 (output filters = 128) x = green_block(x, 128, name='Enc_128_1') x3 = green_block(x, 128, name='x3') x = Conv3D(filters=128, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first', name='Enc_DownSample_128')(x3) ## Green Blocks x4 (output filters = 256) x = green_block(x, 256, name='Enc_256_1') x = green_block(x, 256, name='Enc_256_2') x = green_block(x, 256, name='Enc_256_3') x4 = green_block(x, 256, name='x4') # ------------------------------------------------------------------------- # Decoder # ------------------------------------------------------------------------- ## GT (Groud Truth) Part # ------------------------------------------------------------------------- ### Green Block x1 (output filters=128) x = Conv3D(filters=128, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_GT_ReduceDepth_128')(x4) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_128')(x) x = Add(name='Input_Dec_GT_128')([x, x3]) x = green_block(x, 128, name='Dec_GT_128') ### Green Block x1 (output filters=64) x = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_GT_ReduceDepth_64')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_64')(x) x = Add(name='Input_Dec_GT_64')([x, x2]) x = green_block(x, 64, name='Dec_GT_64') ### Green Block x1 (output filters=32) x = Conv3D(filters=32, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_GT_ReduceDepth_32')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_32')(x) x = Add(name='Input_Dec_GT_32')([x, x1]) x = green_block(x, 32, name='Dec_GT_32') ### Blue Block x1 (output filters=32) x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first', name='Input_Dec_GT_Output')(x) ### Output Block out_GT = Conv3D( filters=output_channels, # No. of tumor classes is 3 kernel_size=(1, 1, 1), strides=1, data_format='channels_first', activation='sigmoid', name='Dec_GT_Output')(x) ## VAE (Variational Auto Encoder) Part # ------------------------------------------------------------------------- ### VD Block (Reducing dimensionality of the data) x = GroupNormalization(groups=8, axis=1, name='Dec_VAE_VD_GN')(x4) x = Activation('relu', name='Dec_VAE_VD_relu')(x) x = Conv3D(filters=16, kernel_size=(3, 3, 3), strides=2, padding='same', data_format='channels_first', name='Dec_VAE_VD_Conv3D')(x) # Not mentioned in the paper, but the author used a Flattening layer here. x = Flatten(name='Dec_VAE_VD_Flatten')(x) x = Dense(256, name='Dec_VAE_VD_Dense')(x) ### VDraw Block (Sampling) z_mean = Dense(128, name='Dec_VAE_VDraw_Mean')(x) z_var = Dense(128, name='Dec_VAE_VDraw_Var')(x) x = Lambda(sampling, name='Dec_VAE_VDraw_Sampling')([z_mean, z_var]) ### VU Block (Upsizing back to a depth of 256) x = Dense((c // 4) * (H // 16) * (W // 16) * (D // 16))(x) x = Activation('relu')(x) x = Reshape(((c // 4), (H // 16), (W // 16), (D // 16)))(x) x = Conv3D(filters=256, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_VAE_ReduceDepth_256')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_256')(x) ### Green Block x1 (output filters=128) x = Conv3D(filters=128, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_VAE_ReduceDepth_128')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_128')(x) x = green_block(x, 128, name='Dec_VAE_128') ### Green Block x1 (output filters=64) x = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_VAE_ReduceDepth_64')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_64')(x) x = green_block(x, 64, name='Dec_VAE_64') ### Green Block x1 (output filters=32) x = Conv3D(filters=32, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_VAE_ReduceDepth_32')(x) x = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_32')(x) x = green_block(x, 32, name='Dec_VAE_32') ### Blue Block x1 (output filters=32) x = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', data_format='channels_first', name='Input_Dec_VAE_Output')(x) ### Output Block out_VAE = Conv3D(filters=4, kernel_size=(1, 1, 1), strides=1, data_format='channels_first', name='Dec_VAE_Output')(x) # Build and Compile the model out = out_GT model = Model(inp, outputs=[out, out_VAE]) # Create the model model.compile(Adam(lr=1e-4), [ loss_gt(dice_e), loss_VAE(input_shape, z_mean, z_var, weight_L2=weight_L2, weight_KL=weight_KL) ], metrics=[dice_coefficient]) return model
def build_model(self, input_dim, output_dim): input = Input(shape=input_dim) labels = Input(name='the_labels', shape=[output_dim], dtype='float32') input_length = Input(name='input_length', shape=[1], dtype='int64') label_length = Input(name='label_length', shape=[1], dtype='int64') #STCNN-1 stcnn1_padding = ZeroPadding3D(padding=(1,2,2), input_shape = input_dim)(input) stcnn1_convolution = Convolution3D(32, 3, 5, 5, subsample=(1,2,2))(stcnn1_padding) stcnn1_bn = BatchNormalization()(stcnn1_convolution) stcnn1_acti = Activation('relu')(stcnn1_bn) #SPATIAL-DROPOUT stcnn1_dp = SpatialDropout3D(0.5)(stcnn1_acti) #MAXPOOLING-1 stcnn1_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn1_dp) #STCNN-2 stcnn2_padding = ZeroPadding3D(padding=(1,2,2), input_shape = input_dim)(stcnn1_maxpool) stcnn2_convolution = Convolution3D(64, 3, 5, 5, subsample=(1,2,2))(stcnn2_padding) stcnn2_bn = BatchNormalization()(stcnn2_convolution) stcnn2_acti = Activation('relu')(stcnn2_bn) #SPATIAL-DROPOUT stcnn2_dp = SpatialDropout3D(0.5)(stcnn2_acti) #MAXPOOLING-2 stcnn2_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn2_dp) #STCNN-3 stcnn3_padding = ZeroPadding3D(padding=(1,2,2), input_shape = input_dim)(stcnn2_maxpool) stcnn3_convolution = Convolution3D(64, 3, 3, 3, subsample=(1,2,2))(stcnn2_padding) stcnn3_bn = BatchNormalization()(stcnn2_convolution) stcnn3_acti = Activation('relu')(stcnn2_bn) #SPATIAL-DROPOUT stcnn3_dp = SpatialDropout3D(0.5)(stcnn2_acti) #MAXPOOLING-3 stcnn3_maxpool = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2))(stcnn2_dp) stcnn3_maxpool_flatten = TimeDistributed(Flatten())(stcnn3_maxpool) #Bi-GRU-1 bigru1 = BiGRU(stcnn3_maxpool_flatten, 512) #Bi-GRU-2 bigru2 = BiGRU(bigru1, 512) #fc linear layer li = TimeDistributed(Dense(28))(stcnn3_maxpool_flatten) #flatten and to 0-9 li_flatten = Flatten()(li) fc_clf = Dense(output_dim)(li_flatten) y_pred = Activation('softmax', name='y_pred')(fc_clf) y_pred_1 = Activation('softmax', name='y_pred_1')(li) loss_out = Lambda(self.ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred_1, labels, input_length, label_length]) self.model = Model(input=[input, labels, input_length, label_length], output=[loss_out,y_pred]) if self._complie_on_build: optimizer = Adam(lr=0.0001) self.model.compile(loss={'ctc': lambda y_true, y_pred: y_pred,'y_pred':'categorical_crossentropy'}, optimizer=optimizer, loss_weights={'ctc':1., 'y_pred':0.5}, metrics={'y_pred':'accuracy'})
def train_3dnet(X, Y, test_size=0.1, validation_split=0.1, random_state=0, layer_type=('conv3d', 'conv3d'), nconvlayers=2, conv_nfilters=(40, 80), kernel_sizes=((3, 3, 3), (2, 2, 2)), conv_strides=((1, 1, 1), (1, 1, 1)), conv_padding='same', dilation_rate=((1, 1, 1), (1, 1, 1)), pool_layers=2, pool_func='ave', pool_sizes=((2, 2, 2), (2, 2, 2)), pool_strides=((2, 2, 2), (2, 2, 2)), dense_units=128, rec_hidden_states=64, activation='relu', learnrate=0.003, batchsize=64, epochs=20, patience=2, min_delta=0.01, retrain=None, verb=1, summary=True, gpu_id='0', plot='tb', tb_path='./logs', full_output=False): """ 3D Convolutional network or Convolutional LSTM network for SODINN-PW and SODINN-SVD. Parameters ---------- ... layer_type : {'conv3d', 'clstm'} str optional batchsize : Batch size per GPU (no need to increase it when ``ngpus`` > 1). """ clear_session() graph = get_default_graph() config = tf.ConfigProto() # Don't pre-allocate memory; allocate as-needed config.gpu_options.allow_growth = True # Only allow a total of half the GPU memory to be allocated # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.visible_device_list = gpu_id session = tf.Session(graph=graph, config=config) set_session(session) ngpus = len(gpu_id.split(',')) batchsize *= ngpus if not nconvlayers == len(conv_nfilters): raise ValueError('`conv_nfilters` has a wrong length') if not nconvlayers == len(kernel_sizes): raise ValueError('`kernel_sizes` has a wrong length') if not nconvlayers == len(conv_strides): raise ValueError('`conv_strides` has a wrong length') if not nconvlayers == len(dilation_rate): raise ValueError('`dilation_rate` has a wrong length') if pool_layers > 0: if not pool_layers == len(pool_sizes): raise ValueError('`pool_sizes` has a wrong length') if pool_strides is not None: if not pool_layers == len(pool_strides): raise ValueError('`pool_strides` has a wrong length') else: pool_strides = [None] * pool_layers if isinstance(layer_type, str): layer_type = [layer_type for _ in range(nconvlayers)] starttime = time_ini() # Mixed train/test sets with Sklearn split res = train_test_split(X, Y, test_size=test_size, random_state=random_state) X_train, X_test, y_train, y_test = res msg = 'Zeros in train: {} | Ones in train: {}' print(msg.format(y_train.tolist().count(0), y_train.tolist().count(1))) msg = 'Zeros in test: {} | Ones in test: {}' print(msg.format(y_test.tolist().count(0), y_test.tolist().count(1))) # adding the "channels" dimension (1) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], X_train.shape[3], 1) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], X_test.shape[3], 1) print("\nShapes of train and test:") print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, '\n') # -------------------------------------------------------------------------- if retrain is not None: M = retrain # Training the network M.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=learnrate, decay=1e-2)) early_stopping = EarlyStopping(monitor='val_loss', patience=patience, min_delta=min_delta, verbose=verb) hist = M.fit(X_train, y_train, batch_size=batchsize, epochs=epochs, initial_epoch=0, verbose=verb, validation_split=0.1, callbacks=[early_stopping], shuffle=True) score = M.evaluate(X_test, y_test, verbose=verb) print('\n Test score/loss:', score[0]) print(' Test accuracy:', score[1]) timing(starttime) fintime = time_fin(starttime) if full_output: return M, hist.history, score, fintime else: return M # -------------------------------------------------------------------------- # Creating the NN model if ngpus > 1: with tf.device('/cpu:0'): M = Sequential() else: M = Sequential() kernel_init = 'glorot_uniform' bias_init = 'random_normal' rec_act = 'hard_sigmoid' rec_init = 'orthogonal' temp_dim = X_train.shape[1] # npcs or pw slices patch_size = X_train.shape[2] input_shape = (temp_dim, patch_size, patch_size, 1) # Stack of Conv3d, (B)CLSTM, (B)LRCN or (B)GRCN layers for i in range(nconvlayers): if layer_type[i] in ('conv3d', 'clstm', 'bclstm'): if pool_func == 'ave': pooling_func = AveragePooling3D elif pool_func == 'max': pooling_func = MaxPooling3D elif layer_type[i] in ('lrcn', 'blrcn', 'grcn', 'bgrcn'): if pool_func == 'ave': pooling_func = AveragePooling2D elif pool_func == 'max': pooling_func = MaxPooling2D else: raise ValueError('pool_func is not recognized') if layer_type[i] == 'conv3d': if not len(kernel_sizes[i]) == 3: raise ValueError( 'Kernel sizes for Conv3d are tuples of 3 values') if not len(conv_strides[i]) == 3: raise ValueError('Strides for Conv3d are tuples of 3 values') if not len(dilation_rate[i]) == 3: raise ValueError('Dilation for Conv3d is a tuple of 3 values') if i == 0: M.add( Conv3D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, bias_initializer=bias_init, name='conv3d_layer1', dilation_rate=dilation_rate[i], data_format='channels_last', input_shape=input_shape)) M.add(SpatialDropout3D(0.5)) else: M.add( Conv3D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, dilation_rate=dilation_rate[i], name='conv3d_layer' + str(i + 1))) M.add(SpatialDropout3D(0.25)) M.add(Activation(activation, name='activ_layer' + str(i + 1))) if pool_layers != 0: M.add( pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')) pool_layers -= 1 M.add(Dropout(rate=0.25, name='dropout_layer' + str(i + 1))) elif layer_type[i] == 'clstm': msg = 'are tuples of 2 integers' if not len(kernel_sizes[i]) == 2: raise ValueError('Kernel sizes for ConvLSTM' + msg) if not len(conv_strides[i]) == 2: raise ValueError('Strides for ConvLSTM') if not len(dilation_rate[i]) == 2: raise ValueError('Dilation rates for ConvLSTM') if i == 0: M.add( ConvLSTM2D( filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, input_shape=input_shape, name='convlstm_layer1', return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True, # TODO: Errors when using dropout, Keras bug? dropout=0.0, recurrent_dropout=0.0)) M.add(SpatialDropout3D(0.5)) else: M.add( ConvLSTM2D( filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, name='convlstm_layer' + str(i + 1), return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True, # TODO: Errors when using dropout, Keras bug? dropout=0.0, recurrent_dropout=0.0)) M.add(SpatialDropout3D(0.25)) if pool_layers != 0: M.add( pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')) pool_layers -= 1 elif layer_type[i] == 'bclstm': msg = 'are tuples of 2 integers' if not len(kernel_sizes[i]) == 2: raise ValueError('Kernel sizes for ConvLSTM' + msg) if not len(conv_strides[i]) == 2: raise ValueError('Strides for ConvLSTM') if not len(dilation_rate[i]) == 2: raise ValueError('Dilation rates for ConvLSTM') if i == 0: M.add( Bidirectional(ConvLSTM2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, name='convlstm_layer1', return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True), input_shape=input_shape)) M.add(SpatialDropout3D(0.5)) else: M.add( Bidirectional( ConvLSTM2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, name='convlstm_layer' + str(i + 1), return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True))) M.add(SpatialDropout3D(0.25)) if pool_layers != 0: M.add( pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')) pool_layers -= 1 elif layer_type[i] in ('lrcn', 'blrcn', 'grcn', 'bgrcn'): if not len(kernel_sizes[i]) == 2: raise ValueError( 'Kernel sizes for LRCN are tuples of 2 values') if not len(conv_strides[i]) == 2: raise ValueError('Strides for LRCN are tuples of 2 values') if not len(dilation_rate[i]) == 2: raise ValueError('Dilation for LRCN is a tuple of 2 values') if i == 0: # TimeDistributed wrapper applies a layer to every temporal # slice of an input. The input should be at least 3D and the # dimension of index one will be considered to be the temporal # dimension. M.add( TimeDistributed(Conv2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, name='lrcn_layer1', activation=activation), input_shape=input_shape)) # This version performs the same function as Dropout, however it # drops entire 2D feature maps instead of individual elements. M.add(TimeDistributed(SpatialDropout2D(0.5))) else: M.add( TimeDistributed( Conv2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, name='lrcn_layer' + str(i + 1), activation=activation))) M.add(TimeDistributed(SpatialDropout2D(0.25))) if pool_layers != 0: M.add( TimeDistributed( pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid'))) pool_layers -= 1 # (B)LRCN or (B)GRCN on Conv2d extracted features if layer_type[-1] == 'lrcn': M.add(TimeDistributed(Flatten(name='flatten'))) M.add(Dropout(0.5, name='dropout_flatten')) M.add( CuDNNLSTM(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False)) M.add(Dropout(0.5, name='dropout_lstm')) elif layer_type[-1] == 'blrcn': M.add(TimeDistributed(Flatten(name='flatten'))) M.add(Dropout(0.5, name='dropout_flatten')) M.add( Bidirectional( LSTM( rec_hidden_states, # TODO: bug CuDNNLSTM? kernel_initializer=kernel_init, return_sequences=False))) M.add(Dropout(0.5, name='dropout_lstm')) elif layer_type[-1] == 'grcn': M.add(TimeDistributed(Flatten(name='flatten'))) M.add(Dropout(0.5, name='dropout_flatten')) M.add( CuDNNGRU(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False)) M.add(Dropout(0.5, name='dropout_lstm')) elif layer_type[-1] == 'bgrcn': M.add(TimeDistributed(Flatten(name='flatten'))) M.add(Dropout(0.5, name='dropout_flatten')) M.add( Bidirectional( CuDNNGRU(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False))) M.add(Dropout(0.5, name='dropout_lstm')) # Otherwise we just flatten and go to dense layers else: M.add(Flatten(name='flatten')) # Fully-connected or dense layer M.add(Dense(units=dense_units, name='dense_layer')) M.add(Activation(activation, name='activ_dense')) M.add(Dropout(rate=0.5, name='dropout_dense')) # Sigmoid unit M.add(Dense(units=1, name='dense_1unit')) M.add(Activation('sigmoid', name='activ_out')) if summary: M.summary() # Callbacks early_stopping = EarlyStopping(monitor='val_loss', patience=patience, min_delta=min_delta, verbose=verb) if plot is not None: if plot == 'tb': tensorboard = TensorBoard(log_dir=tb_path, histogram_freq=1, write_graph=True, write_images=True) callbacks = [early_stopping, tensorboard] elif plot == 'llp': plotlosses = livelossplot.PlotLossesKeras() callbacks = [early_stopping, plotlosses] else: raise ValueError("`plot` method not recognized") else: callbacks = [early_stopping] # Training the network if ngpus > 1: # Multi-GPUs Mpar = multi_gpu_model(M, gpus=ngpus) # Training the network Mpar.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=learnrate, decay=1e-2)) hist = Mpar.fit(X_train, y_train, batch_size=batchsize, epochs=epochs, initial_epoch=0, verbose=verb, shuffle=True, validation_split=validation_split, callbacks=callbacks) score = Mpar.evaluate(X_test, y_test, verbose=verb) else: # Single GPU M.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=learnrate, decay=1e-2)) hist = M.fit(X_train, y_train, batch_size=batchsize, epochs=epochs, initial_epoch=0, verbose=verb, shuffle=True, validation_split=validation_split, callbacks=callbacks) score = M.evaluate(X_test, y_test, verbose=verb) print('\nTest score/loss:', score[0], '\nTest accuracy:', score[1]) timing(starttime) fintime = time_fin(starttime) if full_output: return M, hist.history, score, fintime else: return M
def train_4dnet(X, Y, test_size=0.1, validation_split=0.1, random_state=0, layer_type=('conv3d', 'conv3d'), nconvlayers=2, conv_nfilters=(40, 80), kernel_sizes=((3, 3, 3), (2, 2, 2)), conv_strides=((1, 1, 1), (1, 1, 1)), conv_padding='same', dilation_rate=((1, 1, 1), (1, 1, 1)), pool_layers=2, pool_func='ave', pool_sizes=((2, 2, 2), (2, 2, 2)), pool_strides=((2, 2, 2), (2, 2, 2)), dense_units=128, rec_hidden_states=64, activation='relu', learnrate=0.003, batchsize=64, epochs=20, patience=2, min_delta=0.01, retrain=None, verb=1, summary=True, gpu_id='0', plot='tb', tb_path='./logs', full_output=False): """ """ clear_session() graph = get_default_graph() config = tf.ConfigProto() # Don't pre-allocate memory; allocate as-needed config.gpu_options.allow_growth = True # Only allow a total of half the GPU memory to be allocated # config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.visible_device_list = gpu_id session = tf.Session(graph=graph, config=config) set_session(session) ngpus = len(gpu_id.split(',')) batchsize *= ngpus if not nconvlayers == len(conv_nfilters): raise ValueError('`conv_nfilters` has a wrong length') if not nconvlayers == len(kernel_sizes): raise ValueError('`kernel_sizes` has a wrong length') if not nconvlayers == len(conv_strides): raise ValueError('`conv_strides` has a wrong length') if not nconvlayers == len(dilation_rate): raise ValueError('`dilation_rate` has a wrong length') if pool_layers > 0: if not pool_layers == len(pool_sizes): raise ValueError('`pool_sizes` has a wrong length') if pool_strides is not None: if not pool_layers == len(pool_strides): raise ValueError('`pool_strides` has a wrong length') else: pool_strides = [None] * pool_layers if isinstance(layer_type, str): layer_type = [layer_type for _ in range(nconvlayers)] starttime = time_ini() # Mixed train/test sets with Sklearn split res = train_test_split(X, Y, test_size=test_size, random_state=random_state) X_train, X_test, y_train, y_test = res msg = 'Zeros in train: {} | Ones in train: {}' print(msg.format(y_train.tolist().count(0), y_train.tolist().count(1))) msg = 'Zeros in test: {} | Ones in test: {}' print(msg.format(y_test.tolist().count(0), y_test.tolist().count(1))) # adding the "channels" dimension (1) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], X_train.shape[3], X_train.shape[4], 1) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], X_test.shape[3], X_train.shape[4], 1) print("\nShapes of train and test:") print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, '\n') # -------------------------------------------------------------------------- kernel_init = 'glorot_uniform' bias_init = 'random_normal' rec_act = 'hard_sigmoid' rec_init = 'orthogonal' temp_dim = X_train.shape[1] k_dim = X_train.shape[2] patch_size = X_train.shape[3] input_shape_3d = (temp_dim, patch_size, patch_size, 1) if pool_func == 'ave': pooling_func = AveragePooling3D elif pool_func == 'max': pooling_func = MaxPooling3D # -------------------------------------------------------------------------- # Per branch model # -------------------------------------------------------------------------- input_layer = Input(shape=input_shape_3d, name='input_layer', dtype='float32') for i in range(nconvlayers): # Stack of Conv3d, (B)CLSTM, (B)LRCN or (B)GRCN layers if layer_type[i] in ('conv3d', 'clstm', 'bclstm'): if pool_func == 'ave': pooling_func = AveragePooling3D elif pool_func == 'max': pooling_func = MaxPooling3D elif layer_type[i] in ('lrcn', 'blrcn', 'grcn', 'bgrcn'): if pool_func == 'ave': pooling_func = AveragePooling2D elif pool_func == 'max': pooling_func = MaxPooling2D else: raise ValueError('pool_func is not recognized') if layer_type[i] == 'conv3d': if not len(kernel_sizes[i]) == 3: raise ValueError( 'Kernel sizes for Conv3d are tuples of 3 values') if not len(conv_strides[i]) == 3: raise ValueError('Strides for Conv3d are tuples of 3 values') if not len(dilation_rate[i]) == 3: raise ValueError('Dilation for Conv3d is a tuple of 3 values') if i == 0: x = Conv3D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, bias_initializer=bias_init, name='conv3d_layer1', dilation_rate=dilation_rate[i], data_format='channels_last', input_shape=input_shape_3d)(input_layer) x = SpatialDropout3D(0.5)(x) x = Activation(activation, name='activ_layer1')(x) x = pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')(x) else: x = Conv3D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, bias_initializer=bias_init, name='conv3d_layer' + str(i + 1), dilation_rate=dilation_rate[i])(x) x = SpatialDropout3D(0.25)(x) x = Activation(activation, name='activ_layer' + str(i + 1))(x) x = pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')(x) elif layer_type[i] == 'clstm': msg = 'are tuples of 2 integers' if not len(kernel_sizes[0]) == 2: raise ValueError('Kernel sizes for ConvLSTM' + msg) if not len(conv_strides[0]) == 2: raise ValueError('Strides for ConvLSTM') if not len(dilation_rate[0]) == 2: raise ValueError('Dilation rates for ConvLSTM') if i == 0: x = ConvLSTM2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, input_shape=input_shape_3d, name='convlstm_layer1', return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True, dropout=0.0, recurrent_dropout=0.0)(input_layer) x = SpatialDropout3D(0.5)(x) x = pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid')(x) else: x = ConvLSTM2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, kernel_initializer=kernel_init, name='convlstm_layer' + str(i + 1), return_sequences=True, dilation_rate=dilation_rate[i], activation='tanh', recurrent_activation=rec_act, use_bias=True, recurrent_initializer=rec_init, bias_initializer='zeros', unit_forget_bias=True, dropout=0.0, recurrent_dropout=0.0)(x) x = SpatialDropout3D(0.25)(x) x = pooling_func(pool_size=pool_sizes[1], strides=pool_strides[1], padding='valid')(x) elif layer_type[i] in ('lrcn', 'blrcn', 'grcn', 'bgrcn'): if not len(kernel_sizes[i]) == 2: raise ValueError( 'Kernel sizes for LRCN are tuples of 2 values') if not len(conv_strides[i]) == 2: raise ValueError('Strides for LRCN are tuples of 2 values') if not len(dilation_rate[i]) == 2: raise ValueError('Dilation for LRCN is a tuple of 2 values') # TimeDistributed wrapper applies a layer to every temporal # slice of an input. The input should be at least 3D and the # dimension of index one will be considered to be the temporal # dimension. if i == 0: x = TimeDistributed(Conv2D(filters=conv_nfilters[i], kernel_size=kernel_sizes[i], strides=conv_strides[i], padding=conv_padding, name='lrcn_layer1', activation=activation), input_shape=input_shape_3d)(input_layer) # This version performs the same function as Dropout, however it # drops entire 2D feature maps instead of individual elements. x = TimeDistributed(SpatialDropout2D(0.5))(x) x = TimeDistributed( pooling_func(pool_size=pool_sizes[i], strides=pool_strides[i], padding='valid'))(x) else: x = TimeDistributed( Conv2D(filters=conv_nfilters[1], kernel_size=kernel_sizes[1], strides=conv_strides[1], padding=conv_padding, name='lrcn_layer' + str(i + 1), activation=activation))(x) x = TimeDistributed(SpatialDropout2D(0.25))(x) x = TimeDistributed( pooling_func(pool_size=pool_sizes[1], strides=pool_strides[1], padding='valid'))(x) # Final layer if layer_type[-1] in ('conv3d', 'clstm'): flatten_layer = Flatten(name='flatten')(x) # Fully-connected or dense layer output = Dense(units=dense_units, name='dense_layer')(flatten_layer) output = Activation(activation, name='activ_dense')(output) output = Dropout(rate=0.5, name='dropout_dense')(output) elif layer_type[-1] in ('lrcn', 'blrcn', 'grcn', 'bgrcn'): output = TimeDistributed(Flatten(name='flatten'))(x) output = Dropout(0.5, name='dropout_flatten')(output) model_branch = KerasModel(inputs=input_layer, outputs=output) # -------------------------------------------------------------------------- # -------------------------------------------------------------------------- # Multi-input model # -------------------------------------------------------------------------- inputs = [] outputs = [] for i in range(k_dim): input_ = Input(shape=input_shape_3d, name='input_' + str(i + 1), dtype='float32') output_ = model_branch(input_) inputs.append(input_) outputs.append(output_) # Concatenating the branches. Shape [samples, time steps, features*k_dim] concatenated = concatenate(outputs) if layer_type[1] == 'lrcn': lstm = CuDNNLSTM(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False)(concatenated) concatenated = Dropout(0.5, name='dropout_lstm')(lstm) elif layer_type[1] == 'blrcn': blstm = Bidirectional( LSTM( rec_hidden_states, # TODO: bug CuDNNLSTM? kernel_initializer=kernel_init, return_sequences=False))(concatenated) concatenated = Dropout(0.5, name='dropout_lstm')(blstm) elif layer_type[1] == 'grcn': gru = CuDNNGRU(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False)(concatenated) concatenated = Dropout(0.5, name='dropout_gru')(gru) elif layer_type[1] == 'bgrcn': bgru = Bidirectional( CuDNNGRU(rec_hidden_states, kernel_initializer=kernel_init, return_sequences=False))(concatenated) concatenated = Dropout(0.5, name='dropout_gru')(bgru) # Sigmoid unit prediction = Dense(units=1, name='sigmoid_output_unit', activation='sigmoid')(concatenated) model_final = KerasModel(inputs=inputs, outputs=prediction) # -------------------------------------------------------------------------- if summary: model_branch.summary() # model_final.summary() # Callbacks early_stopping = EarlyStopping(monitor='val_loss', patience=patience, min_delta=min_delta, verbose=verb) if plot is not None: if plot == 'tb': tensorboard = TensorBoard(log_dir=tb_path, histogram_freq=1, write_graph=True, write_images=True) callbacks = [early_stopping, tensorboard] elif plot == 'llp': plotlosses = livelossplot.PlotLossesKeras() callbacks = [early_stopping, plotlosses] else: raise ValueError("`plot` method not recognized") else: callbacks = [early_stopping] X_train = list(np.moveaxis(X_train, 2, 0)) X_test = list(np.moveaxis(X_test, 2, 0)) # Training the network if ngpus > 1: # Multi-GPUs Mpar = multi_gpu_model(model_final, gpus=ngpus) # Training the network Mpar.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=learnrate, decay=1e-2)) hist = Mpar.fit(X_train, y_train, batch_size=batchsize, shuffle=True, epochs=epochs, initial_epoch=0, verbose=verb, validation_split=validation_split, callbacks=callbacks) score = Mpar.evaluate(X_test, y_test, verbose=verb) else: # Single GPU model_final.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=Adam(lr=learnrate, decay=1e-2)) hist = model_final.fit(X_train, y_train, batch_size=batchsize, epochs=epochs, initial_epoch=0, verbose=verb, validation_split=validation_split, callbacks=callbacks, shuffle=True) score = model_final.evaluate(X_test, y_test, verbose=verb) print('\nTest score/loss:', score[0], '\nTest accuracy:', score[1]) timing(starttime) fintime = time_fin(starttime) if full_output: return model_final, hist.history, score, fintime else: return model_final
def build_train_and_evaluate(train_repo, validation_repo, test_repo, folder_logs, filepath_checkpoints, filepath_model): print("build training and validation sequences...") train_sequence = res_val_seq(train_repo, batch_size=32) validation_sequence = res_val_seq(validation_repo, batch_size=32) print("build model...") model_input = Input(shape=(com_cfg.MODEL_INPUT_SHAPE + (1, )), dtype='float32') left_conv_forward_zero_first = Conv3D( filters=com_cfg.LAYER_FILTERS[0], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(model_input) print("left_conv_forward_zero_first.shape == " + str(left_conv_forward_zero_first.shape) + " && dtype == " + str(left_conv_forward_zero_first.dtype)) left_conv_forward_zero_second = Conv3D( filters=com_cfg.LAYER_FILTERS[1], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_conv_forward_zero_first) print("left_conv_forward_zero_second.shape == " + str(left_conv_forward_zero_second.shape) + " && dtype == " + str(left_conv_forward_zero_second.dtype)) left_dropout_zero = SpatialDropout3D( rate=com_cfg.DROPOUT_RATES[0])(left_conv_forward_zero_second) print("left_dropout_zero.shape == " + str(left_dropout_zero.shape) + " && dtype == " + str(left_dropout_zero.dtype)) left_pool_down_zero = MaxPooling3D(pool_size=2, strides=2, padding='same')(left_dropout_zero) print("left_pool_down_zero.shape == " + str(left_pool_down_zero.shape) + " && dtype == " + str(left_pool_down_zero.dtype)) left_conv_forward_one_first = Conv3D( filters=com_cfg.LAYER_FILTERS[1], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_pool_down_zero) print("left_conv_forward_one_first.shape == " + str(left_conv_forward_one_first.shape) + " && dtype == " + str(left_conv_forward_one_first.dtype)) left_conv_forward_one_second = Conv3D( filters=com_cfg.LAYER_FILTERS[2], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_conv_forward_one_first) print("left_conv_forward_one_second.shape == " + str(left_conv_forward_one_second.shape) + " && dtype == " + str(left_conv_forward_one_second.dtype)) left_dropout_one = SpatialDropout3D( rate=com_cfg.DROPOUT_RATES[1])(left_conv_forward_one_second) print("left_dropout_one.shape == " + str(left_dropout_one.shape) + " && dtype == " + str(left_dropout_one.dtype)) left_pool_down_one = MaxPooling3D(pool_size=2, strides=2, padding='same')(left_dropout_one) print("left_pool_down_one.shape == " + str(left_pool_down_one.shape) + " && dtype == " + str(left_pool_down_one.dtype)) left_conv_forward_two_first = Conv3D( filters=com_cfg.LAYER_FILTERS[2], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_pool_down_one) print("left_conv_forward_two_first.shape == " + str(left_conv_forward_two_first.shape) + " && dtype == " + str(left_conv_forward_two_first.dtype)) left_conv_forward_two_second = Conv3D( filters=com_cfg.LAYER_FILTERS[3], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_conv_forward_two_first) print("left_conv_forward_two_second.shape == " + str(left_conv_forward_two_second.shape) + " && dtype == " + str(left_conv_forward_two_second.dtype)) left_dropout_two = SpatialDropout3D( rate=com_cfg.DROPOUT_RATES[2])(left_conv_forward_two_second) print("left_dropout_two.shape == " + str(left_dropout_two.shape) + " && dtype == " + str(left_dropout_two.dtype)) left_pool_down_two = MaxPooling3D(pool_size=2, strides=2, padding='same')(left_dropout_two) print("left_pool_down_two.shape == " + str(left_pool_down_two.shape) + " && dtype == " + str(left_pool_down_two.dtype)) left_conv_forward_three_first = Conv3D( filters=com_cfg.LAYER_FILTERS[3], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_pool_down_two) print("left_conv_forward_three_first.shape == " + str(left_conv_forward_three_first.shape) + " && dtype == " + str(left_conv_forward_three_first.dtype)) left_conv_forward_three_second = Conv3D( filters=com_cfg.LAYER_FILTERS[4], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_conv_forward_three_first) print("left_conv_forward_three_second.shape == " + str(left_conv_forward_three_second.shape) + " && dtype == " + str(left_conv_forward_three_second.dtype)) left_dropout_three = SpatialDropout3D( rate=com_cfg.DROPOUT_RATES[3])(left_conv_forward_three_second) print("left_dropout_three.shape == " + str(left_dropout_three.shape) + " && dtype == " + str(left_dropout_three.dtype)) left_pool_down_three = MaxPooling3D(pool_size=2, strides=2, padding='same')(left_dropout_three) print("left_pool_down_three.shape == " + str(left_pool_down_three.shape) + " && dtype == " + str(left_pool_down_three.dtype)) root_conv_forward_four_first = Conv3D( filters=com_cfg.LAYER_FILTERS[4], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(left_pool_down_three) print("root_conv_forward_four_first.shape == " + str(root_conv_forward_four_first.shape) + " && dtype == " + str(root_conv_forward_four_first.dtype)) root_conv_forward_four_second = Conv3D( filters=com_cfg.LAYER_FILTERS[4], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(root_conv_forward_four_first) print("root_conv_forward_four_second.shape == " + str(root_conv_forward_four_second.shape) + " && dtype == " + str(root_conv_forward_four_second.dtype)) root_dropout_four = SpatialDropout3D( rate=com_cfg.DROPOUT_RATES[4])(root_conv_forward_four_second) print("root_dropout_four.shape == " + str(root_dropout_four.shape) + " && dtype == " + str(root_dropout_four.dtype)) right_up_sample_three = UpSampling3D(size=2)(root_dropout_four) print("right_up_sample_three.shape == " + str(right_up_sample_three.shape) + " && dtype == " + str(right_up_sample_three.dtype)) right_conv_up_three = Conv3D( filters=com_cfg.LAYER_FILTERS[3], kernel_size=2, padding='same', activation='relu', kernel_initializer='he_normal')(right_up_sample_three) print("right_conv_up_three.shape == " + str(right_conv_up_three.shape) + " && dtype == " + str(right_conv_up_three.dtype)) right_merge_threes = concatenate([left_dropout_three, right_conv_up_three], axis=4) print("right_merge_threes.shape == " + str(right_merge_threes.shape) + " && dtype == " + str(right_merge_threes.dtype)) right_conv_forward_three_first = Conv3D( filters=com_cfg.LAYER_FILTERS[3], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_merge_threes) print("right_conv_forward_three_first.shape == " + str(right_conv_forward_three_first.shape) + " && dtype == " + str(right_conv_forward_three_first.dtype)) right_conv_forward_three_second = Conv3D( filters=com_cfg.LAYER_FILTERS[3], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_conv_forward_three_first) print("right_conv_forward_three_second.shape == " + str(right_conv_forward_three_second.shape) + " && dtype == " + str(right_conv_forward_three_second.dtype)) right_up_sample_two = UpSampling3D(size=2)(right_conv_forward_three_second) print("right_up_sample_two.shape == " + str(right_up_sample_two.shape) + " && dtype == " + str(right_up_sample_two.dtype)) right_conv_up_two = Conv3D( filters=com_cfg.LAYER_FILTERS[2], kernel_size=2, padding='same', activation='relu', kernel_initializer='he_normal')(right_up_sample_two) print("right_conv_up_two.shape == " + str(right_conv_up_two.shape) + " && dtype == " + str(right_conv_up_two.dtype)) right_merge_twos = concatenate([left_dropout_two, right_conv_up_two], axis=4) print("right_merge_twos.shape == " + str(right_merge_twos.shape) + " && dtype == " + str(right_merge_twos.dtype)) right_conv_forward_two_first = Conv3D( filters=com_cfg.LAYER_FILTERS[2], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_merge_twos) print("right_conv_forward_two_first.shape == " + str(right_conv_forward_two_first.shape) + " && dtype == " + str(right_conv_forward_two_first.dtype)) right_conv_forward_two_second = Conv3D( filters=com_cfg.LAYER_FILTERS[2], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_conv_forward_two_first) print("right_conv_forward_two_second.shape == " + str(right_conv_forward_two_second.shape) + " && dtype == " + str(right_conv_forward_two_second.dtype)) right_up_sample_one = UpSampling3D(size=2)(right_conv_forward_two_second) print("right_up_sample_one.shape == " + str(right_up_sample_one.shape) + " && dtype == " + str(right_up_sample_one.dtype)) right_conv_up_one = Conv3D( filters=com_cfg.LAYER_FILTERS[1], kernel_size=2, padding='same', activation='relu', kernel_initializer='he_normal')(right_up_sample_one) print("right_conv_up_one.shape == " + str(right_conv_up_one.shape) + " && dtype == " + str(right_conv_up_one.dtype)) right_merge_ones = concatenate([left_dropout_one, right_conv_up_one], axis=4) print("right_merge_ones.shape == " + str(right_merge_ones.shape) + " && dtype == " + str(right_merge_ones.dtype)) right_conv_forward_one_first = Conv3D( filters=com_cfg.LAYER_FILTERS[1], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_merge_ones) print("right_conv_forward_one_first.shape == " + str(right_conv_forward_one_first.shape) + " && dtype == " + str(right_conv_forward_one_first.dtype)) right_conv_forward_one_second = Conv3D( filters=com_cfg.LAYER_FILTERS[1], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_conv_forward_one_first) print("right_conv_forward_one_second.shape == " + str(right_conv_forward_one_second.shape) + " && dtype == " + str(right_conv_forward_one_second.dtype)) right_up_sample_zero = UpSampling3D(size=2)(right_conv_forward_one_second) print("right_up_sample_zero.shape == " + str(right_up_sample_zero.shape) + " && dtype == " + str(right_up_sample_zero.dtype)) right_conv_up_zero = Conv3D( filters=com_cfg.LAYER_FILTERS[0], kernel_size=2, padding='same', activation='relu', kernel_initializer='he_normal')(right_up_sample_zero) print("right_conv_up_zero.shape == " + str(right_conv_up_zero.shape) + " && dtype == " + str(right_conv_up_zero.dtype)) right_merge_zeros = concatenate([left_dropout_zero, right_conv_up_zero], axis=4) print("right_merge_zeros.shape == " + str(right_merge_zeros.shape) + " && dtype == " + str(right_merge_zeros.dtype)) right_conv_forward_zero_first = Conv3D( filters=com_cfg.LAYER_FILTERS[0], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_merge_zeros) print("right_conv_forward_zero_first.shape == " + str(right_conv_forward_zero_first.shape) + " && dtype == " + str(right_conv_forward_zero_first.dtype)) right_conv_forward_zero_second = Conv3D( filters=com_cfg.LAYER_FILTERS[0], kernel_size=3, padding='same', activation='relu', kernel_initializer='he_normal')(right_conv_forward_zero_first) print("right_conv_forward_zero_second.shape == " + str(right_conv_forward_zero_second.shape) + " && dtype == " + str(right_conv_forward_zero_second.dtype)) logits = Conv3D( filters=10, kernel_size=1, padding='same', activation='softmax', kernel_initializer='he_normal')(right_conv_forward_zero_second) print("logits.shape == " + str(logits.shape) + " && dtype == " + str(logits.dtype)) model = Model(inputs=[model_input], outputs=[logits]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy', 'categorical_crossentropy']) model.summary() print("fit model...") early_stop = EarlyStopping(monitor='val_loss', patience=1000, restore_best_weights=True) tensor_board = TensorBoard(log_dir=folder_logs) checkpoints = ModelCheckpoint(filepath=filepath_checkpoints, verbose=1, save_best_only=True) model.fit_generator(train_sequence, steps_per_epoch=300, epochs=1000000, validation_data=validation_sequence, callbacks=[early_stop, tensor_board, checkpoints]) model.save(filepath_model) print("build evaluation sequence...") test_sequence = res_val_seq(test_repo, batch_size=32) print("evaluate model...") results = model.evaluate_generator(test_sequence) print("results == " + str(results))