Beispiel #1
0
def build_model(input_shape, num_classes, weights='imagenet'):
    # create the base pre-trained model

    base_model = DenseNet121(weights=weights, include_top=False, input_shape=input_shape,
                             backend=keras.backend, layers=keras.layers, models=keras.models,
                             utils=keras.utils)

    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu', name='fc2014_1')(x)
    x = Dropout(0.5)(x)
    x = Dense(1024, activation='relu', name='fc2014_2')(x)
    x = Dropout(0.5)(x)
    x = Dense(num_classes, activation='sigmoid', name='fc28')(x)

    # this is the model we will train
    model = Model(inputs=base_model.input, outputs=x, name='pre_trained_resnet50')

    sgd = SGD(lr=0.01, momentum=0.9, nesterov=True, decay=1e-06)

    # compile the model (should be done *after* setting layers to non-trainable)
    model.compile(optimizer=sgd, loss=binary_crossentropy, metrics=['accuracy'])

    return model
def get_densenet121_unet_softmax(input_shape, weights='imagenet'):
    blocks = [6, 12, 24, 16]
    img_input = Input(input_shape + (4, ))

    x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
    x = Activation('relu', name='conv1/relu')(x)
    conv1 = x
    x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = MaxPooling2D(3, strides=2, name='pool1')(x)
    x = dense_block(x, blocks[0], name='conv2')
    conv2 = x
    x = transition_block(x, 0.5, name='pool2')
    x = dense_block(x, blocks[1], name='conv3')
    conv3 = x
    x = transition_block(x, 0.5, name='pool3')
    x = dense_block(x, blocks[2], name='conv4')
    conv4 = x
    x = transition_block(x, 0.5, name='pool4')
    x = dense_block(x, blocks[3], name='conv5')
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
    conv5 = x

    conv6 = conv_block(UpSampling2D()(conv5), 320)
    conv6 = concatenate([conv6, conv4], axis=-1)
    conv6 = conv_block(conv6, 320)

    conv7 = conv_block(UpSampling2D()(conv6), 256)
    conv7 = concatenate([conv7, conv3], axis=-1)
    conv7 = conv_block(conv7, 256)

    conv8 = conv_block(UpSampling2D()(conv7), 128)
    conv8 = concatenate([conv8, conv2], axis=-1)
    conv8 = conv_block(conv8, 128)

    conv9 = conv_block(UpSampling2D()(conv8), 96)
    conv9 = concatenate([conv9, conv1], axis=-1)
    conv9 = conv_block(conv9, 96)

    conv10 = conv_block(UpSampling2D()(conv9), 64)
    conv10 = conv_block(conv10, 64)
    res = Conv2D(3, (1, 1), activation='softmax')(conv10)
    model = Model(img_input, res)

    if weights == 'imagenet':
        densenet = DenseNet121(input_shape=input_shape + (3, ),
                               weights=weights,
                               include_top=False)
        w0 = densenet.layers[2].get_weights()
        w = model.layers[2].get_weights()
        w[0][:, :, [0, 1, 2], :] = 0.9 * w0[0][:, :, :3, :]
        w[0][:, :, 3, :] = 0.1 * w0[0][:, :, 1, :]
        model.layers[2].set_weights(w)
        for i in range(3, len(densenet.layers)):
            model.layers[i].set_weights(densenet.layers[i].get_weights())
            model.layers[i].trainable = False

    return model
Beispiel #3
0
def build_model(config):
    """
    Returns: a model with specified weights
    """

    input_size = config['input_size']
    input_shape = (input_size, input_size, 3)

    if ('pretrain' in config) and config['pretrain']:
        assert config['input_size'] == 224
        weights = 'imagenet'
    else:
        weights = None

    assert config['image_model'] in ['densenet', 'resnet', 'linear']
    if config['image_model'] == 'densenet':
        print("Using Densenet.")
        base_model = DenseNet121(input_shape=input_shape,
                                 weights=weights,
                                 include_top=False,
                                 pooling='avg')
        image_input = base_model.input
        layer = base_model.output
    elif config['image_model'] == 'resnet':
        print("Using Resnet.")
        base_model = ResNet50(input_shape=input_shape,
                              weights=weights,
                              include_top=False,
                              pooling='avg')
        image_input = base_model.input
        layer = base_model.output
    elif config['image_model'] == 'linear':
        print("Using linear model.")
        image_input = Input(shape=input_shape)
        layer = Flatten()(image_input)

    if ('freeze' in config) and config['freeze']:
        for layer in base_model.layers:
            try:
                layer.trainable = False
                print("Freezing {}".format(layer))
            except Exception:
                print("Not trainable {}".format(layer))

    predictions = Dense(14, activation='sigmoid')(layer)
    model = Model(inputs=image_input, outputs=predictions)
    return model
