|
| 1 | + |
| 2 | +#include "net.h" |
| 3 | +#include "mat.h" |
| 4 | + |
| 5 | +#include <opencv2/core/core.hpp> |
| 6 | +#include <opencv2/highgui/highgui.hpp> |
| 7 | +#include <opencv2/imgproc/imgproc.hpp> |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <random> |
| 11 | +#include <algorithm> |
| 12 | +#include <stdio.h> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | + |
| 16 | +using std::string; |
| 17 | +using std::vector; |
| 18 | +using cv::Mat; |
| 19 | + |
| 20 | + |
| 21 | +vector<vector<uint8_t>> get_color_map(); |
| 22 | +void inference(); |
| 23 | + |
| 24 | + |
| 25 | +int main(int argc, char** argv) { |
| 26 | + inference(); |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +void inference() { |
| 32 | + bool use_fp16 = false; |
| 33 | + // load model |
| 34 | + ncnn::Net mod; |
| 35 | +#if NCNN_VULKAN |
| 36 | + int gpu_count = ncnn::get_gpu_count(); |
| 37 | + if (gpu_count <= 0) { |
| 38 | + fprintf(stderr, "we do not have gpu device\n"); |
| 39 | + return; |
| 40 | + } |
| 41 | + mod.opt.use_vulkan_compute = 1; |
| 42 | + mod.set_vulkan_device(1); |
| 43 | +#endif |
| 44 | + mod.load_param("../models/model_v2_sim.param"); |
| 45 | + mod.load_model("../models/model_v2_sim.bin"); |
| 46 | + mod.opt.use_fp16_packed = use_fp16; |
| 47 | + mod.opt.use_fp16_storage = use_fp16; |
| 48 | + mod.opt.use_fp16_arithmetic = use_fp16; |
| 49 | + |
| 50 | + // load image, and copy to ncnn mat |
| 51 | + int oH{1024}, oW{2048}, n_classes{19}; |
| 52 | + float mean[3] = {0.3257f, 0.3690f, 0.3223f}; |
| 53 | + float var[3] = {0.2112f, 0.2148f, 0.2115f}; |
| 54 | + cv::Mat im = cv::imread("../../example.png"); |
| 55 | + if (im.empty()) { |
| 56 | + fprintf(stderr, "cv::imread failed\n"); |
| 57 | + return; |
| 58 | + } |
| 59 | + ncnn::Mat inp = ncnn::Mat::from_pixels_resize( |
| 60 | + im.data, ncnn::Mat::PIXEL_BGR, im.cols, im.rows, oW, oH); |
| 61 | + for (float &el : mean) el *= 255.; |
| 62 | + for (float &el : var) el = 1. / (255. * el); |
| 63 | + inp.substract_mean_normalize(mean, var); |
| 64 | + |
| 65 | + // set input, run, get output |
| 66 | + ncnn::Extractor ex = mod.create_extractor(); |
| 67 | + // ex.set_num_threads(1); |
| 68 | +#if NCNN_VULKAN |
| 69 | + ex.set_vulkan_compute(true); |
| 70 | +#endif |
| 71 | + |
| 72 | + ex.input("input_image", inp); |
| 73 | + ncnn::Mat out; |
| 74 | + ex.extract("preds", out); // output is nchw, as onnx, where here n=1 |
| 75 | + |
| 76 | + // generate colorful output, and dump |
| 77 | + vector<vector<uint8_t>> color_map = get_color_map(); |
| 78 | + Mat pred(cv::Size(oW, oH), CV_8UC3); |
| 79 | + for (int i{0}; i < oH; ++i) { |
| 80 | + uint8_t *ptr = pred.ptr<uint8_t>(i); |
| 81 | + for (int j{0}; j < oW; ++j) { |
| 82 | + // compute argmax |
| 83 | + int idx, offset, argmax{0}; |
| 84 | + float max; |
| 85 | + idx = i * oW + j; |
| 86 | + offset = oH * oW; |
| 87 | + max = out[idx]; |
| 88 | + for (int k{1}; k < n_classes; ++k) { |
| 89 | + idx += offset; |
| 90 | + if (max < out[idx]) { |
| 91 | + max = out[idx]; |
| 92 | + argmax = k; |
| 93 | + } |
| 94 | + } |
| 95 | + // color the result |
| 96 | + ptr[0] = color_map[argmax][0]; |
| 97 | + ptr[1] = color_map[argmax][1]; |
| 98 | + ptr[2] = color_map[argmax][2]; |
| 99 | + ptr += 3; |
| 100 | + } |
| 101 | + } |
| 102 | + cv::imwrite("out.png", pred); |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +vector<vector<uint8_t>> get_color_map() { |
| 108 | + vector<vector<uint8_t>> color_map(256, vector<uint8_t>(3)); |
| 109 | + std::minstd_rand rand_eng(123); |
| 110 | + std::uniform_int_distribution<uint8_t> u(0, 255); |
| 111 | + for (int i{0}; i < 256; ++i) { |
| 112 | + for (int j{0}; j < 3; ++j) { |
| 113 | + color_map[i][j] = u(rand_eng); |
| 114 | + } |
| 115 | + } |
| 116 | + return color_map; |
| 117 | +} |
0 commit comments