我在一个.h5
文件中有一个完全卷积的预训练模型。现在我想更改输入分辨率并再次训练。
我目前的方法是遍历所有层,创建一个新层并分配预训练的权重。
这是一个最小的示例:
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.models import Model
# this would be the pretrained model
input_layer = Input((10, 10, 3))
conv = Conv2D(16, 3)(input_layer)
bnorm = BatchNormalization()(conv)
model = Model(inputs = input_layer, outputs = bnorm)
# now I want to create a new model with the same architecture but different sizes
new_input = Input((100,100,3))
prev_layer = new_input
for old_layer in model.layers[1:]:
weights = old_layer.weights
if type(old_layer) == Conv2D:
filters = old_layer.filters
kernel_size = old_layer.kernel_size
conv_layer = Conv2D(filters = filters,
kernel_size = kernel_size,
)(prev_layer)
prev_layer = conv_layer
elif type(old_layer) == BatchNormalization:
bn_layer = BatchNormalization(weights=weights)
prev_layer = bn_layer(prev_layer)
批量标准化的代码失败。错误信息比较长,关键问题似乎是:
ValueError: Shapes must be equal rank, but are 1 and 0 for 'batch_normalization_3/Assign' (op: 'Assign') with input shapes: [16], [].
完整的错误消息在 pastebin 上:https ://pastebin.com/NVWs4tq2
如果我在批处理规范化的构造函数中删除 weights 参数,则代码可以正常工作。我已经查看了我试图在构造函数中提供的权重以及如果没有提供权重则分配的权重。形状是相同的。
[<tf.Variable 'batch_normalization_1/gamma:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'batch_normalization_1/beta:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'batch_normalization_1/moving_mean:0' shape=(16,) dtype=float32_ref>,
<tf.Variable 'batch_normalization_1/moving_variance:0' shape=(16,) dtype=float32_ref>]
如何将权重加载到 Batchnormalization 中?