将keras设置为TF格式时以TH格式加载权重 [英] Loading weights in TH format when keras is set to TF format

查看:135
本文介绍了将keras设置为TF格式时以TH格式加载权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Keras的image_dim_ordering属性设置为'tf',因此我将模型定义为:

I have Keras' image_dim_ordering property set to 'tf', so I define my models as this:

model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
model.add(Convolution2D(64, 3, 3, activation='relu'))

但是当我调用load_weights方法时,它崩溃了,因为我的模型是使用"th"格式保存的:

But when I call load_weights method, it crashes because my model was saved using "th" format:

Exception: Layer weight shape (3, 3, 3, 64) not compatible with provided weight shape (64, 3, 3, 3)

如何加载这些权重并自动转置它们以修复Tensorflow的格式?

How can I load these weights and automatically transpose them to fix Tensorflow's format?

推荐答案

我向Francois Chollet询问了这个问题(他没有SO帐户),他很高兴地传递了此答复:

I asked Francois Chollet about this (he doesn't have an SO account) and he kindly passed along this reply:

"th"格式表示卷积核将具有形状(深度,input_depth,行,列)

"th" format means that the convolutional kernels will have the shape (depth, input_depth, rows, cols)

"tf"格式表示卷积核将具有(行,列,输入深度,深度)形状

"tf" format means that the convolutional kernels will have the shape (rows, cols, input_depth, depth)

因此,您可以通过np.transpose(x, (2, 3, 1, 0))从前者转换为后者,其中x是卷积内核的值.

Therefore you can convert from the former to the later via np.transpose(x, (2, 3, 1, 0)) where x is the value of the convolution kernel.

以下是进行转换的代码:

Here's some code to do the conversion:

from keras import backend as K

K.set_image_dim_ordering('th')

# build model in TH mode, as th_model
th_model = ...
# load weights that were saved in TH mode into th_model
th_model.load_weights(...)

K.set_image_dim_ordering('tf')

# build model in TF mode, as tf_model
tf_model = ...

# transfer weights from th_model to tf_model
for th_layer, tf_layer in zip(th_model.layers, tf_model.layers):
   if th_layer.__class__.__name__ == 'Convolution2D':
      kernel, bias = layer.get_weights()
      kernel = np.transpose(kernel, (2, 3, 1, 0))
      tf_layer.set_weights([kernel, bias])
  else:
      tf_layer.set_weights(tf_layer.get_weights())

如果模型在Convolution2D图层的下游包含Dense图层,则也需要对第一个Dense图层的权重矩阵进行重新排序.

In case the model contains Dense layers downstream of the Convolution2D layers, then the weight matrix of the first Dense layer would need to be shuffled as well.

这篇关于将keras设置为TF格式时以TH格式加载权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