Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use trained model to make predictions

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use the trained model to make predictions. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Python version: Python2.7

Running platform: Ubuntu14.04

I. Preface

In the previous notes, a trained mnist.cafffemodel has been generated, and then we can use this model to make predictions. Before that, we need one more file: deploy.prototxt. So, let's start with deploy.prototxt.

II. Deploy.prototxt

The deploy.prototxt file is similar to train.prototxt, except that the input data layer of the first layer is deleted and then a description of the data dimension is added. At the same time, remove the final "loss" and "accurary" layers and add the "prob" layer, which is a Softmax probability layer.

1. The first layer of data dimension is described as follows:

Input: "data" describes the dimension of the input data

Input_dim:1 represents the amount of data augmentation for the identification sample, the size of which can be defined by itself. However, crop is usually performed five times, dividing the whole image into multiple flip. A value of 10 means that the sample to be identified will be divided into 10 parts and input to the network for identification. If you recognize relative to the whole image without augmenting the image data, you can set this value to 1

Input_dim:3 this value indicates the number of channels of the processed image. If the image is a RGB image, the number of channels is 3, set the value to 3; if the image is a grayscale image, the number of channels is 1, the value is set to 1

The length of the input_dim:28 image, which can be obtained through the crop_size in the data layer in the network configuration file

The width of the input_dim:28 image can be obtained through the crop_size in the data layer in the network configuration file.

two。 The last layer, "prob" layer:

3. Write code:

#-*-coding: UTF-8-*-import caffe def creat_deploy (): net = caffe.NetSpec () net.conv1 = caffe.layers.Convolution (bottom = 'data', kernel_size = 5, num_output = 20, weight_filler = dict (type =' xavier')) net.pool1 = caffe.layers.Pooling (net.conv1, kernel_size = 2, stride = 2 Pool = caffe.params.Pooling.MAX) net.conv2 = caffe.layers.Convolution (net.pool1, kernel_size = 5, num_output = 50, weight_filler = dict (type = 'xavier')) net.pool2 = caffe.layers.Pooling (net.conv2, kernel_size = 2, stride = 2 Pool = caffe.params.Pooling.MAX) net.fc1 = caffe.layers.InnerProduct (net.pool2, num_output = 500, weight_filler = dict (type = 'xavier')) net.relu1 = caffe.layers.ReLU (net.fc1, in_place = True) net.score = caffe.layers.InnerProduct (net.relu1, num_output = 10 Weight_filler = dict (type = 'xavier') net.prob = caffe.layers.Softmax (net.score) return net.to_proto () def write_net (deploy_proto): # write to deploy.prototxt file with open (deploy_proto) 'w') as fsaw # writes the first layer data description f.write ('input: "data"\ n') f.write (' input_dim:1\ n') f.write ('input_dim:3\ n') f.write (' input_dim:28\ n') f.write ('input_dim:28\ n') f.write (str (creat_deploy ()) ) if _ _ name__ = ='_ main__': my_project_root = "/ home/Jack-Cui/caffe-master/my-caffe-project/" deploy_proto = my_project_root + "mnist/deploy.prototxt" write_net (deploy_proto)

4.deploy.prototxt generates the following content:

Input: "data" input_dim:1input_dim:3input_dim:28input_dim:28layer {name: "conv1" type: "Convolution" bottom: "data" top: "conv1" convolution_param {num_output: 20kernel_size: 5weight_filler {type: "xavier"}} layer {name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { Pool: MAX kernel_size: 2stride: 2} layer {name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" convolution_param {num_output: 50kernel_size: 5weight_filler {type: "xavier"}} layer {name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param {pool: MAX kernel_size : 2stride: 2}} layer {name: "fc1" type: "InnerProduct" bottom: "pool2" top: "fc1" inner_product_param {num_output: 500weight_filler {type: "xavier"}} layer {name: "relu1" type: "ReLU" bottom: "fc1" top: "fc1" layer {name: "score" type: "InnerProduct" bottom: "fc1 "top:" score "inner_product_param {num_output: 10weight_filler {type:" xavier "}} layer {name:" prob "type:" Softmax "bottom:" score "top:" prob "}

III. Prediction

By running the above code, the deploy.prototxt file can be generated in the my-caffe-project/mnist directory, and the generated deploy.prototxt file can be used to make predictions using the trained model, as shown in the following figure:

The model trained in the previous note is in the my-caffe-project directory, as shown in the following figure:

Now you can use deploy.prototxt and mnist_iter_9380.caffemodel to make predictions, and the code is as follows:

#-*-coding: UTF-8-*-import caffe import numpy as npdef test (my_project_root Deploy_proto): caffe_model = my_project_root + 'mnist_iter_9380.caffemodel' # caffe_model file location img = my_project_root +' mnist/test/6/09269.png' # randomly find a picture to be tested labels_filename = my_project_root + 'mnist/test/labels.txt' # category name file, convert the numeric tag back to the category name net = caffe.Net (deploy_proto Caffe_model, caffe.TEST) # load model and deploy# image preprocessing settings transformer = caffe.io.Transformer ({'data': net.blobs [' data'] .data.shape}) # set the shape format of the picture (1min3, 2828) transformer.set_transpose ('data', (2p0re1)) # change the order of dimensions From the original picture (28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28 Change the picture from RGB to BGRim = caffe.io.load_image (img) # load the picture net.blobs ['data'] .data [...] = transformer.preprocess (' data',im) # to perform the image preprocessing operation set above And load the picture into blob out = net.forward () # execute the test labels = np.loadtxt (labels_filename, str Delimiter='\ t') # read the category name file prob = net.blobs ['prob'] .data [0] .flatten () # take out the probability values of the last layer (Softmax) belonging to a category order = prob.argsort () [- 1] # sort the probability values Take out the sequence number where the maximum value is located print 'picture number is:', labels [order] # convert the sequence number to the corresponding category name And print if _ _ name__ = ='_ _ main__': my_project_root = "/ home/Jack-Cui/caffe-master/my-caffe-project/" # my-caffe-project directory deploy_proto = my_project_root + "mnist/deploy.prototxt" # save the test (my_project_root, deploy_proto) of the deploy.prototxt file

The running results are as follows:

You can see that the results are correct, and the picture I randomly selected is the number 6 (mnist/test/6/09269.png).

The above is the prediction shared by the editor on how to use the trained model. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report