Pytorch torchvision.utils.make_grid的一个坑

我在可视化CNN某层的feature map 的时候使用了这Pytorch torchvision.utils.make_grid 方法,结果在输出的时候发现有几个图完全黑了(数值过小)。我觉得不应该呀,Pytorch torchvision.utils.make_grid 有个 scale_each 函数,有它就说明将每个图单独正则化到了(0,1)区间。后来发现,原来哪些正常的图的范围是(-0.1 ~ 0.4),正则化没有问题,但是由于第3-5个图的范围是(1e-7 ~ 1e-9),相对于正则化代码的1e-5 太小了。原本这个 1e-5只是防止除以0加上的,现在反倒是影响到了正常的正则化
在这里插入图片描述

# 正则化源码
    if normalize is True:
        tensor = tensor.clone()  # avoid modifying tensor in-place
        if range is not None:
            assert isinstance(range, tuple), \
                "range has to be a tuple (min, max) if specified. min and max are numbers"

        def norm_ip(img, min, max):
            img.clamp_(min=min, max=max)
            img.add_(-min).div_(max - min + 1e-5)

        def norm_range(t, range):
            if range is not None:
                norm_ip(t, range[0], range[1])
            else:
                norm_ip(t, float(t.min()), float(t.max()))

        if scale_each is True:
            for t in tensor:  # loop over mini-batch dimension
                norm_range(t, range)
        else:
            norm_range(tensor, range)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值