Caffe 层创建失败 - 层类型未知

Posted

技术标签:

【中文标题】Caffe 层创建失败 - 层类型未知【英文标题】:Caffe layer creation failure - Unknown layer type 【发布时间】:2017-03-24 07:35:22 【问题描述】:

我在 Visual Studio 2015 中运行简单的 C++ 代码将图像传递给我的 preTrained caffemodel,但是当我想创建网络时会出现此错误:

Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Input (known types: Convolution, Eltwise, LRN, Pooling, Power, Python, ReLU, Sigmoid, Softmax, Split, TanH)

我的 prototxt 文件:

name: "DeepID_face"
input: "data_1"
input_dim: 1
input_dim: 3
input_dim: 640
input_dim: 480
layer 
  name: "conv1_1"
  type: "Convolution"
  bottom: "data_1"
  top: "conv1_1"
  param 
    name: "conv1_w"
    lr_mult: 1
    decay_mult: 1
  
  param 
    name: "conv1_b"
    lr_mult: 2
    decay_mult: 0
  
  convolution_param 
    num_output: 20
    kernel_size: 4
    stride: 1
    weight_filler 
      type: "gaussian"
      std: 0.01
    
    bias_filler 
      type: "constant"
      value: 0
    
  

layer 
  name: "relu1_1"
  type: "ReLU"
  bottom: "conv1_1"
  top: "conv1_1"

layer 
  name: "norm1_1"
  type: "LRN"
  bottom: "conv1_1"
  top: "norm1_1"
  lrn_param 
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  

layer 
  name: "pool1_1"
  type:  "Pooling"
  bottom: "norm1_1"
  top: "pool1_1"
  pooling_param 
    pool: MAX
    kernel_size: 2
    stride: 2
  

layer 
  name: "conv2_1"
  type: "Convolution"
  bottom: "pool1_1"
  top: "conv2_1"
  param 
    name: "conv2_w"
    lr_mult: 1
    decay_mult: 1
  
  param 
    name: "conv2_b"
    lr_mult: 2
    decay_mult: 0
  
  convolution_param 
    num_output: 40
    kernel_size: 3
    group: 2
    weight_filler 
      type: "gaussian"
      std: 0.01
    
    bias_filler 
      type: "constant"
      value: 0.1
    
  


layer 
  name: "relu2_1"
  type: "ReLU"
  bottom: "conv2_1"
  top: "conv2_1"

layer 
  name: "norm2_1"
  type: "LRN"
  bottom: "conv2_1"
  top: "norm2_1"
  lrn_param 
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  

layer 
  name: "pool2_1"
  type:  "Pooling"
  bottom: "norm2_1"
  top: "pool2_1"
  pooling_param 
    pool: MAX
    kernel_size: 2
    stride: 2
  

layer 
  name: "conv3_1"
  type: "Convolution"
  bottom: "pool2_1"
  top: "conv3_1"
  param 
    name: "conv3_w"
    lr_mult: 1
    decay_mult: 1
  
  param 
    name: "conv3_b"
    lr_mult: 2
    decay_mult: 0
  
  convolution_param 
    num_output: 60
    kernel_size: 3
    weight_filler 
      type: "gaussian"
      std: 0.01
    
    bias_filler 
      type: "constant"
      value: 0
    
  


layer 
  name: "pool3_1"
  type:  "Pooling"
  bottom: "conv3_1"
  top: "pool3_1"
  pooling_param 
    pool: MAX
    kernel_size: 2
    stride: 2
  

layer 
  name: "conv4_1"
  type: "Convolution"
  bottom: "pool3_1"
  top: "conv4_1"
  param 
    name: "conv4_w"
    lr_mult: 1
    decay_mult: 1
  
  param 
    name: "conv4_b"
    lr_mult: 2
    decay_mult: 0
  
  convolution_param 
    num_output: 80
    kernel_size: 2
    stride: 2
    weight_filler 
      type: "gaussian"
      std: 0.01
    
    bias_filler 
      type: "constant"
      value: 0.1
    
  


layer
  name:"flatten_pool3_1"
  type:"Flatten"
  bottom:"pool3_1"
  top:"flatten_pool3_1"

layer
  name:"flatten_conv4_1"
  type:"Flatten"
  bottom:"conv4_1"
  top:"flatten_conv4_1"

layer
  name:"contact_conv"
  type:"Concat"
  bottom:"flatten_conv4_1"
  bottom:"flatten_pool3_1"
  top:"contact_conv"

layer 
  name: "deepid_1"
  type:  "InnerProduct"
  bottom: "contact_conv"
  top: "deepid_1"
  param 
    name: "fc6_w"
    lr_mult: 1
    decay_mult: 1
  
  param 
    name: "fc6_b"
    lr_mult: 2
    decay_mult: 0
  
  inner_product_param 
    num_output: 160
    weight_filler 
      type: "gaussian"
      std: 0.005
    
    bias_filler 
      type: "constant"
      value: 0.1
    
  


layer 
  name: "loss"
  type: "Softmax"
  bottom: "deepid_1"
  top: "loss"

