简单记录一下自己使用caffe的过程和遇到的一些问题。

下载caffe以及安装不具体叙述了。 可參照 http://caffe.berkeleyvision.org/installation.html。


以下准备数据集和训练的过程參照imagenet的过程:可參考  http://drubiano.github.io/2014/06/18/caffe-custom-data.html

1. 将数据集分为train和validate, 分别写到train.txt和val.txt中。 格式每一行文件名称+ 空格 + label (label 是从0開始, 并连续)

00001.jpg 0

00002.jpg 1

00004.jpg 1

00003.jpg 2

2. 将准备好的两个txt, 放到caffe_root/data/myfile/    (caffe_root是caffe的根文件夹, myfile自己命名)。

3. 进入caffe_root/examples/imagenet/ 文件夹中, 改动create_imagenet.sh

DATA=data/myfile

TRAIN_DATA_ROOT=/img_full_dir1 (img_full_dir1 存放训练图片的文件夹)

VAL_DATA_ROOT=/img_full_dir2 (img_full_dir2 存放測试图片的文件夹)

在caffe_root 下运行 ./examples/imagenet/create_imagenet.sh , 能够在examples/imagenet 文件夹下找到, ilsvrc12_train_lmdb  ilsvrc12_val_lmdb两个文件夹

4.  在caffe_root 下运行 ./examples/imagenet/make_imagenet_mean.sh , 结果保存在data/ilsvrc12/imagenet_mean.binaryproto

5.  改动models/bvlc_reference_caffenet 下的solver.prototxt和 train_val.prototxt。 假设不改动网络的结构, 仅仅需改动图片的大小crop_size <imagewidth。

6. 在caffe_root 下运行 ./examples/imagenet/train_caffenet.sh, 进行训练。训练好的模型放在models/bvlc_reference_caffenet

測试网络及模型

1. 在python下执行。 须要先在根文件夹下执行 make all    make pycaffe     make distribute

2. 将data/ilsvrc12/imagenet_mean.binaryproto 转为numpy格式, 在caffe_root 创建一个mycode的目录,创建一个convertmean.py。内容例如以下:

#!/usr/bin/python

import numpy as np
import sys
sys.path.append('/caffe_root/python')<span style="white-space:pre">			</span>#caffe_root is your caffe root dir need change
import caffe

if len(sys.argv) != 3:
    print "Usage: python convert_protomean.py proto.mean out.npy"
    sys.exit()


blob = caffe.proto.caffe_pb2.BlobProto()
data = open( sys.argv[1] , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save( sys.argv[2] , out )

3. 运行 python convertmean.py /caffe_root/data/ilsvrc12/imagenet_mean.binaryproto mean.npy

4. 将一张图片放到mycode的文件夹下,vim testcode.py

#!/usr/bin/python
import numpy as np
caffe_root = '/caffe_root/'<span style="white-space:pre">			</span>#<span style="font-family: Arial, Helvetica, sans-serif;">caffe_root is your caffe root dir need change</span>

import sys
sys.path.insert(0,caffe_root+'python')
import caffe

MODEL_FILE = caffe_root + '/models/bvlc_reference_caffenet/deploy.prototxt'<span style="white-space:pre">			</span>#your net
PRETRAINED = caffe_root + '/models/bvlc_reference_caffenet/caffenet_train.caffemodel'<span style="white-space:pre">		</span>#your model 
IMAGE_FILE = caffe_root + 'mycode/imagename.jpg'<span style="white-space:pre">						</span>#your image

import os
if not os.path.isfile(PRETRAINED):
    print("Downloading pre-trained CaffeNet model...")

caffe.set_mode_cpu()
#net = caffe.Classifier(MODEL_FILE, PRETRAINED,
#   mean=np.load(caffe_root + 'mycode/mean.npy').mean(1).mean(1),
#   channel_swap=(2,1,0),
#   raw_scale=255,
#   image_dims=(256, 256))

net = caffe.Classifier(MODEL_FILE, PRETRAINED)
net.set_raw_scale('data',255)
net.set_channel_swap('data',(2,1,0))
net.set_mean('data',np.load(caffe_root + 'mycode/mean.npy'))

input_image = caffe.io.load_image(IMAGE_FILE)
prediction = net.predict([input_image])
print 'prediction shape:', prediction[0].shape
print 'predicted class:', prediction[0].argmax()


以上就是整个过程, 仅供參考。 引用了很多网上的代码。