Towards-Realtime-MOT源代码学习之build_targets_thres()函数

2023-11-02 12:10

本文主要是介绍Towards-Realtime-MOT源代码学习之build_targets_thres()函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该函数用于获取真实的置信度、目标框与id,其中目标框为投射到18*10大小特征图上的目标框

def build_targets_thres(target, anchor_wh, nA, nC, nGh, nGw):ID_THRESH = 0.5FG_THRESH = 0.5BG_THRESH = 0.4nB = len(target)  # number of images in batchassert(len(anchor_wh)==nA)tbox = torch.zeros(nB, nA, nGh, nGw, 4).cuda()  # batch size, anchors, grid sizetconf = torch.LongTensor(nB, nA, nGh, nGw).fill_(0).cuda()tid = torch.LongTensor(nB, nA, nGh, nGw, 1).fill_(-1).cuda() 

传入进来的参数:nA=4,nC=1,nGh=10,nGw=18,anchor_wh=tensor([[ 2.6562,  7.9688],[ 3.7500, 11.2500],[ 5.3125, 13.1250],[10.6250, 10.0000]], device='cuda:0'),target=[tensor([         [0.0000e+00, 3.5300e+02, 9.1962e-02, 5.0159e-01, 5.3863e-02, 2.5566e-01],
        [0.0000e+00, 3.5500e+02, 6.0608e-01, 4.6625e-01, 5.1671e-02, 2.6615e-01],
        ...
        [0.0000e+00, 4.0100e+02, 4.5224e-02, 1.2969e-01, 4.6705e-02, 2.1583e-01],
        [0.0000e+00, 4.0200e+02, 1.0326e-01, 1.0715e-01, 3.7565e-02, 1.9927e-01]],
        device='cuda:0')],target为一个list,里面装的Tensor的shape为[36, 6],
nB = 1,表示batchsize
tbox.shape = torch.Size([1, 4, 10, 18, 4])
tconf.shape =  torch.Size([1, 4, 10, 18])
tid.shape = torch.Size([1, 4, 10, 18, 1])

for b in range(nB):t = target[b]t_id = t[:, 1].clone().long().cuda()t = t[:,[0,2,3,4,5]]nTb = len(t)  # number of targetsif nTb == 0:continue

进入for循环之后
t=target[0]=tensor([[0.0000e+00, 3.5300e+02, 9.1962e-02, 5.0159e-01, 5.3863e-02, 2.5566e-01],
                               ...
                              [0.0000e+00, 4.0200e+02, 1.0326e-01, 1.0715e-01, 3.7565e-02, 1.9927e-01]],
       device='cuda:0')
t.shape = torch.Size([36, 6])
t_id = tensor([353, 355, 356, 357, 358, 359, 366, 367, 368, 369, 378, 379, 382, 386,
                      388, 394, 401, 402, 353, 355, 356, 357, 358, 359, 366, 367, 368, 369,
                      378, 379, 382, 386, 388, 394, 401, 402], device='cuda:0')
t = t[:,[0,2,3,4,5]]=tensor([ [0.0000, 0.0920, 0.5016, 0.0539, 0.2557],
                                          ...
                                          [0.0000, 0.1033, 0.1072, 0.0376, 0.1993]], device='cuda:0')
nTb = 36

gxy, gwh = t[: , 1:3].clone() , t[:, 3:5].clone()
gxy[:, 0] = gxy[:, 0] * nGw
gxy[:, 1] = gxy[:, 1] * nGh
gwh[:, 0] = gwh[:, 0] * nGw
gwh[:, 1] = gwh[:, 1] * nGh

gxy = t[: , 1:3].clone()=tensor([ [0.0920, 0.5016],
                                                 ...,
                                                 [0.1033, 0.1072]], device='cuda:0')
gwh= t[:, 3:5].clone()=tensor([ [0.0539, 0.2557],
                                                ...,
                                                [0.0376, 0.1993]], device='cuda:0')
gxy.shape=gwh.shape=torch.Size([36, 2]),gxy,gwh均乘上nGh和nGw之后:
gxy=tensor([ [ 1.6553,  5.0159],
                     ...,
                     [ 1.8587,  1.0715]], device='cuda:0')
gwh=tensor([ [0.9695, 2.5566],
                      ...,
                      [0.6762, 1.9927]], device='cuda:0')

gxy[:, 0] = torch.clamp(gxy[:, 0], min=0, max=nGw -1)
gxy[:, 1] = torch.clamp(gxy[:, 1], min=0, max=nGh -1)

此时gxy=tensor([[ 1.6553,  5.0159],
                            ...
                            [ 1.8587,  1.0715]], device='cuda:0')

