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

时间:2017-03-24 07:35:22

标签: c++ deep-learning caffe

我在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)

我的原型文件:

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 } }
}

但不是固定的。 请帮帮我。

2 个答案:

答案 0 :(得分:0)

我最后通过一些更改解决了这个问题,并为这样的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并将看到解决这个问题享受:)。

答案 1 :(得分:0)

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

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