caffe增加自己的layer实战(中)--caffe学习(11)
来源:互联网 发布:嵌入式无人机软件 编辑:程序博客网 时间:2024/06/05 09:20
接上篇: caffe增加自己的layer实战(上)–caffe学习(10)
实现完hpp头文件后就在cpp中实现具体的函数:
目录更改到cpp文件:/caffe/src/caffe/layers
新建文件,命名为:video_data_layers.cpp
1:首先包含头文件
#include <fstream>#include <iostream>#include <string>#include <utility>#include <vector>#include "caffe/data_layers.hpp"#include "caffe/layer.hpp"#include "caffe/proto/caffe.pb.h"#include "caffe/util/io.hpp"#include "caffe/util/math_functions.hpp"#include "caffe/util/rng.hpp"namespace caffe{template <typename Dtype>VideoDataLayer<Dtype>:: ~VideoDataLayer<Dtype>(){ this->JoinPrefetchThread();}
注意这里的#include "caffe/data_layers.hpp"
这里面包含的就是我们上篇中说的对VideoDataLayer的定义,如果你的定义不在这个文件里面,记得包含进来。
2:由于我们是data_layer,不像视觉层还需要实现forward和backword函数,那我们就需要搞清楚我们需要实现什么函数:
从C++原理来说,需要实现在上篇中的hpp中定义的所有函数:
template <typename Dtype>class VideoDataLayer : public BasePrefetchingDataLayer<Dtype> {public: explicit VideoDataLayer(const LayerParameter& param) : BasePrefetchingDataLayer<Dtype>(param) {} virtual ~VideoDataLayer(); virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual inline const char* type() const { return "VideoData"; } virtual inline int ExactNumBottomBlobs() const { return 0; } virtual inline int ExactNumTopBlobs() const { return 2; }protected: shared_ptr<Caffe::RNG> prefetch_rng_; shared_ptr<Caffe::RNG> prefetch_rng_2_; shared_ptr<Caffe::RNG> prefetch_rng_1_; shared_ptr<Caffe::RNG> frame_prefetch_rng_; virtual void ShuffleVideos(); virtual void InternalThreadEntry();#ifdef USE_MPI inline virtual void advance_cursor(){ lines_id_++; if (lines_id_ >= lines_.size()) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; lines_id_ = 0; if (this->layer_param_.video_data_param().shuffle()) { ShuffleVideos(); } } }#endif
同样,我们参考图片的cpp文件是怎么做的,找到image_data_layer.cpp
,目录在cpp文件:/caffe/src/caffe/layers
image_data_layer.cpp
文件内容:
#include <opencv2/core/core.hpp>#include <fstream> // NOLINT(readability/streams)#include <iostream> // NOLINT(readability/streams)#include <string>#include <utility>#include <vector>#include "caffe/data_layers.hpp"#include "caffe/layer.hpp"#include "caffe/util/benchmark.hpp"#include "caffe/util/io.hpp"#include "caffe/util/math_functions.hpp"#include "caffe/util/rng.hpp"namespace caffe {template <typename Dtype>ImageDataLayer<Dtype>::~ImageDataLayer<Dtype>() { this->JoinPrefetchThread();}template <typename Dtype>void ImageDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const int new_height = this->layer_param_.image_data_param().new_height(); const int new_width = this->layer_param_.image_data_param().new_width(); const bool is_color = this->layer_param_.image_data_param().is_color(); string root_folder = this->layer_param_.image_data_param().root_folder(); CHECK((new_height == 0 && new_width == 0) || (new_height > 0 && new_width > 0)) << "Current implementation requires " "new_height and new_width to be set at the same time."; // Read the file with filenames and labels const string& source = this->layer_param_.image_data_param().source(); LOG(INFO) << "Opening file " << source; std::ifstream infile(source.c_str()); string filename; int label; while (infile >> filename >> label) { lines_.push_back(std::make_pair(filename, label)); } if (this->layer_param_.image_data_param().shuffle()) { // randomly shuffle data LOG(INFO) << "Shuffling data"; const unsigned int prefetch_rng_seed = caffe_rng_rand(); prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed)); ShuffleImages(); } LOG(INFO) << "A total of " << lines_.size() << " images."; lines_id_ = 0; // Check if we would need to randomly skip a few data points if (this->layer_param_.image_data_param().rand_skip()) { unsigned int skip = caffe_rng_rand() % this->layer_param_.image_data_param().rand_skip(); LOG(INFO) << "Skipping first " << skip << " data points."; CHECK_GT(lines_.size(), skip) << "Not enough points to skip"; lines_id_ = skip; } // Read an image, and use it to initialize the top blob. cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first, new_height, new_width, is_color); // Use data_transformer to infer the expected blob shape from a cv_image. vector<int> top_shape = this->data_transformer_->InferBlobShape(cv_img); this->transformed_data_.Reshape(top_shape); // Reshape prefetch_data and top[0] according to the batch_size. const int batch_size = this->layer_param_.image_data_param().batch_size(); top_shape[0] = batch_size; this->prefetch_data_.Reshape(top_shape); top[0]->ReshapeLike(this->prefetch_data_); LOG(INFO) << "output data size: " << top[0]->num() << "," << top[0]->channels() << "," << top[0]->height() << "," << top[0]->width(); // label vector<int> label_shape(1, batch_size); top[1]->Reshape(label_shape); this->prefetch_label_.Reshape(label_shape);}template <typename Dtype>void ImageDataLayer<Dtype>::ShuffleImages() { caffe::rng_t* prefetch_rng = static_cast<caffe::rng_t*>(prefetch_rng_->generator()); shuffle(lines_.begin(), lines_.end(), prefetch_rng);}// This function is used to create a thread that prefetches the data.template <typename Dtype>void ImageDataLayer<Dtype>::InternalThreadEntry() { CPUTimer batch_timer; batch_timer.Start(); double read_time = 0; double trans_time = 0; CPUTimer timer; CHECK(this->prefetch_data_.count()); CHECK(this->transformed_data_.count()); ImageDataParameter image_data_param = this->layer_param_.image_data_param(); const int batch_size = image_data_param.batch_size(); const int new_height = image_data_param.new_height(); const int new_width = image_data_param.new_width(); const bool is_color = image_data_param.is_color(); string root_folder = image_data_param.root_folder(); // Reshape according to the first image of each batch // on single input batches allows for inputs of varying dimension. cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first, new_height, new_width, is_color); // Use data_transformer to infer the expected blob shape from a cv_img. vector<int> top_shape = this->data_transformer_->InferBlobShape(cv_img); this->transformed_data_.Reshape(top_shape); // Reshape prefetch_data according to the batch_size. top_shape[0] = batch_size; this->prefetch_data_.Reshape(top_shape); Dtype* prefetch_data = this->prefetch_data_.mutable_cpu_data(); Dtype* prefetch_label = this->prefetch_label_.mutable_cpu_data(); // datum scales const int lines_size = lines_.size(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob timer.Start(); CHECK_GT(lines_size, lines_id_); cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first, new_height, new_width, is_color); CHECK(cv_img.data) << "Could not load " << lines_[lines_id_].first; read_time += timer.MicroSeconds(); timer.Start(); // Apply transformations (mirror, crop...) to the image int offset = this->prefetch_data_.offset(item_id); this->transformed_data_.set_cpu_data(prefetch_data + offset); this->data_transformer_->Transform(cv_img, &(this->transformed_data_)); trans_time += timer.MicroSeconds(); prefetch_label[item_id] = lines_[lines_id_].second; // go to the next iter lines_id_++; if (lines_id_ >= lines_size) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; lines_id_ = 0; if (this->layer_param_.image_data_param().shuffle()) { ShuffleImages(); } } } batch_timer.Stop(); DLOG(INFO) << "Prefetch batch: " << batch_timer.MilliSeconds() << " ms."; DLOG(INFO) << " Read time: " << read_time / 1000 << " ms."; DLOG(INFO) << "Transform time: " << trans_time / 1000 << " ms.";}INSTANTIATE_CLASS(ImageDataLayer);REGISTER_LAYER_CLASS(ImageData);} // namespace caffe
可见总共实现了三个函数:
template <typename Dtype>void ImageDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
template <typename Dtype>void ImageDataLayer<Dtype>::ShuffleImages() { caffe::rng_t* prefetch_rng = static_cast<caffe::rng_t*>(prefetch_rng_->generator()); shuffle(lines_.begin(), lines_.end(), prefetch_rng);}
// This function is used to create a thread that prefetches the data.template <typename Dtype>void ImageDataLayer<Dtype>::InternalThreadEntry() {
其实也就是他自己头文件里面的定义
他只实现了其中的三个函数: DataLayerSetUp,ShuffleImages,InternalThreadEntry
我们同样也只需要实现 DataLayerSetUp,ShuffleImages,InternalThreadEntry
这三个函数,现在将 image_data_layers.cpp
文件内容全部复制粘贴到video_data_layers.cpp
但是对应的函数名字ImageDataLayer改为VideoDataLayer。
接下来就修改完善video_data_layers.cpp
来实现最后的全部功能,同时还有一些小细节需要完善,见下篇博客:
caffe增加自己的layer实战(下)–caffe学习(12)
- caffe增加自己的layer实战(中)--caffe学习(11)
- caffe增加自己的layer实战(上)--caffe学习(10)
- caffe增加自己的layer实战(下)--caffe学习(12)
- caffe增加自己的layer实战(下-续1)--caffe学习(13)
- caffe中增加自己的layer
- caffe增加自己的layer教程集合
- Caffe中增加新的layer以及Caffe中triplet loss layer的实现
- 如何在caffe中增加layer以及caffe中triplet loss layer的实现
- 如何在caffe中增加layer以及caffe中triplet loss layer的实现
- 如何在caffe中增加layer以及caffe中triplet loss layer的实现
- 如何在caffe中增加layer以及caffe中triplet loss layer的实现
- Caffe源码解读(十三):caffe.proto中Layer参数与源代码中Layer的相互关系
- caffe学习(2)------用caffe训练自己的图片
- caffe源码学习(四) layer
- caffe源码学习(五) data layer
- 学习Caffe(四)Loss Layer解析
- Caffe:Layer的相关学习
- 深度学习Caffe实战笔记(6)Windows caffe平台用Siamese网络跑自己的数据
- 用split()根据选项卡在url后面添加锚值
- 用service启动nginx
- 配置根据时间自动配置build
- java kettle 初步
- 文档解析之XML文档读取
- caffe增加自己的layer实战(中)--caffe学习(11)
- Linux平台的SVN服务器的配置及搭建
- LeetCode 1 Two Sum
- Android 系列 5.1使用自定义字体
- window.history
- 蓝桥杯-字母图形
- 好玩的数据集
- ubuntu 14.04 阿里云软件源
- Cocoa Pods使用记录