QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt";
QString modelBin = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\snapshot_iter_10000.caffemodel";
QString imageFile = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\9.png";
//读取存储在caffe模型文件中的网络模型
cv::dnn::Net net = cv::dnn::readNetFromCaffe(modelPrototxt.toStdString(),modelBin.toStdString());
if (net.empty())
{
qDebug() << "readNetFromCaffe faild";
}
//读取图像文件
cv::Mat img = cv::imread(imageFile.toStdString(),0);
if(img.empty())
{
qDebug() << "imread faild";
}
cv::Mat inputBlob = cv::dnn::blobFromImage(img,0.00390625f, cv::Size(28, 28), cv::Scalar(), false);
cv::Mat prob;
cv::TickMeter t;
for (int i = 0; i < 1; i++)
{
//设置网络的输入层名字(和训练网络模型文件里面的 name 对应)
net.setInput(inputBlob, "data");
t.start();
//设置网络的输出层名字(和训练网络模型文件里面的 name 对应)
prob = net.forward("prob");
t.stop();
}
int classId;
double classProb;
cv::Mat probMat = prob.reshape(1, 1); //reshape the blob to 1x1000 matrix
cv::Point classNumber;
cv::minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
classId = classNumber.x;
qDebug() << t.getTimeMicro() << "index:" << classId << "%"<< classProb;
LeNet
name: "LeNet"
input: "data"
input_shape {
dim: 1 # batchsize
dim: 1 # number of channels
dim: 28 # width
dim: 28 # height
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
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"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}