MCPcopy Create free account
hub / github.com/AllentDan/LibtorchTutorials / DecodeBox

Function DecodeBox

lesson7-Detection/src/utils/util.cpp:4–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include "readfile.h"
3
4torch::Tensor DecodeBox(torch::Tensor input, torch::Tensor anchors, int num_classes, int img_size[])
5{
6 int num_anchors = anchors.sizes()[0];
7 int bbox_attrs = 5 + num_classes;
8 int batch_size = input.sizes()[0];
9 int input_height = input.sizes()[2];
10 int input_width = input.sizes()[3];
11 //���㲽��
12 //ÿһ���������Ӧԭ����ͼƬ�϶��ٸ����ص�
13 //���������Ϊ13x13�Ļ���һ��������Ͷ�Ӧԭ����ͼƬ�ϵ�32�����ص�
14 //416 / 13 = 32
15 auto stride_h = img_size[1] / input_height;
16 auto stride_w = img_size[0] / input_width;
17 //�������ijߴ�������������С����ʽ
18 //�������������������϶�Ӧ�Ŀ���
19 auto scaled_anchors = anchors.clone();
20 scaled_anchors.select(1, 0) = scaled_anchors.select(1, 0) / stride_w;
21 scaled_anchors.select(1, 1) = scaled_anchors.select(1, 1) / stride_h;
22
23 //bs, 3 * (5 + num_classes), 13, 13->bs, 3, 13, 13, (5 + num_classes)
24 //cout << "begin view"<<input.sizes()<<endl;
25 auto prediction = input.view({ batch_size, num_anchors,bbox_attrs, input_height, input_width }).permute({ 0, 1, 3, 4, 2 }).contiguous();
26 //cout << "end view" << endl;
27 //����������λ�õĵ�������
28 auto x = torch::sigmoid(prediction.select(-1, 0));
29 auto y = torch::sigmoid(prediction.select(-1, 1));
30 //�����Ŀ��ߵ�������
31 auto w = prediction.select(-1, 2); // Width
32 auto h = prediction.select(-1, 3); // Height
33
34 //������Ŷȣ��Ƿ�������
35 auto conf = torch::sigmoid(prediction.select(-1, 4));
36 //�������Ŷ�
37 auto pred_cls = torch::sigmoid(prediction.narrow(-1, 5, num_classes));// Cls pred.
38
39 auto LongType = x.clone().to(torch::kLong).options();
40 auto FloatType = x.options();
41
42 //����������������ģ��������Ͻ� batch_size, 3, 13, 13
43 auto grid_x = torch::linspace(0, input_width - 1, input_width).repeat({ input_height, 1 }).repeat(
44 { batch_size * num_anchors, 1, 1 }).view(x.sizes()).to(FloatType);
45 auto grid_y = torch::linspace(0, input_height - 1, input_height).repeat({ input_width, 1 }).t().repeat(
46 { batch_size * num_anchors, 1, 1 }).view(y.sizes()).to(FloatType);
47
48 //���������Ŀ���
49 auto anchor_w = scaled_anchors.to(FloatType).narrow(1, 0, 1);
50 auto anchor_h = scaled_anchors.to(FloatType).narrow(1, 1, 1);
51 anchor_w = anchor_w.repeat({ batch_size, 1 }).repeat({ 1, 1, input_height * input_width }).view(w.sizes());
52 anchor_h = anchor_h.repeat({ batch_size, 1 }).repeat({ 1, 1, input_height * input_width }).view(h.sizes());
53
54 //������������������������
55 auto pred_boxes = torch::randn_like(prediction.narrow(-1, 0, 4)).to(FloatType);
56 pred_boxes.select(-1, 0) = x + grid_x;
57 pred_boxes.select(-1, 1) = y + grid_y;
58 pred_boxes.select(-1, 2) = w.exp() * anchor_w;
59 pred_boxes.select(-1, 3) = h.exp() * anchor_h;
60
61 //���ڽ��������Ϊ�����416x416�Ĵ�С

Callers 2

PredictMethod · 0.85
PredictFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected