├── LICENSE ├── README.md ├── opencl_caffe ├── CMakeLists.txt ├── include │ └── opencl_caffe │ │ ├── detector.h │ │ ├── detector_gpu.h │ │ ├── nodelet.h │ │ └── srv.h ├── mainpage.dox ├── nodelet_plugins.xml ├── package.xml ├── resources │ ├── cat.jpg │ └── voc.txt ├── rosdoc.yaml ├── src │ ├── detector_gpu.cpp │ ├── nodelet.cpp │ └── srv.cpp └── tests │ ├── detector.test │ ├── service.test │ ├── unittest_detector.cpp │ └── unittest_srv.cpp ├── opencl_caffe_launch ├── CMakeLists.txt ├── launch │ ├── includes │ │ ├── default.yaml │ │ ├── manager.launch │ │ ├── nodelet.launch │ │ ├── service.launch │ │ └── viewer.launch │ ├── kinect_viewer.launch │ ├── opencl_caffe_nodelet.launch │ ├── opencl_caffe_srv.launch │ ├── realsense_viewer.launch │ └── usb_cam_viewer.launch └── package.xml └── opencl_caffe_viewer ├── CMakeLists.txt ├── package.xml └── src └── opencl_caffe_viewer.cpp /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2017 Intel Corporation 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DISCONTINUATION OF PROJECT 2 | 3 | This project will no longer be maintained by Intel. 4 | 5 | Intel has ceased development and contributions including, but not limited to, maintenance, bug fixes, new releases, or updates, to this project. 6 | 7 | Intel no longer accepts patches to this project. 8 | 9 | If you have an ongoing need to use this project, are interested in independently developing it, or would like to maintain patches for the open source software community, please create your own fork of this project. 10 | 11 | Contact: webadmin@linux.intel.com 12 | # ros_opencl_caffe 13 | # Warning: This repo is deprecated. For latest ROS wrapper for Intel GPU, please refer to our project ros_openvino_toolkit. 14 | # See: https://github.com/intel/ros_openvino_toolkit!! 15 | 16 | ## Introduction 17 | OpenCL Caffe([clCaffe](https://github.com/01org/caffe/tree/inference-optimize)) is an OpenCL backend for Caffe from Intel®. With inference optimized by Intel® OpenCL, clCaffe can be used in most scene in high performance, like objects inference. 18 | 19 | This project is a ROS wrapper for OpenCL Caffe, providing following features: 20 | * A ROS service for objects inference in a ROS image message 21 | * A ROS publisher for objects inference in a ROS image message from a RGB camera 22 | * Demo applications to show the capablilities of ROS service and publisher 23 | 24 | ## Prerequisite 25 | * An x86_64 computer with Ubuntu 16.04 26 | * ROS Kinetic 27 | * RGB camera, e.g. Intel® RealSense™, Microsoft® Kinect™ or standard USB camera 28 | 29 | ## Environment Setup 30 | * Install ROS Kinetic Desktop-Full ([guide](http://wiki.ros.org/kinetic/Installation/Ubuntu)) 31 | * Create a catkin workspace ([guide](http://wiki.ros.org/catkin/Tutorials/create_a_workspace)) 32 | * Install clCaffe ([guide](https://github.com/01org/caffe/wiki/clCaffe)) 33 | * Create a symbol link in `/opt/clCaffe` 34 | ```Shell 35 | sudo ln -s /opt/clCaffe 36 | ``` 37 | * Add clCaffe libraries to LD_LIBRARY_PATH. 38 | ```Shell 39 | export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/clCaffe/build/lib 40 | ``` 41 | * Install ROS package for different cameras as needed. e.g. 42 | 1. Standard USB camera 43 | ```Shell 44 | sudo apt-get install ros-kinetic-usb-cam 45 | ``` 46 | 2. Intel® RealSense™ camera 47 | - Install Intel® RealSense™ SDK 2.0 ([guide](https://github.com/IntelRealSense/librealsense/blob/master/doc/distribution_linux.md)). Refer [here](https://github.com/IntelRealSense/librealsense) for more details about Intel® RealSense™ SDK 2.0. 48 | - Install Intel® RealSense™ ROS ([guide](https://github.com/intel-ros/realsense)) 49 | ```Shell 50 | cd ~/catkin_ws/src 51 | git clone https://github.com/intel-ros/realsense.git 52 | cd realsense 53 | git checkout 2.0.0 54 | cd ~/catkin_ws 55 | catkin_make 56 | ``` 57 | 3. Microsoft® Kinect™ camera 58 | ```Shell 59 | sudo apt-get install ros-kinetic-openni-launch 60 | ``` 61 | 62 | ## Building and Installation 63 | ```Shell 64 |  cd ~/catkin_ws/src 65 | git clone https://github.com/intel/object_msgs 66 | git clone https://github.com/intel/ros_opencl_caffe 67 | cd ~/catkin_ws/ 68 | catkin_make 69 | catkin_make install 70 | source install/setup.bash 71 | ``` 72 | Copy object label file to clCaffe installation location 73 | ```Shell 74 | cp ~/catkin_ws/src/ros_opencl_caffe/opencl_caffe/resources/voc.txt /opt/clCaffe/data/yolo/ 75 | ``` 76 | 77 | ## Running the Demo 78 | ### Inference 79 | 1. Standard USB camera 80 | ```Shell 81 | roslaunch opencl_caffe_launch usb_cam_viewer.launch 82 | ``` 83 | 2. Intel® RealSense™ camera 84 | ```Shell 85 | roslaunch opencl_caffe_launch realsense_viewer.launch 86 | ``` 87 | 3. Microsoft® Kinect™ camera 88 | ```Shell 89 | roslaunch opencl_caffe_launch kinect_viewer.launch 90 | ``` 91 | 92 | ### Service 93 | ```Shell 94 | roslaunch opencl_caffe_launch opencl_caffe_srv.launch 95 | ``` 96 | 97 | ## Test 98 | Use `rostest` for tests 99 | ```Shell 100 | source devel/setup.bash 101 | rostest opencl_caffe service.test 102 | rostest opencl_caffe detector.test 103 | ``` 104 | 105 | ## Known Issues 106 | * Only image messages supported in service demo 107 | * Only test on RealSense D400 series camera, Microsoft Kinect v1 camera and Microsoft HD-300 USB camera 108 | 109 | ## TODO 110 | * Support latest clCaffe 111 | 112 | ###### *Any security issue should be reported using process at https://01.org/security* 113 | -------------------------------------------------------------------------------- /opencl_caffe/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2017 Intel Corporation 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required(VERSION 2.8.3) 16 | project(opencl_caffe) 17 | 18 | find_package(catkin REQUIRED COMPONENTS 19 | roslib 20 | roscpp 21 | sensor_msgs 22 | std_msgs 23 | object_msgs 24 | cv_bridge 25 | roslint 26 | pluginlib 27 | nodelet 28 | ) 29 | find_package(OpenCV REQUIRED) 30 | 31 | #Ignore sign compare warnings in clCaffe 32 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare") 33 | 34 | # Flags 35 | if(UNIX OR APPLE) 36 | # Linker flags. 37 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel") 38 | # GCC specific flags. ICC is compatible with them. 39 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -z noexecstack -z relro -z now") 40 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack -z relro -z now") 41 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 42 | # In Clang, -z flags are not compatible, they need to be passed to linker via -Wl. 43 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 44 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 45 | endif() 46 | 47 | # Compiler flags. 48 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") 49 | # GCC specific flags. 50 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9) 51 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector-strong") 52 | else() 53 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 54 | endif() 55 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 56 | # Clang is compatbile with some of the flags. 57 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 58 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" ) 59 | # Same as above, with exception that ICC compilation crashes with -fPIE option, even 60 | # though it uses -pie linker option that require -fPIE during compilation. Checksec 61 | # shows that it generates correct PIE anyway if only -pie is provided. 62 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector") 63 | endif() 64 | 65 | # Generic flags. 66 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -fno-operator-names -Wformat -Wformat-security -Wall") 67 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 68 | # Dot not forward c++11 flag to GPU beucause it is not supported 69 | set( CUDA_PROPAGATE_HOST_FLAGS OFF ) 70 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2") 71 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") 72 | endif() 73 | 74 | catkin_package( 75 | INCLUDE_DIRS include 76 | LIBRARIES opencl_caffe 77 | CATKIN_DEPENDS roslib roscpp sensor_msgs std_msgs object_msgs 78 | ) 79 | 80 | include_directories( 81 | include 82 | ${catkin_INCLUDE_DIRS} 83 | /opt/clCaffe/include 84 | /opt/clCaffe/build/include 85 | ) 86 | 87 | link_directories( 88 | /opt/clCaffe/build/lib 89 | ) 90 | 91 | add_executable(opencl_caffe_srv 92 | src/srv.cpp 93 | src/detector_gpu.cpp 94 | ) 95 | 96 | add_library(opencl_caffe_nodelet 97 | src/nodelet.cpp 98 | src/detector_gpu.cpp 99 | ) 100 | 101 | add_dependencies(opencl_caffe_srv ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 102 | add_dependencies(opencl_caffe_nodelet ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 103 | 104 | target_link_libraries(opencl_caffe_srv 105 | ${catkin_LIBRARIES} 106 | ${OpenCV_LIBS} 107 | caffe 108 | glog 109 | ) 110 | 111 | target_link_libraries(opencl_caffe_nodelet 112 | ${catkin_LIBRARIES} 113 | ${OpenCV_LIBS} 114 | caffe 115 | ) 116 | 117 | install(TARGETS opencl_caffe_srv opencl_caffe_nodelet 118 | ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 119 | LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 120 | RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 121 | ) 122 | 123 | install(DIRECTORY resources 124 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} 125 | ) 126 | 127 | install(FILES nodelet_plugins.xml 128 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} 129 | ) 130 | 131 | if(CATKIN_ENABLE_TESTING) 132 | # static code check 133 | find_package(roslint REQUIRED) 134 | roslint_cpp() 135 | roslint_add_test() 136 | 137 | # run_tests 138 | # catkin_add_gtest(unittest_detector tests/unittest_detector.cpp) 139 | # target_link_libraries(unittest_detector ${catkin_LIBRARIES} opencl_caffe_nodelet pthread ${GTEST_LIBRARIES} ${UNITEST_LIBRARIES}) 140 | # catkin_add_gtest(unittest_srv tests/unittest_srv.cpp) 141 | # target_link_libraries(unittest_srv ${catkin_LIBRARIES} opencl_caffe_srv pthread ${GTEST_LIBRARIES} ${UNITEST_LIBRARIES}) 142 | 143 | # rostest 144 | find_package(rostest REQUIRED) 145 | add_executable(unittest_detector tests/unittest_detector.cpp) 146 | target_link_libraries(unittest_detector ${catkin_LIBRARIES} opencl_caffe_nodelet pthread ${GTEST_LIBRARIES} ${UNITEST_LIBRARIES}) 147 | add_executable(unittest_srv tests/unittest_srv.cpp) 148 | target_link_libraries(unittest_srv ${catkin_LIBRARIES} pthread ${GTEST_LIBRARIES} ${UNITEST_LIBRARIES}) 149 | add_rostest(tests/detector.test) 150 | add_rostest(tests/service.test) 151 | endif() 152 | -------------------------------------------------------------------------------- /opencl_caffe/include/opencl_caffe/detector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef OPENCL_CAFFE_DETECTOR_H 18 | #define OPENCL_CAFFE_DETECTOR_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | namespace opencl_caffe 25 | { 26 | /** @class Detector 27 | * @brief Base class for detecting. 28 | * This class define a common interface of nueral network inference. 29 | * 1. Load all resources need by a network 30 | * 2. Run inference 31 | */ 32 | class Detector 33 | { 34 | public: 35 | /** Default destructor */ 36 | virtual ~Detector() = default; 37 | /** 38 | * Load resources from file, construct a caffe Net object. 39 | * 40 | * @param[in] net_cfg Network configuration file path 41 | * @param[in] weights Neural network weights file path 42 | * @param[in] labels File path of labels of network output classes 43 | * @return Status of load resources, true for success or false for failed 44 | */ 45 | virtual int loadResources(const std::string& net_cfg, const std::string& weights, const std::string& labels) = 0; 46 | /** 47 | * Public interface of running inference to infer all objects in image. 48 | * 49 | * @param[in] image_msg image message subscribed from camera 50 | * @param[out] objects objects inferred 51 | * @return Status of run inference, true for success or false for failed 52 | */ 53 | virtual int runInference(const sensor_msgs::ImagePtr image_msg, object_msgs::ObjectsInBoxes& objects) = 0; 54 | }; 55 | } // namespace opencl_caffe 56 | 57 | #endif // OPENCL_CAFFE_DETECTOR_H 58 | -------------------------------------------------------------------------------- /opencl_caffe/include/opencl_caffe/detector_gpu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef OPENCL_CAFFE_DETECTOR_GPU_H 18 | #define OPENCL_CAFFE_DETECTOR_GPU_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "opencl_caffe/detector.h" 28 | 29 | namespace opencl_caffe 30 | { 31 | /** @class DetectorGpu 32 | * @brief A implamentation of GPU detecting. 33 | * This class implament a caffe GPU object inference with 16 bit and 32 bit float point. 34 | * 1. Load resources need by caffe network 35 | * 2. Initialize the network (template for fp16 and fp32) 36 | * 3. Pre-process input image 37 | * 4. Infer the objects in image 38 | */ 39 | class DetectorGpu : public Detector 40 | { 41 | private: 42 | /** 16 bit caffe network */ 43 | std::shared_ptr> net_fp16_; 44 | /** 32 bit caffe network */ 45 | std::shared_ptr> net_fp32_; 46 | /** 16 bit input blob pointors of each channel */ 47 | std::vector input_channels_fp16_; 48 | /** 32 bit input blob pointors of each channel */ 49 | std::vector input_channels_fp32_; 50 | /** Origin input width and height */ 51 | cv::Size input_size_; 52 | /** Input width and height after resize */ 53 | cv::Size input_size_resized_; 54 | /** Number of input channels */ 55 | int num_channels_; 56 | /** The list of label names of object classes */ 57 | std::vector labels_list; 58 | /** Whether the GPU support 16 bit float point */ 59 | bool is_fp16_support; 60 | 61 | private: 62 | /** 63 | * Initialize the caffe network, template function is for fp16 and fp32. 64 | * 65 | * @param[in, out] net Caffe network 66 | * @param[in, out] input_channels input blob pointors of each channel 67 | * @param[in] net_cfg Network configuration file path 68 | * @param[in] weights Neural network weights file path 69 | * @param[in] labels File path of labels of network output classes 70 | * @return Status of init network, true for success or false for failed 71 | */ 72 | template 73 | int initNetwork(std::shared_ptr>& net, std::vector& input_channels, 74 | const std::string& net_cfg, const std::string& weights, const std::string& labels); 75 | /** 76 | * Initialize the input blobs for clCaffe 77 | * 78 | * @param[in] image cv::Mat image input 79 | * @param[in, out] input_channels input blob pointors of each channel 80 | * @return No returns 81 | */ 82 | template 83 | void initInputBlob(const cv::Mat& image, Dtype& input_channels_); 84 | /** 85 | * Resize the image first 86 | * 87 | * @param[in] image cv::Mat image input 88 | * @return Resized image with cv::Mat format 89 | */ 90 | cv::Mat resizeImage(const cv::Mat& image); 91 | /** 92 | * Run inference to infer all objects in image. 93 | * 94 | * @param[in, out] net Caffe network 95 | * @param[in, out] input_channels Input blob pointors of each channel 96 | * @param[in] image_msg Image messages as input 97 | * @param[out] objects Objects inferred as output 98 | * @return Status of inference, true for success or false for failed 99 | */ 100 | template 101 | int inference(std::shared_ptr>& net, std::vector& input_channels, 102 | const sensor_msgs::ImagePtr image_msg, object_msgs::ObjectsInBoxes& objects); 103 | 104 | public: 105 | /** Constructor, intialize default value of is_fp16_support */ 106 | DetectorGpu(); 107 | /** Deafult deconstructor */ 108 | ~DetectorGpu() = default; 109 | /** 110 | * Load resources from file, construct a caffe Net object. 111 | * 112 | * @param[in] net_cfg Network configuration file path 113 | * @param[in] weights Neural network weights file path 114 | * @param[in] labels File path of labels of network output classes 115 | * @return Status of load resources, true for success or false for failed 116 | */ 117 | int loadResources(const std::string& net_cfg, const std::string& weights, const std::string& labels); 118 | /** 119 | * Public interface of running inference to infer all objects in image. 120 | * 121 | * @param[in] image_msg image message subscribed from camera 122 | * @param[out] objects objects inferred 123 | * @return Status of run inference, true for success or false for failed 124 | */ 125 | int runInference(const sensor_msgs::ImagePtr image_msg, object_msgs::ObjectsInBoxes& objects); 126 | }; 127 | } // namespace opencl_caffe 128 | 129 | #endif // OPENCL_CAFFE_DETECTOR_GPU_H 130 | -------------------------------------------------------------------------------- /opencl_caffe/include/opencl_caffe/nodelet.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef OPENCL_CAFFE_NODELET_H 18 | #define OPENCL_CAFFE_NODELET_H 19 | 20 | #include 21 | #include 22 | #include "opencl_caffe/detector.h" 23 | 24 | namespace opencl_caffe 25 | { 26 | /** @class Nodelet 27 | * @brief Nodelet of the detector 28 | * This class implement a nodelet for the detector. 29 | * 1. Load all resources need by the detector 30 | * 2. Subscribe the image messages. 31 | */ 32 | class Nodelet : public nodelet::Nodelet 33 | { 34 | private: 35 | /** Shared pointor of a detector */ 36 | std::shared_ptr detector_; 37 | /** Subscriber for subscribing the image messages*/ 38 | ros::Subscriber sub_; 39 | /** Publisher for publishing the objects information messages */ 40 | ros::Publisher pub_; 41 | 42 | public: 43 | /** Nodelet onInit function */ 44 | virtual void onInit(); 45 | /** 46 | * Callback method. Called when image message comes. 47 | * 48 | * @param[in] image_msg Image message 49 | */ 50 | void cbImage(const sensor_msgs::ImagePtr image_msg); 51 | /** 52 | * Load resources for detector. 53 | * 54 | * @param[in] net_config_path Network configuration file path 55 | * @param[in] weights_path Neural network weights file path 56 | * @param[in] labels_path File path of labels of network output classes 57 | */ 58 | void loadResources(const std::string net_config_path, const std::string weights_path, const std::string labels_path); 59 | }; 60 | } // namespace opencl_caffe 61 | 62 | #endif // OPENCL_CAFFE_NODELET_H 63 | -------------------------------------------------------------------------------- /opencl_caffe/include/opencl_caffe/srv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef OPENCL_CAFFE_SRV_H 18 | #define OPENCL_CAFFE_SRV_H 19 | 20 | #include 21 | #include 22 | #include "opencl_caffe/detector.h" 23 | 24 | namespace opencl_caffe 25 | { 26 | /** @class Srv 27 | * @brief Service of the detector 28 | * This class implement a service for the detector. 29 | * 1. Load all resources need by the detector 30 | * 2. Advertise a service 31 | */ 32 | class Srv 33 | { 34 | private: 35 | /** Service server for advertising service */ 36 | ros::ServiceServer service_; 37 | /** Shared pointor of a detector */ 38 | std::shared_ptr detector_; 39 | 40 | public: 41 | /** 42 | * Explicit constructor. 43 | * 44 | * @param[in] n Service node handle 45 | */ 46 | explicit Srv(ros::NodeHandle& n); 47 | /** 48 | * Callback method. 49 | * 50 | * @param[in] req Request of service 51 | * @param[out] resp Response of service 52 | */ 53 | bool handleService(object_msgs::DetectObject::Request& req, object_msgs::DetectObject::Response& resp); 54 | /** 55 | * Load resources for detector. 56 | * 57 | * @param[in] net_config_path Network configuration file path 58 | * @param[in] weights_path Neural network weights file path 59 | * @param[in] labels_path File path of labels of network output classes 60 | */ 61 | void loadResources(const std::string net_config_path, const std::string weights_path, const std::string labels_path); 62 | }; 63 | } // namespace opencl_caffe 64 | 65 | #endif // OPENCL_CAFFE_SRV_H 66 | -------------------------------------------------------------------------------- /opencl_caffe/mainpage.dox: -------------------------------------------------------------------------------- 1 | /** 2 | @mainpage 3 | @htmlinclude manifest.html 4 | 5 | 6 | @b %opencl_caffe is a ROS wrapper for clCaffe . It depends on: 7 | 8 | - clCaffe 9 | - ros_object_msgs 10 | 11 | @b %opencl_caffe includes: 12 | 13 | - A nodelet which subscribe image messages, and publish each object information inferred 14 | - A service which provide a interface of inferring a image 15 | 16 | @section codeapi Code API 17 | 18 | - Base class: opencl_caffe::Detector 19 | - GPU inference implementation: opencl_caffe::DetectorGpu 20 | - Nodelet: opencl_caffe::Nodelet 21 | - Service: opencl_caffe::Srv 22 | 23 | */ 24 | -------------------------------------------------------------------------------- /opencl_caffe/nodelet_plugins.xml: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | OpenCL Caffe ROS Publisher nodelet. 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /opencl_caffe/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | opencl_caffe 17 | 0.3.0 18 | The ros wrapper for OpenCL Caffe 19 | Xiaocheng Dong 20 | Xiaojun Huang 21 | 22 | Apache 2.0 23 | 24 | roslib 25 | roscpp 26 | sensor_msgs 27 | std_msgs 28 | object_msgs 29 | nodelet 30 | pluginlib 31 | rostest 32 | roslib 33 | roscpp 34 | sensor_msgs 35 | std_msgs 36 | object_msgs 37 | nodelet 38 | pluginlib 39 | rostest 40 | catkin 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /opencl_caffe/resources/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intel/ros_opencl_caffe/40215215e9b47254fa9fa0af27e4501dc299ba1f/opencl_caffe/resources/cat.jpg -------------------------------------------------------------------------------- /opencl_caffe/resources/voc.txt: -------------------------------------------------------------------------------- 1 | aeroplane 2 | bicycle 3 | bird 4 | boat 5 | bottle 6 | bus 7 | car 8 | cat 9 | chair 10 | cow 11 | diningtable 12 | dog 13 | horse 14 | motorbike 15 | person 16 | pottedplant 17 | sheep 18 | sofa 19 | train 20 | tvmonitor -------------------------------------------------------------------------------- /opencl_caffe/rosdoc.yaml: -------------------------------------------------------------------------------- 1 | - builder: doxygen 2 | name: C++ API 3 | output_dir: . 4 | file_patterns: '*.c *.cpp *.h *.cc *.hh *.dox' 5 | homepage: 'https://github.com/intel/ros_opencl_caffe' 6 | -------------------------------------------------------------------------------- /opencl_caffe/src/detector_gpu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "opencl_caffe/detector_gpu.h" 22 | 23 | namespace opencl_caffe 24 | { 25 | DetectorGpu::DetectorGpu() : is_fp16_support(false) 26 | { 27 | } 28 | cv::Mat DetectorGpu::resizeImage(const cv::Mat& image) 29 | { 30 | cv::Mat image_resized; 31 | if (image.size() == input_size_) 32 | { 33 | return image; 34 | } 35 | 36 | if (image.cols != image.rows) 37 | { 38 | if ((input_size_.width * 1.0 / image.cols) < (input_size_.height * 1.0 / image.rows)) 39 | { 40 | input_size_resized_.width = input_size_.width; 41 | input_size_resized_.height = (image.rows * input_size_.width) / image.cols; 42 | } 43 | else 44 | { 45 | input_size_resized_.height = input_size_.height; 46 | input_size_resized_.width = (image.cols * input_size_.height) / image.rows; 47 | } 48 | } 49 | cv::resize(image, image_resized, input_size_resized_); 50 | return image_resized; 51 | } 52 | 53 | template 54 | void DetectorGpu::initInputBlob(const cv::Mat& image, Dtype& input_channels_) 55 | { 56 | int dx = 0, dy = 0; 57 | 58 | if (input_size_resized_ != input_size_ && input_size_resized_.width != input_size_resized_.height) 59 | { 60 | dx = (input_size_.width - input_size_resized_.width) / 2; 61 | dy = (input_size_.height - input_size_resized_.height) / 2; 62 | } 63 | 64 | if (dx != 0 || dy != 0) 65 | { 66 | for (int i = 0; i < num_channels_; ++i) 67 | { 68 | for (int pos = 0; pos < input_size_.width * input_size_.height; ++pos) 69 | { 70 | input_channels_[i][pos] = 0.5; 71 | } 72 | } 73 | } 74 | 75 | cv::Mat image_converted; 76 | image.convertTo(image_converted, num_channels_ == 3 ? CV_32FC3 : CV_32FC1); 77 | 78 | image_converted = image_converted / 255.0; 79 | 80 | for (int i = 0; i < input_size_resized_.height; ++i) 81 | { 82 | for (int j = 0; j < input_size_resized_.width; ++j) 83 | { 84 | int pos = (i + dy) * input_size_.width + j + dx; 85 | if (num_channels_ == 3) 86 | { 87 | cv::Vec3f pixel = image_converted.at(i, j); 88 | input_channels_[0][pos] = pixel.val[2]; 89 | input_channels_[1][pos] = pixel.val[1]; 90 | input_channels_[2][pos] = pixel.val[0]; 91 | } 92 | else 93 | { 94 | cv::Scalar pixel = image_converted.at(i, j); 95 | input_channels_[0][pos] = pixel.val[0]; 96 | } 97 | } 98 | } 99 | if (dx == 0 && dy == 0) 100 | { 101 | input_size_resized_.width = 0; 102 | } 103 | } 104 | 105 | template 106 | int DetectorGpu::initNetwork(std::shared_ptr>& net, std::vector& input_channels, 107 | const std::string& net_cfg, const std::string& weights, const std::string& labels) 108 | { 109 | net.reset(new caffe::Net(net_cfg, caffe::TEST, caffe::Caffe::GetDefaultDevice())); 110 | net->CopyTrainedLayersFrom(weights); 111 | 112 | caffe::Blob* input_layer = net->input_blobs()[0]; 113 | num_channels_ = input_layer->channels(); 114 | input_size_ = cv::Size(input_layer->width(), input_layer->height()); 115 | 116 | input_layer->Reshape(1, num_channels_, input_size_.height, input_size_.width); 117 | net->Reshape(); 118 | 119 | Dtype* input_data = input_layer->mutable_cpu_data(); 120 | int w = input_layer->width(); 121 | int h = input_layer->height(); 122 | for (int i = 0; i < input_layer->channels(); ++i) 123 | { 124 | input_channels.push_back(input_data); 125 | input_data += w * h; 126 | } 127 | 128 | cv::Mat image = cv::imread(ros::package::getPath("opencl_caffe") + "/resources/cat.jpg"); 129 | initInputBlob(resizeImage(image), input_channels); 130 | net->Forward(); 131 | return true; 132 | } 133 | 134 | int DetectorGpu::loadResources(const std::string& net_cfg, const std::string& weights, const std::string& labels) 135 | { 136 | if (!boost::filesystem::exists(net_cfg) || !boost::filesystem::exists(weights) || !boost::filesystem::exists(labels)) 137 | { 138 | ROS_ERROR("Network configuration file or weights file not found!"); 139 | return false; 140 | } 141 | 142 | std::ifstream fs(labels); 143 | std::string label_name; 144 | while (getline(fs, label_name)) 145 | { 146 | labels_list.push_back(label_name); 147 | } 148 | 149 | int count = caffe::Caffe::EnumerateDevices(true); 150 | if (count > 0) 151 | { 152 | caffe::Caffe::set_mode(caffe::Caffe::GPU); 153 | caffe::Caffe::SetDevice(0); 154 | caffe::device* dev = caffe::Caffe::GetDefaultDevice(); 155 | if (dev->CheckCapability("cl_intel_subgroups") && dev->CheckCapability("cl_intel_subgroups_short")) 156 | { 157 | ROS_INFO("FP16 is supported, use FP16."); 158 | is_fp16_support = true; 159 | initNetwork(net_fp16_, input_channels_fp16_, net_cfg, weights, labels); 160 | } 161 | else 162 | { 163 | ROS_INFO("FP16 is NOT supported, use FP32."); 164 | is_fp16_support = false; 165 | initNetwork(net_fp32_, input_channels_fp32_, net_cfg, weights, labels); 166 | } 167 | } 168 | else 169 | { 170 | ROS_ERROR("GPU was not found or not supported."); 171 | return false; 172 | } 173 | 174 | ROS_INFO("Load resources completely!"); 175 | return true; 176 | } 177 | 178 | template 179 | int DetectorGpu::inference(std::shared_ptr>& net, std::vector& input_channels, 180 | const sensor_msgs::ImagePtr image_msg, object_msgs::ObjectsInBoxes& objects) 181 | { 182 | try 183 | { 184 | cv::Mat image; 185 | 186 | boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time(); 187 | cv::cvtColor(cv_bridge::toCvShare(image_msg, "rgb8")->image, image, cv::COLOR_RGB2BGR); 188 | initInputBlob(resizeImage(image), input_channels); 189 | net->Forward(); 190 | boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time(); 191 | boost::posix_time::time_duration msdiff = end - start; 192 | 193 | caffe::Blob* result_blob = net->output_blobs()[0]; 194 | const Dtype* result = result_blob->cpu_data(); 195 | const int num_det = result_blob->height(); 196 | 197 | for (int k = 0; k < num_det * 7; k += 7) 198 | { 199 | int classid = static_cast(result[k + 1]); 200 | float confidence = result[k + 2]; 201 | int left = 0, right, top = 0, bot; 202 | if (input_size_resized_.width == 0) 203 | { 204 | left = static_cast((result[k + 3] - result[k + 5] / 2.0) * image.cols); 205 | right = static_cast((result[k + 3] + result[k + 5] / 2.0) * image.cols); 206 | top = static_cast((result[k + 4] - result[k + 6] / 2.0) * image.rows); 207 | bot = static_cast((result[k + 4] + result[k + 6] / 2.0) * image.rows); 208 | } 209 | else 210 | { 211 | left = image.cols * (result[k + 3] - (input_size_.width - input_size_resized_.width) / 2. / input_size_.width) * 212 | input_size_.width / input_size_resized_.width; 213 | top = image.rows * 214 | (result[k + 4] - (input_size_.height - input_size_resized_.height) / 2. / input_size_.height) * 215 | input_size_.height / input_size_resized_.height; 216 | float boxw = result[k + 5] * image.cols * input_size_.width / input_size_resized_.width; 217 | float boxh = result[k + 6] * image.rows * input_size_.height / input_size_resized_.height; 218 | left -= static_cast(boxw / 2); 219 | top -= static_cast(boxh / 2); 220 | right = static_cast(left + boxw); 221 | bot = static_cast(top + boxh); 222 | } 223 | (left < 0) ? left = 0 : left; 224 | (right > image.cols - 1) ? right = image.cols - 1 : right; 225 | (top < 0) ? top = 0 : top; 226 | (bot > image.rows - 1) ? bot = image.rows - 1 : bot; 227 | 228 | object_msgs::ObjectInBox object_in_box; 229 | object_in_box.object.object_name = labels_list[classid]; 230 | object_in_box.object.probability = confidence; 231 | object_in_box.roi.x_offset = left; 232 | object_in_box.roi.y_offset = top; 233 | object_in_box.roi.height = bot - top; 234 | object_in_box.roi.width = right - left; 235 | objects.objects_vector.push_back(object_in_box); 236 | } 237 | 238 | objects.header = image_msg->header; 239 | objects.inference_time_ms = msdiff.total_milliseconds(); 240 | return true; 241 | } 242 | catch (cv_bridge::Exception& e) 243 | { 244 | ROS_ERROR("Could not covert from '%s' to 'rgb8'.", image_msg->encoding.c_str()); 245 | return false; 246 | } 247 | } 248 | 249 | int DetectorGpu::runInference(const sensor_msgs::ImagePtr image_msg, object_msgs::ObjectsInBoxes& objects) 250 | { 251 | return is_fp16_support ? inference(net_fp16_, input_channels_fp16_, image_msg, objects) : 252 | inference(net_fp32_, input_channels_fp32_, image_msg, objects); 253 | } 254 | } // namespace opencl_caffe 255 | -------------------------------------------------------------------------------- /opencl_caffe/src/nodelet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include "opencl_caffe/detector_gpu.h" 20 | #include "opencl_caffe/nodelet.h" 21 | 22 | PLUGINLIB_EXPORT_CLASS(opencl_caffe::Nodelet, nodelet::Nodelet) 23 | 24 | namespace opencl_caffe 25 | { 26 | void Nodelet::onInit() 27 | { 28 | ros::NodeHandle pnh = getPrivateNodeHandle(); 29 | std::string net_config_path, weights_path, labels_path; 30 | if (!pnh.getParam("net_config_path", net_config_path)) 31 | { 32 | ROS_WARN("param net_cfg_path not set, use default"); 33 | } 34 | if (!pnh.getParam("weights_path", weights_path)) 35 | { 36 | ROS_WARN("param weights_path not set, use default"); 37 | } 38 | if (!pnh.getParam("labels_path", labels_path)) 39 | { 40 | ROS_WARN("param labels_path not set, use default"); 41 | } 42 | 43 | loadResources(net_config_path, weights_path, labels_path); 44 | pub_ = getNodeHandle().advertise("inference", 1); 45 | } 46 | 47 | void Nodelet::cbImage(const sensor_msgs::ImagePtr image_msg) 48 | { 49 | object_msgs::ObjectsInBoxes objects; 50 | if (detector_->runInference(image_msg, objects)) 51 | { 52 | pub_.publish(objects); 53 | } 54 | else 55 | { 56 | ROS_ERROR("Inference failed."); 57 | } 58 | } 59 | 60 | void Nodelet::loadResources(const std::string net_config_path, const std::string weights_path, 61 | const std::string labels_path) 62 | { 63 | detector_.reset(new DetectorGpu()); 64 | sub_.shutdown(); 65 | 66 | if (detector_->loadResources(net_config_path, weights_path, labels_path)) 67 | { 68 | sub_ = getNodeHandle().subscribe("/usb_cam/image_raw", 1, &Nodelet::cbImage, this); 69 | } 70 | else 71 | { 72 | ROS_FATAL("Load resource failed."); 73 | ros::shutdown(); 74 | } 75 | } 76 | } // namespace opencl_caffe 77 | -------------------------------------------------------------------------------- /opencl_caffe/src/srv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include "opencl_caffe/detector_gpu.h" 21 | #include "opencl_caffe/srv.h" 22 | 23 | namespace opencl_caffe 24 | { 25 | Srv::Srv(ros::NodeHandle& n) 26 | { 27 | std::string net_config_path, weights_path, labels_path; 28 | if (!n.getParam("net_config_path", net_config_path)) 29 | { 30 | ROS_WARN("param net_cfg_path not set, use default"); 31 | } 32 | if (!n.getParam("weights_path", weights_path)) 33 | { 34 | ROS_WARN("param weights_path not set, use default"); 35 | } 36 | if (!n.getParam("labels_path", labels_path)) 37 | { 38 | ROS_WARN("param labels_path not set, use default"); 39 | } 40 | 41 | loadResources(net_config_path, weights_path, labels_path); 42 | service_ = n.advertiseService("run_inference", &Srv::handleService, this); 43 | } 44 | 45 | bool Srv::handleService(object_msgs::DetectObject::Request& req, object_msgs::DetectObject::Response& resp) 46 | { 47 | for (auto image_path : req.image_paths) 48 | { 49 | cv_bridge::CvImage cv_image; 50 | sensor_msgs::Image ros_image; 51 | cv_image.image = cv::imread(image_path); 52 | cv_image.encoding = "bgr8"; 53 | cv_image.toImageMsg(ros_image); 54 | 55 | object_msgs::ObjectsInBoxes objects; 56 | 57 | if (!detector_->runInference(boost::make_shared(ros_image), objects)) 58 | { 59 | ROS_ERROR("Detect object failed."); 60 | return false; 61 | } 62 | 63 | resp.objects.push_back(objects); 64 | } 65 | 66 | return true; 67 | } 68 | 69 | void Srv::loadResources(const std::string net_config_path, const std::string weights_path, 70 | const std::string labels_path) 71 | { 72 | detector_.reset(new DetectorGpu()); 73 | if (!detector_->loadResources(net_config_path, weights_path, labels_path)) 74 | { 75 | ROS_FATAL("Load resource failed."); 76 | ros::shutdown(); 77 | } 78 | } 79 | } // namespace opencl_caffe 80 | 81 | int main(int argc, char** argv) 82 | { 83 | ros::init(argc, argv, "opencl_caffe"); 84 | 85 | ros::NodeHandle n("~"); 86 | opencl_caffe::Srv srv(n); 87 | 88 | ros::spin(); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /opencl_caffe/tests/detector.test: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /opencl_caffe/tests/service.test: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 20 | 21 | -------------------------------------------------------------------------------- /opencl_caffe/tests/unittest_detector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "opencl_caffe/detector_gpu.h" 27 | 28 | TEST(UnitTestDetector, testDetectorGPU) 29 | { 30 | opencl_caffe::DetectorGpu detector; 31 | 32 | ros::NodeHandle n("~"); 33 | std::string net_config_path, weights_path, labels_path; 34 | if (!n.getParam("net_config_path", net_config_path)) 35 | { 36 | ROS_WARN("param net_cfg_path not set, use default"); 37 | } 38 | if (!n.getParam("weights_path", weights_path)) 39 | { 40 | ROS_WARN("param weights_path not set, use default"); 41 | } 42 | if (!n.getParam("labels_path", labels_path)) 43 | { 44 | ROS_WARN("param labels_path not set, use default"); 45 | } 46 | 47 | // use ASSERT instead of EXPECT 48 | ASSERT_TRUE(boost::filesystem::exists(net_config_path)); 49 | ASSERT_TRUE(boost::filesystem::exists(weights_path)); 50 | ASSERT_TRUE(boost::filesystem::exists(labels_path)); 51 | 52 | ASSERT_TRUE(detector.loadResources(net_config_path, weights_path, labels_path)); 53 | 54 | cv::Mat image = cv::imread(ros::package::getPath("opencl_caffe") + "/resources/cat.jpg"); 55 | sensor_msgs::ImagePtr image_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg(); 56 | object_msgs::ObjectsInBoxes obejcts; 57 | ASSERT_TRUE(detector.runInference(image_msg, obejcts)); 58 | ASSERT_GE(obejcts.objects_vector.size(), 1); 59 | } 60 | 61 | int main(int argc, char** argv) 62 | { 63 | testing::InitGoogleTest(&argc, argv); 64 | ros::init(argc, argv, "ropencl_caffe_test"); 65 | return RUN_ALL_TESTS(); 66 | } 67 | -------------------------------------------------------------------------------- /opencl_caffe/tests/unittest_srv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | TEST(UnitTestSrv, testSrv) 30 | { 31 | ros::NodeHandle n; 32 | ros::ServiceClient client = n.serviceClient("opencl_caffe/opencl_caffe_srv/run_inference"); 33 | object_msgs::DetectObject inf; 34 | 35 | inf.request.image_paths.push_back(ros::package::getPath("opencl_caffe") + "/resources/cat.jpg"); 36 | inf.request.image_paths.push_back(ros::package::getPath("opencl_caffe") + "/resources/cat.jpg"); 37 | client.waitForExistence(ros::Duration(120)); 38 | ASSERT_TRUE(client.call(inf)); 39 | ASSERT_EQ(inf.response.objects.size(), 2); 40 | ASSERT_GE(inf.response.objects[0].objects_vector.size(), 1); 41 | ASSERT_GE(inf.response.objects[1].objects_vector.size(), 1); 42 | } 43 | 44 | int main(int argc, char** argv) 45 | { 46 | testing::InitGoogleTest(&argc, argv); 47 | ros::init(argc, argv, "opencl_caffe_test"); 48 | return RUN_ALL_TESTS(); 49 | } 50 | -------------------------------------------------------------------------------- /opencl_caffe_launch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2017 Intel Corporation 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required(VERSION 2.8.3) 16 | project(opencl_caffe_launch) 17 | 18 | find_package( 19 | catkin REQUIRED 20 | ) 21 | 22 | catkin_package( 23 | LIBRARIES ${PROJECT_NAME} 24 | CATKIN_DEPENDS 25 | roslaunch 26 | opencl_caffe 27 | opencl_caffe_viewer 28 | ) 29 | 30 | install(DIRECTORY launch/ 31 | DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch 32 | ) 33 | 34 | # Flags 35 | if(UNIX OR APPLE) 36 | # Linker flags. 37 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel") 38 | # GCC specific flags. ICC is compatible with them. 39 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -z noexecstack -z relro -z now") 40 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack -z relro -z now") 41 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 42 | # In Clang, -z flags are not compatible, they need to be passed to linker via -Wl. 43 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 44 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 45 | endif() 46 | 47 | # Compiler flags. 48 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") 49 | # GCC specific flags. 50 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9) 51 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector-strong") 52 | else() 53 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 54 | endif() 55 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 56 | # Clang is compatbile with some of the flags. 57 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 58 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" ) 59 | # Same as above, with exception that ICC compilation crashes with -fPIE option, even 60 | # though it uses -pie linker option that require -fPIE during compilation. Checksec 61 | # shows that it generates correct PIE anyway if only -pie is provided. 62 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector") 63 | endif() 64 | 65 | # Generic flags. 66 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -fno-operator-names -Wformat -Wformat-security -Wall") 67 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 68 | # Dot not forward c++11 flag to GPU beucause it is not supported 69 | set( CUDA_PROPAGATE_HOST_FLAGS OFF ) 70 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2") 71 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") 72 | endif() 73 | 74 | # test 75 | if(CATKIN_ENABLE_TESTING) 76 | # launch check 77 | find_package( 78 | catkin REQUIRED COMPONENTS 79 | roslaunch) 80 | roslaunch_add_file_check( 81 | launch) 82 | endif() 83 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/includes/default.yaml: -------------------------------------------------------------------------------- 1 | net_config_path: "/opt/clCaffe/models/yolo/yolo416/yolo_fused_deploy.prototxt" 2 | weights_path: "/opt/clCaffe/models/yolo/yolo416/fused_yolo.caffemodel" 3 | labels_path: "/opt/clCaffe/data/yolo/voc.txt" 4 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/includes/manager.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/includes/nodelet.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/includes/service.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/includes/viewer.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/kinect_viewer.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/opencl_caffe_nodelet.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/opencl_caffe_srv.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/realsense_viewer.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /opencl_caffe_launch/launch/usb_cam_viewer.launch: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /opencl_caffe_launch/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | opencl_caffe_launch 17 | 0.3.0 18 | The opencl_caffe launch package 19 | Xiaocheng Dong 20 | Xiaojun Huang 21 | 22 | Apache 2.0 23 | 24 | catkin 25 | roslaunch 26 | nodelet 27 | usb_cam 28 | openni_launch 29 | realsense_ros_camera 30 | opencl_caffe 31 | opencl_caffe_viewer 32 | roslaunch 33 | nodelet 34 | usb_cam 35 | openni_launch 36 | realsense_ros_camera 37 | opencl_caffe 38 | opencl_caffe_viewer 39 | 40 | -------------------------------------------------------------------------------- /opencl_caffe_viewer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2017 Intel Corporation 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required(VERSION 2.8.3) 16 | project(opencl_caffe_viewer) 17 | 18 | find_package(catkin REQUIRED COMPONENTS 19 | roslib 20 | roscpp 21 | sensor_msgs 22 | std_msgs 23 | object_msgs 24 | dynamic_reconfigure 25 | message_filters 26 | cv_bridge 27 | roslint 28 | ) 29 | find_package(OpenCV REQUIRED) 30 | 31 | # Flags 32 | if(UNIX OR APPLE) 33 | # Linker flags. 34 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel") 35 | # GCC specific flags. ICC is compatible with them. 36 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -z noexecstack -z relro -z now") 37 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack -z relro -z now") 38 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 39 | # In Clang, -z flags are not compatible, they need to be passed to linker via -Wl. 40 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 41 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now") 42 | endif() 43 | 44 | # Compiler flags. 45 | if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") 46 | # GCC specific flags. 47 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9) 48 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector-strong") 49 | else() 50 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 51 | endif() 52 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 53 | # Clang is compatbile with some of the flags. 54 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE -fstack-protector") 55 | elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" ) 56 | # Same as above, with exception that ICC compilation crashes with -fPIE option, even 57 | # though it uses -pie linker option that require -fPIE during compilation. Checksec 58 | # shows that it generates correct PIE anyway if only -pie is provided. 59 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector") 60 | endif() 61 | 62 | # Generic flags. 63 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -fno-operator-names -Wformat -Wformat-security -Wall") 64 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 65 | # Dot not forward c++11 flag to GPU beucause it is not supported 66 | set( CUDA_PROPAGATE_HOST_FLAGS OFF ) 67 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2") 68 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie") 69 | endif() 70 | 71 | catkin_package( 72 | LIBRARIES opencl_caffe_viewer 73 | CATKIN_DEPENDS roslib roscpp sensor_msgs std_msgs object_msgs opencl_caffe 74 | ) 75 | 76 | include_directories( 77 | include 78 | ${catkin_INCLUDE_DIRS} 79 | ) 80 | 81 | add_executable(opencl_caffe_viewer src/opencl_caffe_viewer.cpp) 82 | add_dependencies(opencl_caffe_viewer ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 83 | 84 | target_link_libraries(opencl_caffe_viewer 85 | ${catkin_LIBRARIES} 86 | ${OpenCV_LIBS} 87 | ) 88 | 89 | install(TARGETS opencl_caffe_viewer 90 | ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 91 | LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} 92 | RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} 93 | ) 94 | 95 | if(CATKIN_ENABLE_TESTING) 96 | # static code check 97 | find_package(roslint REQUIRED) 98 | roslint_cpp() 99 | endif() 100 | -------------------------------------------------------------------------------- /opencl_caffe_viewer/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | opencl_caffe_viewer 17 | 0.3.0 18 | viewer for ROS OpenCL Caffe 19 | Xiaocheng Dong 20 | Xiaojun Huang 21 | 22 | Apache 2.0 23 | 24 | roslib 25 | roscpp 26 | sensor_msgs 27 | std_msgs 28 | object_msgs 29 | opencl_caffe 30 | dynamic_reconfigure 31 | roslib 32 | roscpp 33 | sensor_msgs 34 | std_msgs 35 | object_msgs 36 | opencl_caffe 37 | dynamic_reconfigure 38 | catkin 39 | 40 | 41 | -------------------------------------------------------------------------------- /opencl_caffe_viewer/src/opencl_caffe_viewer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Intel Corporation 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | void cbTimeSync(const sensor_msgs::ImageConstPtr& img, const object_msgs::ObjectsInBoxes::ConstPtr& objs) 26 | { 27 | try 28 | { 29 | cv::Mat cv_image = cv_bridge::toCvShare(img, "bgr8")->image; 30 | for (auto obj : objs->objects_vector) 31 | { 32 | std::stringstream ss; 33 | ss << obj.object.object_name << ':' << obj.object.probability; 34 | cv::rectangle(cv_image, cvPoint(obj.roi.x_offset, obj.roi.y_offset), 35 | cvPoint(obj.roi.x_offset + obj.roi.width, obj.roi.y_offset + obj.roi.height), 36 | cv::Scalar(255, 242, 35)); 37 | cv::putText(cv_image, ss.str(), cvPoint(obj.roi.x_offset, obj.roi.y_offset + 20), cv::FONT_HERSHEY_PLAIN, 1.0f, 38 | cv::Scalar(0, 255, 255)); 39 | } 40 | 41 | cv::imshow("opencl_caffe_viewer", cv_image); 42 | cv::waitKey(5); 43 | } 44 | catch (cv_bridge::Exception& e) 45 | { 46 | ROS_ERROR("Could not convert from '%s' to 'bgr8'.", img->encoding.c_str()); 47 | } 48 | } 49 | 50 | int main(int argc, char** argv) 51 | { 52 | ros::init(argc, argv, "opencl_caffe_viewer"); 53 | ros::NodeHandle n; 54 | 55 | message_filters::Subscriber cam_sub(n, "/usb_cam/image_raw", 1); 56 | message_filters::Subscriber objs_sub(n, "opencl_caffe/inference", 1); 57 | 58 | message_filters::TimeSynchronizer ts(cam_sub, objs_sub, 60); 59 | ts.registerCallback(boost::bind(&cbTimeSync, _1, _2)); 60 | ros::spin(); 61 | 62 | return 0; 63 | } 64 | --------------------------------------------------------------------------------