#include <array>
#include <vector>
int main()
{
/* 16表示由Input到backbone最后一个卷积层(此处称为featuremap)的下采样倍数,
即feature map上一个像素对应Input上16*16的区域 */
const int kBaseSize = 16;
const int kScaleNum = 3; // anchors的面积种类数,此处使用三种面积,即128*128, 256*256, 512*512
std::array<int, kScaleNum> scales{ 8, 16, 32 }; // Input上16*16的区域的(宽、高)单边放大倍数
std::array<int, kScaleNum> anchors_areas{ 0 }; // 3种anchors面积
for (int i = 0; i < kScaleNum; ++i)
{
anchors_areas[i] = pow(kBaseSize * scales[i], 2);
}
/* 假设backbone采用VGG,则feature map尺寸为50*38,参考https://zhuanlan.zhihu.com/p/31426458 */
const int kFeatMapWidth = 50;
const int kFeatMapHeight = 38;
/* Input尺度上每个anchor的中心点坐标 */
std::vector<std::vector<std::pair<float, float>>> input_anchor_centers(kFeatMapHeight); // first存储x,second存储y
for (int i = 0; i < kFeatMapHeight; ++i)
{
input_anchor_centers[i].resize(kFeatMapWidth);
}
for (int i = 0; i < kFeatMapHeight; ++i)
{
for (int j = 0; j < kFeatMapWidth; ++j)
{
input_anchor_centers[i][j].first = ((j + 1) * kBaseSize - 1) / 2.f;
input_anchor_centers[i][j].second = ((i + 1) * kBaseSize - 1) / 2.f;
}
}
/* 计算anchors,存储形式左上角坐标、右下角坐标 */
typedef struct _ANCHOR_RECTANGLE_
{
int tl_x;
int tl_y;
int br_x;
int br_y;
}rect;
const int kSinglePixelNum = 9; // feature map上每个像素对应的anchors数量
std::vector<rect> anchors(kFeatMapWidth * kFeatMapHeight * kSinglePixelNum);
const int kWHRatio = 3; // 同一面积矩形宽:高比种类[0.5, 1., 2.]
std::array<float, kWHRatio> ratios{ 0.5f, 1.f, 2.f };
unsigned ct = 0;
// 逐行逐列依次求取对应feature map上每个像素位置的9个anchors
for (int i = 0; i < kFeatMapHeight; ++i)
{
for (int j = 0; j < kFeatMapWidth; ++j)
{
for (int m = 0; m < kScaleNum; ++m) // 遍历每种面积
{
float area = anchors_areas[m];
for (int n = 0; n < kWHRatio; ++n) // 遍历当前面积的三种比例
{
float w = 0.f, h = 0.f;
if (0 == n) // ratios[0]==0.5f
{
w = round(sqrt(area / 2.f));
h = round(2 * w);
}
else if (1 == n) // ratios[1]==1.f
{
w = h = sqrt(area);
}
else if (2 == n) // ratios[2]==2.f
{
h = round(sqrt(area / 2.f));
w = round(2 * h);
}
int tl_x = input_anchor_centers[i][j].first - floor(w / 2);
int tl_y = input_anchor_centers[i][j].second - floor(h / 2);
int br_x = input_anchor_centers[i][j].first + floor( w / 2);
int br_y = input_anchor_centers[i][j].second + floor( h / 2);
anchors[ct++] = rect{ tl_x, tl_y, br_x, br_y };
}
}
}
}
system("pause");
return 0;
}
直观感受FasterRCNN的anchors是怎么生成的
最新推荐文章于 2022-11-01 11:56:01 发布
关键词由CSDN通过智能技术生成