centernet笔记 - inference阶段后处理

2024-04-24 11:08

本文主要是介绍centernet笔记 - inference阶段后处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

预备知识,mxnet.ndarray的一些操作
shape_array

shape_array([[1,2,3,4], [5,6,7,8]]) = [2,4]  # 获取ndarry的shape
# Returns a 1D int64 array containing the shape of data.

split

Splits an array along a particular axis into multiple sub-arrays.
cc = [  1  80 128 128]
N, C, H, W = cc.split(num_outputs=4, axis=0)
# N = [1], C = [80], H = [128], W = [128]

topk

topk(data=None, axis=_Null, k=_Null, ret_typ=_Null, is_ascend=_Null, dtype=_Null, out=None, name=None, **kwargs)
# data : The input array
# axis : 从哪个维度选取topk,默认-1,即最后一个维度
# ret_typ : return type,可以选择返回值,或者index,或者both
# is_ascend : 是否逆序,默认为0,返回 top k largest

cast

Casts all elements of the input to a new type.
# 将array中元素转换为指定的类型
cast([0.9, 1.3], dtype='int32') = [0, 1]

slice_like

# Slices a region of the array like the shape of another array. 
x = [[  1.,   2.,   3.,   4.],[  5.,   6.,   7.,   8.],[  9.,  10.,  11.,  12.]]
y = [[  0.,   0.,   0.],[  0.,   0.,   0.]]
slice_like(x, y) = [[ 1.,  2.,  3.][ 5.,  6.,  7.]]
slice_like(x, y, axes=(0, 1)) = [[ 1.,  2.,  3.][ 5.,  6.,  7.]]
slice_like(x, y, axes=(0)) = [[ 1.,  2.,  3.,  4.][ 5.,  6.,  7.,  8.]]
slice_like(x, y, axes=(-1)) = [[  1.,   2.,   3.][  5.,   6.,   7.][  9.,  10.,  11.]]

expand_dims

# Inserts a new axis of size 1 into the array shape For example, given x with shape (2,3,4),
# then expand_dims(x, axis=1) will return a new array with shape (2,1,3,4).
mxnet.ndarray.expand_dims(data=None, axis=_Null, out=None, name=None, **kwargs)
# axis=-1, 则在末尾增加一个维度

tile

# 将array复制n次
x = [[1, 2],[3, 4]]
tile(x, reps=(2,3)) = [[ 1.,  2.,  1.,  2.,  1.,  2.],[ 3.,  4.,  3.,  4.,  3.,  4.],[ 1.,  2.,  1.,  2.,  1.,  2.],[ 3.,  4.,  3.,  4.,  3.,  4.]]

gather_nd
参考:https://discuss.gluon.ai/t/topic/2413/3

# 从array中,按照输入的索引取出其中的元素
data = [[0, 1], [2, 3]]
indices = [[1, 1, 0], [0, 1, 0]]
gather_nd(data, indices) = [2, 3, 0] # 取出(1,0),(1,1),(0,0)的元素

centernet训练与预测过程中,图片的几何变化
训练阶段:原图 -> (512,512) -> (128,128)
预测阶段:(128,128) -> (512,512) -> 原图

在inference阶段,模型的输出是在128x128的feature shape上,在feature map上得到的结果,需要乘以scale=4.0,才能映射到512x512上,接着,还必须经过512x512到原图的一个仿射变换,才能得到模型最终输出值

核心操作及解码
核心操作

# self.heatmap_nms = nn.MaxPool2D(pool_size=3, strides=1, padding=1)
heatmap = outs[0]
keep = F.broadcast_equal(self.heatmap_nms(heatmap), heatmap)
results = self.decoder(keep * heatmap, outs[1], outs[2])

outs为模型输出,在heatmap所在维度上做3x3的max pooling,把峰值点找出来
解码

    def hybrid_forward(self, F, x, wh, reg):"""Forward of decoder"""import pdbpdb.set_trace()# 这里假设batch_size = 4, resize w和h为512x512_, _, out_h, out_w = x.shape_array().split(num_outputs=4, axis=0) # 获取feature map的H,Wscores, indices = x.reshape((0, -1)).topk(k=self._topk, ret_typ='both') # 获取top_100的scores和indices,x nx80x128x128 -> nx(80x128x128), 某类的128x128元素连在一起indices = F.cast(indices, 'int64')topk_classes = F.cast(F.broadcast_div(indices,(out_h * out_w)),'float32')  #  根据indices,获取top_100对应的类别,0-79topk_indices = F.broadcast_mod(indices, (out_h * out_w))  # 获取在128x128平面拉平后的索引topk_ys = F.broadcast_div(topk_indices, out_w) # 1维索引恢复出2维平面上的ytopk_xs = F.broadcast_mod(topk_indices, out_w) # 1维索引恢复出2维平面上的xcenter = reg.transpose((0, 2, 3, 1)).reshape((0, -1, 2)) # shape: (4, 16384, 2)wh = wh.transpose((0, 2, 3, 1)).reshape((0, -1, 2)) # shape: (4, 16384, 2)batch_indices = F.cast(F.arange(256).slice_like(center, axes=(0)).expand_dims(-1).tile(reps=(1, self._topk)), 'int64') # shape: (4, 100), 依次为全0,全1,全2,全3reg_xs_indices = F.zeros_like(batch_indices, dtype='int64') # shape: (4, 100),全0reg_ys_indices = F.ones_like(batch_indices, dtype='int64') # shape: (4, 100),全1reg_xs = F.concat(batch_indices, topk_indices, reg_xs_indices, dim=0).reshape((3, -1)) # shape: (3, 400)‘’‘reg_xs = [[   0    0    0 ...    3    3    3][9664 8207 9425 ... 9639 5593 9044][   0    0    0 ...    0    0    0]]’‘’reg_ys = F.concat(batch_indices, topk_indices, reg_ys_indices, dim=0).reshape((3, -1))‘’‘reg_ys = [[   0    0    0 ...    3    3    3][9664 8207 9425 ... 9639 5593 9044][   1    1    1 ...    1    1    1]]’‘’xs = F.cast(F.gather_nd(center, reg_xs).reshape((-1, self._topk)), 'float32') # shape : (4, 100)ys = F.cast(F.gather_nd(center, reg_ys).reshape((-1, self._topk)), 'float32') # shape : (4, 100)topk_xs = F.cast(topk_xs, 'float32') + xstopk_ys = F.cast(topk_ys, 'float32') + ys # feature map上坐标+偏移 = 真实坐标w = F.cast(F.gather_nd(wh, reg_xs).reshape((-1, self._topk)), 'float32')  # noqah = F.cast(F.gather_nd(wh, reg_ys).reshape((-1, self._topk)), 'float32')  # noqahalf_w = w / 2half_h = h / 2results = [topk_xs - half_w,topk_ys - half_h,topk_xs + half_w,topk_ys + half_h]results = F.concat(*[tmp.expand_dims(-1) for tmp in results], dim=-1) # shape: (4, 100, 4)return topk_classes, scores, results * self._scale # (4, 100),(4, 100),(4, 100, 4)

