版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MOU_IT/article/details/83099167
1、deploy.prototxt
首先是deploy.prototxt文件的生成, deploy.prototxt文件和train.prototxt相似,区别在于第一层的输入数据层被删除,然后添加一个数据维度的描述。同时,移除了最后的”loss”和”accurary”层,加入”prob”层,也就是一个Softmax概率层。
(1)第一层数据维度描述如下:
input:"data"
input_dim:1
input_dim:3
input_dim:28
input_dim:28
input:”data” 对输入数据维度进行描述;
input_dim:1 表示对待识别样本进行数据增广的数量,该值的大小可自行定义。但一般会进行5次crop,将整幅图像分为多个flip。该值为10则表示会将待识别的样本分为10部分输入到网络进行识别。如果相对整幅图像进行识别而不进行图像数据增广,则可将该值设置为1;
input_dim:3 该值表示处理的图像的通道数,若图像为RGB图像则通道数为3,设置该值为3;若图像为灰度图,通道数为1则设置该值为1;
input_dim:28 图像的长度,可以通过网络配置文件中的数据层中的crop_size来获取;
input_dim:28 图像的宽度,可以通过网络配置文件中的数据层中的crop_size来获取。
(2)最后一层”prob”层:
layer{
name:"prob"
type:"Softmax"
bottom:"score"
top:"prob"
}
deploy.prototxt一般不推荐用代码来生成,熟悉train.prototxt的可以将其复制一份进行相应的修改如下:
input:"data"
input_dim:1
input_dim:3
input_dim:28
input_dim:28
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
convolution_param {
num_output: 20
kernel_size: 5
weight_filler {
type: "xavier"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
convolution_param {
num_output: 50
kernel_size: 5
weight_filler {
type: "xavier"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "fc1"
type: "InnerProduct"
bottom: "pool2"
top: "fc1"
inner_product_param {
num_output: 500
weight_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: 10
weight_filler {
type: "xavier"
}
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "score"
top: "prob"
}
2、预测:
现在就可以使用deploy.prototxt和mnist_iter_xxxx.caffemodel做预测了,编写代码如下:
# -*- coding: UTF-8 -*-
import caffe
import numpy as np
def test(my_project_root, deploy_proto):
caffe_model = my_project_root + 'mnist_iter_xxxx.caffemodel' #caffe_model文件的位置
img = my_project_root + 'mnist/test/6/09269.png' #随机找的一张待测图片
labels_filename = my_project_root + 'mnist/test/labels.txt' #类别名称文件,将数字标签转换回类别名称
net = caffe.Net(deploy_proto, caffe_model, caffe.TEST) #加载model和deploy
#图片预处理设置
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) #设定图片的shape格式(1,3,28,28)
transformer.set_transpose('data', (2,0,1)) #改变维度的顺序,由原始图片(28,28,3)变为(3,28,28)
transformer.set_raw_scale('data', 255) # 缩放到【0,255】之间
transformer.set_channel_swap('data', (2,1,0)) #交换通道,将图片由RGB变为BGR
im = caffe.io.load_image(img) #加载图片
net.blobs['data'].data[...] = transformer.preprocess('data',im) #执行上面设置的图片预处理操作,并将图片载入到blob中
out = net.forward() #执行测试
labels = np.loadtxt(labels_filename, str, delimiter='\t') #读取类别名称文件
prob = net.blobs['prob'].data[0].flatten() #取出最后一层(Softmax)属于某个类别的概率值
order = prob.argsort()[-1] #将概率值排序,取出最大值所在的序号
print '图片数字为:',labels[order] #将该序号转换成对应的类别名称,并打印
if __name__ == '__main__':
my_project_root = "/home/Jack-Cui/caffe-master/my-caffe-project/" #my-caffe-project目录
deploy_proto = my_project_root + "mnist/deploy.prototxt" #保存deploy.prototxt文件的位置
test(my_project_root, deploy_proto)
参考:https://blog.csdn.net/c406495762/article/details/70446385