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

C++ 在C+中对CIFAR10数据进行分类时出错+;使用caffemodel,c++,opencv,computer-vision,deep-learning,caffe,C++,Opencv,Computer Vision,Deep Learning,Caffe,我正在尝试在CIFAR10数据上部署一个经过培训的caffemodel.h5文件 这是网络协议TXT文件 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:

我正在尝试在CIFAR10数据上部署一个经过培训的caffemodel.h5文件

这是网络协议TXT文件

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++代码,我尝试用CalpMeldel.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

需要一些帮助来解决这个问题

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


solver.prototxt
中,将
snapshot_格式:HDF5
更改为
snapshot_格式:BINARYPROTO

是否将模型保存为
BINARYPROTO
?(这只是扩展名
.caffemodel
,而不是
.caffemodel.h5
)?我在使用hdf5保存的模型时遇到了一些类似的问题。没有,因为该模型保存为.caffemodel.h5。在将模型保存为.caffemodel而不是.caffemodel.h5时,我使用此方法进行了培训。在您正在使用的
solver.prototxt
中,更改为感谢!我将使用BinaryProto谢,这是使用.caffemodel工作的
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