我已经使用 Caffe 为MNIST训练了 LeNet ,现在我想将此模型导出到 Keras 中使用 .

为此,我尝试从caffe.Net中提取权重并使用它们来初始化Keras的网络 . 但是,我收到了两个模型的不同预测 . 所以我试图逐层调试它们,从第一个开始 . 我测试的代码如下:

# import caffe and load facce.Net from prototxt and caffemodel files
import sys
sys.path.append('/opt/caffe/python')
import caffe
net = caffe.Net('lenet_train_test.prototxt', 'save/mnist_iter_500000.caffemodel', caffe.TEST)

# this should be the kernel weights and bias for the first convolution layer 'conv1'
c1_w = net.params['conv1'][0].data
c1_b = net.params['conv1'][1].data

# import Keras and build a convolution layer using the same parameters as in lenet_train_test.prototxt and instantiate it with the weights above
import keras
from keras.layers import Convolution2D    
conv1 = Convolution2D(20, 5, 5, border_mode='valid', input_shape=(1, 28, 28), weights=[c1_w,c1_b], activation='linear')

from keras.models import Sequential
model=Sequential()
model.add(conv1)
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])

# load MNIST data and do scaling like I did when training Caffe model
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_test = X_test.astype('float32')
X_test *= 0.00392157

# set the first test example and get the prediction 
net.blobs['data'].data[0] = X_test[0][0]
net.forward()
out0 = net.blobs['conv1'].data[0] # this is the prediction for 20 kernels
m0 = out0[0]                      # just consider the first one
m0.shape  # >>> (24,24)

# pass the same example through the conv1 layer in Keras model
import numpy as np
a = np.zeros( (1,1,28,28) )
a[0] = X_test[0]
out1 = model.predict_on_batch(a)  # this is the prediction for 20 kernels

m1 = out1[0][0]                   # just consider the first one
m1.shape  # >>> (24,24) the same size

# I get a lots of 'False'
m0 == m1

我在图层构造中做错了吗?或者也许Caffe和Keras以不同的方式实现了convolution2D?