如何自动生成caffe脚本 c
发布网友
发布时间:2022-04-09 16:02
我来回答
共1个回答
热心网友
时间:2022-04-09 17:31
想调用模型简单办看examples/cpp_classification面cpp文件教何调用caffe获取类结...(没接触caffe建议直接按照文件操作能比较简单面我代码我知道没接触caffe看起难度)
代码我看着太习惯所前自稍微写简易版本知道传附件懒直接代码贴
先简单解释何使用代码复制文件放examples面自创建文件夹面写main函数调用类比:
复制保存caffe/examples/myproject/net_operator.hpp同目录写main.cppmain函数面#include "net_operator.hpp"使用类:
const string net_prototxt = "..."; // 网络prototxt文件用绝路径面同理
const string pre_trained_file = "..."; // 训练.caffemodel文件
const string img_path = "..."; // 要测试图片路径
// 创建NetOperator象
NetOperator net_operator(net_prototxt, pre_trained_file);
Blob *blob = net_operator.processImage(img_path);
// blob层输结至于blob面存放数据需要看看官网定义
写完main.cppcaffe目录make编译写文件应执行文件比按我面写makecaffe/build/examples/myproject文件夹面main.bin执行文件执行文件并直接代码目录所前面我建议写路径用绝路径
另外要获取层输需要修改processImage函数返值通NetOperator员变量net_获取需要blob比blob名称"label"想获取blob通net_->blob_by_name("label")获取获取shared_ptr<Blob >类型搜boost shared_ptr知道跟普通指针同
接贴代码:
#include caffe.hpp>
#include core/core.hpp>
#include highgui/highgui.hpp>
#include imgproc/imgproc.hpp>
#include
#include
#include
#include
#include
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
class NetOperator
{
public:
NetOperator(const string& net_prototxt);
NetOperator(const string& net_prototxt, const string& trained_file);
~NetOperator() { }
int batch_size() { return batch_size_; }
Blob* processImage(const string &img_path, bool is_color = true);
Blob* processImages(const vector &img_paths, bool is_color = true);
private:
void createNet(const string& net_prototxt);
// read the image and store it in the idx position of images in the blob
void readImageToBlob(const string &img_path, int idx = 0, bool is_color = true);
shared_ptr<Net > net_;
cv::Size input_geometry_;
int batch_size_;
int num_channels_;
Blob* input_blob_;
TransformationParameter transform_param_;
shared_ptr<DataTransformer > data_transformer_;
Blob transformed_data_;
};
NetOperator::NetOperator(const string& net_prototxt) {
createNet(net_prototxt);
}
NetOperator::NetOperator(const string& net_prototxt, const string& trained_file) {
createNet(net_prototxt);
net_->CopyTrainedLayersFrom(trained_file);
}
void NetOperator::createNet(const string& net_prototxt) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
net_.reset(new Net(net_prototxt, TEST));
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* input_layer = net_->input_blobs()[0];
batch_size_ = input_layer->num();
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());
// reshape the output shape of the DataTransformer
vector top_shape(4);
top_shape[0] = 1;
top_shape[1] = num_channels_;
top_shape[2] = input_geometry_.height;
top_shape[3] = input_geometry_.width;
this->transformed_data_.Reshape(top_shape);
}
Blob* NetOperator::processImage(const string &img_path, bool is_color) {
// reshape the net for the input
input_blob_ = net_->input_blobs()[0];
input_blob_->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
readImageToBlob(img_path, 0, is_color);
net_->ForwardPrefilled();
return net_->output_blobs()[0];
}
Blob* NetOperator::processImages(const vector &img_paths, bool is_color) {
int img_num = img_paths.size();
// reshape the net for the input
input_blob_ = net_->input_blobs()[0];
input_blob_->Reshape(img_num, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
for (int i=0; i<img_num; i++) {
readImageToBlob(img_paths[i], i, is_color);
}
net_->ForwardPrefilled();
return net_->output_blobs()[0];
}
void NetOperator::readImageToBlob(const string &img_path, int idx, bool is_color) {
// read the image and resize to the target size
cv::Mat img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(img_path, cv_read_flag);
if (!cv_img_origin.data) {
LOG(ERROR) << "Could not open or find file " << img_path;
return ;
}
if (input_geometry_.height > 0 && input_geometry_.width > 0) {
cv::resize(cv_img_origin, img, input_geometry_);
} else {
img = cv_img_origin;
}
// transform the image to a blob using DataTransformer
// create a DataTransformer using default TransformationParameter (no transformation)
data_transformer_.reset(
new DataTransformer(transform_param_, TEST));
data_transformer_->InitRand();
// set the output of DataTransformer to the idx image of the input blob
int offset = input_blob_->offset(idx);
this->transformed_data_.set_cpu_data(input_blob_->mutable_cpu_data() + offset);
// transform the input image
data_transformer_->Transform(img, &(this->transformed_data_));
}