Classify images using python interface in caffe

I have fine tuned the existed model and trained it before. The issue about how to classify the images will come along. So In this chapter, I will explain how to call python interface to classify images.

Here is the python source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
caffe_root = "/home/your_name/Downloads/caffe-master/"
import sys
sys.path.insert(0, caffe_root+"python")
import os
import caffe
import io

caffe.set_mode_cpu()
MODEL_FILE = caffe_root + "models/colon_caffenet/colon_deploy.prototxt"
#change the deploy file same as the train_val.prototxt
PRETRAINED = caffe_root + "models/colon_caffenet/caffenet_train_iter_80.caffemodel"
MEAN = caffe_root + "examples/colon/colon_mean.npy"
# later I will tell how to tranform *.binaryproto to *.npy
net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean = np.load(MEAN).mean(1).mean(1),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))

filewriter = open(caffe_root+"data/colon/test_result.txt","w+")
for root,dirs,files in os.walk(caffe_root+"data/colon/test/"): # all the images are in test folder
for file in files
IMAGE_FILE = os.path.join(root,file).decode('gbk').encode('utf8')
input_image = caffe.io.load_image(IMAGE_FILE)
prediction = net.predict([input_image])
string = os.path.basename(IMAGE_FILE)+" "+str(prediction[0].argmax())+"\n"
filewriter(string)
print os.path.basename(IMAGE_FILE), prediction[0].argmax()

filewriter.close()

convert binaryproto to npy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import caffe
import numpy as np
import sys

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 )

Meets lots of problems when import caffe

When python executes “import caffe”, many problems comes out. All problems are about the python packages. Although I have set up the python library pointing to anaconda2 lib, it seems programme will find all the dependencies in /usr/lib/python2.7/dist-packages. So if you have encountered the same issues, copy all dependencies in /anaconda2/lib/python2.7/site-packages to /usr/lib/python2.7/dist-packages

1
sudo cp -r /anaconda2/lib/python2.7/site-packages/* /usr/lib/python2.7/dist-packages

Import cv2: No Module named cv2

copy /usr/lib/python2.7/dist-packages/cv2.so to /anaconda2/lib/python2.7/site-packages/

ValueError: Mean shape incompatible with input shape

The problem is related to io.py, here is the solution

Let go to line 253-253 in caffe-master/python/caffe/io.py Replace

1
2
if ms != self.inputs[in_][1:]:
raise ValueError('Mean shape incompatible with input shape.')

by

1
2
3
4
5
6
7
if ms != self.inputs[in_][1:]:
print(self.inputs[in_])
in_shape = self.inputs[in_][1:]
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max - m_min) + m_min
#raise ValueError('Mean shape incompatible with input shape.')

Rebuild your code. It will be fine.