detect_yolox.cpp 9.9 KB
Newer Older
M
Megvii Engine Team 已提交
1
#include <thread>
2
#include "example.h"
M
Megvii Engine Team 已提交
3 4 5 6 7 8 9 10 11 12 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
#if LITE_BUILD_WITH_MGE
#include <cstdio>

#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#define STB_IMAGE_RESIZE_STATIC
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"

#define STB_IMAGE_WRITE_STATIC
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#define NMS_THRESH       0.25
#define BBOX_CONF_THRESH 0.6

constexpr int INPUT_W = 640;
constexpr int INPUT_H = 640;

using namespace lite;
using namespace example;

namespace {

void preprocess_image(
        uint8_t* image, const int width, const int height, const int channel,
        std::shared_ptr<Tensor> tensor) {
    auto layout = tensor->get_layout();
    for (size_t i = 0; i < layout.ndim; i++) {
        printf("model input shape[%zu]=%zu \n", i, layout.shapes[i]);
    }

    //! resize to target shape
    float r = std::min(INPUT_W / (width * 1.0), INPUT_H / (height * 1.0));
    int unpad_w = r * width;
    int unpad_h = r * height;

    std::shared_ptr<std::vector<uint8_t>> resize_int8 =
            std::make_shared<std::vector<uint8_t>>(unpad_w * unpad_h * channel);
    stbir_resize_uint8(
            image, width, height, 0, resize_int8->data(), unpad_w, unpad_h, 0, channel);

    std::shared_ptr<std::vector<uint8_t>> padded;
    if (unpad_h != INPUT_H || unpad_w != INPUT_W) {
        padded = std::make_shared<std::vector<uint8_t>>(
                INPUT_H * INPUT_W * channel, 114);
        for (int h = 0; h < unpad_h; h++) {
            for (int w = 0; w < unpad_w; w++) {
                for (int c = 0; c < channel; c++) {
                    (*padded)[h * INPUT_W * channel + w * channel + c] =
                            (*resize_int8)[h * unpad_w * channel + w * channel + c];
                }
            }
        }
    } else {
        padded = resize_int8;
    }

    tensor->set_layout({{1, 3, 640, 640}, 4});

    std::vector<float> mean = {0.485, 0.456, 0.406};
    std::vector<float> std = {0.229, 0.224, 0.225};

    //! convert form rgb to bgr, relayout from hwc to chw, normalization copy to tensor
    float* in_data = static_cast<float*>(tensor->get_memory_ptr());
    size_t pixels = INPUT_H * INPUT_W;
    for (size_t i = 0; i < pixels; i++) {
        in_data[i] = (padded->at(i * channel + 0) / 255.0f - mean[0]) / std[0];
        in_data[i + 1 * pixels] =
                (padded->at(i * channel + 1) / 255.0f - mean[1]) / std[1];
        in_data[i + 2 * pixels] =
                (padded->at(i * channel + 2) / 255.0f - mean[2]) / std[2];
    }
}

struct Rect {
    float x;
    float y;
    float height;
    float width;

    float area() const { return height * width; }

