Using Caffe pre-trained model for image classification

Source: Internet
Author: User

I mainly analyze how to use Caffe pre-trained model for image classification
Caffe's examples the specific program of the task, to understand the process, as long as you read the program can Configure the Python environment, import NumPy, and set the display section

# set up Python environment:numpy for numerical routines, and matplotlib for plotting
import NumPy as NP
import m Atplotlib.pyplot as PLT
# display plots in this notebook
%matplotlib inline

# Set display defaults
plt.rc params[' figure.figsize ' = (ten)        # Large images plt.rcparams[' image.interpolation '
] = ' Nearest '  Don ' t interpolate:show square pixels
plt.rcparams[' image.cmap ' = ' Gray '  # use grayscale output rather than a ( Potentially misleading) color heatmap
Import Caffe (in fact, Pycaffe)
# The Caffe module needs to is on the Python path;
#  we ' ll add it here explicitly.
Import sys
caffe_root = '.  /'  # This file should is run from {caffe_root}/examples (otherwise to this line)
Sys.path.insert (0, Caffe_root  + ' python ')

import Caffe
# If you have ' No module named _caffe ', either you have not built Pycaffe or you have the Wrong path.
Download Models

Below, to determine if there is caffemodel under the models path under the Caffe_root path, and if not, take advantage of scripts in the Download_model_ folder under Caffe_root binary.py file Download the Caffe model
e.g., Caffenet's Caffemodel name is: Bvlc_reference_caffenet.caffemodel, placed under Caffe_root path under models path Bvlc_reference_ Caffenet folder (Models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel)

Caffe_root Level Two directory level Three directory/file level four directory/file
/models /bvlc_reference_caffenet /bvlc_reference_caffenet.caffemodel
/scripts /download_model_binary.py
/exampes /Currently running programs
/python /caffe /imagenet/...

-‘.. /' that represents the directory of the current running program, for example, the Caffe_root folder import model, and preprocessing to read from the hard disk into net

# set Caffe mode, which is set to CPU mode
caffe.set_mode_cpu ()

# caffenet Network results prototxt file
model_def = caffe_root + ' models/ Bvlc_reference_caffenet/deploy.prototxt '

# Caffenet's pre-trained model, which is the caffenet of the whole training models parameters
Model_weights = Caffe_root + ' Models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel '

# read Caffenet net = from hard drive
Caffe.net (Model_def,      # Defines the structure of the model
                model_weights,  # contains the trained weights
                Caffe. Test)     # Use test mode (e.g., don ' t perform dropout)
setting up preprocessing transformer
# load the mean imagenet image (as distributed with Caffe) for subtraction mu = np.load (caffe_roo T + ' python/caffe/imagenet/ilsvrc_2012_mean.npy ') mu = Mu.mean (1). mean (1) # Average over pixels to obtain the mean (BGR)  Pixel values print ' mean-subtracted values: ', zip (' BGR ', mu) # Create transformer for the input called ' data ' transformer = Caffe.io.Transformer ({' Data ': net.blobs[' data '].data.shape}) transformer.set_transpose (' Data ', (2,0,1)) # move Image channels to outermost dimension Transformer.set_mean (' Data ', mu) # subtract the Dataset-mean value in EAC H channel transformer.set_raw_scale (' Data ', 255) # Rescale from [0, 1] to [0, 255] transformer.set_channel_swap (' data ', (2,1,0)] # Swap channels from RGB to BGR 

Caffenet the training images is preprocessed in the traning image, the new image must be pre-trained in order to be able to use the preprocessing model to classify it. In this program, the use of the Caffe.io.Transformer
The code is as follows, and the code is briefly explained (not fully understood, but incrementally improved) the mean value of all images that import imagenet data

Here the Ilsvrc_2012_mean.npy file is numpy when the data file, type is created transformer

The main function of the transformer is
(1) Conversion of the dimensions of the array corresponding to the read image
To identify the image, you need to use Python to read the image, Python read the image format: High image, image width, image channel
In order to adapt to the Caffe data format, it needs to be transformed into: Image channel, high image, wide image
(2) All pixel values of each channel of the input image are subtracted from the mean value of three channel of all images in the Imagenet database, that is, mu
(3) The image pixel value in the Rescale,python of the test image is [0,1], in order to use the Caffe model, the pixel value of the image needs to be changed back to [0,255]
(4) The input image of the three-channel sequence to transform, ordinary images are r-g-b, but Caffe processing RGB image, transform it into b-g-r import image, classification set net input shape

# Set the size of the input (we can skip this if we ' re happy
#  with the default; we can also change it later, e.g. , for different batch sizes)
net.blobs[' data '].reshape (        # Batch Size
                          3,         # 3-channel (BGR) images
                          )  # Image size is 227x227
load image, and preprocessing using transformer
# using LOAD_IAMGE to import images from the hard disk, the image obtained is a (360, 480, 3) ndarray image
= caffe.io.load_image (caffe_root + ' examples/images /cat.jpg ')
# preprocessing the image to get the Ndarray shape (3,,,)
transformed_image = transformer.preprocess (' Data ', image '
print transformed_image.shape
# Displays the image
Plt.imshow
using the network to classify the input images
# Copy the preprocessed image to the net-allocated memory
net.blobs[' data '].data[...] = transformed_image

# Compute the network output, it is a dict, Key-prob corresponds to the prob value
output = Net.forward () of the input image to

remove the prob vector corresponding to the input image from Dict, its scale is (1000,) 
Output_prob = output[' prob '][0]  # The output probability vector

for the ' the ' the ' the ' the ' the ', ' Batch print ' predicted class is: ', ou Tput_prob.argmax ()

The input is a pair of cat, the program runs the result is:

Predicted class is:281 find the label that corresponds to Prob's largest location

# import imagenet Data set label file

# to determine if the label file exists, and if not, download the document if not
os.path.exists (labels_file):
    !.. /data/ilsvrc12/get_ilsvrc_aux.sh

# import lables from TXT file, which is a (1000,) ndarray
labels = np.loadtxt (Labels_file, STR, delimiter= ' t ')

print ' Output label: ', Labels[output_prob.argmax ()]

Result is

Output label:n02123045 tabby, tabby cat View 5-top forecast Results

# sort top Five predictions from Softmax output
top_inds = Output_prob.argsort () [:: -1][:5]  # reverse sort and take Five largest items

print ' Probabilities and Labels: '
zip (output_prob[top_inds), Labels[top_inds]

The results are as follows:

Probabilities and Labels:
OUT[27]:
[(0.31243625, ' n02123045 tabby, tabby cat '),
(0.23797157, ' n02123159 Tiger Cat '),
(0.12387245, ' n02124075 Egyptian Cat '),
(0.10075716, ' n02119022 red fox, Vulpes vulpes '),
(0.070957333, ' n02127052 Lynx, Catamount ')]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.