#2108·ncnn

ncnn/examples/yolov4.cpp, yolov4->opt.use_vulkan_compute = true , detection is Error.

Author: lovemoryCreated Sep 13, 2020Updated Aug 3, 2026

Issue description: 47th row: yolov4->opt.use_vulkan_compute = true; detection is Error. 47th row : yolov4->opt.use_vulkan_compute = false; detection is OK.

cpp code copy from ncnn/examples/yolov4.cpp , and modify some lines to support input *.img code is : `// Tencent is pleased to support the open source community by making ncnn available. // // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. // // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except // in compliance with the License. You may obtain a copy of the License at // // https://opensource.org/licenses/BSD-3-Clause // // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License.

#include "net.h"

#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>

#include

#include <stdio.h>

#define NCNN_PROFILING #define YOLOV4_TINY //Using yolov4_tiny, if undef, using original yolov4

#ifdef NCNN_PROFILING #include "benchmark.h" #endif

struct Object { cv::Rect_ rect; int label; float prob; };

static int init_yolov4(ncnn::Net* yolov4, int* target_size) { /* --> Set the params you need for the ncnn inference <-- */

yolov4->opt.num_threads = 4; //You need to compile with libgomp for multi thread support

yolov4->opt.use_vulkan_compute = true; //You need to compile with libvulkan for gpu support

yolov4->opt.use_winograd_convolution = true;
yolov4->opt.use_sgemm_convolution = true;
yolov4->opt.use_fp16_packed = true;
yolov4->opt.use_fp16_storage = true;
yolov4->opt.use_fp16_arithmetic = true;
yolov4->opt.use_packing_layout = true;
yolov4->opt.use_shader_pack8 = false;
yolov4->opt.use_image_storage = false;

/* --> End of setting params <-- */
int ret = 0;

// original pretrained model from https://github.com/AlexeyAB/darknet
// the ncnn model https://drive.google.com/drive/folders/1YzILvh0SKQPS_lrb33dmGNq7aVTKPWS0?usp=sharing
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models

#ifdef YOLOV4_TINY const char* yolov4_param = "yolov4-tiny-opt.param"; const char* yolov4_model = "yolov4-tiny-opt.bin"; target_size = 416; #else const char yolov4_param = "yolov4-opt.param"; const char* yolov4_model = "yolov4-opt.bin"; *target_size = 608; #endif

ret = yolov4->load_param(yolov4_param);
if (ret != 0)
{
    return ret;
}

ret = yolov4->load_model(yolov4_model);
if (ret != 0)
{
    return ret;
}

return 0;

}

static int detect_yolov4(const cv::Mat& bgr, std::vector& objects, int target_size, ncnn::Net* yolov4) { int img_w = bgr.cols; int img_h = bgr.rows;

ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_size, target_size);
//ncnn::Mat in = ncnn::Mat::from_pixels(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows);
const float mean_vals[3] = {0, 0, 0};
const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
in.substract_mean_normalize(mean_vals, norm_vals);

ncnn::Extractor ex = yolov4->create_extractor();

ex.input("data", in);

ncnn::Mat out;
ex.extract("output", out);

objects.clear();
fprintf(stdout,"out.h = %d\n", out.h);
for (int i = 0; i < out.h; i++)
{
    const float* values = out.row(i);

    Object object;
    object.label = values[0];
    object.prob = values[1];
    object.rect.x = values[2] * img_w;
    object.rect.y = values[3] * img_h;
    object.rect.width = values[4] * img_w - object.rect.x;
    object.rect.height = values[5] * img_h - object.rect.y;

    objects.push_back(object);
}

return 0;

} static int frame_count_index= 0; static int draw_objects(const cv::Mat& bgr, const std::vector& objects, int is_streaming) { static const char* class_names[] = {"background", "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" };

cv::Mat image = bgr.clone();

for (size_t i = 0; i < objects.size(); i++)
{
    const Object& obj = objects[i];

    fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
            obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);

    cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));

    char text[256];
    sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);

    int baseLine = 0;
    cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

    int x = obj.rect.x;
    int y = obj.rect.y - label_size.height - baseLine;
    if (y < 0)
        y = 0;
    if (x + label_size.width > image.cols)
        x = image.cols - label_size.width;

    cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
                  cv::Scalar(255, 255, 255), -1);

    cv::putText(image, text, cv::Point(x, y + label_size.height),
                cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}


cv::imwrite("ncnn_output.jpg",image );

char savefilename[200]="img_fcw_04_yolo.bmp" ;
if (1)
{
	frame_count_index++;
	memset(savefilename, 0, sizeof(savefilename));
	sprintf (savefilename, "result_image/img_fcw_04_yolo_%05d.jpg", frame_count_index);
	cv::imwrite(savefilename,image );
}

return 0;

}

int main(int argc, char** argv) { cv::Mat frame; std::vector objects;

ncnn::Net yolov4;

const char* devicepath;

int target_size = 0;
int is_streaming = 0;

if (argc < 2)
{
    fprintf(stderr, "Usage: %s [v4l inpude device or image]\n", argv[0]);
    return -1;
}

devicepath = argv[1];

#ifdef NCNN_PROFILING double t_load_start = ncnn::get_current_time(); #endif

int ret = init_yolov4(&yolov4, &target_size); //We load model and param first!
if (ret != 0)
{
    fprintf(stderr, "Failed to load model or param, error %d", ret);
    return -1;
}

#ifdef NCNN_PROFILING double t_load_end = ncnn::get_current_time(); fprintf(stdout, "NCNN Init time %.02lfms\n", t_load_end - t_load_start); #endif

int i = 0;
for (i = 1; i < argc; i++)
{
    fprintf(stdout, "file: %s\t", argv[i]);
    fprintf(stdout, "i = %d start\n", i);
    frame = cv::imread(argv[i], 1);
    if (frame.empty())
    {
        fprintf(stderr, "Failed to read image %s.\n", argv[1]);
        return -1;
    }
    // cv::Rect myROI(780, 420, 416, 416);
    // cv::Mat croppedImage = frame(myROI);

#ifdef NCNN_PROFILING double t_detect_start = ncnn::get_current_time(); #endif

    detect_yolov4(frame, objects, target_size, &yolov4); //Create an extractor and run detection

#ifdef NCNN_PROFILING double t_detect_end = ncnn::get_current_time(); fprintf(stdout, "NCNN detection time %.02lfms\n", t_detect_end - t_detect_start); #endif

#ifdef NCNN_PROFILING double t_draw_start = ncnn::get_current_time(); #endif

    draw_objects(frame, objects, is_streaming); //Draw detection results on opencv image

#ifdef NCNN_PROFILING double t_draw_end = ncnn::get_current_time(); fprintf(stdout, "NCNN OpenCV draw result time %.02lfms\n", t_draw_end - t_draw_start); #endif

    fprintf(stdout, "file: %s\t", argv[i]);
    fprintf(stdout, "i = %d end\n\n", i);
}

return 0;

}**Android.mk is:**LOCAL_PATH := $(call my-dir) $(warning "the value of LOCAL_PATH is $(LOCAL_PATH)")

include $(CLEAR_VARS) LOCAL_MODULE := opencv-core-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/arm64-v8a/libopencv_core.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-imgcodecs-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/arm64-v8a/libopencv_imgcodecs.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-imgproc-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/arm64-v8a/libopencv_imgproc.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-ittnotify-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/libittnotify.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-tbb-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/libtbb.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := webp-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibwebp.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-IlmImf-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/libIlmImf.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-jpeg-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibjpeg-turbo.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-jasper-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibjasper.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-png-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibpng.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-tiff-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibtiff.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-tegra-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/libtegra_hal.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-dnn-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/arm64-v8a/libopencv_dnn.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-protobuf-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/3rdparty/libs/arm64-v8a/liblibprotobuf.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := opencv-ximgproc-prebuilt LOCAL_SRC_FILES := ../../../../extern/lib/opencv_4.4.0_opencl/arm64-v8a/libopencv_ximgproc.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := libncnn-prebuilt LOCAL_SRC_FILES := ../../../../extern/ncnn/arm64-v8a/libncnn.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := glslang-prebuilt LOCAL_SRC_FILES :=../../../../extern/ncnn/arm64-v8a/libglslang.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := SPIRV-prebuilt LOCAL_SRC_FILES := ../../../../extern/ncnn/arm64-v8a/libSPIRV.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := OGLCompiler-prebuilt LOCAL_SRC_FILES := ../../../../extern/ncnn/arm64-v8a/libOGLCompiler.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := OSDependent-prebuilt LOCAL_SRC_FILES := ../../../../extern/ncnn/arm64-v8a/libOSDependent.a include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS) LOCAL_MODULE := ncnn_yolov4tiny_test LOCAL_CFLAGS := -Werror -Wno-write-strings -D__supportneon__ -D__DEBUG__ -D_DEBUG_PROCTIME_ -D__FCW_ENABLE_YOLO4TINY__ LOCAL_LDFLAGS := -pie -fPIE

LOCAL_CFLAGS += -fopenmp LOCAL_CPPFLAGS += -fopenmp LOCAL_LDFLAGS += -fopenmp

LOCAL_SRC_FILES += ../adas_vehicle_det_ncnn_yolov4.cpp LOCAL_C_INCLUDES += ${LOCAL_PATH}/../../../../extern/include/opencv_4.4.0 LOCAL_C_INCLUDES += ${LOCAL_PATH}/../../../../extern/ncnn/include/ncnn

#LOCAL_LDLIBS := -lz -llog LOCAL_LDLIBS := -lz -llog -ljnigraphics -lvulkan -landroid CXXFLAGS := -D_GLIBCXX_DEBUG -O2 LOCAL_STATIC_LIBRARIES += opencv-dnn-prebuilt LOCAL_STATIC_LIBRARIES += opencv-imgcodecs-prebuilt LOCAL_STATIC_LIBRARIES += opencv-ximgproc-prebuilt LOCAL_STATIC_LIBRARIES += opencv-imgproc-prebuilt LOCAL_STATIC_LIBRARIES += opencv-core-prebuilt LOCAL_STATIC_LIBRARIES += opencv-protobuf-prebuilt LOCAL_STATIC_LIBRARIES += opencv-ittnotify-prebuilt LOCAL_STATIC_LIBRARIES += opencv-tbb-prebuilt LOCAL_STATIC_LIBRARIES += opencv-IlmImf-prebuilt LOCAL_STATIC_LIBRARIES += opencv-jasper-prebuilt LOCAL_STATIC_LIBRARIES += opencv-jpeg-prebuilt LOCAL_STATIC_LIBRARIES += opencv-png-prebuilt LOCAL_STATIC_LIBRARIES += opencv-tiff-prebuilt LOCAL_STATIC_LIBRARIES += opencv-tegra-prebuilt LOCAL_STATIC_LIBRARIES += webp-prebuilt LOCAL_STATIC_LIBRARIES += libncnn-prebuilt LOCAL_STATIC_LIBRARIES += glslang-prebuilt LOCAL_STATIC_LIBRARIES += SPIRV-prebuilt LOCAL_STATIC_LIBRARIES += OGLCompiler-prebuilt LOCAL_STATIC_LIBRARIES += OSDependent-prebuilt include $(BUILD_EXECUTABLE)

Application.mk is: APP_ABI := arm64-v8a APP_STL := c++_shared APP_CPPFLAGS := -frtti -fexceptions APP_PLATFORM := android-24

test output:

47th row : yolov4->opt.use_vulkan_compute = false; detection is OK.

mercury:/data/local/tmp/200811120236 $ ./ncnn_yolov4tiny_test input_image/input_img_00001.jpg NCNN Init time 6174.19ms file: input_image/input_img_00001.jpg i = 1 start out.h = 1 NCNN detection time 650.85ms 8 = 0.41767 at 1249.40 19.08 651.86 x 740.22 NCNN OpenCV draw result time 308.95ms file: input_image/input_img_00001.jpg i = 1 end

47th row: yolov4->opt.use_vulkan_compute = true; detection is Error.

mercury:/data/local/tmp/200811120236 $ ./ncnn_yolov4tiny_test input_image/input_img_00001.jpg [0 Adreno (TM) 506] queueC=0[3] queueG=0[3] queueT=0[3] [0 Adreno (TM) 506] bugsbn1=1 buglbia=0 bugcopc=0 bugihfa=0 [0 Adreno (TM) 506] fp16p=1 fp16s=0 fp16a=0 int8s=0 int8a=0 NCNN Init time 11329.38ms file: input_image/input_img_00001.jpg i = 1 start out.h = 1160 NCNN detection time 1696.75ms 1 = 0.33333 at 915.46 738.17 162.94 x 143.67 1 = 0.33333 at 841.61 738.17 162.94 x 143.67 1 = 0.33333 at 1063.15 738.17 162.94 x 143.67 1 = 0.33333 at 989.30 738.17 162.94 x 143.67 1 = 0.33333 at 767.76 738.17 162.94 x 143.67 1 = 0.33333 at 546.22 738.17 162.94 x 143.67 1 = 0.33333 at 472.38 738.17 162.94 x 143.67 1 = 0.33333 at 693.92 738.17 162.94 x 143.67 1 = 0.33333 at 620.07 738.17 162.94 x 143.67 1 = 0.33333 at 1136.99 738.17 162.94 x 143.67 1 = 0.33333 at 1653.92 738.17 162.94 x 143.67 1 = 0.33333 at 1580.07 738.17 162.94 x 143.67 1 = 0.33333 at 1801.61 738.17 162.94 x 143.67 1 = 0.33333 at 1727.76 738.17 162.94 x 143.67 1 = 0.33333 at 1506.22 738.17 162.94 x 143.67 1 = 0.33333 at 1284.69 738.17 162.94 x 143.67 1 = 0.33333 at 1210.84 738.17 162.94 x 143.67 1 = 0.33333 at 1432.38 738.17 162.94 x 143.67 1 = 0.33333 at 1358.53 738.17 162.94 x 143.67

`