以及我想用 Visual Studio 2015 编译的简单代码:

#define USE_OPENCV
#include <cuda_runtime.h>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include "caffe/caffe.hpp"
#include "caffe/util/io.hpp"
#include "caffe/blob.hpp"
#include <opencv2\opencv.hpp>
#include <boost/shared_ptr.hpp>

using namespace caffe;
using namespace std;

int main(int argc, char** argv) 

    Caffe::set_mode(Caffe::CPU);
    caffe::string netS = "C:/Users/127051/Documents/Visual Studio 2015/Projects/C++/Caffe/CaffeTest/x64/Release/net_struct.prototxt";
    caffe::string netW = "C:/Users/127051/Documents/Visual Studio 2015/Projects/C++/Caffe/CaffeTest/x64/Release/net_weights.caffemodel";
    Datum datum;
    cv::Mat img = cv::imread("D:/FEI/All/1-08.jpg");
    if (img.empty())
        LOG(ERROR) << "Error during file reading";
    else
        caffe::CVMatToDatum(img, &datum);
    //get the net
    boost::shared_ptr<Net<float> > net_;
    net_.reset(new Net<float>(netS, TEST));
    //get trained net
    net_->CopyTrainedLayersFrom(netW);

    //get the blob
    Blob<float>* blob = new Blob<float>(1, datum.channels(), datum.height(), datum.width());

    //get the blobproto
    BlobProto blob_proto;
    blob_proto.set_num(1);
    blob_proto.set_channels(datum.channels());
    blob_proto.set_height(datum.height());
    blob_proto.set_width(datum.width());
    const int data_size = datum.channels() * datum.height() * datum.width();
    int size_in_datum = std::max<int>(datum.data().size(),
        datum.float_data_size());
    for (int i = 0; i < size_in_datum; ++i) 
        blob_proto.add_data(0.);
    
    const string& data = datum.data();
    if (data.size() != 0) 
        for (int i = 0; i < size_in_datum; ++i) 
            blob_proto.set_data(i, blob_proto.data(i) + (uint8_t)data[i]);
        
    

    //set data into blob
    blob->FromProto(blob_proto);

    //fill the vector
    vector<Blob<float>*> bottom;
    bottom.push_back(blob);
    float type = 0.0;

    const vector<Blob<float>*>& result = net_->Forward(bottom, &type);

    //Here I can use the argmax layer, but for now I do a simple for :)
    float max = 0;
    float max_i = 0;
    for (int i = 0; i < 1000; ++i) 
        float value = result[0]->cpu_data()[i];
        if (max < value) 
            max = value;
            max_i = i;
        
    
    LOG(ERROR) << "max: " << max << " i " << max_i;

    return 0;

我也设置了 (/OPT:NOREF) 但没有修复它。还将图层格式更改为:

layer 
  name: "data_1"
  type: "Input"
  top: "data_1"
  input_param  shape:  dim: 1 dim: 3 dim: 640 dim: 480  

但不固定。 请帮帮我。

【问题讨论】:

您需要将top: "data" 更改为top: "data_1",因为您的第一个conv 层底部是data_1dim: 3 内层。 【参考方案1】:

我终于通过一些更改解决了这个问题,并在 caffe 源代码中添加了一些代码,如下所示:

如果您想注册未注册的图层,请执行以下步骤:

1) 首先在 caffe\layers 注释中层源代码 REGISTER_LAYER_CLASS

2) 其次,在 layer_factory.cpp 中添加一些代码,如下所示,我为输入层编写的代码:

// Get input layer according to engine.
template <typename Dtype>
shared_ptr<Layer<Dtype> > GetInputLayer(const LayerParameter& param) 
    int engine = 0;
#ifdef USE_CUDNN
        engine = 1;
#endif

    if (engine == 0) 
        return shared_ptr<Layer<Dtype> >(new InputLayer<Dtype>(param));
#ifdef USE_CUDNN
    
    else if (engine == 1) 
        return shared_ptr<Layer<Dtype> >(new CuDNNInputLayer<Dtype>(param));
#endif
    
    else 
        LOG(FATAL) << "Layer " << param.name() << " has unknown engine.";
        throw;  // Avoids missing return warning
    


REGISTER_LAYER_CREATOR(Input, GetInputLayer);

最后重新编译 caffe 会看到解决了这个问题,享受:)。

【讨论】:

【参考方案2】:

有同样的问题。以上接受的答案可能有效,但在某些情况下,它与 caffe 链接问题有关。我通过在 VS 项目属性中包含 caffe 构建目录来解决它。

Project -> Properties -> C/C++ -> General -> Additional Include Directories -> C:\Projects\caffe\build

【讨论】:

以上是关于Caffe 层创建失败 - 层类型未知的主要内容,如果未能解决你的问题,请参考以下文章

转 Caffe学习系列:数据层及参数

如何在Caffe中配置每一个层的结构

OpenCV 未知层类型运行暗网检测

[caffe]网络各层参数设置

[转] caffe视觉层Vision Layers 及参数

如何使用 caffe batch norm