• aspect ratios:高宽比率

假设 window 的尺寸为:\((w, h)\),锚框的尺寸为:\((w_1, h_1)\),则有:

\[\begin{cases}
\frac{w_1h_1}{wh} = s^2\\
\frac{h_1}{w_1} = \frac{h}{w} r
\end{cases}
\]

可以化简为:

\[\begin{cases}
w_s = \frac{w_1}{s} = \frac{w}{\sqrt{r}} \\
h_s = \frac{h_1}{s} = h \sqrt{r}
\end{cases}
\]

我们可以有两种编程实现方式:

1 \(w=h\)

\[\begin{cases}
w_s = \frac{w_1}{s} = \frac{w}{\sqrt{r}} = \text{round}(\sqrt{\frac{wh}{r}})\\
h_s = \frac{h_1}{s} = h \sqrt{r} = \sqrt{whr} = \text{round}(w_s r)
\end{cases}
\]

编程实现:

import numpy as np

class AnchorBase:
def __init__(self, base_size, scales, ratios):
self.scales = np.array(scales) #
self.ratios = np.array(ratios) #
self.num_anchors = len(self.ratios) * len(self.scales) # 锚框的个数
self.base_size = base_size # 滑动窗口的大小
if isinstance(base_size, int):
self._w, self._h = [base_size]*2
elif len(base_size) == 2:
self._w, self._h = base_size
elif len(base_size) == 1:
self._w, self._h = base_size*2 self._anchor = np.array([1, 1, self._w, self._h]) - 1 @property
def anchor(self):
return self._anchor @anchor.setter
def anchor(self, new_anchor):
self._anchor = new_anchor @property
def w(self):
'''
锚框的宽度
'''
return self.anchor[2] - self.anchor[0] + 1 @property
def h(self):
'''
锚框的高度
'''
return self.anchor[3] - self.anchor[1] + 1 @property
def size(self):
'''
锚框的面积
'''
return self.w * self.h @property
def _whctrs(self):
"""
Return x center, and y center for an anchor (window). 锚框的中心坐标
"""
x_ctr = self.anchor[0] + 0.5 * (self.w - 1)
y_ctr = self.anchor[1] + 0.5 * (self.h - 1)
return np.array([x_ctr, y_ctr]) @staticmethod
def _coordinate(aspect, ctr):
'''
依据宽高组合计算锚框的坐标
'''
k = (aspect - 1) / 2
return np.concatenate([ctr - k, ctr + k], axis=1) class AnchorRCNN(AnchorBase):
def __init__(self, base_size, scales, ratios):
super().__init__(base_size, scales, ratios)
self.anchors = self.gen_anchors() @property
def ratio_aspects(self):
'''
依据 ratios 获取锚框的所有宽高组合
'''
size_ratios = self.size / self.ratios
ws = np.round(np.sqrt(size_ratios))
hs = np.round(ws * self.ratios)
return np.stack([ws, hs], axis=1) @property
def ratio_anchors(self):
return self._coordinate(self.ratio_aspects, self._whctrs) @property
def scale_aspects(self):
'''
依据 scales 获取锚框的所有宽高组合
'''
ws = self.w * self.scales
hs = self.h * self.scales
return np.stack([ws, hs], axis=1) @property
def scale_anchors(self):
return self._coordinate(self.scale_aspects, self._whctrs) def gen_anchors(self):
'''
获取最终的 base_anchors
'''
anchors = []
for anchor in self.ratio_anchors:
self.anchor = anchor
anchors.append(self.scale_anchors)
return np.concatenate(anchors)
scales = [8, 16, 32]  # 尺度,面积比
ratios = [0.5, 1, 2] # window(滑动窗口) 与锚框的面积的比率(aspect ratios)
base_size = 16 # 滑动窗口的大小 self = AnchorRCNN(base_size, scales, ratios) self.anchors
array([[ -84.,  -40.,   99.,   55.],
[-176., -88., 191., 103.],
[-360., -184., 375., 199.],
[ -56., -56., 71., 71.],
[-120., -120., 135., 135.],
[-248., -248., 263., 263.],
[ -36., -80., 51., 95.],
[ -80., -168., 95., 183.],
[-168., -344., 183., 359.]])
self.ratio_anchors
array([[-3. ,  2.5, 18. , 12.5],
[ 0. , 0. , 15. , 15. ],
[ 2.5, -3. , 12.5, 18. ]])

2

\[\begin{cases}
\frac{w_1}{w} = \frac{s}{\sqrt{r}} = \text{round}(\frac{s}{\sqrt{r}})\\
\frac{h_1}{h} = s \sqrt{r} = \text{round}(\frac{w_1}{w} r)
\end{cases}
\]

\[\begin{cases}
S = [s_1, s_1, \cdots, s_m]\\
R = [r_1, r_2, \cdots, r_n]
\end{cases}
\]

则有(下面的运算均是元素级别的元素):

\[\begin{cases}
W = (\frac{s_i}{\sqrt{r_j}}) = \frac{S}{\sqrt{R}}\\
H = (s_i \sqrt{r_j}) = W \cdot R
\end{cases}
\]

