zoukankan      html  css  js  c++  java
  • [Pytorch] pytorch笔记 <一>

    pytorch笔记 - torchvision.utils.make_grid

    torchvision.utils.make_grid

    torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale_each=False)
    # 将一小batch图片变为一张图。nrow表示每行多少张图片的数量。
    
    # 给一个batch为4的图片,h和w分别为32,channel为3,看看结果
    images,labels = dataiter.next()
    print(images.shape) 
    #torch.Size([4, 3, 32, 32]) bchw
    print(torchvision.utils.make_grid(images).shape) 
    #torch.Size([3, 36, 138])
    

    怎么理解这个输出结果呢?第一个dim当然就是channel,因为合并成一张图片了嘛,所以batch这个维度就融合了,变成了chw,这里c还是原来的channel数,h比原来增加了4,w = 32*4 + 10,c很好理解,那么为什么h增加了4,w增加了10呢?

    我想办法把batch_size调整成了3,结果如下:

    #torch.Size([3, 3, 32, 32])
    #torch.Size([3, 36, 104])
    

    通过结果才看到,原来函数参数里还有个padding和nrow。直接去官网查文档:

    • tensor (Tensor or list) – 4D mini-batch Tensor of shape (B x C x H x W) or a list of images all of the same size.
    • nrow (int, optional) – Number of images displayed in each row of the grid. The Final grid size is (B / nrow, nrow). Default is 8.
    • padding (int, optional) – amount of padding. Default is 2.
    • normalize (bool, optional) – If True, shift the image to the range (0, 1), by subtracting the minimum and dividing by the maximum pixel value.
    • range (tuple, optional) – tuple (min, max) where min and max are numbers, then these numbers are used to normalize the image. By default, min and max are computed from the tensor.
    • scale_each (bool, optional) – If True, scale each image in the batch of images separately rather than the (min, max) over all images.
    • pad_value (float, optional) – Value for the padded pixels.

    很明显,当batch为3的时候,w应该为3*32 = 96,但是我们考虑到每张图片的padding其实是2,因此,每张图片其实变成了36*36的图片,所以最终应该为w = 36/* 3 =108才对呀?

    显然上面的想法还是不对,思考了一会,算是想明白了。

    三张图片,padding在水平方向并没有每张图片都padding,而是两张图片之间只有一个padding,这样3张图片空隙有两个,加上最左和最右,水平方向上其实是4* 2 =8,所以w增加了8,这样96 + 8 = 104 就对了。同理,竖直方向上也是这样处理的。

  • 相关阅读:
    推荐:《TestDrive ASP.NET MVC》 节选与作者访谈
    30天敏捷结果(15):让自己处于宁静状态
    MDSF:如何使用GMF来做TOGAF建模工具
    强烈推荐:好书、好人、好谚语
    推荐:50个加强表单的jQuery插件
    101与金根回顾敏捷个人:(1)基于MBTI模型发现你的职业性格
    30天敏捷结果(19):你在为谁做事?
    30天敏捷生活(13):获得他人的反馈
    SourceForge.net上的新项目(2005/07/05)
    Open License,开源的许可生成器,实现你自己的许可管理器/应用
  • 原文地址:https://www.cnblogs.com/aoru45/p/10629025.html
Copyright © 2011-2022 走看看