def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1, nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4, subsample_initial_block=False, pooling=None, activation='softmax'): ''' Build the DenseNet model # Arguments nb_classes: number of classes img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels) include_top: flag to include the final Dense layer depth: number or layers nb_dense_block: number of dense blocks to add to end (generally = 3) growth_rate: number of filters to add per dense block nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate nb_layers_per_block: number of layers in each dense block. Can be a -1, positive integer or a list. If -1, calculates nb_layer_per_block from the depth of the network. If positive integer, a set number of layers per dense block. If list, nb_layer is used as provided. Note that list size must be (nb_dense_block + 1) bottleneck: add bottleneck blocks reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression dropout_rate: dropout rate weight_decay: weight decay rate subsample_initial_block: Changes model type to suit different datasets. Should be set to True for ImageNet, and False for CIFAR datasets. When set to True, the initial convolution will be strided and adds a MaxPooling2D before the initial dense block. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'. Note that if sigmoid is used, classes must be 1. # Returns a keras tensor # Raises ValueError: in case of invalid argument for `reduction` or `nb_dense_block` ''' with K.name_scope('DenseNet'): concat_axis = 1 if K.image_data_format() == 'channels_first' else -1 if reduction != 0.0: if not (reduction <= 1.0 and reduction > 0.0): raise ValueError('`reduction` value must lie between 0.0 and 1.0') # layers in each dense block if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple: nb_layers = list(nb_layers_per_block) # Convert tuple to list if len(nb_layers) != (nb_dense_block): raise ValueError('If `nb_dense_block` is a list, its length must match ' 'the number of layers provided by `nb_layers`.') final_nb_layer = nb_layers[-1] nb_layers = nb_layers[:-1] else: if nb_layers_per_block == -1: assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4 if nb_layers_per_block == -1' count = int((depth - 4) / 3) if bottleneck: count = count // 2 nb_layers = [count for _ in range(nb_dense_block)] final_nb_layer = count else: final_nb_layer = nb_layers_per_block nb_layers = [nb_layers_per_block] * nb_dense_block # compute initial nb_filter if -1, else accept users initial nb_filter if nb_filter <= 0: nb_filter = 2 * growth_rate # compute compression factor compression = 1.0 - reduction # Initial convolution if subsample_initial_block: initial_kernel = (7, 7) initial_strides = (2, 2) else: initial_kernel = (3, 3) initial_strides = (1, 1) x = Conv2D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same', name='initial_conv2D', strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay))(img_input) if subsample_initial_block: x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name='initial_bn')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # Add dense blocks for block_idx in range(nb_dense_block - 1): x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck, dropout_rate=dropout_rate, weight_decay=weight_decay, block_prefix='dense_%i' % block_idx) # add transition_block x = __transition_block(x, nb_filter, compression=compression, weight_decay=weight_decay, block_prefix='tr_%i' % block_idx) nb_filter = int(nb_filter * compression) # The last dense_block does not have a transition_block x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck, dropout_rate=dropout_rate, weight_decay=weight_decay, block_prefix='dense_%i' % (nb_dense_block - 1)) x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name='final_bn')(x) x = Activation('relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dense(nb_classes, activation=activation)(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) if pooling == 'max': x = GlobalMaxPooling2D()(x) return x
def Deeplabv3(img_input, alpha=1., OS=16): # img_input = Input(shape=input_shape) # x=32, 32, 2048 x, atrous_rates, skip1 = Xception(img_input, alpha, OS=OS) # 全部求平均后,再利用expand_dims扩充维度,1x1 b4 = GlobalAveragePooling2D()(x) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) # 压缩filter b4 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='image_pooling')(b4) b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4) b4 = Activation('relu')(b4) size_before = tf.keras.backend.int_shape(x) # 直接利用resize_images扩充hw # b4 = 64,64,256 b4 = Lambda(lambda x: tf.image.resize_images(x, size_before[1:3]))(b4) # 调整通道 b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x) b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0) b0 = Activation('relu', name='aspp0_activation')(b0) # rate值与OS相关,SepConv_BN为先3x3膨胀卷积,再1x1卷积,进行压缩 # 其膨胀率就是rate值 # rate = 6 (12) b1 = SepConv_BN(x, 256, 'aspp1', rate=atrous_rates[0], depth_activation=True, epsilon=1e-5) # rate = 12 (24) b2 = SepConv_BN(x, 256, 'aspp2', rate=atrous_rates[1], depth_activation=True, epsilon=1e-5) # rate = 18 (36) b3 = SepConv_BN(x, 256, 'aspp3', rate=atrous_rates[2], depth_activation=True, epsilon=1e-5) # 其实实际的意义就是对Xception的输出结果进行 x = Concatenate()([b4, b0, b1, b2, b3]) # 利用conv2d压缩 x = Conv2D(256, (1, 1), padding='same', use_bias=False, name='concat_projection')(x) x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x) x = Activation('relu')(x) x = Dropout(0.1)(x) # skip1.shape[1:3] 为 128,128 # skip1 128, 128, 256 x = Lambda(lambda xx: tf.image.resize_images(x, skip1.shape[1:3]))(x) # 128, 128, 48 dec_skip1 = Conv2D(48, (1, 1), padding='same', use_bias=False, name='feature_projection0')(skip1) dec_skip1 = BatchNormalization(name='feature_projection0_BN', epsilon=1e-5)(dec_skip1) dec_skip1 = Activation('relu')(dec_skip1) # 128,128,304 x = Concatenate()([x, dec_skip1]) x = SepConv_BN(x, 256, 'decoder_conv0', depth_activation=True, epsilon=1e-5) x = SepConv_BN(x, 256, 'decoder_conv1', depth_activation=True, epsilon=1e-5) x = Conv2D(4, (1, 1), padding='same')(x) size_before3 = tf.keras.backend.int_shape(img_input) x = Lambda(lambda xx: tf.image.resize_images(xx, size_before3[1:3]))(x) # x = Reshape((-1,4))(x) x = Softmax()(x) inputs = img_input model = Model(inputs, x, name='deeplabv3plus') return model
def train_model(training_imagesize=224, batchsize=32, epochs=25): keras.backend.clear_session() base_model = VGG16(weights='imagenet', include_top=False, input_tensor=Input(shape=(training_imagesize, training_imagesize, 3))) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(100, activation='relu')(x) x = Dense(50, activation='relu')(x) x = Dense(10, activation='relu')(x) preds = Dense(2, activation='softmax')(x) # final layer with softmax activation model = Model(inputs=base_model.input, outputs=preds) # Assigning which layers are to be trainined/unfrozen # for layer in model.layers[:20]: # layer.trainable = True # for layer in model.layers[20:]: # layer.trainable = True train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, vertical_flip=True) train_generator = train_datagen.flow_from_directory('./data/train/', # this is where you specify the path to the main data folder target_size=(training_imagesize, training_imagesize), color_mode='rgb', batch_size=batchsize, class_mode='categorical', shuffle=True) adam_optimizer = Adam(lr=0.0001, decay=0.9) model.compile(optimizer=adam_optimizer, loss='binary_crossentropy', metrics=['accuracy']) # test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) # included in our dependencies test_generator = test_datagen.flow_from_directory('./data/test/', target_size=(training_imagesize, training_imagesize), color_mode='rgb', batch_size=batchsize, class_mode='categorical', shuffle=True) step_size_train = train_generator.n // train_generator.batch_size #Large model support callback to control the amount of gpu ram being used to prevent out of ram error lms_callback = LMSKerasCallback() fit_history = model.fit_generator(generator=train_generator, steps_per_epoch=step_size_train, epochs=epochs, validation_data=test_generator, validation_steps=test_generator.n // test_generator.batch_size, callbacks=[lms_callback]) # Reporting of the model for analysis purposes plt.figure(1, figsize=(15, 8)) plt.subplot(221) plt.plot(fit_history.history['acc']) plt.plot(fit_history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'valid']) plt.subplot(222) plt.plot(fit_history.history['loss']) plt.plot(fit_history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'valid']) plt.savefig("Graphical Outputs.pdf") plt.clf() # clears entire figure trainloss = fit_history.history['loss'] testloss = fit_history.history['val_loss'] trainaccuracy = fit_history.history['acc'] testaccuracy = fit_history.history['val_acc'] np.savetxt("train_loss.csv", trainloss, delimiter=",", fmt='%s') np.savetxt("test_loss.csv", testloss, delimiter=",", fmt='%s') np.savetxt("train_accuracy.csv", trainaccuracy, delimiter=",", fmt='%s') np.savetxt("test_accuracy.csv", testaccuracy, delimiter=",", fmt='%s') # Saved model which is erased from memory so there is no chance of interference model.save('animalmodel.h5') return None
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=(224, 224, 3), pooling="max", classes=2): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, dropout_keep_prob=0.8): """Instantiates the Inception-ResNet v2 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `"image_data_format": "channels_last"` in your Keras config at `~/.keras/keras.json`. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299, instead of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing function is different (i.e., do not use `imagenet_utils.preprocess_input()` with this model. Use `preprocess_input()` defined in this module instead). # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or `'imagenet'` (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is `False` (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `'avg'` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `'max'` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. dropout_keep_prob: dropout keep rate after pooling and before the classification layer, only to be specified if `include_top` is `True`. # Returns A Keras `Model` instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Stem block: 35 x 35 x 192 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid', name='Conv2d_1a_3x3') x = conv2d_bn(x, 32, 3, padding='valid', name='Conv2d_2a_3x3') x = conv2d_bn(x, 64, 3, name='Conv2d_2b_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_3a_3x3')(x) x = conv2d_bn(x, 80, 1, padding='valid', name='Conv2d_3b_1x1') x = conv2d_bn(x, 192, 3, padding='valid', name='Conv2d_4a_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_5a_3x3')(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 name_fmt = partial(_generate_layer_name, prefix='Mixed_5b') branch_0 = conv2d_bn(x, 96, 1, name=name_fmt('Conv2d_1x1', 0)) branch_1 = conv2d_bn(x, 48, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 64, 5, name=name_fmt('Conv2d_0b_5x5', 1)) branch_2 = conv2d_bn(x, 64, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0c_3x3', 2)) branch_pool = AveragePooling2D(3, strides=1, padding='same', name=name_fmt('AvgPool_0a_3x3', 3))(x) branch_pool = conv2d_bn(branch_pool, 64, 1, name=name_fmt('Conv2d_0b_1x1', 3)) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_5b')(branches) # 10x Block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = _inception_resnet_block(x, scale=0.17, block_type='Block35', block_idx=block_idx) # Mixed 6a (Reduction-A block): 17 x 17 x 1088 name_fmt = partial(_generate_layer_name, prefix='Mixed_6a') branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 256, 3, name=name_fmt('Conv2d_0b_3x3', 1)) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 2))(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_6a')(branches) # 20x Block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = _inception_resnet_block(x, scale=0.1, block_type='Block17', block_idx=block_idx) # Mixed 7a (Reduction-B block): 8 x 8 x 2080 name_fmt = partial(_generate_layer_name, prefix='Mixed_7a') branch_0 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 0)) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_2 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 288, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 2)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 3))(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_7a')(branches) # 10x Block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = _inception_resnet_block(x, scale=0.2, block_type='Block8', block_idx=block_idx) x = _inception_resnet_block(x, scale=1., activation=None, block_type='Block8', block_idx=10) # Final convolution block x = conv2d_bn(x, 1536, 1, name='Conv2d_7b_1x1') if include_top: # Classification block x = GlobalAveragePooling2D(name='AvgPool')(x) x = Dropout(1.0 - dropout_keep_prob, name='Dropout')(x) x = Dense(classes, name='Logits')(x) x = Activation('softmax', name='Predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='AvgPool')(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='MaxPool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor` if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model model = Model(inputs, x, name='inception_resnet_v2') # Load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='e693bd0210a403b3192acc6073ad2e96') else: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='d19885ff4a710c122648d3b5c3b684e4') model.load_weights(weights_path) return model
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception v3 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=299, min_size=139, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn(x, 32, 3, 3, padding='valid') x = conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid') x = conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 1: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 3: 17 x 17 x 768 branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # mixed 4: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 128, 1, 1) branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 128, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 160, 1, 1) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 160, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # mixed 7: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 192, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # mixed 8: 8 x 8 x 1280 branch3x3 = conv2d_bn(x, 192, 1, 1) branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn(x, 192, 1, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = conv2d_bn(x, 320, 1, 1) branch3x3 = conv2d_bn(x, 384, 1, 1) branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = conv2d_bn(x, 448, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='inception_v3') # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='9a0d58056eeedaa3f26cb7ebd46da564') else: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='bcbd6486424b2319ff4ef7d526e38f63') model.load_weights(weights_path) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model
## 84.5% with rmsprop/img.aug/dropout ## 86.09% with batchnorm/dropout/img.aug/adam(10)/rmsprop(140) ## InceptionV3 for dp in [0.4, 0.5, 0.6, 0.7]: print("Load Model") K.clear_session() # base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3))) # base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3))) # base_model = VGG19(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3))) base_model = Xception(weights='imagenet', include_top=False, input_tensor=Input(shape=(299, 299, 3))) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dropout(dp)(x) x = Dense(4096)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.7)(x) predictions = Dense(101, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) import time filename = time.strftime("%Y%m%d_%H%M") + "_xception_2xdp_" + str(dp) # serialize model to JSON model_json = model.to_json() with open(filename + "_model.json", "w") as json_file:
def SSD300(input_shape, num_classes=21): """SSD300 architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net2 = MarkNet(input_shape=(64, 64, 3)) net = {} # Block 1 input_tensor = Input(shape=input_shape) # prior layerに引数として渡す際利用する img_size = (input_shape[1], input_shape[0]) net['input'] = input_tensor net['conv1_1'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(net['input']) net['conv1_2'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(net['conv1_1']) net['pool1'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool1')(net['conv1_2']) # Block 2 net['conv2_1'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(net['pool1']) net['conv2_2'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(net['conv2_1']) net['pool2'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool2')(net['conv2_2']) # Block 3 net['conv3_1'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(net['pool2']) net['conv3_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(net['conv3_1']) net['conv3_3'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(net['conv3_2']) net['pool3'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool3')(net['conv3_3']) # Block 4 net['conv4_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(net['pool3']) net['conv4_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(net['conv4_1']) net['conv4_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(net['conv4_2']) net['pool4'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool4')(net['conv4_3']) # Block 5 net['conv5_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_1')(net['pool4']) net['conv5_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_2')(net['conv5_1']) net['conv5_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_3')(net['conv5_2']) net['pool5'] = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same', name='pool5')(net['conv5_3']) # FC6 net['fc6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='fc6')(net['pool5']) # x = Dropout(0.5, name='drop6')(x) # FC7 net['fc7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='fc7')(net['fc6']) # x = Dropout(0.5, name='drop7')(x) # Block 6 net['conv6_1'] = Convolution2D(256, 1, 1, activation='relu', border_mode='same', name='conv6_1')(net['fc7']) net['conv6_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv6_2')(net['conv6_1']) # Block 7 net['conv7_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv7_1')(net['conv6_2']) net['conv7_2'] = ZeroPadding2D()(net['conv7_1']) net['conv7_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='valid', name='conv7_2')(net['conv7_2']) # Block 8 net['conv8_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7_2']) net['conv8_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Last Pool 最終出力 net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2']) from keras.layers import Lambda # Prediction from conv4_3 net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3']) num_priors = 3 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv4_3_norm_mbox_loc')(net['conv4_3_norm']) net['conv4_3_norm_mbox_loc'] = x flatten = Flatten(name='conv4_3_norm_mbox_loc_flat') net['conv4_3_norm_mbox_loc_flat'] = flatten(net['conv4_3_norm_mbox_loc']) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv4_3_norm']) net['conv4_3_norm_mbox_conf'] = x flatten = Flatten(name='conv4_3_norm_mbox_conf_flat') net['conv4_3_norm_mbox_conf_flat'] = flatten(net['conv4_3_norm_mbox_conf']) priorbox = PriorBox(img_size, 30.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_norm_mbox_priorbox') net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm']) # Prediction from fc7 num_priors = 6 net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='fc7_mbox_loc')(net['fc7']) flatten = Flatten(name='fc7_mbox_loc_flat') net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc']) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['fc7']) flatten = Flatten(name='fc7_mbox_conf_flat') net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf']) priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # Prediction from conv6_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x flatten = Flatten(name='conv6_2_mbox_loc_flat') net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc']) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv6_2']) net['conv6_2_mbox_conf'] = x flatten = Flatten(name='conv6_2_mbox_conf_flat') net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # Prediction from conv7_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x flatten = Flatten(name='conv7_2_mbox_loc_flat') net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc']) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv7_2']) net['conv7_2_mbox_conf'] = x flatten = Flatten(name='conv7_2_mbox_conf_flat') net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # Prediction from conv8_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x flatten = Flatten(name='conv8_2_mbox_loc_flat') net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc']) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv8_2']) net['conv8_2_mbox_conf'] = x flatten = Flatten(name='conv8_2_mbox_conf_flat') net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # Prediction from pool6 num_priors = 6 x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6']) net['pool6_mbox_loc_flat'] = x name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) x = Dense(num_priors * num_classes, name=name)(net['pool6']) # Marknetとのmarge # merge = Add()([x, net2['dense2m']]) net['pool6_mbox_conf_flat'] = x # merge#x priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox') if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) net['pool6_reshaped'] = Reshape(target_shape, name='pool6_reshaped')(net['pool6']) net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped']) # Gather all predictions net['mbox_loc'] = concatenate([ net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat'] ], axis=1, name='mbox_loc') net['mbox_conf'] = concatenate([ net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat'] ], axis=1, name='mbox_conf') net['mbox_priorbox'] = concatenate([ net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox'] ], axis=1, name='mbox_priorbox') if hasattr(net['mbox_loc'], '_keras_shape'): num_boxes = net['mbox_loc']._keras_shape[-1] // 4 elif hasattr(net['mbox_loc'], 'int_shape'): num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) # 最終出力 net['predictions'] = concatenate( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], axis=2, name='predictions') model = Model(net['input'], net['predictions']) # モデルの構造プロット keras.utils.plot_model(model, "./ssdmodel.png", show_shapes=True) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def MobileNetV2(input_shape, classes, weight_decay, feat_dropout=0., input_tensor=None): ''' The function defines the MobileNet_V2 structure according to the Input column of Table 2 in the original paper. :param input_shape: size of the input tensor :param classes: number of classes in the data :param weight_decay: hyperparameter for the l2 penalty :param feat_dropout: dropout level applied to the output of the last hidden layer :param input_tensor: Optional input tensor if exists. :return: Keras model defined for classification ''' if input_tensor is not None: img_input = Input(tensor=input_tensor) else: img_input = Input(input_shape) x = conv_block(img_input, 32, weight_decay=weight_decay, name='conv1', strides=(2, 2)) x = InvertedResidualBlock(x, expand=1, out_channels=16, repeats=1, stride=1, weight_decay=weight_decay, block_id=1) x = InvertedResidualBlock(x, expand=6, out_channels=24, repeats=2, stride=2, weight_decay=weight_decay, block_id=2) x = InvertedResidualBlock(x, expand=6, out_channels=32, repeats=3, stride=2, weight_decay=weight_decay, block_id=3) x = InvertedResidualBlock(x, expand=6, out_channels=64, repeats=4, stride=2, weight_decay=weight_decay, block_id=4) x = InvertedResidualBlock(x, expand=6, out_channels=96, repeats=3, stride=1, weight_decay=weight_decay, block_id=5) x = InvertedResidualBlock(x, expand=6, out_channels=160, repeats=3, stride=2, weight_decay=weight_decay, block_id=6) x = InvertedResidualBlock(x, expand=6, out_channels=320, repeats=1, stride=1, weight_decay=weight_decay, block_id=7) x = conv_block(x, 1280, weight_decay=weight_decay, name='conv2', kernel=(1, 1), strides=1) x = GlobalAveragePooling2D()(x) if feat_dropout != 0.: x = Dropout(feat_dropout, name='dropout')(x) x = Dense(classes, kernel_regularizer=l2(weight_decay), name='fc_pred')(x) x = Activation('softmax', name='act_softmax')(x) return Model(inputs=img_input, outputs=x)
def build_model_up(input_shape, n_label): input_img = Input(shape=input_shape) # segmentation network seg_conv1 = conv(input_img, 32, 5, 'seg_conv1_01') seg_conv1 = conv(seg_conv1, 32, 5, 'seg_conv1_02') # 500 seg_maxp1 = MaxPooling2D(pool_size=(2, 2))(seg_conv1) # 250 seg_conv2 = conv(seg_maxp1, 64, 5, 'seg_conv2_01') # 250 seg_conv2 = conv(seg_conv2, 64, 5, 'seg_conv2_02') seg_conv2 = conv(seg_conv2, 64, 5, 'seg_conv2_03') # 250 seg_maxp2 = MaxPooling2D(pool_size=(2, 2))(seg_conv2) # 125 seg_conv3 = conv(seg_maxp2, 64, 5, 'seg_conv3_01') # 125 seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_02') seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_03') seg_conv3 = conv(seg_conv3, 64, 5, 'seg_conv3_04') # 125 seg_maxp3 = MaxPooling2D(pool_size=(2, 2))(seg_conv3) # 62 seg_conv4 = conv(seg_maxp3, 1024, 5, 'seg_conv4_01') # 62 seg_mask = Conv2D(n_label, 1, strides=(1, 1), padding='same', kernel_initializer='he_normal', activation='softmax', name='seg_mask')(seg_conv4) seg_upsp5 = UpSampling2D()(seg_conv4) # 124 seg_upsp5 = ZeroPadding2D(padding=((0, 1), (0, 1)))(seg_upsp5) # 125 seg_upsp5 = concatenate([seg_upsp5, seg_conv3], axis=3) seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_01') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_02') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_03') #seg_conv5 = conv(seg_upsp5, 64, 5, 'seg_conv5_04') # 125 seg_upsp6 = UpSampling2D()(seg_conv5) # 256 seg_upsp6 = concatenate([seg_upsp6, seg_conv2], axis=3) seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_01') #seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_02') #seg_conv6 = conv(seg_upsp6, 64, 5, 'seg_conv6_03') # 250 seg_upsp7 = UpSampling2D()(seg_conv6) # 512 seg_upsp7 = concatenate([seg_upsp7, seg_conv1], axis=3) seg_conv7 = conv(seg_upsp7, 32, 5, 'seg_conv7_01') #seg_conv7 = conv(seg_upsp7, 32, 5, 'seg_conv7_02') # 500 seg_output = Conv2D(n_label, 1, strides=(1, 1), padding='same', kernel_initializer='he_normal', activation='softmax', name='seg_output')(seg_conv7) # decision network dec_conv1 = concatenate([seg_conv4, seg_mask], axis=3) dec_conv1 = MaxPooling2D(pool_size=(2, 2))(dec_conv1) dec_conv2 = conv(dec_conv1, 16, 5, 'dec_conv2_01') dec_conv2 = MaxPooling2D(pool_size=(2, 2))(dec_conv2) dec_conv3 = conv(dec_conv2, 32, 5, 'dec_conv3_01') dec_conv3 = MaxPooling2D(pool_size=(2, 2))(dec_conv3) dec_conv4 = conv(dec_conv3, 64, 5, 'dec_conv4_01') # classification class1 = GlobalMaxPooling2D()(dec_conv4) class2 = GlobalAveragePooling2D()(dec_conv4) class3 = GlobalAveragePooling2D()(seg_mask) class4 = GlobalMaxPooling2D()(seg_mask) classes = concatenate([class1, class2, class3, class4], axis=1) class_output = Dense(n_label, activation='softmax', name='class_output')(classes) model = Model(inputs=input_img, outputs=[seg_output, class_output]) return model
path_to_home = os.path.expanduser("~") path_to_split_datasets = path_to_split_datasets.replace("~", path_to_home) path_to_train = os.path.join(path_to_split_datasets, "train") path_to_validation = os.path.join(path_to_split_datasets, "validation") # parameters for CNN if use_vgg: base_model = VGG(include_top=False, weights=None, input_shape=(64, 64, 13)) else: base_model = DenseNet(include_top=False, weights=None, input_shape=(64, 64, 13)) # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model) # let's add a fully-connected layer if use_vgg: # only in VGG19 a fully connected nn is added for classfication # DenseNet tends to overfitting if using additionally dense layers top_model = Dense(2048, activation='relu')(top_model) top_model = Dense(2048, activation='relu')(top_model) # and a logistic layer predictions = Dense(num_classes, activation='softmax')(top_model) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # print network structure model.summary()
def build_model(seed=None): if seed: np.random.seed(seed) input_layer = Input(shape=SHAPE) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(input_layer) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', dim_ordering='tf')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', dim_ordering='tf')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', dim_ordering='tf')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', dim_ordering='tf')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', dim_ordering='tf')(x) x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(1001, activation='softmax', name='fc10')(x) model = Model(input_layer, x) return model
def squeezenet(data, leaky, exclude_top, res): '''squeezenet implementation with structure as in original paper. note bottleneck replaces number of classes as this is used for regression. ''' bottleneck = 128 if exclude_top: input = data else: input = Input(shape=data.shape[1:]) x = Conv2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1' + res)(input) x = Activation(LeakyReLU(), name='relu_conv1' + res)(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1' + res)(x) x = fire_module(x, fire_id=2, leaky=leaky, res=res, squeeze_param=16, expand_param=64) x = fire_module(x, fire_id=3, leaky=leaky, res=res, squeeze_param=16, expand_param=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3' + res)(x) x = fire_module(x, fire_id=4, leaky=leaky, res=res, squeeze_param=32, expand_param=128) x = fire_module(x, fire_id=5, leaky=leaky, res=res, squeeze_param=32, expand_param=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5' + res)(x) x = fire_module(x, fire_id=6, leaky=leaky, res=res, squeeze_param=48, expand_param=192) x = fire_module(x, fire_id=7, leaky=leaky, res=res, squeeze_param=48, expand_param=192) x = fire_module(x, fire_id=8, leaky=leaky, res=res, squeeze_param=64, expand_param=256) x = fire_module(x, fire_id=9, leaky=leaky, res=res, squeeze_param=64, expand_param=256) x = Dropout(0.5, name='drop9' + res)(x) x = Conv2D(bottleneck, (1, 1), padding='valid', name='conv10' + res)(x) x = Activation('relu', name='relu_conv10' + res)(x) x = GlobalAveragePooling2D()(x) if exclude_top: return x else: output_layer = Dense(2, activation='linear')(x) model = Model(input, output_layer, name='squeezenet') model.compile(loss='mean_absolute_error', optimizer='adam') return model
activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=32, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=64, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=128, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(GlobalAveragePooling2D()) model.add(Dense(num_labels, activation='softmax')) model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam') model.summary() score = model.evaluate(x_test, y_test, verbose=1) accuracy = 100 * score[1] print("Pre-training accuracy: %.4f%%" % accuracy) from keras.callbacks import ModelCheckpoint
def SENet(model_params, input_tensor=None, input_shape=None, include_top=True, num_classes=1000, weights='imagenet', **kwargs): # Instantiates the ResNet, SEResNet architecture. """ Args: include_top: whether to include the FC layer at the top of the network. weights: `None` (random initialization), 'imagenet' or the path to any weights. input_tensor: optional Keras tensor (output of `layers.Input()`) input_shape: tuple, only to be specified if `include_top` is False. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights` or invalid input shape. """ # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) residual_block = model_params.residual_block init_filters = model_params.init_filters bn_params = get_bn_params() # Give the input if input_tensor is None: input = Input(shape=input_shape, name='input') else: if not K.is_keras_tensor(input_tensor): input = Input(tensor=input_tensor, shape=input_shape) else: input = input_tensor x = input if model_params.input_3x3: x = ZeroPadding2D(1)(x) x = Conv2D(init_filters, kernel_size=(3, 3), strides=2, use_bias=False, kernel_initializer='he_uniform')(x) x = BatchNormalization(**bn_params)(x) x = Activation('relu')(x) x = ZeroPadding2D(1)(x) x = Conv2D(init_filters, kernel_size=(3, 3), use_bias=False, kernel_initializer='he_uniform')(x) x = BatchNormalization(**bn_params)(x) x = Activation('relu')(x) x = ZeroPadding2D(1)(x) x = Conv2D(init_filters * 2, kernel_size=(3, 3), use_bias=False, kernel_initializer='he_uniform')(x) x = BatchNormalization(**bn_params)(x) x = Activation('relu')(x) else: x = ZeroPadding2D(3)(x) x = Conv2D(init_filters, kernel_size=(7, 7), strides=2, use_bias=False, kernel_initializer='he_uniform')(x) x = BatchNormalization(**bn_params)(x) x = Activation('relu')(x) x = ZeroPadding2D(1)(x) x = MaxPooling2D(pool_size=(3, 3), strides=2)(x) # Give the body of the ResNet filters = model_params.init_filters * 2 for i, stage in enumerate(model_params.repetitions): # Increase number of filters with each stage filters *= 2 for j in range(stage): # Decrease spatial dimensions for each stage (except the first--maxpool) if i == 0 and j == 0: x = residual_block(filters, reduction=model_params.reduction, strides=1, groups=model_params.groups, is_first=True, **kwargs)(x) elif i != 0 and j == 0: x = residual_block(filters, reduction=model_params.reduction, strides=2, groups=model_params.groups, **kwargs)(x) else: x = residual_block(filters, reduction=model_params.reduction, strides=1, groups=model_params.groups, **kwargs)(x) if include_top: x = GlobalAveragePooling2D()(x) if model_params.dropout is not None: x = Dropout(model_params.dropout)(x) x = Dense(num_classes)(x) x = Activation('softmax', name='output')(x) # Ensure the model considers any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = input model = Model(inputs, x) if weights: if type(weights) == str and os.path.exists(weights): model.load_weights(weights) else: load_model_weights(model, model_params.model_name, weights, num_classes, include_top, **kwargs) return model
def set_net(self, relative_size): """ creates neural net and stores the custom and inception_v3 layers (the latter only if self.inception_pre is true). The neural net uses a selection of the blocks from the inception_v3 model. It size can be (relativly) adjusted using the relative size. :param relative_size: [float] Magnification of each layer :return: none """ # set channel axis, necessary for the concatenation layer (retrieved from the Keras backend) if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 # input (name matches that as provided by the generator) in grayscale, therefore only one channel raw = Input(shape=(*self.img_size, 1), name='raw') inputs = [raw] x = conv2d_bn_alt(raw, relative_size * 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn_alt(x, relative_size * 32, 3, 3, padding='valid') x = conv2d_bn_alt(x, relative_size * 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn_alt(x, relative_size * 80, 1, 1, padding='valid') x = conv2d_bn_alt(x, relative_size * 100, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0: branch1x1 = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch5x5 = conv2d_bn_alt(x, relative_size * 48, 1, 1) branch5x5 = conv2d_bn_alt(branch5x5, relative_size * 64, 5, 5) branch3x3dbl = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn_alt(branch_pool, 32, 1, 1) x = concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis) # mixed 1: branch1x1 = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch5x5 = conv2d_bn_alt(x, relative_size * 48, 1, 1) branch5x5 = conv2d_bn_alt(branch5x5, relative_size * 64, 5, 5) branch3x3dbl = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn_alt(branch_pool, relative_size * 64, 1, 1) x = concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis) # mixed 2: branch1x1 = conv2d_bn_alt(x, relative_size * 64, 1, 1) # mirrored compared to mixed 1 (in order to past ci-test) branch3x3dbl = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch5x5 = conv2d_bn_alt(x, relative_size * 48, 1, 1) branch5x5 = conv2d_bn_alt(branch5x5, relative_size * 64, 5, 5) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn_alt(branch_pool, relative_size * 64, 1, 1) x = concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis) # mixed 3: branch3x3 = conv2d_bn_alt(x, relative_size * 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn_alt(x, relative_size * 64, 1, 1) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3) branch3x3dbl = conv2d_bn_alt(branch3x3dbl, relative_size * 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis) # mixed 4: branch1x1 = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch7x7 = conv2d_bn_alt(x, relative_size * 128, 1, 1) branch7x7 = conv2d_bn_alt(branch7x7, relative_size * 128, 1, 7) branch7x7 = conv2d_bn_alt(branch7x7, relative_size * 192, 7, 1) branch7x7dbl = conv2d_bn_alt(x, relative_size * 128, 1, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 128, 7, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 128, 1, 7) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 128, 7, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn_alt(branch_pool, relative_size * 192, 1, 1) x = concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis) # mixed 5: branch1x1 = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch7x7 = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch7x7 = conv2d_bn_alt(branch7x7, relative_size * 192, 1, 7) branch7x7 = conv2d_bn_alt(branch7x7, relative_size * 192, 7, 1) branch7x7dbl = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 192, 7, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 192, 1, 7) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 192, 7, 1) branch7x7dbl = conv2d_bn_alt(branch7x7dbl, relative_size * 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn_alt(branch_pool, relative_size * 192, 1, 1) x = concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis) # mixed 6: branch3x3 = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch3x3 = conv2d_bn_alt(branch3x3, relative_size * 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn_alt(x, relative_size * 192, 1, 1) branch7x7x3 = conv2d_bn_alt(branch7x7x3, relative_size * 192, 1, 7) branch7x7x3 = conv2d_bn_alt(branch7x7x3, relative_size * 192, 7, 1) branch7x7x3 = conv2d_bn_alt(branch7x7x3, relative_size * 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis) # end custom inception architecture x = GlobalAveragePooling2D()(x) # set dense layer (especially needed if pretrained is true) x = Dense(int(round(256 * relative_size)), activation='relu')(x) # if finger feature is true include in the model if self.finger_feature: # name matches generator input, 5 as right/left hand is not taken into account finger = Input(shape=(10, ), name='finger') inputs.append(finger) # combine with the output of the dense layer x_end = concatenate([x, finger]) else: x_end = x # final softmax/dense layer for prediction y = Dense(self.num_classes, activation='softmax')(x_end) neural_net_model = models.Model(inputs=[*inputs], outputs=y) # set neural net self.neural_net = neural_net_model
def build_resnet(repetitions=(2, 2, 2, 2), include_top=True, input_tensor=None, input_shape=None, classes=1000, block_type='usual'): # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=101, data_format='channels_last', require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape, name='data') else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # get parameters for model layers no_scale_bn_params = get_bn_params(scale=False) bn_params = get_bn_params() conv_params = get_conv_params() init_filters = 64 if block_type == 'basic': conv_block = basic_conv_block identity_block = basic_identity_block else: conv_block = usual_conv_block identity_block = usual_identity_block # resnet bottom x = BatchNormalization(name='bn_data', **no_scale_bn_params)(img_input) x = ZeroPadding2D(padding=(3, 3))(x) x = Conv2D(init_filters, (7, 7), strides=(2, 2), name='conv0', **conv_params)(x) x = BatchNormalization(name='bn0', **bn_params)(x) x = Activation('relu', name='relu0')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='pooling0')(x) # resnet body for stage, rep in enumerate(repetitions): for block in range(rep): filters = init_filters * (2**stage) # first block of first stage without strides because we have maxpooling before if block == 0 and stage == 0: x = conv_block(filters, stage, block, strides=(1, 1))(x) elif block == 0: x = conv_block(filters, stage, block, strides=(2, 2))(x) else: x = identity_block(filters, stage, block)(x) x = BatchNormalization(name='bn1', **bn_params)(x) x = Activation('relu', name='relu1')(x) # resnet top if include_top: x = GlobalAveragePooling2D(name='pool1')(x) x = Dense(classes, name='fc1')(x) x = Activation('softmax', name='softmax')(x) # Ensure that the model takes into account any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x) return model
def InceptionResNet2(input_shape=(100,100,1), nclass=24): img_input = Input(shape=input_shape) x = conv2d(img_input,32,3,2,'valid',True,name='conv1') x = conv2d(x,32,3,1,'valid',True,name='conv2') x = conv2d(x,64,3,1,'valid',True,name='conv3') x_11 = MaxPooling2D(3,strides=1,padding='valid',name='stem_br_11'+'_maxpool_1')(x) x_12 = conv2d(x,64,3,1,'valid',True,name='stem_br_12') x = Concatenate(axis=3, name = 'stem_concat_1')([x_11,x_12]) x_21 = conv2d(x,64,1,1,'same',True,name='stem_br_211') x_21 = conv2d(x_21,64,[1,7],1,'same',True,name='stem_br_212') x_21 = conv2d(x_21,64,[7,1],1,'same',True,name='stem_br_213') x_21 = conv2d(x_21,96,3,1,'valid',True,name='stem_br_214') x_22 = conv2d(x,64,1,1,'same',True,name='stem_br_221') x_22 = conv2d(x_22,96,3,1,'valid',True,name='stem_br_222') x = Concatenate(axis=3, name = 'stem_concat_2')([x_21,x_22]) x_31 = conv2d(x,192,3,1,'valid',True,name='stem_br_31') x_32 = MaxPooling2D(3,strides=1,padding='valid',name='stem_br_32'+'_maxpool_2')(x) x = Concatenate(axis=3, name = 'stem_concat_3')([x_31,x_32]) #Inception-ResNet-A modules x = incresA(x,0.15,name='incresA_1') x = incresA(x,0.15,name='incresA_2') x = incresA(x,0.15,name='incresA_3') x = incresA(x,0.15,name='incresA_4') #35 × 35 to 17 × 17 reduction module. x_red_11 = MaxPooling2D(3,strides=2,padding='valid',name='red_maxpool_1')(x) x_red_12 = conv2d(x,384,3,2,'valid',True,name='x_red1_c1') x_red_13 = conv2d(x,256,1,1,'same',True,name='x_red1_c2_1') x_red_13 = conv2d(x_red_13,256,3,1,'same',True,name='x_red1_c2_2') x_red_13 = conv2d(x_red_13,384,3,2,'valid',True,name='x_red1_c2_3') x = Concatenate(axis=3, name='red_concat_1')([x_red_11,x_red_12,x_red_13]) #Inception-ResNet-B modules x = incresB(x,0.1,name='incresB_1') x = incresB(x,0.1,name='incresB_2') x = incresB(x,0.1,name='incresB_3') x = incresB(x,0.1,name='incresB_4') x = incresB(x,0.1,name='incresB_5') x = incresB(x,0.1,name='incresB_6') x = incresB(x,0.1,name='incresB_7') #17 × 17 to 8 × 8 reduction module. x_red_21 = MaxPooling2D(3,strides=2,padding='valid',name='red_maxpool_2')(x) x_red_22 = conv2d(x,256,1,1,'same',True,name='x_red2_c11') x_red_22 = conv2d(x_red_22,384,3,2,'valid',True,name='x_red2_c12') x_red_23 = conv2d(x,256,1,1,'same',True,name='x_red2_c21') x_red_23 = conv2d(x_red_23,256,3,2,'valid',True,name='x_red2_c22') x_red_24 = conv2d(x,256,1,1,'same',True,name='x_red2_c31') x_red_24 = conv2d(x_red_24,256,3,1,'same',True,name='x_red2_c32') x_red_24 = conv2d(x_red_24,256,3,2,'valid',True,name='x_red2_c33') x = Concatenate(axis=3, name='red_concat_2')([x_red_21,x_red_22,x_red_23,x_red_24]) #Inception-ResNet-C modules x = incresC(x,0.2,name='incresC_1') x = incresC(x,0.2,name='incresC_2') x = incresC(x,0.2,name='incresC_3') #TOP x = GlobalAveragePooling2D(data_format='channels_last')(x) x = Dropout(0.6)(x) x = Dense(nclass, activation='softmax')(x) model = Model(img_input,x,name='inception_resnet_v2') model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters, depth, width, bottleneck, weight_decay, pooling): '''Creates a SE ResNet model with specified parameters Args: initial_conv_filters: number of features for the initial convolution include_top: Flag to include the last dense layer filters: number of filters per block, defined as a list. filters = [64, 128, 256, 512 depth: number or layers in the each block, defined as a list. ResNet-50 = [3, 4, 6, 3] ResNet-101 = [3, 6, 23, 3] ResNet-152 = [3, 8, 36, 3] width: width multiplier for network (for Wide ResNet) bottleneck: adds a bottleneck conv to reduce computation weight_decay: weight_decay (l2 norm) pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. Returns: a Keras Model ''' channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 N = list(depth) # branches branches = [] for i in range(len(img_input)): # block 1 (initial conv block) branch = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2), kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input[i]) branch = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(branch) # block 2 (projection block) for i in range(N[0]): if bottleneck: branch = _resnet_bottleneck_block(branch, filters[0], width) else: branch = _resnet_block(branch, filters[0], width) branches.append(branch) x = concatenate(branches) ''' # block 1 (initial conv block) x = Conv2D(initial_conv_filters, (7, 7), padding='same', use_bias=False, strides=(2, 2), kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # block 2 (projection block) for i in range(N[0]): if bottleneck: x = _resnet_bottleneck_block(x, filters[0], width) else: x = _resnet_block(x, filters[0], width) ''' # block 3 - N for k in range(1, len(N)): if bottleneck: x = _resnet_bottleneck_block(x, filters[k], width, strides=(2, 2)) else: x = _resnet_block(x, filters[k], width, strides=(2, 2)) for i in range(N[k] - 1): if bottleneck: x = _resnet_bottleneck_block(x, filters[k], width) else: x = _resnet_block(x, filters[k], width) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dense(classes, use_bias=False, kernel_regularizer=l2(weight_decay), activation='sigmoid', name='output')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) return x
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the VGG16 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 input channels, and width and height should be no smaller than 48. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vgg16') # load weights if weights == 'imagenet': if include_top: weights_path = WEIGHTS_PATH else: weights_path = WEIGHTS_PATH_NO_TOP model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def Deeplabv3(input_shape=(200, 300, 3), classes=23, alpha=1.): """ DeepLabV3+网络结构 :param input_shape: 输入图片的原始形状 :param classes: 语义分割的类别(包括背景) :param alpha: mobileV2中的超参,a<(0,1].所有层的 通道数(channel) 乘以a参数(四舍五入) 模型大小近似下降到原来的 a2倍,计算量下降到原来的 α2倍。这里不过多的引入。 :return:model: keras封装的模型 -type:keras.models.Model """ img_input = Input(shape=input_shape) """ Encoder: DCNN """ # (25, 38, 320) x, skip1 = mobilenetV2(img_input, alpha) # 25,38 size_before = tf.keras.backend.int_shape(x) """ Encoder: 1*1 conv + 3*3 atrous conv(with different rate:6,12,18) + image pooling """ # 全部求平均后,再利用expand_dims扩充维度,1x1 # shape = 320: (25,38,320) -> (38,320) -> (320,) b4 = GlobalAveragePooling2D()(x) # 1x320 b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) # 1x1x320 b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) # 压缩filter b4 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='image_pooling')(b4) b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4) b4 = Activation('relu')(b4) # 直接利用resize_images扩充hw # b4 = 25,38,256 b4 = Lambda(lambda x: tf.image.resize_images(x, size_before[1:3]))(b4) # 调整通道 # b0-b4的维度都会一样(后面要全部接在一起) b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x) b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0) b0 = Activation('relu', name='aspp0_activation')(b0) # SepConv_BN为先3x3膨胀卷积,再1x1卷积,进行压缩 b1 = SepConv_BN(x, 256, 'aspp1', rate=6, depth_activation=True, epsilon=1e-5) b2 = SepConv_BN(x, 256, 'aspp2', rate=12, depth_activation=True, epsilon=1e-5) b3 = SepConv_BN(x, 256, 'aspp3', rate=18, depth_activation=True, epsilon=1e-5) # 和论文里面一致,全部接在一起再加一个1x1的卷积算压缩是完成了encoder的部分 # 25,38,256*5=1280 x = Concatenate()([b4, b0, b1, b2, b3]) # 利用conv2d压缩 # 25,38,256 x = Conv2D(256, (1, 1), padding='same', use_bias=False, name='concat_projection_after_aspp')(x) x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x) x = Activation('relu')(x) x = Dropout(0.1)(x) """ 论文中说上采样by4 """ # skip1.shape[1:3] 为 200,300 # 200,300,256(200=50*4, 300=75*4) x = Lambda(lambda xx: tf.image.resize_images(x, skip1.shape[1:3]))(x) """ Decoder: """ # 50, 75, 48 dec_skip1 = Conv2D(48, (1, 1), padding='same', use_bias=False, name='feature_projection0')(skip1) dec_skip1 = BatchNormalization(name='feature_projection0_BN', epsilon=1e-5)(dec_skip1) dec_skip1 = Activation('relu')(dec_skip1) # 50,57,256+48=304 x = Concatenate()([x, dec_skip1]) # 50,75,256 x = SepConv_BN(x, 256, 'decoder_conv0', depth_activation=True, epsilon=1e-5) # 50,75,256 x = SepConv_BN(x, 256, 'decoder_conv1', depth_activation=True, epsilon=1e-5) # 200,300,3,取出最终需要还原的图片大小 size_before3 = tf.keras.backend.int_shape(img_input) # 50,75,23 x = Conv2D(classes, (1, 1), padding='same')(x) # 上采样回原始大小+23个语义类别(200,300,23),和上面说的上采样一样 x = Lambda(lambda xx: tf.image.resize_images(xx, size_before3[1:3]))(x) # =flatten(h,w),(60000, 23) x = Reshape((-1, classes))(x) # 一张图片上每一个像素点,在23个类别的softmax概率分布,一共60000个点 x = Softmax()(x) inputs = img_input model = Model(inputs, x, name='deeplabv3plus') return model
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') #determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor #Block_1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) #Block_2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) #Block_3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) #Block_4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) #Block_5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) #Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input #create model model = Model(inputs, x, name='vgg16') #load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def mini_XCEPTION(input_shape, num_classes, l2_regularization=0.01): regularization = l2(l2_regularization) # base img_input = Input(input_shape) x = Conv2D(8, (3, 3), strides=(1, 1), kernel_regularizer=regularization, use_bias=False)(img_input) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(8, (3, 3), strides=(1, 1), kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) # module 1 residual = Conv2D(16, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(16, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = SeparableConv2D(16, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = layers.add([x, residual]) # module 2 residual = Conv2D(32, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(32, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = SeparableConv2D(32, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = layers.add([x, residual]) # module 3 residual = Conv2D(64, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(64, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = SeparableConv2D(64, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = layers.add([x, residual]) # module 4 residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) x = SeparableConv2D(128, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = SeparableConv2D(128, (3, 3), padding='same', kernel_regularizer=regularization, use_bias=False)(x) x = BatchNormalization()(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = layers.add([x, residual]) x = Conv2D( num_classes, (3, 3), # kernel_regularizer=regularization, padding='same')(x) x = GlobalAveragePooling2D()(x) output = Activation('softmax', name='predictions')(x) model = Model(img_input, output) return model
def init_model(preload=None, declare=False, use_inception=True, use_resnet=False): print 'Compiling model...' if use_multiscale and use_inception and use_resnet: raise ValueError('Incorrect params') if not declare and preload: return load_model(preload) if use_multiscale: return multiscale_model(preload) if use_multicrop: return multicrop_model(preload) if use_resnet: if not preload: weights_path = ROOT + '/resnet50_tf_notop.h5' body = ResNet50(input_shape=(img_width, img_width, channels), weights_path=weights_path) for layer in body.layers: layer.trainable = False head = body.output batchnormed = BatchNormalization(axis=3)(head) avgpooled = GlobalAveragePooling2D()(batchnormed) # dropout = Dropout(0.3) (avgpooled) dense = Dense(128)(avgpooled) batchnormed = BatchNormalization()(dense) relu = PReLU()(batchnormed) dropout = Dropout(0.2)(relu) output = Dense(1, activation="sigmoid")(dropout) model = Model(body.input, output) if preload: model.load_weights(preload) return model if use_inception: if preload: return load_model(preload) return inception_v4() else: model = Sequential() model.add( ZeroPadding2D((1, 1), input_shape=(img_width, img_height, channels))) model.add(Convolution2D(16, 3, 3, activation="linear")) model.add(ELU()) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(16, 3, 3, activation="linear")) model.add(ELU()) model.add(MaxPooling2D(pool_size=(5, 5))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(32, 3, 3, activation="linear")) model.add(ELU()) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(32, 3, 3, activation="linear")) model.add(ELU()) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(32, 3, 3, activation="linear")) model.add(ELU()) model.add(MaxPooling2D(pool_size=(3, 3))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation="linear")) model.add(ELU()) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation="linear")) model.add(ELU()) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation="linear")) model.add(ELU()) model.add(MaxPooling2D(pool_size=(3, 3))) model.add(Flatten()) model.add(Dense(64, activation='linear')) model.add(ELU()) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) if preload: model.load_weights(preload) return model
n_class for n_class, n_score in zip(all_labels, c_y) if n_score > 0.5 ])) c_ax.axis('off') plt.show() from keras.applications.mobilenet import MobileNet from keras.layers import GlobalAveragePooling2D, Dense, Dropout, Flatten from keras.models import Sequential from keras.applications import densenet, Xception, NASNetMobile xception_model = Xception(input_shape=t_x.shape[1:], weights=None, include_top=False) multi_disease_model = Sequential() multi_disease_model.add(xception_model) multi_disease_model.add(GlobalAveragePooling2D()) multi_disease_model.add(Dropout(0.5)) multi_disease_model.add(Dense(512)) multi_disease_model.add(Dropout(0.5)) multi_disease_model.add(Dense(len(all_labels), activation='sigmoid')) multi_disease_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy', 'mae']) multi_disease_model.summary() from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau weight_path = "{}_weights.best.hdf5".format('xray_class') checkpoint = ModelCheckpoint(weight_path, monitor='val_loss',
# }}} # {{{ Group 4 model.add(Conv2D(64, (1,1), padding="same", data_format="channels_first")) model.add(BatchNormalization()) model.add(Activation("relu")) model.add(AveragePooling2D(pool_size=(5, 5), strides=2, padding="same", data_format="channels_first")) # }}} # {{{ Group 5 model.add(Conv2D(128, (1,1), padding="same", data_format="channels_first")) model.add(BatchNormalization()) model.add(Activation("relu")) model.add(GlobalAveragePooling2D(data_format="channels_first")) # }}} model.add(Dense(2)) model.add(Activation('softmax')) model.compile(loss='binary_crossentropy', optimizer="adam", metrics=['accuracy']) ep=10 i=0 while True: i+=ep model.fit(X, Xt, batch_size=64, epochs=ep, validation_data=(Y, Yt), shuffle=True) print "****", i, "epochs done"
generic_utils.get_custom_objects().update({'swish': swish}) model = Sequential() model.add(Conv2D(32, kernel_size=(5), activation='swish', input_shape=(224, 224, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BatchNormalization(momentum=0.75, epsilon=0.001)) model.add(Conv2D(64, kernel_size=(4), activation='swish')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BatchNormalization(momentum=0.75, epsilon=0.001)) model.add(Conv2D(128, kernel_size=(3), activation='swish')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BatchNormalization(momentum=0.75, epsilon=0.001)) model.add(Conv2D(256, kernel_size=(2), activation='swish')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(BatchNormalization(momentum=0.75, epsilon=0.001)) model.add(GlobalAveragePooling2D()) model.add(Dense(133, activation='softmax')) print(model.summary()) # Compile the Model model.compile(optimizer=Adam(lr=2e-3), loss='categorical_crossentropy', metrics=['accuracy']) # Train the Model epochs = 20 checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.from_scratch_withdropout.hdf5', verbose=1, save_best_only=True) hist = model.fit(train_tensors, train_targets, validation_data=(valid_tensors, valid_targets), epochs=epochs, batch_size=20, callbacks=[checkpointer], verbose=1) # Load the Model with the Best Validation Loss model.load_weights('saved_models/weights.best.from_scratch_withdropout.hdf5') # Test the Model dog_breed_predictions = [np.argmax(model.predict(np.expand_dims(tensor, axis=0))) for tensor in test_tensors] test_accuracy = 100*np.sum(np.array(dog_breed_predictions)==np.argmax(test_targets, axis=1))/len(dog_breed_predictions) print('Test accuracy: {:.4f}%'.format(test_accuracy))
def build(width=224, height=224, depth=3, classes=1000, weightsPath=None): input_shape = (height, width, depth) model = Sequential() model.add(Conv2D(32, (3, 3), padding="same", input_shape=input_shape)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(64, (3, 3), padding="same", input_shape=input_shape)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(128, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(64, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(128, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(128, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) '''model.add(Conv2D(classes, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1))''' model.add(GlobalAveragePooling2D(data_format="channels_last")) model.add(Dense(classes)) model.add(BatchNormalization()) model.add(Activation("sigmoid")) # model.add(Dropout(0.5)) #Não há dropout no paper original # model.add(Dense(classes)) '''if classes == 1: model.add(Activation("sigmoid")) else: model.add(Activation("softmax"))''' if weightsPath is not None: model.load_weights(weightsPath) return model
def SSD(input_shape, num_classes=21): """SSD300 architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net = {} # Block 1 input_tensor = Input(shape=input_shape) img_size = (input_shape[1], input_shape[0]) input0 = input_tensor conv1_1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(input0) conv1_2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(conv1_1) pool1 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool1')(conv1_2) # Block 2 conv2_1 = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(pool1) conv2_2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(conv2_1) pool2 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool2')(conv2_2) # Block 3 conv3_1 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(pool2) conv3_2 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(conv3_1) conv3_3 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(conv3_2) conv3_4 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_4')(conv3_3) pool3 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool3')(conv3_4) # Block 4 conv4_1 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(pool3) conv4_2 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(conv4_1) conv4_3 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(conv4_2) conv4_4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_4')(conv4_3) pool4 = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='pool4')(conv4_4) # Block 5 conv5_1 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(pool4) conv5_2 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(conv5_1) conv5_3 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(conv5_2) conv5_4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_4')(conv5_3) pool5 = MaxPooling2D((3, 3), strides=(1, 1), padding='same', name='pool5')(conv5_4) # FC6 fc6 = Conv2D(1024, (3, 3), dilation_rate=(6, 6), activation='relu', padding='same', name='fc6')(pool5) #fc6 = Dropout(0.5, name='drop6')(fc6) # FC7 fc7 = Conv2D(1024, (1, 1), activation='relu', padding='same', name='fc7')(fc6) #fc7 = Dropout(0.5, name='drop7')(fc7) # Block 6 conv6_1 = Conv2D(256, (1, 1), activation='relu', padding='same', name='conv6_1')(fc7) conv6_2 = Conv2D(512, (3, 3), strides=(2, 2), activation='relu', padding='same', name='conv6_2')(conv6_1) # Block 7 conv7_1 = Conv2D(128, (1, 1), activation='relu', padding='same', name='conv7_1')(conv6_2) conv7_2 = ZeroPadding2D()(conv7_1) conv7_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', name='conv7_2')(conv7_2) # Block 8 conv8_1 = Conv2D(128, (1, 1), activation='relu', padding='same', name='conv8_1')(conv7_2) conv8_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='same', name='conv8_2')(conv8_1) # Last Pool pool6 = GlobalAveragePooling2D(name='pool6')(conv8_2) # Prediction from conv4_3 conv4_3_norm = Normalize(num_classes - 1, name='conv4_3_norm')(conv4_3) num_priors = 3 conv4_3_norm_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv4_3_norm_mbox_loc')(conv4_3_norm) conv4_3_norm_mbox_loc_flat = Flatten( name='conv4_3_norm_mbox_loc_flat')(conv4_3_norm_mbox_loc) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv4_3_norm_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv4_3_norm) conv4_3_norm_mbox_conf_flat = Flatten( name='conv4_3_norm_mbox_conf_flat')(conv4_3_norm_mbox_conf) conv4_3_norm_mbox_priorbox = PriorBox( img_size, 30.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_norm_mbox_priorbox')(conv4_3_norm) # Prediction from fc7 num_priors = 6 fc7_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='fc7_mbox_loc')(fc7) fc7_mbox_loc_flat = Flatten(name='fc7_mbox_loc_flat')(fc7_mbox_loc) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) fc7_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(fc7) fc7_mbox_conf_flat = Flatten(name='fc7_mbox_conf_flat')(fc7_mbox_conf) fc7_mbox_priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox')(fc7) # Prediction from conv6_2 num_priors = 6 conv6_2_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv6_2_mbox_loc')(conv6_2) conv6_2_mbox_loc_flat = Flatten( name='conv6_2_mbox_loc_flat')(conv6_2_mbox_loc) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv6_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv6_2) conv6_2_mbox_conf_flat = Flatten( name='conv6_2_mbox_conf_flat')(conv6_2_mbox_conf) conv6_2_mbox_priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox')(conv6_2) # Prediction from conv7_2 num_priors = 6 conv7_2_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv7_2_mbox_loc')(conv7_2) conv7_2_mbox_loc_flat = Flatten( name='conv7_2_mbox_loc_flat')(conv7_2_mbox_loc) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv7_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv7_2) conv7_2_mbox_conf_flat = Flatten( name='conv7_2_mbox_conf_flat')(conv7_2_mbox_conf) conv7_2_mbox_priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox')(conv7_2) # Prediction from conv8_2 num_priors = 6 conv8_2_mbox_loc = Conv2D(num_priors * 4, (3, 3), padding='same', name='conv8_2_mbox_loc')(conv8_2) conv8_2_mbox_loc_flat = Flatten( name='conv8_2_mbox_loc_flat')(conv8_2_mbox_loc) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) conv8_2_mbox_conf = Conv2D(num_priors * num_classes, (3, 3), padding='same', name=name)(conv8_2) conv8_2_mbox_conf_flat = Flatten( name='conv8_2_mbox_conf_flat')(conv8_2_mbox_conf) conv8_2_mbox_priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox')(conv8_2) # Prediction from pool6 num_priors = 6 pool6_mbox_loc_flat = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(pool6) name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) pool6_mbox_conf_flat = Dense(num_priors * num_classes, name=name)(pool6) if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) pool6_reshaped = Reshape(target_shape, name='pool6_reshaped')(pool6) pool6_mbox_priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox')(pool6_reshaped) # Gather all predictions mbox_loc = concatenate([ conv4_3_norm_mbox_loc_flat, fc7_mbox_loc_flat, conv6_2_mbox_loc_flat, conv7_2_mbox_loc_flat, conv8_2_mbox_loc_flat, pool6_mbox_loc_flat ], axis=1, name='mbox_loc') mbox_conf = concatenate([ conv4_3_norm_mbox_conf_flat, fc7_mbox_conf_flat, conv6_2_mbox_conf_flat, conv7_2_mbox_conf_flat, conv8_2_mbox_conf_flat, pool6_mbox_conf_flat ], axis=1, name='mbox_conf') mbox_priorbox = concatenate([ conv4_3_norm_mbox_priorbox, fc7_mbox_priorbox, conv6_2_mbox_priorbox, conv7_2_mbox_priorbox, conv8_2_mbox_priorbox, pool6_mbox_priorbox ], axis=1, name='mbox_priorbox') if hasattr(mbox_loc, '_keras_shape'): num_boxes = mbox_loc._keras_shape[-1] // 4 elif hasattr(mbox_loc, 'int_shape'): num_boxes = K.int_shape(mbox_loc)[-1] // 4 mbox_loc = Reshape((num_boxes, 4), name='mbox_loc_final')(mbox_loc) mbox_conf = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(mbox_conf) mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf) predictions = concatenate([mbox_loc, mbox_conf, mbox_priorbox], axis=2, name='predictions') model = Model(input0, predictions) return model