【发布时间】:2019-02-09 20:55:43
【问题描述】:
我正在尝试从一个非常简单的 Caffe 模型中获取权重并将其解释为功能齐全的 Keras 模型。
这是Caffe中model的原始定义,我们称之为simple.prototxt:
input: "im_data"
input_shape {
dim: 1
dim: 3
dim: 1280
dim: 1280
}
layer {
name: "conv1"
type: "Convolution"
bottom: "im_data"
top: "conv1"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 96
kernel_size: 11
pad: 5
stride: 4
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
pad: 0
stride: 2
}
}
layer {
name: "norm1"
type: "LRN"
bottom: "pool1"
top: "norm1"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "norm1"
top: "conv2"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
convolution_param {
num_output: 256
kernel_size: 5
pad: 2
group: 2
}
}
layer {
name: "relu2"
type: "ReLU"
bottom: "conv2"
top: "conv2"
}
Caffe 中的层定义可能看起来很复杂,但它只是将尺寸为1280x1280x3 的图像传递给卷积层,然后将其最大池化并传递给最终的卷积层。
这是它在 Keras 中的实现,它要简单得多:
from keras.models import Model
from keras.layers import Input, BatchNormalization,
from keras.activations import relu, softmax
im_data = Input(shape=(1280, 1280, 3),
dtype='float32',
name='im_data')
conv1 = Conv2D(filters=96,
kernel_size=11,
strides=(4, 4),
activation=relu,
padding='same',
name='conv1')(im_data)
pooling1 = MaxPooling2D(pool_size=(3, 3),
strides=(2, 2),
padding='same',
name='pooling1')(conv1)
normalized1 = BatchNormalization()(pooling1) # https://stats.stackexchange.com/questions/145768/importance-of-local-response-normalization-in-cnn
conv2 = Conv2D(filters=256,
kernel_size=5,
activation=relu,
padding='same',
name='conv2')(normalized1)
model = Model(inputs=[im_data], outputs=conv2)
问题:
虽然两个模型似乎在每一层都有相似的参数,但问题是它们的权重形状不相等。我知道 Caffe 的形状顺序与 Keras 不同,但顺序不是这里的问题。
问题是 Keras 的最后一个卷积层与 Caffe 的最后一个卷积层在 3 维的值不同。见下文。
Caffe 的权重形状:
>>> net = caffe.net('simple.prototxt', 'premade_weights.caffemodel', caffe.TEST)
>>> for i in range(len(net.layers)):
... if len(net.layers[i].blobs) != 0: # if layer has no weights
... print(("name", net._layer_names[i]))
... print("weight_shapes", [v.data.shape for v in net.layers[i].blobs])
('name', 'conv1')
('weight_shapes', [(96, 3, 11, 11), (96,)])
('name', 'conv2')
('weight_shapes', [(256, 48, 5, 5), (256,)])
Keras 的权重形状:
>>> for layer in model.layers:
... if len(layer.get_weights()) != 0:
... print(("name", layer.name))
... print(("weight_shapes", [w.shape for w in layer.get_weights()]))
('name', 'conv1')
('weight_shapes', [(11, 11, 3, 96), (96,)])
('name', 'conv2')
('weight_shapes', [(5, 5, 96, 256), (256,)])
这似乎是一种奇怪的行为。如您所见,Caffe 和 Keras 中的 conv1 形状是相等的(忽略顺序)。但是在 Caffe 中 conv2 形状是 [(256, 48, 5, 5), (256,)]),而在 Keras 'conv2' 中形状是 [(5, 5, 96, 256), (256,)],注意,48*2=96。
另外,注意 conv2 层直接位于最大池化层之后,因此 Keras 中的最大池化层可能有问题。
问题:
我是否正确解释了从 Caffe 到 Keras 的模型定义?尤其是最大池化层及其参数?
非常感谢!
【问题讨论】:
标签: python keras deep-learning caffe