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

相关文章

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链