Browse Source

example now runs on gpu when vulkan enabled

tags/20190320
nihui 7 years ago
parent
commit
253bef2f7b
10 changed files with 164 additions and 23 deletions
  1. +14
    -9
      examples/CMakeLists.txt
  2. +16
    -0
      examples/fasterrcnn.cpp
  3. +16
    -0
      examples/mobilenetssd.cpp
  4. +16
    -0
      examples/mobilenetv2ssdlite.cpp
  5. +16
    -0
      examples/rfcn.cpp
  6. +16
    -0
      examples/shufflenetv2.cpp
  7. +17
    -0
      examples/squeezenet.cpp
  8. +16
    -0
      examples/squeezenetssd.cpp
  9. +16
    -0
      examples/yolov2.cpp
  10. +21
    -14
      examples/yolov3.cpp

+ 14
- 9
examples/CMakeLists.txt View File

@@ -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})

+ 16
- 0
examples/fasterrcnn.cpp View File

@@ -4,7 +4,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_fasterrcnn(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 16
- 0
examples/mobilenetssd.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_mobilenet(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 16
- 0
examples/mobilenetv2ssdlite.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_mobilenetv2(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 16
- 0
examples/rfcn.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_rfcn(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 16
- 0
examples/shufflenetv2.cpp View File

@@ -18,12 +18,20 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#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<float>& 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<float> cls_scores;
detect_shufflenetv2(m, cls_scores);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

print_topk(cls_scores, 3);

return 0;


+ 17
- 0
examples/squeezenet.cpp View File

@@ -18,11 +18,20 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#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<float>& 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<float> cls_scores;
detect_squeezenet(m, cls_scores);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

print_topk(cls_scores, 3);

return 0;


+ 16
- 0
examples/squeezenetssd.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_squeezenet(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 16
- 0
examples/yolov2.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_yolov2(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


+ 21
- 14
examples/yolov3.cpp View File

@@ -18,7 +18,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#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<Object>& 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<Object> objects;
detect_yolov3(m, objects);

#if NCNN_VULKAN
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

draw_objects(m, objects);

return 0;


Loading…
Cancel
Save