c++ - 使用 caffemodel 在 C++ 中对 CIFAR10 数据进行分类时出错

标签 c++ opencv computer-vision deep-learning caffe

我正在尝试在 CIFAR10 数据上部署一个已经训练好的 caffemodel.h5 文件。

这是网络原型(prototype)文件。

name: "CIFAR10_quick_test"
input: "data"
input_dim: 10
input_dim: 3
input_dim: 32
input_dim: 32
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 32
    pad: 2
    kernel_size: 5
    stride: 1
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "pool1"
  top: "pool1"
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 32
    pad: 2
    kernel_size: 5
    stride: 1

  }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2"
  top: "conv2"
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: AVE
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "conv3"
  type: "Convolution"
  bottom: "pool2"
  top: "conv3"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 64
    pad: 2
    kernel_size: 5
    stride: 1
  }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "conv3"
  top: "conv3"
}
layer {
  name: "pool3"
  type: "Pooling"
  bottom: "conv3"
  top: "pool3"
  pooling_param {
    pool: AVE
    kernel_size: 3
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool3"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 64
  }
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
  }
}
layer {
  name: "prob"
  type: "Softmax"
  bottom: "ip2"
  top: "prob"
}

这是带有 OpenCV DNN 模块的 C++ 代码,我在其中尝试使用 caffemodel.h5 对飞机进行分类。

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
/* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
{
    Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
    Point classNumber;
    minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
    *classId = classNumber.x;
}
std::vector<String> readClassNames(const char *filename = "labels_cifar10.txt")
{
    std::vector<String> classNames;
    std::ifstream fp(filename);
    if (!fp.is_open())
    {
        std::cerr << "File with classes labels not found: " << filename << std::endl;
        exit(-1);
    }
    std::string name;

    while (!fp.eof())
    {
        std::getline(fp, name);
        if (name.length())
            classNames.push_back( name.substr(name.find(' ')+1) );
    }
    fp.close();
    return classNames;
}
int main(int argc, char **argv)
{
    String modelTxt = "cifar10_quick.prototxt";
    String modelBin = "cifar10_quick_iter_5000.caffemodel.h5";
    String imageFile = (argc > 1) ? argv[1] : "aeroplane.jpg";
    Ptr<dnn::Importer> importer;
    try                                     //Try to import Caffe GoogleNet model
    {
        importer = dnn::createCaffeImporter(modelTxt, modelBin);
    }
    catch (const cv::Exception &err)        //Importer can throw errors, we will catch them
    {
        std::cerr << err.msg << std::endl;
    }
    if (!importer)
    {
        std::cerr << "Can't load network by using the following files: " << std::endl;
        std::cerr << "prototxt:   " << modelTxt << std::endl;
        std::cerr << "caffemodel: " << modelBin << std::endl;
        exit(-1);
    }
    dnn::Net net;
    importer->populateNet(net);
    importer.release();                     //We don't need importer anymore
    Mat img = imread(imageFile);
    if (img.empty())
    {
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;
        exit(-1);
    }
    resize(img, img, Size(32, 32));       //GoogLeNet accepts only 224x224 RGB-images
    dnn::Blob inputBlob = dnn::Blob(img);   //Convert Mat to dnn::Blob image batch
    net.setBlob(".data", inputBlob);        //set the network input

    net.forward();                          //compute output
    dnn::Blob prob = net.getBlob("prob");   //gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    std::vector<String> classNames = readClassNames();
    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
    return 0;
} //main

此方法非常适合在 MNIST 数字分类上训练 LeNet。但是,我在此处运行输出时收到错误消息。

OpenCV Error: Assertion failed (!bias || blobs.size() == 2) in ConvolutionLayer, file /home/Downloads/opencv-3.1.0/opencv_contrib/modules/dnn/src/layers/convolution_layer.cpp, line 62
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/Downloads/opencv-3.1.0/opencv_contrib/modules/dnn/src/layers/convolution_layer.cpp:62: error: (-215) !bias || blobs.size() == 2 in function ConvolutionLayer

Aborted

需要一些帮助来解决这里的问题。

最佳答案

使用 .caffemodel 而不是 .caffemodel.h5 的快照格式解决了这个问题

solver.prototxt 中,将 snapshot_format: HDF5 更改为 snapshot_format: BINARYPROTO

关于c++ - 使用 caffemodel 在 C++ 中对 CIFAR10 数据进行分类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327712/

相关文章:

opencv - HOG预处理标准化

opencv - 如何运行基本的OpenCL人脸检测代码

c++ - 广度优先搜索中的问题

python - OpenCV 确定交叉/重叠区域

OpenCv : draw a white filled polygon

c++ - 如何在 CUDA 的核函数中将两个 openCV 矩阵相乘?

python - 使用 Python OpenCV 从图像中删除边框

c++ - 在 C++ 类中具有公共(public)属性

c++ 将 cv::Mat 从 CV_8U 转换为 CV_32F

c++ - 使用 C 类将华氏度转换为摄氏度