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