    Rect operator&(Rect other) const {
        Rect ret;
        float x_start = std::max(x, other.x);
        float x_end = std::min(x + width, other.width);
        ret.x = x_start;
        ret.width = (x_end - x_start) > 0 ? x_end - x_start : 0;

        float y_start = std::max(y, other.y);
        float y_end = std::min(y + height, other.height);
        ret.y = y_start;
        ret.height = (y_end - y_start) > 0 ? y_end - y_start : 0;
        return ret;
    }
};

struct Object {
    Rect rect;
    int label;
    float prob;
};

struct GridAndStride {
    int grid0;
    int grid1;
    int stride;
};

static void generate_grids_and_stride(
        const int target_size, std::vector<int>& strides,
        std::vector<GridAndStride>& grid_strides) {
    for (auto stride : strides) {
        int num_grid = target_size / stride;
        for (int g1 = 0; g1 < num_grid; g1++) {
            for (int g0 = 0; g0 < num_grid; g0++) {
                grid_strides.push_back((GridAndStride){g0, g1, stride});
            }
        }
    }
}

static void generate_yolox_proposals(
        std::vector<GridAndStride> grid_strides, const float* feat_ptr,
        float prob_threshold, std::vector<Object>& objects) {
    const int num_class = 80;
    const int num_anchors = grid_strides.size();

    for (int anchor_idx = 0; anchor_idx < num_anchors; anchor_idx++) {
        const int grid0 = grid_strides[anchor_idx].grid0;
        const int grid1 = grid_strides[anchor_idx].grid1;
        const int stride = grid_strides[anchor_idx].stride;

        const int basic_pos = anchor_idx * 85;

        float x_center = (feat_ptr[basic_pos + 0] + grid0) * stride;
        float y_center = (feat_ptr[basic_pos + 1] + grid1) * stride;
        float w = exp(feat_ptr[basic_pos + 2]) * stride;
        float h = exp(feat_ptr[basic_pos + 3]) * stride;
        float x0 = x_center - w * 0.5f;
        float y0 = y_center - h * 0.5f;

        float box_objectness = feat_ptr[basic_pos + 4];
        for (int class_idx = 0; class_idx < num_class; class_idx++) {
            float box_cls_score = feat_ptr[basic_pos + 5 + class_idx];
            float box_prob = box_objectness * box_cls_score;
            if (box_prob > prob_threshold) {
                Object obj;
                obj.rect.x = x0;
                obj.rect.y = y0;
                obj.rect.width = w;
                obj.rect.height = h;
                obj.label = class_idx;
                obj.prob = box_prob;

                objects.push_back(obj);
            }

        }  // class loop

    }  // point anchor loop
}

void qsort_descent_inplace(std::vector<Object>& faceobjects, int left, int right) {
    int i = left;
    int j = right;
    float p = faceobjects[(left + right) / 2].prob;

    while (i <= j) {
        while (faceobjects[i].prob > p)
            i++;

        while (faceobjects[j].prob < p)
            j--;

        if (i <= j) {
            // swap
            std::swap(faceobjects[i], faceobjects[j]);

            i++;
            j--;
        }
    }
    if (left < j)
        qsort_descent_inplace(faceobjects, left, j);
    if (i < right)
        qsort_descent_inplace(faceobjects, i, right);
}

void qsort_descent_inplace(std::vector<Object>& objects) {
    if (objects.empty())
        return;
    qsort_descent_inplace(objects, 0, objects.size() - 1);
}

inline float intersection_area(const Object& a, const Object& b) {
    Rect inter = a.rect & b.rect;
    return inter.area();
}

void nms_sorted_bboxes(
        const std::vector<Object>& faceobjects, std::vector<int>& picked,
        float nms_threshold) {
    picked.clear();

    const int n = faceobjects.size();

    std::vector<float> areas(n);
    for (int i = 0; i < n; i++) {
        areas[i] = faceobjects[i].rect.area();
    }

    for (int i = 0; i < n; i++) {
        const Object& a = faceobjects[i];

        int keep = 1;
        for (int j = 0; j < (int)picked.size(); j++) {
            const Object& b = faceobjects[picked[j]];

            // intersection over union
            float inter_area = intersection_area(a, b);
            float union_area = areas[i] + areas[picked[j]] - inter_area;
            // float IoU = inter_area / union_area
            if (inter_area / union_area > nms_threshold)
                keep = 0;
        }

        if (keep)
            picked.push_back(i);
    }
}

void decode_outputs(
        const float* prob, std::vector<Object>& objects, float scale, const int img_w,
        const int img_h) {
    std::vector<Object> proposals;
    std::vector<int> strides = {8, 16, 32};
    std::vector<GridAndStride> grid_strides;

    generate_grids_and_stride(INPUT_W, strides, grid_strides);
    generate_yolox_proposals(grid_strides, prob, BBOX_CONF_THRESH, proposals);
    qsort_descent_inplace(proposals);

    std::vector<int> picked;
    nms_sorted_bboxes(proposals, picked, NMS_THRESH);
    int count = picked.size();
    objects.resize(count);

    for (int i = 0; i < count; i++) {
        objects[i] = proposals[picked[i]];

        // adjust offset to original unpadded
        float x0 = (objects[i].rect.x) / scale;
        float y0 = (objects[i].rect.y) / scale;
        float x1 = (objects[i].rect.x + objects[i].rect.width) / scale;
        float y1 = (objects[i].rect.y + objects[i].rect.height) / scale;

        // clip
        x0 = std::max(std::min(x0, (float)(img_w - 1)), 0.f);
        y0 = std::max(std::min(y0, (float)(img_h - 1)), 0.f);
        x1 = std::max(std::min(x1, (float)(img_w - 1)), 0.f);
        y1 = std::max(std::min(y1, (float)(img_h - 1)), 0.f);

        objects[i].rect.x = x0;
        objects[i].rect.y = y0;
        objects[i].rect.width = x1 - x0;
        objects[i].rect.height = y1 - y0;
    }
}

void draw_objects(
        uint8_t* image, int width, int height, int channel,
        const std::vector<Object>& objects) {
279 280 281 282
    (void)image;
    (void)width;
    (void)height;
    (void)channel;
M
Megvii Engine Team 已提交
283 284 285 286 287 288 289 290
    for (size_t i = 0; i < objects.size(); i++) {
        const Object& obj = objects[i];

        printf("Object: %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);
    }
}

291
bool detect_yolox(const Args& args) {
M
Megvii Engine Team 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    std::string network_path = args.model_path;
    std::string input_path = args.input_path;

    int width, height, channel;
    uint8_t* image = stbi_load(input_path.c_str(), &width, &height, &channel, 0);
    printf("Input image %s with height=%d, width=%d, channel=%d\n", input_path.c_str(),
           width, height, channel);

    //! create and load the network
    std::shared_ptr<Network> network = std::make_shared<Network>();
    network->load_model(network_path);
    //! set input data to input tensor

    auto input_tensor = network->get_io_tensor("data");

    preprocess_image(image, width, height, channel, input_tensor);

    network->forward();
    network->wait();

    float* predict_ptr =
            static_cast<float*>(network->get_output_tensor(0)->get_memory_ptr());

    float scale = std::min(INPUT_W / (width * 1.0), INPUT_H / (height * 1.0));
    std::vector<Object> objects;
    decode_outputs(predict_ptr, objects, scale, width, height);

    draw_objects(image, width, height, channel, objects);

    stbi_image_free(image);
    return 0;
}
324 325 326
}  // namespace

REGIST_EXAMPLE("detect_yolox", detect_yolox);
M
Megvii Engine Team 已提交
327 328 329 330

#endif

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}