def get_model(img_size,
              num_channels,
              num_classes,
              model_name='densenet121',
              pretrain=False):
    weights = 'imagenet' if pretrain else None
    if pretrain:
        assert img_size == 224
    input_tensor = Input(shape=(img_size, img_size, num_channels))

    if model_name == 'densenet121':
        model = DenseNet121(include_top=False,
                            weights=weights,
                            input_tensor=input_tensor,
                            pooling='avg')
        preprocessor = pi_densenet121

    elif model_name == 'resnet50':
        model = ResNet50(include_top=False,
                         weights=weights,
                         input_tensor=input_tensor,
                         pooling='avg')
        preprocessor = pi_resnet50

    elif model_name == 'vgg19':
        model = VGG19(include_top=False,
                      weights=weights,
                      input_tensor=input_tensor,
                      pooling='avg')
        preprocessor = pi_vgg19

    else:
        raise Exception("Unrecognized model name: %s" % model_name)

    output_tensor = model(input_tensor)
    output_tensor = Dense(num_classes, activation='sigmoid')(output_tensor)
    complete_model = Model(input_tensor, output_tensor)
    return complete_model, preprocessor
def get_densenet121_unet_sigmoid_gn(input_shape=(CONFIG.img_h, CONFIG.img_w, CONFIG.img_c),
                                    output_channels=1,
                                    weights='imagenet'):
    blocks = [6, 12, 24, 16]
    img_input = Input(input_shape)

    x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)

    x = GroupNormalization(axis=GN_AXIS, groups=16,
                           scale=False,
                           name='conv1/gn')(x)
    x = Activation('relu', name='conv1/relu')(x)
    conv1 = x
    x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = MaxPooling2D(3, strides=2, name='pool1')(x)
    x = dense_block(x, blocks[0], name='conv2')
    conv2 = x
    x = transition_block(x, 0.5, name='pool2')
    x = dense_block(x, blocks[1], name='conv3')
    conv3 = x
    x = transition_block(x, 0.5, name='pool3')
    x = dense_block(x, blocks[2], name='conv4')
    conv4 = x
    x = transition_block(x, 0.5, name='pool4')
    x = dense_block(x, blocks[3], name='conv5')
    x = GroupNormalization(axis=GN_AXIS, groups=32,
                           scale=False,
                           name='conv5/gn')(x)
    conv5 = x

    # squeeze and excite block
    conv5 = squeeze_excite_block(conv5)

    conv6 = conv_block(UpSampling2D()(conv5), 320)
    conv6 = concatenate([conv6, conv4], axis=-1)
    conv6 = conv_block(conv6, 320)

    conv7 = conv_block(UpSampling2D()(conv6), 256)
    conv7 = concatenate([conv7, conv3], axis=-1)
    conv7 = conv_block(conv7, 256)

    conv8 = conv_block(UpSampling2D()(conv7), 128)
    conv8 = concatenate([conv8, conv2], axis=-1)
    conv8 = conv_block(conv8, 128)

    conv9 = conv_block(UpSampling2D()(conv8), 96)
    conv9 = concatenate([conv9, conv1], axis=-1)
    conv9 = conv_block(conv9, 96)

    conv10 = conv_block(UpSampling2D()(conv9), 64)
    conv10 = conv_block(conv10, 64)
    res = Conv2D(output_channels, (1, 1), activation='sigmoid')(conv10)
    model = Model(img_input, res)

    if weights == 'imagenet':
        densenet = DenseNet121(input_shape=(input_shape[0], input_shape[1], 3), weights=weights, include_top=False)
        print("Loading imagenet weights.")
        for i in tqdm(range(2, len(densenet.layers) - 1)):
            model.layers[i].set_weights(densenet.layers[i].get_weights())
            model.layers[i].trainable = False

    return model