且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将keras设置为TF格式时以TH格式加载权重

更新时间:2023-12-02 18:07:40

我向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.