Using the trained Caffe network to get the classification of the input image

Source: Internet
Author: User
Tags join

This document describes: If you have trained a caffe network, how to use this network for image classification.

The following is an example of a mnist network.

Mnist is used to classify handwritten numerals 0-9. When the user has written a number, the image enters the Mnist network, and then the network calculates the probability of each number, which is considered to be the maximum number of probabilities. Python is required to install Python using the network, so please install Python and set the path as described in http://caffe.berkeleyvision.org/installation.html Python and/or Matlab wrapper (optional) Chapter find $caffe_root\python\classify.py, copy it to another place, for example $caffe_root\exam Ples\mnist, named classifytest.py

Open this file. The file imports the Caffe related files, and then reads the parameters in ARG, such as Input_file, Output_file, Model_def, Pretrained_model, and so on, and some parameters seem irrelevant to what we want to use. For example, Channel_swap (mnist input black and white images, only a single channel), Input_scale (default is 1), etc., but the code snippet of these parameters must not be deleted, the deletion may result in a subsequent call to Python error. if you feel that entering these parameters in the terminal is too cumbersome, you can add a "dafault= default value you want" to modify the parameters

Change the value of "--model_def" in the parameter to the prototxt of the network structure, for example, I changed it:

Parser.add_argument (
        "--model_def",
        default=os.path.join (Pycaffe_dir,
                "... /.. /examples/mnist/lenet.prototxt "),
        help=" Model definition file. "
)
Change the parameter Pretrained_model to the previously trained parameter file, for example I changed:

Parser.add_argument (
        "--pretrained_model",
        default=os.path.join (Pycaffe_dir,
                "... /.. /examples/mnist/lenet_iter_10000.caffemodel "),
        help=" trained model weights file. "
)

Because mnist requires a grayscale image for the input, you need to convert the RGB graph to grayscale and change the Channel_swap:

Add at the beginning of the file

From Skimage.color import Rgb2gray

Convert the RGB graph to grayscale after reading the picture into the inputs

inputs = [Rgb2gray (input) for input in inputs]

Change Channel_swap:

Channel_swap = [0]

. py file changes This is the case, if your model is not the same, you need to take a different change, extrapolate.

Prepare a picture as an input image, this image must be 28*28 size JPEG format picture, content is arbitrary, but after all, mnist for classifying numbers, we use the Windows Drawing tool to draw a black white number ' 0 ', and then upload this image to $caffe_ Root\examples\mnist, named Testformnist0.jpg

Enter Terminal:

CD $CAFFE _root

Python examples/mnist/classifytest.pyexamples/mnist/testformnist0.jpg output


The first parameter is Python, which means using Python to execute the second argument. The third parameter is the location of the input file Input_file, and the fourth parameter is the location of the output file Output_file

The results are as follows:


where probability (input== ' 0 ') = 0.967, the network correctly classifies this image.

If you want to display a result in the middle, you can do so by modifying the network structure definition file. For example, I want to see what the result was before the Softmax_loss layer:

Open Lenet_prototxt

The Final Document is

Layers {
  name: "Prob"
  type:softmax
  Bottom: "ip2"
  Top: "Prob"
}

Indicates that there is a layer, input ip2 (inner_product2), output prob (the probability of each number), the type of layer is Softmax, and the entire network results, this layer is the topmost layer, its output will be the output of the entire network.

So we just need to delete this layer, then the previous top layer is replaced by the second top layer, the second top layer of the result is the result of Softmax.

Delete and then run Python:

I can't understand it at all.

It is true that the parameters and results of the deep Learning network are not easy to understand, and some times do require intermediate results from the network.




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.