由512x512映射到原图

affine_mat = get_post_transform(orig_width, orig_height, 512, 512) # 获取仿射变换矩阵
bbox[0:2] = affine_transform(bbox[0:2], affine_mat) # 对x1,y1做仿射变换
bbox[2:4] = affine_transform(bbox[2:4], affine_mat) # 对x2,y2做仿射变换

这篇关于centernet笔记 - inference阶段后处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/931580

相关文章

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

《offer来了》第二章学习笔记

1.集合 Java四种集合:List、Queue、Set和Map 1.1.List:可重复 有序的Collection ArrayList: 基于数组实现,增删慢,查询快,线程不安全 Vector: 基于数组实现,增删慢,查询快,线程安全 LinkedList: 基于双向链实现,增删快,查询慢,线程不安全 1.2.Queue:队列 ArrayBlockingQueue:

操作系统实训复习笔记(1)

目录 Linux vi/vim编辑器(简单) (1)vi/vim基本用法。 (2)vi/vim基础操作。 进程基础操作(简单) (1)fork()函数。 写文件系统函数(中等) ​编辑 (1)C语言读取文件。 (2)C语言写入文件。 1、write()函数。  读文件系统函数(简单) (1)read()函数。 作者本人的操作系统实训复习笔记 Linux

LVGL快速入门笔记

目录 一、基础知识 1. 基础对象(lv_obj) 2. 基础对象的大小(size) 3. 基础对象的位置(position) 3.1 直接设置方式 3.2 参照父对象对齐 3.3 获取位置 4. 基础对象的盒子模型(border-box) 5. 基础对象的样式(styles) 5.1 样式的状态和部分 5.1.1 对象可以处于以下状态States的组合: 5.1.2 对象

DDS信号的发生器(验证篇)——FPGA学习笔记8

前言:第一部分详细讲解DDS核心框图,还请读者深入阅读第一部分,以便理解DDS核心思想 三刷小梅哥视频总结! 小梅哥https://www.corecourse.com/lander 一、DDS简介         DDS(Direct Digital Synthesizer)即数字合成器,是一种新型的频率合成技术,具有低成本、低功耗、高分辨率、频率转换时间短、相位连续性好等优点,对数字信

数据库原理与安全复习笔记(未完待续)

1 概念 产生与发展:人工管理阶段 → \to → 文件系统阶段 → \to → 数据库系统阶段。 数据库系统特点:数据的管理者(DBMS);数据结构化;数据共享性高,冗余度低,易于扩充;数据独立性高。DBMS 对数据的控制功能:数据的安全性保护;数据的完整性检查;并发控制;数据库恢复。 数据库技术研究领域:数据库管理系统软件的研发;数据库设计;数据库理论。数据模型要素 数据结构:描述数据库

【软考】信息系统项目管理师(高项)备考笔记——信息系统项目管理基础

信息系统项目管理基础 日常笔记 项目的特点:临时性(一次性)、独特的产品、服务或成果、逐步完善、资源约束、目的性。 临时性是指每一个项目都有确定的开始和结束日期独特性,创造独特的可交付成果,如产品、服务或成果逐步完善意味着分步、连续的积累。例如,在项目早期,项目范围的说明是粗略的,随着项目团队对目标和可交付成果的理解更完整和深入时,项目的范围也就更具体和详细。 战略管理包括以下三个过程

【软考】信息系统项目管理师(高项)备考笔记——信息化与信息系统

信息化与信息系统 最近在备考信息系统项目管理师软考证书,特记录笔记留念,也希望可以帮到有需求的人。 因为这是从notion里导出来的,格式上可能有点问题,懒的逐条修改了,还望见谅! 日常笔记 核心知识 信息的质量属性:1.精确性 2.完整性 3.可靠性 4.及时性 5.经济性 6.可验证下 7.安全性 信息的传输技术(通常指通信、网络)是信息技术的核心。另外,噪声影响的是信道

flex布局学习笔记(flex布局教程)

前端笔试⾯试经常会问到:不定宽⾼如何⽔平垂直居中。最简单的实现⽅法就是flex布局,⽗元素加上如下代码即 可: display: flex; justify-content: center; align-items :center; 。下⾯详细介绍下flex布局吧。   2009年,W3C提出了 Flex布局,可以简便⼂完整⼂响应式地实现各种页⾯布局。⽬前已得到了所有浏览器的⽀持,这意味着,现