caffe windows10 ssd配置

主要都是根据这篇文章配置的http://blog.csdn.net/muwu5635/article/details/60874721

说一下我碰到的问题。

文章里说到:复制data\VOC0712create_data.batcaffe根目录下,修改如下root_dir,但是貌似没有给出具体怎么写。

本人有点懒,直接将代码贴出来

@Echo off
Echo caffe create_annoset Batch
:: https://github.com/conner99/caffe/blob/ssd-microsoft/tools/convert_annoset.cpp
:: You can modify the parameters in create_data.bat if needed.
:: It will create lmdb files for trainval and test with encoded original image:
::  - D:\caffe-ssd-microsoft\data\VOC0712\trainval_lmdb
::  - D:\caffe-ssd-microsoft\data\VOC0712\test_lmdb


set root_dir=D:\caffe-ssd-microsoft
cd %root_dir%


set redo=1
set data_root_dir=data\VOC0712
set mapfile=%data_root_dir%\labelmap_voc.prototxt
set anno_type=detection
set db=lmdb
set min_dim=0
set max_dim=0
set width=0
set height=0


set "extra_cmd=--encode-type=jpg --encoded"


if %redo%==1 (
set "extra_cmd=%extra_cmd% --redo"
)


for %%s in (trainval test) do (
echo Creating %%s lmdb...


python %root_dir%\scripts\create_annoset.py ^
--anno-type=%anno_type% ^
--label-map-file=%mapfile% ^
--min-dim=%min_dim% ^
--max-dim=%max_dim% ^
--resize-width=%width% ^
--resize-height=%height% ^
--check-label %extra_cmd% ^
%data_root_dir% ^
%data_root_dir%\%%s.txt ^
%data_root_dir%\%%s_%db%
)
pause

可能碰到的情况 :缺少protobuf,缺少_caffe什么什么的

缺少protobuf:

