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 就对了。同理,竖直方向上也是这样处理的。

最新文章

  1. 关于vue.js中class与style绑定的学习
  2. UML 2.0(装载)
  3. 针对安卓java入门:数据类型
  4. PPT美化大师
  5. MCS-51系列和80C51系列单片机是否相同
  6. FILTER 哪里没展开改哪里
  7. Git基础教程(一)
  8. MyBatis之ObjectFactory
  9. tensorflow官方文档中的sub 和mul中的函数已经在API中改名了
  10. NuGet的使用、部署、搭建私有服务
  11. HTML中padding和margin的区别和用法
  12. 【转】CentOS 6.3(x86_32)下安装Oracle 10g R2
  13. Notepad++ 运行java(转)
  14. Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
  15. css中,在高度已知,写出三栏布局,其中左栏、右栏宽度各位300px,中间自适应
  16. C++版 - 剑指offer 面试题31:连续子数组的最大和 题解
  17. Docker命令详解(build篇)
  18. Linux 基础目录和命令
  19. (数据科学学习手札10)系统聚类实战(基于R)
  20. 【算法】CDQ分治 -- 三维偏序 & 动态逆序对

热门文章

  1. thinkPHP 全局函数
  2. 18-----BBS论坛
  3. spring boot——常用注解
  4. java——异常类、异常捕获、finally、异常抛出、自定义异常
  5. Hive 变量和属性
  6. vue写的ToDoList
  7. g++ 出现 undefined reference to ......
  8. [转]asp.net URL中包含中文参数造成乱码的解决方法
  9. jq实例
  10. System.Net.Mail