Skip to content

Commit 5e008c8

Browse files
committed
Merge pull request #276 from Wangyida:cnn_3dobj
2 parents 8d2c132 + 44663a2 commit 5e008c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+139683
-1
lines changed

modules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $ cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -D BUILD_opencv_re
4747
19. **opencv_xfeatures2d**: Extra 2D Features Framework containing experimental and non-free 2D feature algorithms.
4848

4949
20. **opencv_ximgproc**: Extended Image Processing: Structured Forests / Domain Transform Filter / Guided Filter / Adaptive Manifold Filter / Joint Bilateral Filter / Superpixels.
50-
50+
5151
21. **opencv_xobjdetect**: Integral Channel Features Detector Framework.
5252

5353
22. **opencv_xphoto**: Additional photo processing algorithms: Color balance / Denoising / Inpainting.

modules/cnn_3dobj/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
2+
3+
find_package(Caffe)
4+
if(Caffe_FOUND)
5+
message(STATUS "Caffe: YES")
6+
set(HAVE_CAFFE 1)
7+
else()
8+
message(STATUS "Caffe: NO")
9+
endif()
10+
11+
find_package(Protobuf)
12+
if(Protobuf_FOUND)
13+
message(STATUS "Protobuf: YES")
14+
set(HAVE_PROTOBUF 1)
15+
else()
16+
message(STATUS "Protobuf: NO")
17+
endif()
18+
19+
find_package(Glog)
20+
if(Glog_FOUND)
21+
message(STATUS "Glog: YES")
22+
set(HAVE_GLOG 1)
23+
else()
24+
message(STATUS "Glog: NO")
25+
endif()
26+
27+
if(HAVE_CAFFE)
28+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cnn_3dobj_config.hpp.in
29+
${CMAKE_CURRENT_SOURCE_DIR}/include/opencv2/cnn_3dobj_config.hpp @ONLY)
30+
31+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
32+
33+
if(${Caffe_FOUND})
34+
include_directories(${Caffe_INCLUDE_DIR})
35+
endif()
36+
set(the_description "CNN for 3D object recognition and pose estimation including a completed Sphere View on 3D objects")
37+
ocv_define_module(cnn_3dobj opencv_core opencv_imgproc opencv_viz opencv_highgui OPTIONAL WRAP python)
38+
39+
if(${Caffe_FOUND})
40+
target_link_libraries(opencv_cnn_3dobj ${Caffe_LIBS} ${Glog_LIBS} ${Protobuf_LIBS})
41+
endif()
42+
endif()

modules/cnn_3dobj/FindCaffe.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Caffe package for CNN Triplet training
2+
unset(Caffe_FOUND)
3+
4+
find_path(Caffe_INCLUDE_DIR NAMES caffe/caffe.hpp caffe/common.hpp caffe/net.hpp caffe/proto/caffe.pb.h caffe/util/io.hpp caffe/vision_layers.hpp
5+
HINTS
6+
/usr/local/include)
7+
8+
find_library(Caffe_LIBS NAMES caffe
9+
HINTS
10+
/usr/local/lib)
11+
12+
if(Caffe_LIBS AND Caffe_INCLUDE_DIR)
13+
set(Caffe_FOUND 1)
14+
endif()

modules/cnn_3dobj/FindGlog.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Glog package for CNN Triplet training
2+
unset(Glog_FOUND)
3+
4+
find_library(Glog_LIBS NAMES glog
5+
HINTS
6+
/usr/local/lib)
7+
8+
if(Glog_LIBS)
9+
set(Glog_FOUND 1)
10+
endif()

modules/cnn_3dobj/FindProtobuf.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Protobuf package for CNN Triplet training
2+
unset(Protobuf_FOUND)
3+
4+
find_library(Protobuf_LIBS NAMES protobuf
5+
HINTS
6+
/usr/local/lib)
7+
8+
if(Protobuf_LIBS)
9+
set(Protobuf_FOUND 1)
10+
endif()