1.下载protobuf(地址:https://github.com/google/protobuf/releases),下载两个版本,一个protoc-3.3.0-win32.zip,一个Source code (zip)

2.protoc-3.0.0-win32\bin\protoc.exe 拷贝进入Source code 文件夹下 src中

 3、进入Source code 文件夹下Python文件夹,cmd执行 python setup.py build, python setup.py install,如果出现ImportError: No module named setuptools,解决方案(http://blog.sina.com.cn/s/blog_3fe961ae0100zgav.html)

缺少_caffe:是因为ssd在原版caffe的基础上增加了一些东西,所以之前用原版caffe生成的pycaffe少了必要的文件。在caffe-ssd-microsoft下生成pycaffe,再把pycaffe文件夹下的caffe文件夹复制到C:\ProgramData\Anaconda2\Lib\site-packages下就OK了。注意caffe-windows-microsoft默认是关闭了python接口的,你要在D:\caffe-ssd-microsoft\windows的CommonSettings.props里把python的接口改成true,地址也改一下。


最后用于显示的ssd_detect.cpp  原文章里只有图片,并且缺了一点,这里帮大家改好了,代码如下:

// This is a demo code for using a SSD model to do detection.
// The code is modified from examples/cpp_classification/classification.cpp.
// Usage:
//    ssd_detect [FLAGS] model_file weights_file list_file
//
// where model_file is the .prototxt file defining the network architecture, and
// weights_file is the .caffemodel file containing the network parameters, and
// list_file contains a list of image files with the format as follows:
//    folder/img1.JPEG
//    folder/img2.JPEG
// list_file can also contain a list of video files with the format as follows:
//    folder/video1.mp4
//    folder/video2.mp4
//
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
#include <algorithm>
#include <iomanip>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>


#ifdef USE_OPENCV
using namespace caffe;  // NOLINT(build/namespaces)


class Detector {
 public:
  Detector(const string& model_file,
           const string& weights_file,
           const string& mean_file,
           const string& mean_value);


  std::vector<vector<float> > Detect(const cv::Mat& img);


 private:
  void SetMean(const string& mean_file, const string& mean_value);


  void WrapInputLayer(std::vector<cv::Mat>* input_channels);


  void Preprocess(const cv::Mat& img,
                  std::vector<cv::Mat>* input_channels);


 private:
  shared_ptr<Net<float> > net_;
  cv::Size input_geometry_;
  int num_channels_;
  cv::Mat mean_;
};


Detector::Detector(const string& model_file,
                   const string& weights_file,
                   const string& mean_file,
                   const string& mean_value) {
#ifdef CPU_ONLY
  Caffe::set_mode(Caffe::CPU);
#else
  Caffe::set_mode(Caffe::GPU);
#endif


  /* Load the network. */
  net_.reset(new Net<float>(model_file, TEST));
  net_->CopyTrainedLayersFrom(weights_file);


  CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
  CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";


  Blob<float>* input_layer = net_->input_blobs()[0];
  num_channels_ = input_layer->channels();
  CHECK(num_channels_ == 3 || num_channels_ == 1)
    << "Input layer should have 1 or 3 channels.";
  input_geometry_ = cv::Size(input_layer->width(), input_layer->height());


  /* Load the binaryproto mean file. */
  SetMean(mean_file, mean_value);
}


std::vector<vector<float> > Detector::Detect(const cv::Mat& img) {
  Blob<float>* input_layer = net_->input_blobs()[0];
  input_layer->Reshape(1, num_channels_,
                       input_geometry_.height, input_geometry_.width);
  /* Forward dimension change to all layers. */
  net_->Reshape();


  std::vector<cv::Mat> input_channels;
  WrapInputLayer(&input_channels);


  Preprocess(img, &input_channels);


  net_->Forward();


  /* Copy the output layer to a std::vector */
  Blob<float>* result_blob = net_->output_blobs()[0];
  const float* result = result_blob->cpu_data();
  const int num_det = result_blob->height();
  vector<vector<float> > detections;
  for (int k = 0; k < num_det; ++k) {
    if (result[0] == -1) {
      // Skip invalid detection.
      result += 7;
      continue;
    }
    vector<float> detection(result, result + 7);
    detections.push_back(detection);
    result += 7;
  }
  return detections;
}


/* Load the mean file in binaryproto format. */
void Detector::SetMean(const string& mean_file, const string& mean_value) {
  cv::Scalar channel_mean;
  if (!mean_file.empty()) {
    CHECK(mean_value.empty()) <<
      "Cannot specify mean_file and mean_value at the same time";
    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. */
    channel_mean = cv::mean(mean);
    mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
  }
  if (!mean_value.empty()) {
    CHECK(mean_file.empty()) <<
      "Cannot specify mean_file and mean_value at the same time";
    stringstream ss(mean_value);
    vector<float> values;
    string item;
    while (getline(ss, item, ',')) {
      float value = std::atof(item.c_str());
      values.push_back(value);
    }
    CHECK(values.size() == 1 || values.size() == num_channels_) <<
      "Specify either 1 mean_value or as many as channels: " << num_channels_;


    std::vector<cv::Mat> channels;
    for (int i = 0; i < num_channels_; ++i) {
      /* Extract an individual channel. */
      cv::Mat channel(input_geometry_.height, input_geometry_.width, CV_32FC1,
          cv::Scalar(values[i]));
      channels.push_back(channel);
    }
    cv::merge(channels, mean_);
  }
}


/* Wrap the input layer of the network in separate cv::Mat objects
 * (one per channel). This way we save one memcpy operation and we
 * don't need to rely on cudaMemcpy2D. The last preprocessing
 * operation will write the separate channels directly to the input
 * layer. */
void Detector::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
  Blob<float>* input_layer = net_->input_blobs()[0];


  int width = input_layer->width();
  int height = input_layer->height();
  float* input_data = input_layer->mutable_cpu_data();
  for (int i = 0; i < input_layer->channels(); ++i) {
    cv::Mat channel(height, width, CV_32FC1, input_data);
    input_channels->push_back(channel);
    input_data += width * height;
  }
}


void Detector::Preprocess(const cv::Mat& img,
                            std::vector<cv::Mat>* input_channels) {
  /* Convert the input image to the input image format of the network. */
  cv::Mat sample;
  if (img.channels() == 3 && num_channels_ == 1)
    cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
  else if (img.channels() == 4 && num_channels_ == 1)
    cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
  else if (img.channels() == 4 && num_channels_ == 3)
    cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
  else if (img.channels() == 1 && num_channels_ == 3)
    cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
  else
    sample = img;


  cv::Mat sample_resized;
  if (sample.size() != input_geometry_)
    cv::resize(sample, sample_resized, input_geometry_);
  else
    sample_resized = sample;


  cv::Mat sample_float;
  if (num_channels_ == 3)
    sample_resized.convertTo(sample_float, CV_32FC3);
  else
    sample_resized.convertTo(sample_float, CV_32FC1);


  cv::Mat sample_normalized;
  cv::subtract(sample_float, mean_, sample_normalized);


  /* This operation will write the separate BGR planes directly to the
   * input layer of the network because it is wrapped by the cv::Mat
   * objects in input_channels. */
  cv::split(sample_normalized, *input_channels);


  CHECK(reinterpret_cast<float*>(input_channels->at(0).data)
        == net_->input_blobs()[0]->cpu_data())
    << "Input channels are not wrapping the input layer of the network.";
}


DEFINE_string(mean_file, "",
    "The mean file used to subtract from the input image.");
DEFINE_string(mean_value, "104,117,123",
    "If specified, can be one value or can be same as image channels"
    " - would subtract from the corresponding channel). Separated by ','."
    "Either mean_file or mean_value should be provided, not both.");
DEFINE_string(file_type, "image",
    "The file type in the list_file. Currently support image and video.");
DEFINE_string(out_file, "",
    "If provided, store the detection results in the out_file.");
DEFINE_double(confidence_threshold, 0.01,
    "Only store detections with score higher than the threshold.");


int main(int argc, char** argv) {
  ::google::InitGoogleLogging(argv[0]);
  // Print output to stderr (while still logging)
  FLAGS_alsologtostderr = 1;


#ifndef GFLAGS_GFLAGS_H_
  namespace gflags = google;
#endif


  gflags::SetUsageMessage("Do detection using SSD mode.\n"
        "Usage:\n"
        "    ssd_detect [FLAGS] model_file weights_file list_file\n");
  gflags::ParseCommandLineFlags(&argc, &argv, true);


  if (argc < 4) {
    gflags::ShowUsageWithFlagsRestrict(argv[0], "examples/ssd/ssd_detect");
    return 1;
  }
  char *labelname[] = { "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" };
  const string& model_file = argv[1];
  const string& weights_file = argv[2];
  const string& mean_file = FLAGS_mean_file;
  const string& mean_value = FLAGS_mean_value;
  const string& file_type = FLAGS_file_type;
  const string& out_file = FLAGS_out_file;
  const float confidence_threshold = FLAGS_confidence_threshold;


  // Initialize the network.
  Detector detector(model_file, weights_file, mean_file, mean_value);


  // Set the output mode.
  std::streambuf* buf = std::cout.rdbuf();
  std::ofstream outfile;
  if (!out_file.empty()) {
    outfile.open(out_file.c_str());
    if (outfile.good()) {
      buf = outfile.rdbuf();
    }
  }
  std::ostream out(buf);


  // Process image one by one.
  std::ifstream infile(argv[3]);
  std::string file;
  while (infile >> file) {
 if (file_type == "image") {
 cv::Mat img = cv::imread(file, -1);
 CHECK(!img.empty()) << "Unable to decode image " << file;
 std::vector<vector<float> > detections = detector.Detect(img);


 /* Print the detection results. */
 for (int i = 0; i < detections.size(); ++i) {
 const vector<float>& d = detections[i];
 // Detection format: [image_id, label, score, xmin, ymin, xmax, ymax].
 CHECK_EQ(d.size(), 7);
 const float score = d[2];
 if (score >= confidence_threshold) {
 out << file << " ";
 out << static_cast<int>(d[1]) << " ";
 out << score << " ";
 out << static_cast<int>(d[3] * img.cols) << " ";
 out << static_cast<int>(d[4] * img.rows) << " ";
 out << static_cast<int>(d[5] * img.cols) << " ";
 out << static_cast<int>(d[6] * img.rows) << std::endl;
 int posx = static_cast<int>(d[3] * img.cols);
 int posy = static_cast<int>(d[4] * img.rows);
 int posw = static_cast<int>(d[5] * img.cols) - posx;
 int posh = static_cast<int>(d[5] * img.rows) - posy;
 cv::Rect pos(posx, posy, posw, posh);
 cv::rectangle(img, pos, cv::Scalar(0, static_cast<int>(d[1]) / 21.0 * 255, 255));
 std::string words = std::string(labelname[static_cast<int>(d[1])]);
 cv::putText(img, words, cv::Point(posx, posy), CV_FONT_HERSHEY_COMPLEX, 0.4, cv::Scalar(0, static_cast<int>(d[1]) / 21.0 * 255, 255));
 }
 }
 cv::imshow("SSD", img);
 std::string save_name = file;
 save_name = save_name.substr(0, save_name.find_last_of('.'));
 save_name = save_name + "_SSD_detect.jpg";
 std::cout << save_name << std::endl;
 cv::imwrite(save_name, img);
 cv::waitKey(0);
 }
     else if (file_type == "video") {
      cv::VideoCapture cap(file);
      if (!cap.isOpened()) {
        LOG(FATAL) << "Failed to open video: " << file;
      }
      cv::Mat img;
      int frame_count = 0;
      while (true) {
        bool success = cap.read(img);
        if (!success) {
          LOG(INFO) << "Process " << frame_count << " frames from " << file;
          break;
        }
        CHECK(!img.empty()) << "Error when read frame";
        std::vector<vector<float> > detections = detector.Detect(img);


        /* Print the detection results. */
        for (int i = 0; i < detections.size(); ++i) {
          const vector<float>& d = detections[i];
          // Detection format: [image_id, label, score, xmin, ymin, xmax, ymax].
          CHECK_EQ(d.size(), 7);
          const float score = d[2];
          if (score >= confidence_threshold) {
            out << file << "_";
            out << std::setfill('0') << std::setw(6) << frame_count << " ";
            out << static_cast<int>(d[1]) << " ";
            out << score << " ";
            out << static_cast<int>(d[3] * img.cols) << " ";
            out << static_cast<int>(d[4] * img.rows) << " ";
            out << static_cast<int>(d[5] * img.cols) << " ";
            out << static_cast<int>(d[6] * img.rows) << std::endl;
          }
        }
        ++frame_count;
      }
      if (cap.isOpened()) {
        cap.release();
      }
    } else {
      LOG(FATAL) << "Unknown file_type: " << file_type;
    }
  }
  return 0;
}
#else
int main(int argc, char** argv) {
  LOG(FATAL) << "This example requires OpenCV; compile with USE_OPENCV.";


}
#endif  // USE_OPENCV

再编译一下就行了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值