Caffe Commons
- misc code
@
// Convert macro to string #define STRINGIFY(m) #m #define AS_STRING(m) STRINGIFY(m)
- misc code
- commons,
D:\tzx\git\caffe-rc3\src\caffe\common.cpp
@
class caffe { ... static Caffe& Get(); // Thread local context for Caffe. enum Brew { CPU, GPU }; inline static Brew mode() { return Get().mode_; } ... // This random number generator facade hides boost and CUDA rng // implementation from one another (for cross-platform compatibility). class RNG { public: RNG(); explicit RNG(unsigned int seed); explicit RNG(const RNG&); RNG& operator=(const RNG&); void* generator(); private: class Generator; shared_ptr<Generator> generator_; }; ... // Prints the current GPU status. static void DeviceQuery(); // Parallel training info inline static int solver_count() { return Get().solver_count_; } inline static void set_solver_count(int val) { Get().solver_count_ = val; } inline static bool root_solver() { return Get().root_solver_; } inline static void set_root_solver(bool val) { Get().root_solver_ = val; } protected: #ifndef CPU_ONLY cublasHandle_t cublas_handle_; curandGenerator_t curand_generator_; #endif shared_ptr<RNG> random_generator_; Brew mode_; int solver_count_; bool root_solver_; private: // The private constructor to avoid duplicate instantiation. Caffe(); DISABLE_COPY_AND_ASSIGN(Caffe); };
- commons,
Caffe Utils
- math_functions.cpp,
D:\tzx\git\caffe-rc3\src\caffe\util\math_functions.cpp
@
todo
- math_functions.cpp,
- io.cpp,
D:\tzx\git\caffe-rc3\src\caffe\util\io.cpp
@
bool ReadProtoFromTextFile(const char* filename, Message* proto) { int fd = open(filename, O_RDONLY); CHECK_NE(fd, -1) << "File not found: " << filename; FileInputStream* input = new FileInputStream(fd); bool success = google::protobuf::TextFormat::Parse(input, proto); delete input; close(fd); return success; }
#ifdef USE_OPENCV cv::Mat ReadImageToCVMat(const string& filename, const int height, const int width, const bool is_color) { cv::Mat cv_img; int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE); cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag); if (!cv_img_origin.data) { LOG(ERROR) << "Could not open or find file " << filename; return cv_img_origin; } if (height > 0 && width > 0) { cv::resize(cv_img_origin, cv_img, cv::Size(width, height)); } else { cv_img = cv_img_origin; } return cv_img; }
// Do the file extension and encoding match? static bool matchExt(const std::string & fn, std::string en) { size_t p = fn.rfind('.'); std::string ext = p != fn.npos ? fn.substr(p) : fn; std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); std::transform(en.begin(), en.end(), en.begin(), ::tolower); if ( ext == en ) return true; if ( en == "jpg" && ext == "jpeg" ) return true; return false; }
bool ReadImageToDatum(const string& filename, const int label, const int height, const int width, const bool is_color, const std::string & encoding, Datum* datum) { cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color); if (cv_img.data) { if (encoding.size()) { if ( (cv_img.channels() == 3) == is_color && !height && !width && matchExt(filename, encoding) ) return ReadFileToDatum(filename, label, datum); std::vector<uchar> buf; cv::imencode("."+encoding, cv_img, buf); datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]), buf.size())); datum->set_label(label); datum->set_encoded(true); return true; } CVMatToDatum(cv_img, datum); datum->set_label(label); return true; } else { return false; } }
bool ReadFileToDatum(const string& filename, const int label, Datum* datum) { std::streampos size; fstream file(filename.c_str(), ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); std::string buffer(size, ' '); file.seekg(0, ios::beg); file.read(&buffer[0], size); file.close(); datum->set_data(buffer); datum->set_label(label); datum->set_encoded(true); return true; } else { return false; } }
- io.cpp,
Caffe Layers
absval_layer.cpp
@
文档:Caffe: caffe::AbsValLayer
Class Template Reference 。Caffe: include/caffe/layers/absval_layer.hpp Source File
- header file:
include/caffe/layers/absval_layer.hpp
@
Caffe: include/caffe/layers/absval_layer.hpp Source File
#ifndef CAFFE_ABSVAL_LAYER_HPP_ #define CAFFE_ABSVAL_LAYER_HPP_ #include <vector> #include "caffe/blob.hpp" #include "caffe/layer.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/layers/neuron_layer.hpp" namespace caffe { template <typename Dtype> class AbsValLayer : public NeuronLayer<Dtype> { public: explicit AbsValLayer(const LayerParameter& param) : NeuronLayer<Dtype>(param) {} virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual inline const char* type() const { return "AbsVal"; } virtual inline int ExactNumBottomBlobs() const { return 1; } virtual inline int ExactNumTopBlobs() const { return 1; } protected: virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top); virtual void Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); virtual void Backward_gpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); }; } // namespace caffe #endif // CAFFE_ABSVAL_LAYER_HPP_
- source code:
src/caffe/layers/absval_layer.cpp
@
#include <vector> #include "caffe/layers/absval_layer.hpp" #include "caffe/util/math_functions.hpp" namespace caffe { template <typename Dtype> void AbsValLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { NeuronLayer<Dtype>::LayerSetUp(bottom, top); CHECK_NE(top[0], bottom[0]) << this->type() << " Layer does not " "allow in-place computation."; } template <typename Dtype> void AbsValLayer<Dtype>::Forward_cpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const int count = top[0]->count(); Dtype* top_data = top[0]->mutable_cpu_data(); caffe_abs(count, bottom[0]->cpu_data(), top_data); } template <typename Dtype> void AbsValLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { const int count = top[0]->count(); const Dtype* top_diff = top[0]->cpu_diff(); if (propagate_down[0]) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); caffe_cpu_sign(count, bottom_data, bottom_diff); caffe_mul(count, bottom_diff, top_diff, bottom_diff); } } #ifdef CPU_ONLY STUB_GPU(AbsValLayer); #endif INSTANTIATE_CLASS(AbsValLayer); REGISTER_LAYER_CLASS(AbsVal); } // namespace caffe
- header file: