Skip to content

Commit

Permalink
Merge pull request BVLC#12 from jessebrizzi/DEV-26376
Browse files Browse the repository at this point in the history
DEV-26376: Recode python layer to C++ in detection net
  • Loading branch information
jessebrizzi authored Aug 31, 2017
2 parents 129272b + 8d7cdb7 commit 3dd04cc
Show file tree
Hide file tree
Showing 19 changed files with 1,317 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PYTHON_LIB := $(shell python-config --prefix)/lib
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/cuda/include/thrust/system/cuda/detail/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
Expand Down
16 changes: 9 additions & 7 deletions Makefile.config.ec2.gpu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ USE_CUDNN := 1
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

Expand All @@ -32,12 +32,14 @@ CUDA_DIR := /usr/local/cuda

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
Expand Down Expand Up @@ -84,10 +86,10 @@ PYTHON_LIB := $(shell python-config --prefix)/lib
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/cuda/include/thrust/system/cuda/detail/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
Expand Down
2 changes: 1 addition & 1 deletion Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PYTHON_LIB := /usr/lib
# WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/cuda/include/thrust/system/cuda/detail/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
Expand Down
2 changes: 1 addition & 1 deletion Makefile.config.local.gpu
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ PYTHON_LIB := $(shell python-config --prefix)/lib
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/cuda/include/thrust/system/cuda/detail/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This fork of caffe was created off of the [1.0](https://github.com/BVLC/caffe/re
- [smooth_L1_loss_ohem_layer](https://github.com/curalate/caffe/pull/2)
- [smooth_l1_loss_layer](https://github.com/curalate/caffe/pull/2)
- [softmax_loss_ohem_layer](https://github.com/curalate/caffe/pull/2)
- [frcnn_proposal_layer](https://github.com/curalate/caffe/pull/12)

[![License](https://img.shields.io/badge/license-BSD-blue.svg)](LICENSE)

Expand Down
84 changes: 84 additions & 0 deletions include/caffe/layers/frcnn_proposal_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// ------------------------------------------------------------------
// Xuanyi . Refer to Dong Jian
// 2016/03/31
// ------------------------------------------------------------------
#ifndef CAFFE_FRCNN_PROPOSAL_LAYER_HPP_
#define CAFFE_FRCNN_PROPOSAL_LAYER_HPP_

#include <vector>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"

namespace caffe {

/*************************************************
FrcnnProposalLayer
Outputs object detection proposals by applying estimated bounding-box
transformations to a set of regular boxes (called "anchors").
bottom: 'rpn_cls_prob_reshape'
bottom: 'rpn_bbox_pred'
bottom: 'im_info'
top: 'rpn_rois'
**************************************************/
template <typename Dtype>
class FrcnnProposalLayer : public Layer<Dtype> {
public:
explicit FrcnnProposalLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top){};

virtual inline const char* type() const { return "FrcnnProposal"; }

virtual inline int MinBottomBlobs() const { return 3; }
virtual inline int MaxBottomBlobs() const { return 3; }
virtual inline int MinTopBlobs() const { return 1; }
virtual inline int MaxTopBlobs() const { return 2; }

#ifndef CPU_ONLY
virtual ~FrcnnProposalLayer() {
if (this->anchors_) {
CUDA_CHECK(cudaFree(this->anchors_));
}
if (this->transform_bbox_) {
CUDA_CHECK(cudaFree(this->transform_bbox_));
}
if (this->mask_) {
CUDA_CHECK(cudaFree(this->mask_));
}
if (this->selected_flags_) {
CUDA_CHECK(cudaFree(this->selected_flags_));
}
if (this->gpu_keep_indices_) {
CUDA_CHECK(cudaFree(this->gpu_keep_indices_));
}
}
#endif

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);
#ifndef CPU_ONLY
// CUDA CU
float* anchors_;
float* transform_bbox_;
unsigned long long *mask_;
int *selected_flags_;
int *gpu_keep_indices_;
#endif
};

} // namespace caffe

#endif // CAFFE_FRCNN_PROPOSAL_LAYER_HPP_
10 changes: 10 additions & 0 deletions include/caffe/util/frcnn_gpu_nms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CAFFE_FRCNN_GPU_NMS_HPP_
#define CAFFE_FRCNN_GPU_NMS_HPP_

namespace caffe {

void gpu_nms(int* keep_out, int* num_out, const float* boxes_dev, int boxes_num,
int boxes_dim, float nms_overlap_thresh, int device_id=-1);

} // namespace caffe
#endif // CAFFE_FRCNN_UTILS_HPP_
28 changes: 28 additions & 0 deletions include/caffe/util/frcnn_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ------------------------------------------------------------------
// Xuanyi . Refer to Dong Jian
// 2016/04/01
// ------------------------------------------------------------------
#ifndef CAFFE_FRCNN_HELPER_HPP_
#define CAFFE_FRCNN_HELPER_HPP_

#include "caffe/util/frcnn_utils.hpp"

namespace caffe {

template <typename Dtype>
Point4f<Dtype> bbox_transform(const Point4f<Dtype>& ex_rois,const Point4f<Dtype>& gt_rois);

template <typename Dtype>
std::vector<Point4f<Dtype> > bbox_transform(const std::vector<Point4f<Dtype> >& ex_rois,
const std::vector<Point4f<Dtype> >& gt_rois);

template <typename Dtype>
Point4f<Dtype> bbox_transform_inv(const Point4f<Dtype>& box, const Point4f<Dtype>& delta);

template <typename Dtype>
std::vector<Point4f<Dtype> > bbox_transform_inv(const Point4f<Dtype>& box,
const std::vector<Point4f<Dtype> >& deltas);

} // namespace caffe

#endif
40 changes: 40 additions & 0 deletions include/caffe/util/frcnn_param.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ------------------------------------------------------------------
// Xuanyi . Refer to Dong Jian
// 2016/03/31
// ------------------------------------------------------------------
#ifndef CAFFE_FRCNN_PRARM_HPP_
#define CAFFE_FRCNN_PRARM_HPP_

#include <vector>
#include <string>

namespace caffe{

class FrcnnParam {
public:

static float rpn_nms_thresh;
static int rpn_pre_nms_top_n;
static int rpn_post_nms_top_n;
// Proposal height and width both need to be greater than RPN_MIN_SIZE (at
// orig image scale)
static float rpn_min_size;

static float test_rpn_nms_thresh;
static int test_rpn_pre_nms_top_n;
static int test_rpn_post_nms_top_n;
// Proposal height and width both need to be greater than RPN_MIN_SIZE (at
// orig image scale)
static float test_rpn_min_size;

static int feat_stride;
static std::vector<float> anchors;
static int n_classes;
// ========================================
static void load_param(const std::string default_config_path);
static void print_param();
};

}

#endif // CAFFE_FRCNN_PRARM_HPP_
Loading

0 comments on commit 3dd04cc

Please sign in to comment.