直观感受FasterRCNN的anchors是怎么生成的

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值