【问题标题】:Lua/Torch reshape tableLua/Torch 重塑表
【发布时间】: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


    【解决方案1】:

    您可以尝试使用函数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]
    

    【讨论】:

    • 列向量有 3x200x200=120,000 个条目。前 3 个值是第一个像素的 rgb 值,后 3 个值是第二个像素的 rgb 值,依此类推。我不需要将其重塑为 3 个元组的 200x200 矩阵。我该怎么做?
    • @Sibi 在这种情况下,输出可以这样改成图像:从第1列到第200*200列是图像的第一个通道,从第200*200列+1到第1列2*200*200为第二通道,从2*200*200+1到最后(3*200*200)为第三通道。我将编辑答案,以便更容易看到。
    猜你喜欢
    • 2016-06-23
    • 2017-11-09
    • 2016-03-24
    • 2015-06-11
    • 2018-02-16
    • 2016-07-27
    • 2016-03-25
    • 2023-03-28
    • 2016-09-05
    相关资源
    最近更新 更多