1

我已经使用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?

4

0 回答 0