(pytorch)我想将[0 255]整数张量标准化为[0 1]浮点张量

问题描述 投票:1回答:1

我想将[0 255]整数张量标准化为[0 1]浮点张量。

我使用cifar10数据集,并希望处理整数图像张量。所以我让它们成为整数张量加载数据集时,我使用了“ transforms.ToTensor()”,因此将值设置为[0 1] float

tensor([[[0.4588, 0.4588, 0.4588,  ..., 0.4980, 0.4980, 0.5020],
         [0.4706, 0.4706, 0.4706,  ..., 0.5098, 0.5098, 0.5137],
         [0.4824, 0.4824, 0.4824,  ..., 0.5216, 0.5216, 0.5294],
         ...,
         [0.3098, 0.3020, 0.2863,  ..., 0.4549, 0.3608, 0.3137],
         [0.2902, 0.2902, 0.2902,  ..., 0.4431, 0.3333, 0.3020],
         [0.2706, 0.2941, 0.2941,  ..., 0.4157, 0.3529, 0.3059]],

        [[0.7725, 0.7725, 0.7725,  ..., 0.7569, 0.7569, 0.7608],
         [0.7765, 0.7765, 0.7765,  ..., 0.7608, 0.7608, 0.7686],
         [0.7765, 0.7765, 0.7765,  ..., 0.7608, 0.7608, 0.7725],
         ...,
         [0.6510, 0.6314, 0.6078,  ..., 0.6941, 0.6510, 0.6392],
         [0.6314, 0.6235, 0.6118,  ..., 0.6784, 0.6196, 0.6275],
         [0.6157, 0.6235, 0.6157,  ..., 0.6549, 0.6431, 0.6314]],

使它们成为[0 255]整数张量。

temp = np.floor(temp_images*256)
temp_int = torch.tensor(temp, dtype=torch.int32)
temp_images = torch.clamp(temp, 0, 255)

结果是

torch.IntTensor
tensor([[[[ 94., 100., 100.,  ...,  98., 100., 102.],
          [ 86., 100., 101.,  ...,  83.,  91., 103.],
          [ 90., 100.,  99.,  ...,  80.,  66.,  86.],
          ...,
          [ 92.,  92.,  90.,  ...,  77., 107., 119.],
          [ 76.,  91., 100.,  ...,  95., 158., 170.],
          [ 86.,  83.,  87.,  ...,  97., 176., 205.]],

         [[105., 111., 111.,  ..., 109., 112., 113.],
          [ 97., 111., 112.,  ...,  94., 102., 114.],
          [101., 111., 110.,  ...,  90.,  77.,  97.],
          ...,
          [111., 110., 108.,  ...,  88., 120., 131.],
          [ 95., 108., 114.,  ..., 105., 165., 172.],
          [106., 100., 101.,  ..., 108., 183., 206.]],

         [[ 62.,  68.,  68.,  ...,  66.,  68.,  70.],
          [ 55.,  69.,  70.,  ...,  51.,  59.,  71.],
          [ 59.,  69.,  68.,  ...,  48.,  34.,  54.],
          ...,
          [ 59.,  59.,  56.,  ...,  54.,  95., 107.],
          [ 49.,  61.,  66.,  ...,  76., 152., 166.],
          [ 61.,  55.,  54.,  ...,  73., 170., 206.]]],

在将它们转发到网络之前,我想再次使它们[0 1]浮动张量。

所以我尝试了

transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))

但是,结果没有被标准化为[0 1],而是变得更大了...!

tensor([[[117., 117., 117.,  ..., 127., 127., 128.],
         [120., 120., 120.,  ..., 130., 130., 131.],
         [123., 123., 123.,  ..., 133., 133., 135.],
         ...,
         [ 79.,  77.,  73.,  ..., 116.,  92.,  80.],
         [ 74.,  74.,  74.,  ..., 113.,  85.,  77.],
         [ 69.,  75.,  75.,  ..., 106.,  90.,  78.]],

        [[197., 197., 197.,  ..., 193., 193., 194.],
         [198., 198., 198.,  ..., 194., 194., 196.],
         [198., 198., 198.,  ..., 194., 194., 197.],

tensor([[[233., 233., 233.,  ..., 253., 253., 255.],
         [239., 239., 239.,  ..., 259., 259., 261.],
         [245., 245., 245.,  ..., 265., 265., 269.],
         ...,
         [157., 153., 145.,  ..., 231., 183., 159.],
         [147., 147., 147.,  ..., 225., 169., 153.],
         [137., 149., 149.,  ..., 211., 179., 155.]],

        [[393., 393., 393.,  ..., 385., 385., 387.],
         [395., 395., 395.,  ..., 387., 387., 391.],
         [395., 395., 395.,  ..., 387., 387., 393.],
         ...,
         [331., 321., 309.,  ..., 353., 331., 325.],
         [321., 317., 311.,  ..., 345., 315., 319.],
         [313., 317., 313.,  ..., 333., 327., 321.]],

我如何将[0 255]整数张量标准化为[0 1]浮点张量?] >>

我想将[0 255]整数张量标准化为[0 1]浮点张量。我使用cifar10数据集,并希望处理整数图像张量。所以我在加载数据集时使它们成为整数张量,所以我使用了“ ...

python casting pytorch tensor normalize
1个回答
0
投票

问题是您似乎误解了transforms.Normalize的功能。引用PyTorch documentation

© www.soinside.com 2019 - 2024. All rights reserved.