style_transfer.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:neural_style 作者: metaflow-ai 项目源码 文件源码
def st_conv_inception(input_shape, weights_path=None, mode=0, nb_res_layer=5):
    if K.image_dim_ordering() == 'tf':
        channel_axis = 3
    else:
        channel_axis = 1

    input = Input(shape=input_shape, name='input_node', dtype=K.floatx())
    # Downsampling
    c = Convolution2D(13, 9, 9, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(input)
    bn11 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c)
    a11 = Activation('relu')(bn11)
    mp11 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(input)
    m = merge([a11, mp11], mode='concat', concat_axis=channel_axis) # 16 layers

    c12 = Convolution2D(48, 3, 3, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(2, 2),  border_mode='same', activation='linear')(m)
    bn12 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c12)
    a12 = Activation('relu')(bn12)
    mp12 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(m)
    m = merge([a12, mp12], mode='concat', concat_axis=channel_axis) # 64 layers

    c13 = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(m)
    bn13 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c13)
    last_out = Activation('relu')(bn13)

    for i in range(nb_res_layer):
        out = naive_inception_layer(last_out, K.image_dim_ordering(), channel_axis, mode)
        last_out = merge([last_out, out], mode='sum')

    ct = ConvolutionTranspose2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(last_out)
    bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct)
    a = Activation('relu')(bn)

    ct = ConvolutionTranspose2D(16, 3, 3, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a)
    bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct)
    a = Activation('relu')(bn)    

    c = Convolution2D(3, 9, 9, dim_ordering=K.image_dim_ordering(), 
        init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(a)
    out = ScaledSigmoid(scaling=255., name="output_node")(c)

    model = Model(input=[input], output=[out])

    if weights_path:
        model.load_weights(weights_path)

    return model
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号