[深度学习] labels2Dto3D 函数

labels2Dto3D 函数

函数代码:

def labels2Dto3D(labels, cell_size, add_dustbin=True):
    '''
    Change the shape of labels into 3D. Batch of labels.

    :param labels:
        tensor [batch_size, 1, H, W]
    :param cell_size:
        8
    :return:
         labels: tensors[batch_size, 65, Hc, Wc]
    '''
    batch_size, channel, H, W = labels.shape
    Hc, Wc = H // cell_size, W // cell_size
    space2depth = SpaceToDepth(8)
    labels = space2depth(labels)
    if add_dustbin:
        dustbin = labels.sum(dim=1)
        dustbin = 1 - dustbin
        dustbin[dustbin < 1.] = 0
        labels = torch.cat((labels, dustbin.view(batch_size, 1, Hc, Wc)), dim=1)
        ## norm
        dn = labels.sum(dim=1)
        labels = labels.div(torch.unsqueeze(dn, 1))
    return labels

逐句解析

batch_size, channel, H, W = labels.shape
Hc, Wc = H // cell_size, W // cell_size

batch_size=64,channel=1,H=120,W=160

Hc=15 Wc=20

space2depth = SpaceToDepth(8)
labels = space2depth(labels)

SpaceToDepth是一个类,创建了一个 SpaceToDepth对象,构造函数参数输入 8,进去看看:

https://blog.csdn.net/weixin_44179561/article/details/128058411?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128058411%22%2C%22source%22%3A%22weixin_44179561%22%7D

也就是说,(64,1,120,160)的labels 经过 space2depth 的处理,转换成了 (64,64,15,20)的labels

由于 add_dustbin = True 继续往下

dustbin = labels.sum(dim=1)
dustbin = 1 - dustbin
dustbin[dustbin < 1.] = 0
labels = torch.cat((labels, dustbin.view(batch_size, 1, Hc, Wc)), dim=1)

这几步需要结合起来理解,总的来说是在 labels 的64个通道后面接上一个表示无特征点的通道:

在这里插入图片描述
在这里插入图片描述

dn = labels.sum(dim=1)

dn:(64,15,20),65个通道的总和

labels = labels.div(torch.unsqueeze(dn, 1))

torch.unsqueeze(dn, 1):(64,1,15,20)

labels中每个通道都除以这个总和

labels:(64,65,15,20)

return labels 返回并 .float() 转换成浮点数,然后赋值给 lebels_3D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值