gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:49:06 25 4
gpt4 key购买 nike

我正在使用 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)

关于python - 将权重恢复到 VGG-16 网络时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63862004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com