Tensor must be 4-D with last dim 1, 3, or 4,bug记录

在学习PyTorch的过程中遇到坑,使用torchvision.utils.make_grid处理图片后得到的Tensor缺少batch size维度,不适用于TensorFlow的tf.summary.image。解决方案是利用tf.expand_dims增加维度。此外,文章还提到了torchvision.utils.save_image的用法。
摘要由CSDN通过智能技术生成

torch学习入坑(1)
记录一下第一个坑,防止自己忘记。
在用torchvision.utils.make_grid处理完图片之后,维度会变为三维,如果需要使用tf.summary.image ,需要增加batch size 的那一维,需要用到的函数为 tf.expand_dims。

注意:

1、torchvision.utils.make_grid函数
输入:Tensor of shape (B x C x H x W)
输出:多个图片拼接成的一个大图 三维 没有batch size 那一维
like this:
batch size 16

以下解释来自官网
链接http://pytorch.org/docs/master/torchvision/utils.html
torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)
scale_each=False, pad_value=0)

作用:Make a grid of images.
Parameters:

    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, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值