Caffe study notes - image classification with trained caffemodel

The caffe program comes with a cat picture, and the storage path is examples/images/cat.jpg in the caffe root directory. Now we want to use a trained caffemodel to classify this picture. The team that developed caffe used imagenet images and caffenet models to train a caffemodel for everyone to download. To classify images, this caffemodel is the best. Whether we use c++ to classify or use the python interface to classify, we should prepare the following three files:

1. caffemodel file

You can directly enter the address in the browser to download, or you can run the script file to download. Since it runs in the Windows environment, enter the address directly in the browser to download, the download address is: http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

The file name is: bvlc_reference_caffenet.caffemodel, and the file size is about 230M. In order to unify the code, download the caffemodel file to the models/bvlc_reference_caffenet/ folder in the caffe root directory. If you are in Ubuntu environment, you can also run the script file to download:

# sudo ./scripts/download_model_binary.py models/bvlc_reference_caffenet

2. Mean file

With the caffemodel file, the corresponding mean file is required. In the testing phase, the mean value needs to be subtracted from the test data. I couldn't find a place to download this file, so I didn't subtract the mean when testing. If you use a script to download in the Ubuntu environment, execute it in the caffe root directory:

# sudo sh ./data/ilsvrc12/get_ilsvrc_aux.sh

After executing and downloading, the mean files are placed in the data/ilsvrc12/ folder.

3、synset_words.txt文件

This file contains the names of 1000 classes in the data/ilsvrc12/ folder.

Here, we use the PYTHON interface to implement the classification, referring to the code written by denny. Write a py file named classify_cat.py

#coding=utf-8
#Load necessary libraries
import numpy as np

import sys,os

#Set the current directory
caffe_root = 'D:/caffe/caffe-master/caffe-master/'
os.chdir(caffe_root)
#sys.path.insert(0, caffe_root + 'python')
#sys.path.append('D:/caffe/caffe-master/caffe-master/python')
sys.path.append('D:/caffe/caffe-master/caffe-master/python/caffe')
import caffe


net_file=caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
caffe_model=caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
mean_file=caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'

net = caffe.Net(net_file,caffe_model,caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))

im=caffe.io.load_image(caffe_root+'examples/images/cat.jpg')
net.blobs['data'].data[...] = transformer.preprocess('data',im)
out = net.forward()


imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')

top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
for i in np.arange(top_k.size):
    print (top_k[i], labels[top_k[i]])

There are two ways to execute this file in the CMD window:

(1) First convert the path to the location of the py file and then execute:


(2) Direct execution


After execution, the following results are obtained:


Caffe has a built-in classify.py file, which is not run here, so I won't explain it further. If you are interested, you can read Denny's blog.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325740059&siteId=291194637