Python Keras:将现有权重加载到Batchnormalization层中
我在Python Keras:将现有权重加载到Batchnormalization层中,python,tensorflow,neural-network,keras,keras-2,Python,Tensorflow,Neural Network,Keras,Keras 2,我在.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
.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:形状的秩必须相等,但其值为1和0
带有输入形状[16]的“批处理规格化3/Assign”(op:“Assign”),
[…]
完整的错误消息位于pastebin上:
如果我删除batchnormalization构造函数中的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>]
[,,
,
,
]
如何将权重加载到批处理中