gt_boxes = torch.cat([gxy, gwh], dim=1)  # Shape Ngx4 (xc, yc, w, h)anchor_mesh = generate_anchor(nGh, nGw, anchor_wh)
anchor_list = anchor_mesh.permute(0,2,3,1).contiguous().view(-1, 4) 

gt_boxes=tensor([[ 1.6553,  5.0159,  0.9695,  2.5566],
                              ...,
                              [ 1.8587,  1.0715,  0.6762,  1.9927]], device='cuda:0')
anchor_mesh = tensor([[[[ 0.0000,  1.0000,  2.0000,  ..., 15.0000, 16.0000, 17.0000],
                                         ...,
                                         [ 0.0000,  1.0000,  2.0000,  ..., 15.0000, 16.0000, 17.0000]],
                                         ...,

                                         [[ 7.9688,  7.9688,  7.9688,  ...,  7.9688,  7.9688,  7.9688],
                                          ...,
                                          [ 7.9688,  7.9688,  7.9688,  ...,  7.9688,  7.9688,  7.9688]]],
                                          ...,
                                        [[[ 0.0000,  1.0000,  2.0000,  ..., 15.0000, 16.0000, 17.0000],
                                           ...,
                                           [ 0.0000,  1.0000,  2.0000,  ..., 15.0000, 16.0000, 17.0000]],
                                            ...,

                                          [[10.0000, 10.0000, 10.0000,  ..., 10.0000, 10.0000, 10.0000],
                                             ...,
                                           [10.0000, 10.0000, 10.0000,  ..., 10.0000, 10.0000, 10.0000]]]],
       device='cuda:0') 
anchor_mesh.shape= torch.Size([4, 4, 10, 18])
这里调用的generate_anchor函数如下:
anchor_list = tensor([[ 0.0000,  0.0000,  2.6562,  7.9688],
                                  ...,
                                  [17.0000,  9.0000, 10.6250, 10.0000]], device='cuda:0')
anchor_list.shape= torch.Size([720, 4])

iou_pdist = bbox_iou(anchor_list, gt_boxes)            # Shape (nA x nGh x nGw) x Ng
iou_max, max_gt_index = torch.max(iou_pdist, dim=1)    # Shape (nA x nGh x nGw), bothiou_map = iou_max.view(nA, nGh, nGw)       
gt_index_map = max_gt_index.view(nA, nGh, nGw)

这里调用了bbox_iou()函数计算iou,bbox_iou()函数如下:

iou_pdist = tensor([ [0.0016, 0.0000, 0.0465,  ..., 0.0000, 0.0857, 0.0000],
                                 ...,
                                [0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000]],device='cuda:0')
iou_pdist.shape = torch.Size([720, 36]),[720]个anchor和[36]个gt_boxes([4]表示目标框)分别求iou
iou_max.shape = 720,找出最大的720个iou值  
max_gt_index.shape=720,找出最大的720个iou值对应的索引    
iou_map.shape =   torch.Size([4, 10, 18]),此处的view函数用于重构张量的维度  
gt_index_map =torch.Size([4, 10, 18])

id_index = iou_map > ID_THRESH #将iou_map > ID_THRESH的位置置1,反之置0
fg_index = iou_map > FG_THRESH                                                    
bg_index = iou_map < BG_THRESH 
ign_index = (iou_map < FG_THRESH) * (iou_map > BG_THRESH)
#这一步是把fg_index作为索引,如fg_index中的一个元素为[0,0,1],等于指定tconf中的[0,0,1]元素
tconf[b][fg_index] = 1
tconf[b][bg_index] = 0
tconf[b][ign_index] = -1

id_index.shape = fg_index.shape =bg_index.shape = ign_index.shape =torch.Size([4, 10, 18])
tconf=tensor([[[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          ...,
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                          ...,

                         [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                           ...,
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]], device='cuda:0')
tconf.shape=torch.Size([1, 4, 10, 18])

gt_index = gt_index_map[fg_index]
gt_box_list = gt_boxes[gt_index]
gt_id_list = t_id[gt_index_map[id_index]]

gt_index = tensor([], device='cuda:0', dtype=torch.int64)
gt_box_list = tensor([], device='cuda:0', size=(0, 4)),得到符合条件的gt_boxes
gt_id_list = tensor([], device='cuda:0', dtype=torch.int64),

if torch.sum(fg_index) > 0:tid[b][id_index] =  gt_id_list.unsqueeze(1)fg_anchor_list = anchor_list.view(nA, nGh, nGw, 4)[fg_index] delta_target = encode_delta(gt_box_list, fg_anchor_list)tbox[b][fg_index] = delta_target

这里未进入if循环

return tconf, tbox, tid

最后将真实的置信度tconf、真实的目标框tbox、真实的id即tid返回

这篇关于Towards-Realtime-MOT源代码学习之build_targets_thres()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与