将binaryproto文件加载到caffe库中

时间:2017-02-24 06:07:00

标签: c++ caffe

我有一个binaryproto文件并尝试加载到caffe库中。 加载二进制文件的整个API如下所示。

/* Load the mean file in binaryproto format. */
void ExtractFeature::SetMean(const string& mean_file) {
      BlobProto blob_proto;
      ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);

      /* Convert from BlobProto to Blob<float> */
      Blob<float> mean_blob;
      mean_blob.FromProto(blob_proto);
      CHECK_EQ(mean_blob.channels(), num_channels_)
        << "Number of channels of mean file doesn't match input layer.";

      /* The format of the mean file is planar 32-bit float BGR or grayscale. */
      std::vector<cv::Mat> channels;
      float* data = mean_blob.mutable_cpu_data();
      for (int i = 0; i < num_channels_; ++i) {
        /* Extract an individual channel. */
        cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
        channels.push_back(channel);
        data += mean_blob.height() * mean_blob.width();
      }

      /* Merge the separate channels into a single image. */
      cv::Mat mean;
      cv::merge(channels, mean);

      /* Compute the global mean pixel value and create a mean image
       * filled with this value. */
      cv::Scalar channel_mean = cv::mean(mean);
      mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}

mean_blob.FromProto(blob_proto);

我总是有错误
/* Convert from BlobProto to Blob<float> */
      Blob<float> mean_blob;
      mean_blob.FromProto(blob_proto);

我提供了函数调用的正确文件路径。

错误如下。

F0224 14:04:49.170449  5005 blob.cpp:115] Check failed: data_ 
*** Check failure stack trace: ***
    @     0x7fb25d11cdcd  google::LogMessage::Fail()
    @     0x7fb25d11ed08  google::LogMessage::SendToLog()
    @     0x7fb25d11c963  google::LogMessage::Flush()
    @     0x7fb25d11f63e  google::LogMessageFatal::~LogMessageFatal()
    @     0x7fb25d6cddb0  caffe::Blob<>::mutable_cpu_data()
    @     0x7fb25d6ce4aa  caffe::Blob<>::FromProto()
    @           0x40848f  ExtractFeature::SetMean()
    @           0x40836a  ExtractFeature::ExtractFeature()
    @           0x40ca7c  main
    @     0x7fb25c865f45  __libc_start_main
    @           0x407f19  (unknown)
Aborted (core dumped)
Program received signal SIGABRT, Aborted.
0x00007ffff5a20c37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

可能有什么不对?没有此类文件或目录错误,但文件存在且提供了正确的路径。

编辑: 在ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto)阅读文件后,我打印blob_proto at gdb并找到

(gdb) p blob_proto
$2 = {<google::protobuf::Message> = {<google::protobuf::MessageLite> = {
      _vptr.MessageLite = 0x7ffff6daad50 <vtable for caffe::BlobProto+16>}, <No data fields>}, static kShapeFieldNumber = 7, static kDataFieldNumber = 5, 
  static kDiffFieldNumber = 6, static kDoubleDataFieldNumber = 8, 
  static kDoubleDiffFieldNumber = 9, static kNumFieldNumber = 1, 
  static kChannelsFieldNumber = 2, static kHeightFieldNumber = 3, 
  static kWidthFieldNumber = 4, 
  _internal_metadata_ = {<google::protobuf::internal::InternalMetadataWithArenaBase<google::protobuf::UnknownFieldSet, google::protobuf::internal::InternalMetadataWithArena>> = {ptr_ = 0x0, static kPtrTagMask = <optimized out>, 
      static kPtrValueMask = <optimized out>}, <No data fields>}, 
  _has_bits_ = {has_bits_ = {28}}, _cached_size_ = 0, data_ = {
    static kInitialSize = <optimized out>, current_size_ = 921600, 
    total_size_ = 1048576, static kRepHeaderSize = 8, rep_ = 0x633a390}, 
  _data_cached_byte_size_ = 4238813, diff_ = {
    static kInitialSize = <optimized out>, current_size_ = 0, total_size_ = 0, 
    static kRepHeaderSize = 8, rep_ = 0x0}, 
  _diff_cached_byte_size_ = 78777232, double_data_ = {
    static kInitialSize = <optimized out>, current_size_ = 0, total_size_ = 0, 
    static kRepHeaderSize = 8, rep_ = 0x0}, 
  _double_data_cached_byte_size_ = 0, double_diff_ = {
    static kInitialSize = <optimized out>, current_size_ = 0, total_size_ = 0, 
    static kRepHeaderSize = 8, rep_ = 0x0}, 
---Type <return> to continue, or q <return> to quit---
  _double_diff_cached_byte_size_ = -9848, shape_ = 0x0, num_ = 0, 
  channels_ = 480, height_ = 640, width_ = 3}

这意味着blob_proto的图像格式错误(实际图像有通道3,宽度640和高度480)。

这可能是因为两个原因 (1)保存mean.binaryproto文件格式错误。 我保存mean.binaryproto文件的程序是

def create_protobinary(avg_img):
    #avg_img is your numpy array with the average data 
    blob = caffe.proto.caffe_pb2.BlobProto();
    blob.channels, blob.height, blob.width = avg_img.shape;
    blob.data.extend(avg_img.astype(float).flat);
    binaryproto_file = open('mean.binaryproto', 'wb' );
    binaryproto_file.write(blob.SerializeToString());
    binaryproto_file.close();

(2)或ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto); 将文件读入blob_proto有问题。

我该如何解决这个问题?

0 个答案:

没有答案