py-faster-rcnn中demo.py代码与C++版本的代码对比: part02 初始化, 创建Net

这里”C++版本的代码”是指: https://github.com/galian123/cpp_faster_rcnn_detect .

py-faster-rcnn中demo.py代码, 是指 https://github.com/rbgirshick/py-faster-rcnn/blob/master/tools/demo.py 以及
https://github.com/rbgirshick/py-faster-rcnn/tree/master/lib 目录下的一些代码.

涉及到的.py文件都是 https://github.com/rbgirshick/py-faster-rcnn/ 中的.

★ 初始化对比

♦ python代码

demo.py

cfg.TEST.HAS_RPN = True  # Use RPN for proposals
caffe.set_mode_gpu()
caffe.set_device(args.gpu_id)
cfg.GPU_ID = args.gpu_id

caffe包的初始化文件 py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py
这个文件的部分内容如下:

from ._caffe import init_log, log, set_mode_cpu, set_mode_gpu, set_device, Layer, get_solver, layer_type_list, set_random_seed, solver_count, set_solver_count, solver_rank, set_solver_rank, set_multiprocess, has_nccl
from ._caffe import __version__

set_mode_cpu, set_mode_gpu等是从_caffe中导入的, 而_caffe是由C++代码实现的.
_caffe 就是在py-faster-rcnn/caffe-fast-rcnn目录中执行make pycaffe生成的_caffe.so.

caffe.set_mode_gpu()调用的是_caffe.so (_caffe.cpp)中的set_mode_gpu()

void set_mode_gpu() { Caffe::set_mode(Caffe::GPU); }

caffe.set_device(args.gpu_id) 调用的是:Caffe::SetDevice()

♦ C++代码

    Caffe::SetDevice(gpuid);
    Caffe::set_mode(Caffe::GPU);

★ 创建 Net

♦ python代码

demo.py

    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
  • caffe.Net 到底是什么?
    从caffe的帮助信息中, 可以看到caffe包的初始化文件 py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py,
    这个文件的部分内容如下:
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
from .proto.caffe_pb2 import TRAIN, TEST

其中, Net 是通过from .pycaffe import Net导入到caffe包中的.
Net是从pycaffe模块来的, 看看pycaffe.py的代码:
py-faster-rcnn/caffe-fast-rcnn/python/caffe/pycaffe.py

from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
        RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer

可见, Net是从_caffe引入的. _caffe的源代码是py-faster-rcnn/caffe-fast-rcnn/python/caffe/_caffe.cpp.
通过boost.python声明了Net类:

typedef float Dtype;

  bp::class_<Net<Dtype>, shared_ptr<Net<Dtype> >, boost::noncopyable >("Net", bp::no_init)
    .def("__init__", bp::make_constructor(&Net_Init_Load))

所以, caffe.Net(prototxt, caffemodel, caffe.TEST)将会创建Net实例, 执行构造函数Net_Init_Load (C++代码):

shared_ptr<Net<Dtype> > Net_Init_Load(string param_file, string pretrained_param_file, int phase) {
  shared_ptr<Net<Dtype> > net(new Net<Dtype>(param_file, static_cast<Phase>(phase)));
  net->CopyTrainedLayersFrom(pretrained_param_file);
   return net;
}
  • 查看_caffe 模块的帮助信息:
>>> import caffe
>>> from caffe import _caffe
>>> help(_caffe)
>>> 

Help on module caffe._caffe in caffe:
NAME
    caffe._caffe
FILE
    /home/wanglc/git/py-faster-rcnn/caffe-fast-rcnn/python/caffe/_caffe.so

♦ C++代码

src/main/faster_rcnn_detect.cpp

    Detector det = Detector(model_file, weights_file,
        class_num, max_size, scale_size, conf_thresh, nms_thresh);

src/util/faster_rcnn.cpp

Detector::Detector(const string& model_file, const string& weights_file,
    int class_num, int max_size, int scale_size, float conf_thresh, float nms_thresh) {

    net_ = boost::shared_ptr<Net<float> >(new Net<float>(model_file, caffe::TEST));
    net_->CopyTrainedLayersFrom(weights_file);

    CLASS_NUM = class_num;
    MAX_SIZE = max_size;
    SCALE_SIZE = scale_size;
    CONF_THRESH = conf_thresh;
    NMS_THRESH = nms_thresh;

    cout << "Detector init success!" << endl;
}

————– 分割线 ————–
本系列文章如下:

猜你喜欢

转载自blog.csdn.net/u013553529/article/details/79029240