class Anchor(AnchorBase):
def __init__(self, base_size, scales, ratios):
super().__init__(base_size, scales, ratios) @property
def W(self):
'''
计算 w_1/ w
'''
W = self.scales[:, None] / np.sqrt(self.ratios)
return np.round(W) @property
def H(self):
'''
计算 h_1/ h
'''
H = self.W * self.ratios
return np.round(H) @property
def aspect(self):
'''
所有的宽高组合
'''
return np.stack([self.W.flatten(), self.H.flatten()], axis=1) @property
def base_anchors(self):
return self._coordinate(self.aspect, self._whctrs) @property
def anchors(self):
'''
获取最终的 base_anchors
'''
return self.base_anchors * np.array([self.w, self.h]*2)
scales = [8, 16, 32]  # 尺度,面积比
ratios = [0.5, 1, 2] # window(滑动窗口) 与锚框的面积的比率(aspect ratios)
base_size = [16, 8]
self = Anchor(base_size, scales, ratios) self.anchors
array([[  40.,    8.,  200.,   48.],
[ 64., 0., 176., 56.],
[ 80., -16., 160., 72.],
[ -56., -16., 296., 72.],
[ 0., -32., 240., 88.],
[ 40., -56., 200., 112.],
[-232., -56., 472., 112.],
[-128., -96., 368., 152.],
[ -56., -152., 296., 208.]])

Anchor 的两种编程实现的更多相关文章

  1. JAVA学习篇--JAVA两种编程模式控制

    在Drp项目中,解说了两种编程模式Model 1和Model2.以下是对这两种模式的简单理解.以及因为Model2是基于MVC架构的模式,就将我们易混淆的MVC与我们之前学的三层架构进行对照学习一下. ...

  2. Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发WebFlux 支持两种编程风(姿)格(势) 使用@Controller这种基于注解

    概述 什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要深入了解 Spring WebFlux, 首先要了知道 R ...

  3. Edit Distance问题在两种编程范式下的求解

    本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...

  4. [python]两种编程思维--面向过程和面向对象

    例如:eg:炒一份西红柿鸡蛋 一.面向过程 面向过程的编程思维,如下 二.面向对象 制作一台炒菜机器人,然后告诉机器人做一道西红柿炒鸡蛋.在这里,我们直接面对的是机器人,而非炒菜的过程,所以这里机器人 ...

  5. EF三种编程方式的区别Database first ,Model first ,code first

    首先对于EF中先出现的datebase  first和model first两种编程方式,其的区别根据字面意思很容易能够理解. datebase  first就是代表数据库优先,那么前提就是先创建数据 ...

  6. angular2系列教程(六)两种pipe:函数式编程与面向对象编程

    今天,我们要讲的是angualr2的pipe这个知识点. 例子

  7. Arduino下LCD1602综合探究(上)——1602的两种驱动方式,如何使LCD的控制编程变得更简单

    一.前言: LCD ( Liquid Crystal Display 的简称)液晶显示器,已经逐渐替代CRT成为主流的显示设备之一,因此也成为了单片机发烧友绕不过的话题之一:而LCD1602更是很多单 ...

  8. Reactor事件驱动的两种设计实现:面向对象 VS 函数式编程

    Reactor事件驱动的两种设计实现:面向对象 VS 函数式编程 这里的函数式编程的设计以muduo为例进行对比说明: Reactor实现架构对比 面向对象的设计类图如下: 函数式编程以muduo为例 ...

  9. [转] LBYL与EAFP两种防御性编程风格

    检查数据可以让程序更健壮,用术语来说就是防御性编程.检查数据的时候,有这样的两种不同的风格.LBYL:Look Before You Leap  EAFP:Easier to Ask Forgiven ...

随机推荐

  1. 手动创建binary log files和手动编辑binary log index file会有什么影响

    基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...

  2. mongodb系列~mongodb数据迁移

    一 简介:今天来聊聊mongo的数据迁移二 迁移   1 具体迁移命令   nohup mongodump --port --db dbname --collection tablename --qu ...

  3. python - class类 (五) 继承补充-子类继承父类属性/函数方法

    子类继承父类属性/函数方法: #方式一:(原生方式,不建议使用) class Dongwu(object): def __init__(self,name,sex,old): self.name = ...

  4. 12章 搜索框架ElasticSearch介绍和整合SpringBoot 4节课

    1.搜索引擎知识和搜索框架elasticsearch基本介绍     简介:通过京东电商 介绍什么是搜索引擎,和开源搜索框架ElasticSearch6.x新特性介绍 前言:介绍ES的主要特点和使用场 ...

  5. NSURLResponse下载

    // // ViewController.m // 05-NSURLConnestion(下载) // // Created by jerry on 15/10/24. // Copyright (c ...

  6. oaracel 函数_行转列

    wm_concat(varchar2) 组函数

  7. 【Python学习笔记】调整matplotlib的图例legend的位置

    有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...

  8. 转载:2.2.1 块配置项《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19626.html 块配置项由一个块配置项名和一对大括号组成.具体示例如下:events {-} http { upstream ba ...

  9. 解读使用Daisy-chain(菊花链)方式筛选一定范围内素数的代码

    go version go1.11 windows/amd64 本文为解读 参考链接1 中的 菊花链 一节 的示例程序,此程序和 参考链接2 中代码有些类似:前者有范围,后者是无限循环.清楚了 参考链 ...

  10. python在windows下安装

    打开python官方网站:https://www.python.org/downloads/ 点击下载 翻到底下的file目录下 选择对应的32,64位系统进行安装 一般来说选择Windows x86 ...