Пример #1
0
def get_resnet_unet(input_shape, weights='imagenet'):
    inp = Input(input_shape + (3, ))

    x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(inp)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    conv1 = x
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = resnet_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')
    enc1 = x

    x = resnet_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')
    enc2 = x

    x = resnet_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')
    enc3 = x

    x = resnet_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')
    enc4 = x

    up6 = concatenate([UpSampling2D()(enc4), enc3], axis=-1)
    conv6 = conv_block(up6, 128)
    conv6 = conv_block(conv6, 128)

    up7 = concatenate([UpSampling2D()(conv6), enc2], axis=-1)
    conv7 = conv_block(up7, 96)
    conv7 = conv_block(conv7, 96)

    up8 = concatenate([UpSampling2D()(conv7), enc1], axis=-1)
    conv8 = conv_block(up8, 64)
    conv8 = conv_block(conv8, 64)

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

    up10 = concatenate([UpSampling2D()(conv9), inp], axis=-1)
    conv10 = conv_block(up10, 32)
    conv10 = conv_block(conv10, 32)
    #    conv10 = SpatialDropout2D(0.33)(conv10)
    res = Conv2D(1, (1, 1), activation='sigmoid')(conv10)
    model = Model(inp, res)

    if weights == 'imagenet':
        resnet = ResNet50(input_shape=input_shape + (3, ),
                          include_top=False,
                          weights=weights)
        for i in range(2, len(resnet.layers) - 1):
            model.layers[i].set_weights(resnet.layers[i].get_weights())
            model.layers[i].trainable = False

    return model
Пример #2
0
def get_resnet50_linknet(input_shape, weights='imagenet'):
    inp = Input(input_shape + (9, ))

    x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(inp)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = resnet_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')
    enc1 = x

    x = resnet_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')
    enc2 = x

    x = resnet_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')
    enc3 = x

    x = resnet_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')
    enc4 = x

    dec4 = linknet_deconv_block(enc4, 2048, 1024)
    dec4 = layers.add([dec4, enc3])
    dec3 = linknet_deconv_block(dec4, 1024, 512)
    dec3 = layers.add([dec3, enc2])
    dec2 = linknet_deconv_block(dec3, 512, 256)
    dec2 = layers.add([dec2, enc1])
    dec1 = linknet_deconv_block(dec2, 256, 64)

    x = Conv2DTranspose(64, (3, 3), strides=(2, 2), padding='same')(dec1)
    x = BatchNormalization(axis=bn_axis)(x)
    x = Activation('relu')(x)

    x = Conv2D(48, (3, 3), padding='same')(x)
    x = BatchNormalization(axis=bn_axis)(x)
    x = Activation('relu')(x)

    x = Conv2D(1, (1, 1), activation='sigmoid')(x)

    model = Model(inp, x)
    if weights == 'imagenet':
        resnet = ResNet50(input_shape=input_shape + (3, ),
                          include_top=False,
                          weights=weights)
        for i in range(2, len(resnet.layers) - 1):
            model.layers[i].set_weights(resnet.layers[i].get_weights())
            model.layers[i].trainable = False

    return model
Пример #3
0
def get_resnet_unet(input_shape, weights='imagenet'):
    inp = Input(input_shape + (9,))
    
    x = Conv2D(
        64, (7, 7), strides=(2, 2), padding='same', name='conv1')(inp)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    conv1 = x
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = resnet_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')
    enc1 = x
    
    x = resnet_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')
    enc2 = x
    
    x = resnet_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')
    enc3 = x
    
    x = resnet_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')
    enc4 = x
        
    up6 = concatenate([UpSampling2D()(enc4), enc3], axis=-1)
    conv6 = conv_block(up6, 128)
    conv6 = conv_block(conv6, 128)

    up7 = concatenate([UpSampling2D()(conv6), enc2], axis=-1)
    conv7 = conv_block(up7, 96)
    conv7 = conv_block(conv7, 96)

    up8 = concatenate([UpSampling2D()(conv7), enc1], axis=-1)
    conv8 = conv_block(up8, 64)
    conv8 = conv_block(conv8, 64)

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

    up10 = concatenate([UpSampling2D()(conv9), inp], axis=-1)
    conv10 = conv_block(up10, 32)
    conv10 = conv_block(conv10, 32)
#    conv10 = SpatialDropout2D(0.33)(conv10)
    res = Conv2D(1, (1, 1), activation='sigmoid')(conv10)
    model = Model(inp, res)
    
    if weights == 'imagenet':
        resnet = ResNet50(input_shape=input_shape + (3,), include_top=False, weights=weights)
        for i in range(2, len(resnet.layers)-1):
            model.layers[i].set_weights(resnet.layers[i].get_weights())
            model.layers[i].trainable = False
        
    return model