【发布时间】:2016-11-23 05:50:37
【问题描述】:
我已经加载了一个200x200 rgb 图像,并通过神经网络将其重塑为1x(200x200x3) 向量,使用
img1=torch.reshape(img,1,image_size*image_size*3)
我得到的输出也是一个1x(200x200x3) 向量。如何将其重塑为大小为200x200 的 rgb 图像以便打印?
【问题讨论】:
标签: arrays lua lua-table torch
我已经加载了一个200x200 rgb 图像,并通过神经网络将其重塑为1x(200x200x3) 向量,使用
img1=torch.reshape(img,1,image_size*image_size*3)
我得到的输出也是一个1x(200x200x3) 向量。如何将其重塑为大小为200x200 的 rgb 图像以便打印?
【问题讨论】:
标签: arrays lua lua-table torch
您可以尝试使用函数permute(x, y, z)将图像更改为3x200x200的形状,然后您应该可以使用itorch.image(your_image)打印它。字母x, y, z是Tensor中列的索引。这里有一个例子。
x = torch.Tensor(3,4,2,5)
> x:size()
3
4
2
5
[torch.LongStorage of size 4]
y = x:permute(2,3,1,4) -- equivalent to y = x:transpose(1,3):transpose(1,2)
> y:size()
4
2
3
5
[torch.LongStorage of size 4]
在此步骤之后,只需执行与您相同的操作即可。假设我们的 img_size 是 5。
th> t = torch.Tensor(5,5,3)
[0.0001s]
th> t = t:permute(3,1,2)
[0.0001s]
th> t:size()
3
5
5
[torch.LongStorage of size 3]
[0.0001s]
th> t[{2}]:fill(2)
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
[torch.DoubleTensor of size 5x5]
[0.0003s]
th> t[{3}]:fill(3)
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
[torch.DoubleTensor of size 5x5]
[0.0004s]
th> w = t:reshape(1,t:size(2)*t:size(3)*3)
[0.0001s]
th> w
Columns 1 to 26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
Columns 27 to 52
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3
Columns 53 to 75
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[torch.DoubleTensor of size 1x75]
th> x = w:reshape(3,5,5)
[0.0001s]
th> x
(1,.,.) =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
(2,.,.) =
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
(3,.,.) =
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
[torch.DoubleTensor of size 3x5x5]
【讨论】: