diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index bce4c96b3..48abf80ab 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,29 +7,34 @@ endif() include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) include_directories(${CMAKE_CURRENT_BINARY_DIR}/../src) +set(NCNN_EXAMPLE_LINK_LIBRARIES ncnn ${OpenCV_LIBS}) +if(NCNN_VULKAN) + list(APPEND NCNN_EXAMPLE_LINK_LIBRARIES ${Vulkan_LIBRARY}) +endif() + add_executable(squeezenet squeezenet.cpp) -target_link_libraries(squeezenet ncnn ${OpenCV_LIBS}) +target_link_libraries(squeezenet ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(fasterrcnn fasterrcnn.cpp) -target_link_libraries(fasterrcnn ncnn ${OpenCV_LIBS}) +target_link_libraries(fasterrcnn ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(rfcn rfcn.cpp) -target_link_libraries(rfcn ncnn ${OpenCV_LIBS}) +target_link_libraries(rfcn ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(yolov2 yolov2.cpp) -target_link_libraries(yolov2 ncnn ${OpenCV_LIBS}) +target_link_libraries(yolov2 ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(yolov3 yolov3.cpp) -target_link_libraries(yolov3 ncnn ${OpenCV_LIBS}) +target_link_libraries(yolov3 ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(mobilenetv2ssdlite mobilenetv2ssdlite.cpp) -target_link_libraries(mobilenetv2ssdlite ncnn ${OpenCV_LIBS}) +target_link_libraries(mobilenetv2ssdlite ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(mobilenetssd mobilenetssd.cpp) -target_link_libraries(mobilenetssd ncnn ${OpenCV_LIBS}) +target_link_libraries(mobilenetssd ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(squeezenetssd squeezenetssd.cpp) -target_link_libraries(squeezenetssd ncnn ${OpenCV_LIBS}) +target_link_libraries(squeezenetssd ${NCNN_EXAMPLE_LINK_LIBRARIES}) add_executable(shufflenetv2 shufflenetv2.cpp) -target_link_libraries(shufflenetv2 ncnn ${OpenCV_LIBS}) +target_link_libraries(shufflenetv2 ${NCNN_EXAMPLE_LINK_LIBRARIES}) diff --git a/examples/fasterrcnn.cpp b/examples/fasterrcnn.cpp index 36f22b937..ab905a0b9 100644 --- a/examples/fasterrcnn.cpp +++ b/examples/fasterrcnn.cpp @@ -4,7 +4,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -102,6 +106,10 @@ static int detect_fasterrcnn(const cv::Mat& bgr, std::vector& objects) { ncnn::Net fasterrcnn; +#if NCNN_VULKAN + fasterrcnn.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // original pretrained model from https://github.com/rbgirshick/py-faster-rcnn // py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt // https://dl.dropboxusercontent.com/s/o6ii098bu51d139/faster_rcnn_models.tgz?dl=0 @@ -328,9 +336,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_fasterrcnn(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/mobilenetssd.cpp b/examples/mobilenetssd.cpp index 1e7972a9a..c85898f51 100755 --- a/examples/mobilenetssd.cpp +++ b/examples/mobilenetssd.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -31,6 +35,10 @@ static int detect_mobilenet(const cv::Mat& bgr, std::vector& objects) { ncnn::Net mobilenet; +#if NCNN_VULKAN + mobilenet.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // model is converted from https://github.com/chuanqi305/MobileNet-SSD // and can be downloaded from https://drive.google.com/open?id=0ByaKLD9QaPtucWk0Y0dha1VVY0U mobilenet.load_param("mobilenet_ssd_voc_ncnn.param"); @@ -137,9 +145,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_mobilenet(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/mobilenetv2ssdlite.cpp b/examples/mobilenetv2ssdlite.cpp index 8f4abd1f4..7659eb576 100755 --- a/examples/mobilenetv2ssdlite.cpp +++ b/examples/mobilenetv2ssdlite.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN class Noop : public ncnn::Layer {}; DEFINE_LAYER_CREATOR(Noop) @@ -34,6 +38,10 @@ static int detect_mobilenetv2(const cv::Mat& bgr, std::vector& objects) { ncnn::Net mobilenetv2; +#if NCNN_VULKAN + mobilenetv2.use_vulkan_compute = true; +#endif // NCNN_VULKAN + mobilenetv2.register_custom_layer("Silence", Noop_layer_creator); // original pretrained model from https://github.com/chuanqi305/MobileNetv2-SSDLite @@ -143,9 +151,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_mobilenetv2(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/rfcn.cpp b/examples/rfcn.cpp index 2da214e8b..b15b79c90 100644 --- a/examples/rfcn.cpp +++ b/examples/rfcn.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -116,6 +120,10 @@ static int detect_rfcn(const cv::Mat& bgr, std::vector& objects) { ncnn::Net rfcn; +#if NCNN_VULKAN + rfcn.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // original pretrained model from https://github.com/YuwenXiong/py-R-FCN // https://github.com/YuwenXiong/py-R-FCN/blob/master/models/pascal_voc/ResNet-50/rfcn_end2end/test_agnostic.prototxt // https://1drv.ms/u/s!AoN7vygOjLIQqUWHpY67oaC7mopf @@ -342,9 +350,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_rfcn(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/shufflenetv2.cpp b/examples/shufflenetv2.cpp index 6823e4101..7d2487985 100644 --- a/examples/shufflenetv2.cpp +++ b/examples/shufflenetv2.cpp @@ -18,12 +18,20 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN static int detect_shufflenetv2(const cv::Mat& bgr, std::vector& cls_scores) { ncnn::Net shufflenetv2; +#if NCNN_VULKAN + shufflenetv2.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe // models can be downloaded from https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe/releases shufflenetv2.load_param("shufflenet_v2_x0.5.param"); @@ -108,9 +116,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector cls_scores; detect_shufflenetv2(m, cls_scores); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + print_topk(cls_scores, 3); return 0; diff --git a/examples/squeezenet.cpp b/examples/squeezenet.cpp index b4a7515e3..e132daffb 100644 --- a/examples/squeezenet.cpp +++ b/examples/squeezenet.cpp @@ -18,11 +18,20 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN static int detect_squeezenet(const cv::Mat& bgr, std::vector& cls_scores) { ncnn::Net squeezenet; + +#if NCNN_VULKAN + squeezenet.use_vulkan_compute = true; +#endif // NCNN_VULKAN + squeezenet.load_param("squeezenet_v1.1.param"); squeezenet.load_model("squeezenet_v1.1.bin"); @@ -89,9 +98,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector cls_scores; detect_squeezenet(m, cls_scores); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + print_topk(cls_scores, 3); return 0; diff --git a/examples/squeezenetssd.cpp b/examples/squeezenetssd.cpp index 909dd0ba6..c51145a12 100755 --- a/examples/squeezenetssd.cpp +++ b/examples/squeezenetssd.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -31,6 +35,10 @@ static int detect_squeezenet(const cv::Mat& bgr, std::vector& objects) { ncnn::Net squeezenet; +#if NCNN_VULKAN + squeezenet.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // original pretrained model from https://github.com/chuanqi305/SqueezeNet-SSD // squeezenet_ssd_voc_deploy.prototxt // https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc @@ -137,9 +145,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_squeezenet(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/yolov2.cpp b/examples/yolov2.cpp index ad7e74ba6..686fdc51c 100644 --- a/examples/yolov2.cpp +++ b/examples/yolov2.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -31,6 +35,10 @@ static int detect_yolov2(const cv::Mat& bgr, std::vector& objects) { ncnn::Net yolov2; +#if NCNN_VULKAN + yolov2.use_vulkan_compute = true; +#endif // NCNN_VULKAN + // original pretrained model from https://github.com/eric612/MobileNet-YOLO // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy.prototxt // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy_iter_80000.caffemodel @@ -141,9 +149,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_yolov2(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0; diff --git a/examples/yolov3.cpp b/examples/yolov3.cpp index a32d76bc4..0393777c6 100644 --- a/examples/yolov3.cpp +++ b/examples/yolov3.cpp @@ -18,7 +18,11 @@ #include #include +#include "platform.h" #include "net.h" +#if NCNN_VULKAN +#include "gpu.h" +#endif // NCNN_VULKAN struct Object { @@ -31,31 +35,26 @@ static int detect_yolov3(const cv::Mat& bgr, std::vector& objects) { ncnn::Net yolov3; - // original pretrained model from https://github.com/eric612/Caffe-YOLOv3-Windows - // yolov3_deploy.prototxt - // https://github.com/eric612/MobileNet-YOLO/blob/master/models/darknet_yolov3/yolov3.prototxt - // https://drive.google.com/file/d/12nLE6GtmwZxDiulwdEmB3Ovj5xx18Nnh/view - yolov3.load_param("yolo.param"); - yolov3.load_model("yolo.bin"); +#if NCNN_VULKAN + yolov3.use_vulkan_compute = true; +#endif // NCNN_VULKAN - // https://github.com/eric612/MobileNet-YOLO + // original pretrained model from https://github.com/eric612/MobileNet-YOLO // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov3/mobilenet_yolov3_lite_deploy.prototxt // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov3/mobilenet_yolov3_lite_deploy.caffemodel + yolov3.load_param("mobilenet_yolov3.param"); + yolov3.load_model("mobilenet_yolov3.bin"); - - const int target_size = 416; + const int target_size = 320; int img_w = bgr.cols; int img_h = bgr.rows; ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size); - // the Caffe-yolov3-Windows style - // X' = X * scale - mean - const float mean_vals[3] = {1.0f, 1.0f, 1.0f}; + const float mean_vals[3] = {127.5f, 127.5f, 127.5f}; const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f}; - in.substract_mean_normalize(0, norm_vals); - in.substract_mean_normalize(mean_vals, 0); + in.substract_mean_normalize(mean_vals, norm_vals); ncnn::Extractor ex = yolov3.create_extractor(); ex.set_num_threads(4); @@ -147,9 +146,17 @@ int main(int argc, char** argv) return -1; } +#if NCNN_VULKAN + ncnn::create_gpu_instance(); +#endif // NCNN_VULKAN + std::vector objects; detect_yolov3(m, objects); +#if NCNN_VULKAN + ncnn::destroy_gpu_instance(); +#endif // NCNN_VULKAN + draw_objects(m, objects); return 0;