modules/cnn_3dobj/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#Convolutional Neural Network for 3D object classification and pose estimation.
2+
===========================================================
3+
#Module Description on cnn_3dobj:
4+
####This learning structure construction and feature extraction concept is based on Convolutional Neural Network, the main reference paper could be found at:
5+
<https://cvarlab.icg.tugraz.at/pubs/wohlhart_cvpr15.pdf>.
6+
####The author provided Codes on Theano on:
7+
<https://cvarlab.icg.tugraz.at/projects/3d_object_detection/>.
8+
####I implemented the training and feature extraction codes mainly based on CAFFE project(<http://caffe.berkeleyvision.org/>) which will be compiled as libcaffe for the cnn_3dobj OpenCV module, codes are mainly concentrating on triplet and pair-wise jointed loss layer, the training data arrangement is also important which basic training information.
9+
####Codes about my triplet version of caffe are released on Github:
10+
<https://github.com/Wangyida/caffe/tree/cnn_triplet>.
11+
####You can git it through:
12+
```
13+
$ git clone https://github.com/Wangyida/caffe/tree/cnn_triplet.
14+
```
15+
===========================================================
16+
#Module Building Process:
17+
####Prerequisite for this module: protobuf and caffe, for the libcaffe installation, you can install it on standard system path for the aim of being able to be linked by this OpenCV module when compiling and function using. Using: -D CMAKE_INSTALL_PREFIX=/usr/local as an building option when you cmake, the building process on Caffe on system could be like this:
18+
```
19+
$ cd <caffe_source_directory>
20+
$ mkdir biuld
21+
$ cd build
22+
$ cmake -D CMAKE_INSTALL_PREFIX=/usr/local ..
23+
$ make all -j4
24+
$ sudo make install
25+
```
26+
####After all these steps, the headers and libs of CAFFE will be set on /usr/local/ path, and when you compiling opencv with opencv_contrib modules as below, the protobuf and caffe will be recognized as already installed while building. Protobuf is needed.
27+
28+
#Compiling OpenCV
29+
```
30+
$ cd <opencv_source_directory>
31+
$ mkdir build
32+
$ cd build
33+
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_VTK=ON -D INSTALL_TESTS=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
34+
$ make -j4
35+
$ sudo make install
36+
```
37+
##Tips on compiling problems:
38+
####If you encouter the no declaration errors when you 'make', it might becaused that you have installed the older version of cnn_3dobj module and the header file changed in a newly released version of codes. This problem is the cmake and make can't detect the header should be updated and it keeps the older header remains in /usr/local/include/opencv2 whithout updating. This error could be solved by remove the installed older version of cnn_3dobj module by:
39+
```
40+
$ cd /
41+
$ cd usr/local/include/opencv2/
42+
$ sudo rm -rf cnn_3dobj.hpp
43+
```
44+
####And redo the compiling steps above again.
45+
===========================================================
46+
#Building samples
47+
```
48+
$ cd <opencv_contrib>/modules/cnn_3dobj/samples
49+
$ mkdir build
50+
$ cd build
51+
$ cmake ..
52+
$ make
53+
```
54+
===========================================================
55+
#Demos
56+
##Demo1: training data generation
57+
####Imagas generation from different pose, by default there are 4 models used, there will be 276 images in all which each class contains 69 iamges, if you want to use additional .ply models, it is necessary to change the class number parameter to the new class number and also give it a new class label. If you will train net work and extract feature from RGB images set the parameter rgb_use as 1.
58+
```
59+
$ ./sphereview_test -plymodel=../data/3Dmodel/ape.ply -label_class=0 -cam_head_x=0 -cam_head_y=0 -cam_head_z=1
60+
```
61+
####press 'Q' to start 2D image genaration
62+
```
63+
$ ./sphereview_test -plymodel=../data/3Dmodel/ant.ply -label_class=1 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0
64+
```
65+
```
66+
$ ./sphereview_test -plymodel=../data/3Dmodel/cow.ply -label_class=2 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0
67+
```
68+
```
69+
$ ./sphereview_test -plymodel=../data/3Dmodel/plane.ply -label_class=3 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0
70+
```
71+
```
72+
$ ./sphereview_test -plymodel=../data/3Dmodel/bunny.ply -label_class=4 -cam_head_x=0 -cam_head_y=-1 -cam_head_z=0
73+
```
74+
```
75+
$ ./sphereview_test -plymodel=../data/3Dmodel/horse.ply -label_class=5 -cam_head_x=0 -cam_head_y=0 -cam_head_z=-1
76+
```
77+
####When all images are created in images_all folder as a collection of training images for network tranining and as a gallery of reference images for the classification part, then proceed on.
78+
####After this demo, the binary files of images and labels will be stored as 'binary_image' and 'binary_label' in current path, you should copy them into the leveldb folder in Caffe triplet training, for example: copy these 2 files in <caffe_source_directory>/data/linemod and rename them as 'binary_image_train', 'binary_image_test' and 'binary_label_train', 'binary_label_train'. Here I use the same as trianing and testing data, you can use different data for training and testing the performance in the CAFFE training process. It's important to observe the loss of testing data to check whether training data is suitable for the your aim. Loss should be obseved as keep decreasing and remain on a much smaller number than the initial loss.
79+
####You could start triplet tranining using Caffe like this:
80+
```
81+
$ cd
82+
$ cd <caffe_source_directory>
83+
$ ./examples/triplet/create_3d_triplet.sh
84+
$ ./examples/triplet/train_3d_triplet.sh
85+
```
86+
####After doing this, you will get .caffemodel files as the trained parameter of net work. I have already provide the net definition .prototxt files and the pretrained .caffemodel in <opencv_contrib>/modules/cnn_3dobj/testdata/cv folder, you could just use them without training in caffe.
87+
===========================================================
88+
##Demo2: feature extraction and classification
89+
```
90+
$ cd
91+
$ cd <opencv_contrib>/modules/cnn_3dobj/samples/build
92+
```
93+
####Classifier, this will extracting the feature of a single image and compare it with features of gallery samples for prediction. This demo uses a set of images for feature extraction in a given path, these features will be a reference for prediction on target image. The caffe model and network prototxt file is attached in <opencv_contrib>/modules/cnn_3dobj/testdata/cv. Just run:
94+
```
95+
$ ./classify_test
96+
```
97+
####if the classification and pose estimation issue need to extract mean got from all training images, you can run this:
98+
```
99+
$ ./classify_test -mean_file=../data/images_mean/triplet_mean.binaryproto
100+
```
101+
===========================================================
102+
##Demo3: model performance test
103+
####This demo will have a test on the performance of trained CNN model on several images. If the the model fail on telling different samples from seperate classes or confused on samples with similar pose but from different classes, it will give some information on the model analysis.
104+
```
105+
$ ./model_test
106+
```
107+
===========================================================
108+
#Test
109+
####If you want to have a test on cnn_3dobj module, the path of test data must be set in advance:
110+
```
111+
$ export OPENCV_TEST_DATA_PATH=<opencv_contrib>/modules/cnn_3dobj/testdata
112+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ifndef __OPENCV_CNN_3DOBJ_CONFIG_HPP__
2+
#define __OPENCV_CNN_3DOBJ_CONFIG_HPP__
3+
// HAVE CAFFE
4+
#cmakedefine HAVE_CAFFE
5+
#endif

modules/cnn_3dobj/doc/cnn_3dobj.bib

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@Article{hinterstoisser2008panter,
2+
author = {Hinterstoisser, S. and Benhimane, S. and and Lepetit, V. and Navab, N.},
3+
title = {Simultaneous Recognition and Homography Extraction of Local Patches with a Simple Linear Classifier},
4+
booktitle = {BMVC British Machine Vision Conference 2008},
5+
year = {2008}
6+
}
7+
8+
@inproceedings{wohlhart15,
9+
author = {Paul Wohlhart and Vincent Lepetit},
10+
title = {Learning Descriptors for Object Recognition and 3D Pose Estimation},
11+
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
12+
year = {2015}
13+
}

0 commit comments

Comments
 (0)