将权重恢复到 VGG-16 网络时出错

Error restoring weights into a VGG-16 network

我正在使用 Python 3.7.7 和 Tensorflow 2.1.0。

我想创建一个 VGG16 自动编码器网络,向其加载权重文件,然后获取其编码器和解码器。

获取VGG16自编码器网络的函数有:

def get_vgg16_encoder(input_size=(200, 200, 1)):
    inputs = Input(input_size, name='input')

    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(inputs)
    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool_1')(conv1)

    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')(pool1)
    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool_2')(conv2)

    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')(pool2)
    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')(conv3)
    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool_3')(conv3)

    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')(pool3)
    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')(conv4)
    conv4 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool_4')(conv4)

    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')(pool4)
    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')(conv5)
    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')(conv5)
    pool5 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool_5')(conv5)

    return pool5, inputs

def get_vgg16_decoder(pool5):

    upsp1 = UpSampling2D(size = (2, 2), name = 'upsp1')(pool5)
    conv1 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv1_1_dec')(upsp1)
    conv1 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv1_2_dec')(conv1)
    conv1 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv1_3_dec')(conv1)

    upsp2 = UpSampling2D(size = (2, 2), name = 'upsp2')(conv1)
    conv2 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv2_1_dec')(upsp2)
    conv2 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv2_2_dec')(conv2)
    conv2 = Conv2D(512, 3, activation = 'relu', padding = 'same', name = 'conv2_3_dec')(conv2)
    zero1 = ZeroPadding2D(padding=((1, 0), (1, 0)), data_format='channels_last', name='zero1')(conv2)

    upsp3 = UpSampling2D(size = (2, 2), name = 'upsp3')(zero1)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', name = 'conv3_1_dec')(upsp3)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', name = 'conv3_2_dec')(conv3)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', name = 'conv3_3_dec')(conv3)

    upsp4 = UpSampling2D(size = (2, 2), name = 'upsp4')(conv3)
    conv4 = Conv2D(128, 3, activation = 'relu', padding = 'same', name = 'conv4_1_dec')(upsp4)
    conv4 = Conv2D(128, 3, activation = 'relu', padding = 'same', name = 'conv4_2_dec')(conv4)

    upsp5 = UpSampling2D(size = (2, 2), name = 'upsp5')(conv4)
    conv5 = Conv2D(64, 3, activation = 'relu', padding = 'same', name = 'conv5_1_dec')(upsp5)
    conv5 = Conv2D(64, 3, activation = 'relu', padding = 'same', name = 'conv5_2_dec')(conv5)

    conv6 = Conv2D(1, 3, activation = 'relu', padding = 'same', name = 'conv6_dec')(conv5)

    return conv6


def get_vgg16(img_shape=(200, 200, 1)):
    enc = get_vgg16_encoder(img_shape)

    dec = get_vgg16_decoder(enc[0])

    model = Model(inputs=enc[-1], outputs=dec)

    return model

主要功能是:

def get_vgg_encoder_decoder_trained(img_shape):   
    # Get the model
    model = get_vgg16(img_shape)

    # Compile it.
    model.compile(tf.keras.optimizers.Adam(lr=(1e-4) * 2),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    # Add the weights.
    path_to_weights = os.path.join(weights_path, weights_filename)
    model.load_weights(path_to_weights)
   

    # Get the encoder
    first_encoder_layer = 0
    last_encoder_layer = 18

    encoder = Model(inputs=model.layers[first_encoder_layer].input,
                      outputs=model.layers[last_encoder_layer].output,
                      name='encoder')

    # extract decoder fitted weights
    restored_w = []
    for w in model.layers[last_encoder_layer + 1:]:
        restored_w.extend(w.get_weights())

    # reconstruct decoder architecture setting the fitted weights
    new_inp = [Input(l.shape[1:]) for l in get_vgg16_encoder(img_shape)]
    new_dec = get_vgg16_decoder(new_inp[-1])
    decoder = Model(new_inp, new_dec)
    decoder.set_weights(restored_w)

    return encoder, decoder

您可以下载权重文件 here

但是当我尝试恢复权重时出现以下错误:

ValueError: Layer weight shape (3, 3, 1, 512) not compatible with provided weight shape (3, 3, 512, 512)

可能错误是这段代码我看不懂:

    new_inp = [Input(l.shape[1:]) for l in get_vgg16_encoder(img_shape)]
    new_dec = get_vgg16_decoder(new_inp[-1])

有什么建议吗?

我修改最后一段代码解决了这个错误:

# reconstruct decoder architecture setting the fitted weights
new_inp = [Input(l.shape[1:]) for l in get_vgg16_encoder(img_shape)]
new_dec = get_vgg16_decoder(new_inp[0])
decoder = Model(new_inp[0], new_dec)
decoder.set_weights(restored_w)