0

I want to export a set of pre-trained weights from Tensorflow to Keras. The problem is that batch normalization layers in Tensorflow embed only Beta and Gamma as trainable weights, whereas in Keras, we have Moving_mean and Moving_variance as well. I am confused where to obtain these weights from.

4

1 回答 1

1

试试tf.train.NewCheckpointReader。我最近将一个 CNN 模型从 TF 转换为 Keras,用它导出移动均值/方差权重没有问题。

reader = tf.train.NewCheckpointReader(ckpt_file)
for key in reader.get_variable_to_shape_map():
    path = os.path.join(output_folder, get_filename(key))
    arr = reader.get_tensor(key)
    np.save(path, arr)
    print("tensor_name: ", key)

whereget_filename()只是一个将张量名称转换为正确文件名的函数。(例如,用下划线替换斜线)

如果您对更多细节感兴趣,完整的代码可能会有所帮助。

于 2017-08-01T17:16:48.650 回答