一.
二.生成deploy.prototxt文件的python代码:
# -*- coding: utf-8 -*-
caffe_root = '/home/xwang/Tools/caffe/'
import sys
sys.path.insert(0, caffe_root + 'python')
from caffe import layers as L,params as P,to_proto
root='/home/xwang/Tools/caffe/'
deploy='/home/xwang/Tools/caffe/examples/mnist/lenet_deploy.prototxt'
def create_deploy():
#少了第一层,data层
conv1=L.Convolution(name='conv1',bottom='data', kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))
pool1=L.Pooling(conv1,name='pool1',pool=P.Pooling.MAX, kernel_size=2, stride=2)
conv2=L.Convolution(pool1, name='conv2',kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))
pool2=L.Pooling(conv2, name='pool2',top='pool2', pool=P.Pooling.MAX, kernel_size=2, stride=2)
fc3=L.InnerProduct(pool2, name='ip1',num_output=500,weight_filler=dict(type='xavier'))
relu3=L.ReLU(fc3, name='relu1',in_place=True)
fc4 = L.InnerProduct(relu3, name='ip2',num_output=10,weight_filler=dict(type='xavier'))
#最后没有accuracy层,但有一个Softmax层
prob=L.Softmax(fc4, name='prob')
return to_proto(prob)
def write_deploy():
with open(deploy, 'w') as f:
f.write('name:"LeNet"\n')
f.write('layer {\n')
f.write('name:"data"\n')
f.write('type:"Input"\n')
f.write('top:"data"\n')
f.write('input_param { shape : {')
f.write('dim:1 ')
f.write('dim:3 ')
f.write('dim:28 ')
f.write('dim:28 ')
f.write('} }\n}\n\n')
f.write(str(create_deploy()))
if __name__ == '__main__':
write_deploy()
注意,将生成的lenet_deploy.prototxt文件改名为deploy.prototxt,然后需要对deploy.prototxt的输入参数做微小修改:
input_param { shape : {dim:1 dim:1 dim:28 dim:28 } }
即将第二个dim即通道数由3改为1。
三.此时便可以对模型进行测试了,test.py的代码如下:
import sys
import os
import logging
import numpy as np
import pandas as pd
import caffe
caffe_root = '/home/ganlei/caffe'
sys.path.insert(0, caffe_root + 'python')
DATA_ROOT = '/home/ganlei/caffe/data/kaggle_mnist'
MODEL_ROOT = '/home/ganlei/caffe/examples/kaggle_mnist'
join = os.path.join
TEST = join(DATA_ROOT, 'test.csv')
OUTPUT = join(DATA_ROOT, 'result.csv')
CAFFE_MODEL = join(MODEL_ROOT, 'lenet_iter_10000.caffemodel')
CAFFE_SOLVER = join(MODEL_ROOT, 'deploy.prototxt')
# logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
sh.setFormatter(formatter)
logger.addHandler(sh)
# load test dataset
logger.info('Load test dataset from %s', TEST)
df = pd.read_csv(TEST)
data = df.values
data = data.reshape((len(data), 28, 28, 1))
data = data / 255.
# set caffe net
net = caffe.Classifier(CAFFE_SOLVER, CAFFE_MODEL)
# predict
logger.info('Start predict')
BATCH_SIZE = 100
iter_k = 0
labels = []
while True:
logger.info('ITER %d', iter_k)
batch = data[iter_k*BATCH_SIZE: (iter_k+1)*BATCH_SIZE]
if batch.size == 0:
break
result = net.predict(batch)
for label in np.argmax(result, 1):
labels.append(label)
iter_k = iter_k + 1
logger.info('Prediction Done')
# write to file
logger.info('Save result to %s', OUTPUT)
if os.path.exists(OUTPUT):
os.remove(OUTPUT)
with open(OUTPUT, 'w') as fd:
fd.write('ImageId,Label\n')
for idx, label in enumerate(labels):
fd.write(str(idx+1))
fd.write(',')
fd.write(str(label))
fd.write('\n')
在caffe目录下python2.7 test.py即可。
四.得到结果及上传到kaggle
等到程序运行完后,就会在/home/ganlei/caffe/data/kaggle_mnist/下得到test.csv文件,这就是测试结果了,我们把他上传到kaggle上去就好了。