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

lda*_*vid 6 keras tensorflow

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

model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
Run Code Online (Sandbox Code Playgroud)

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

Exception: Layer weight shape (3, 3, 3, 64) not compatible with provided weight shape (64, 3, 3, 3)
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*den 11

我向Francois Chollet询问了这个问题(他没有SO账号),他顺便地回复了这个问题:


"th"格式意味着卷积内核将具有形状(depth,input_depth,rows,cols)

"tf"格式意味着卷积内核将具有形状(rows,cols,input_depth,depth)

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

以下是进行转换的一些代码:

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())
Run Code Online (Sandbox Code Playgroud)

如果模型包含Convolution2D层下游的密集层,则第一个Dense层的权重矩阵也需要进行混洗.