def build_model(self): input = Input(batch_shape=self.input_shape, name='input_1') conv1_1 = Convolution2D(64, 3, 3, activation='relu')(input) conv1_2 = Convolution2D(64, 3, 3, activation='relu')(conv1_1) conv1_out = MaxPooling2D()(conv1_2) conv2_1 = Convolution2D(128, 3, 3, activation='relu')(conv1_out) dropout2 = Dropout(0.5)(conv2_1) conv2_2 = Convolution2D(128, 3, 3, activation='relu')(dropout2) conv2_out = MaxPooling2D()(conv2_2) conv3_1 = Convolution2D(256, 3, 3, activation='relu')(conv2_out) dropout3 = Dropout(0.5)(conv3_1) conv3_2 = Convolution2D(256, 3, 3, activation='relu')(dropout3) conv3_out = MaxPooling2D()(conv3_2) conv4_1 = Convolution2D(512, 3, 3, activation='relu')(conv3_out) dropout4 = Dropout(0.5)(conv4_1) conv4_2 = Convolution2D(512, 3, 3, activation='relu')(dropout4) conv4_out = MaxPooling2D()(conv4_2) conv5_1 = Convolution2D(1024, 3, 3, activation='relu')(conv4_out) dropout5 = Dropout(0.5)(conv5_1) conv5_2 = Convolution2D(1024, 3, 3, activation='relu')(dropout5) conv5_out = MaxPooling2D()(conv5_2) up_conv1 = self.upconv2_2(conv5_out, conv4_out, 512) # conv6_out = self.conv3_3(up_conv1, 512) up_conv2 = self.upconv2_2(up_conv1, conv3_out, 256) # conv7_out = self.conv3_3(up_conv2, 256) up_conv3 = self.upconv2_2(up_conv2, conv2_out, 128) # conv8_out = self.conv3_3(up_conv3, 128) up_conv4 = self.upconv2_2(up_conv3, conv1_out, 64) # conv9_out = self.conv3_3(up_conv4, 64) out_shape = [dim.value for dim in input.get_shape()] out_shape = [self.batch_size] + out_shape[1:3] + [self.no_classes] output = Deconvolution2D(self.no_classes, 5, 5, out_shape, subsample=(2, 2), activation='sigmoid', name='class_out')(up_conv4) model = Model(input, output) optimizer = Adam(lr=self.learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) model.compile(optimizer=optimizer, loss={'class_out': 'binary_crossentropy'}, metrics=['binary_accuracy']) if self.weight_file: model.load_weights(self.weight_file) model.summary() return model
def build_model(self): input = Input(batch_shape=self.input_shape, name='input_1') fileter_size = 3 # Block 1 conv1_1 = Convolution2D(64, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv1_1')(input) conv1_2 = Convolution2D(64, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv1_2')(conv1_1) conv1_out = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same', name='pool1')(conv1_2) # Block 2 conv2_1 = Convolution2D(128, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv2_1')(conv1_out) conv2_2 = Convolution2D(128, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv2_2')(conv2_1) conv2_out = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same', name='pool2')(conv2_2) # Block 3 conv3_1 = Convolution2D(256, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv3_1')(conv2_out) conv3_2 = Convolution2D(256, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv3_2')(conv3_1) conv3_3 = Convolution2D(256, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv3_3')(conv3_2) conv3_out = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same', name='pool3')(conv3_3) # Block 4 conv4_1 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv4_1')(conv3_out) conv4_2 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv4_2')(conv4_1) conv4_3 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv4_3')(conv4_2) conv4_out = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same', name='pool4')(conv4_3) # Block 5 conv5_1 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv5_1')(conv4_out) conv5_2 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv5_2')(conv5_1) conv5_3 = Convolution2D(512, fileter_size, fileter_size, activation='relu', border_mode='same', name='conv5_3')(conv5_2) conv5_out = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same', name='pool5')(conv5_3) # Block 6 conv6_1 = Convolution2D(4096, 7, 7, activation='relu', border_mode='same', name='conv6_1')(conv5_out) conv6_out = Dropout(0.5)(conv6_1) # Block 7 conv7_1 = Convolution2D(4096, 1, 1, activation='relu', border_mode='same', name='conv7_1')(conv6_out) conv7_out = Dropout(0.5)(conv7_1) # De1 score_conv7_out = Convolution2D(self.no_classes, 1, 1, border_mode='same')(conv7_out) score_pool4 = Convolution2D(self.no_classes, 1, 1, border_mode='same')(conv4_out) out_shape = [dim.value for dim in score_pool4.get_shape()] up_conv_1 = Deconvolution2D(self.no_classes, 4, 4, output_shape=out_shape, border_mode="same", subsample=(2, 2))(score_conv7_out) upscore_1 = merge([score_pool4, up_conv_1], mode='sum', concat_axis=-1) # De2 score_pool3 = Convolution2D(self.no_classes, 1, 1, border_mode='same')(conv3_out) out_shape = [dim.value for dim in score_pool3.get_shape()] up_conv_2 = Deconvolution2D(self.no_classes, 4, 4, output_shape=out_shape, border_mode="same", subsample=(2, 2))(upscore_1) upscore_2 = merge([score_pool3, up_conv_2], mode='sum', concat_axis=-1) # up_conv1 = self.upconv2_2(conv7_out, conv4_out, 512) # conv6_out = self.convfileter_size_fileter_size(up_conv1, 512) # up_conv2 = self.upconv2_2(up_conv1, conv3_out, 256) # conv7_out = self.convfileter_size_fileter_size(up_conv2, 256) out_shape = [dim.value for dim in input.get_shape()] out_shape = [self.batch_size ] + out_shape[1:fileter_size] + [self.no_classes] output = Deconvolution2D(self.no_classes, 16, 16, output_shape=out_shape, border_mode="same", subsample=(8, 8))(upscore_2) output = Reshape((self.input_shape[1] * self.input_shape[2], self.no_classes))(output) output = Activation(activation='softmax', name='class_out')(output) model = Model(input, output) return model