ToTensor Transform always returns all 1.0 value when transforming jpg

I’m trying to apply transform on images however I keep getting all 1.0 value when image was transformed.

Code to replicate the issue:
import numpy as np
from PIL import Image
from torchvision import transforms

trans = transforms.Compose([transforms.ToTensor()])
img=“yourpath/tan_0001_new_MelSpec.jpg”

demo = Image.open(img)
demo_img = trans(demo)

I’m expecting float value between 0 and 1 for demo_img however I get all 1.0 for some reasons.
Below is the jpg I had this issue with.

Thank you in advance.

I cannot reproduce any issue using your image and see the expected transformed output:

trans = transforms.Compose([transforms.ToTensor()])
img = PIL.Image.open("/home/pbialecki/Downloads/test01.jpeg")
print(np.unique(img, return_counts=True))
# (array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
#         13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
#         26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
#         39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
#         52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
#         65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
#         78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
#         91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
#        104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
#        117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
#        130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
#        143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
#        156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
#        169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
#        182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
#        195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
#        208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
#        221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
#        234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
#        247, 248, 249, 250, 251, 252, 253, 254, 255], dtype=uint8), array([  4194,   2373,   3516,   4392,   4672,   5215,   4912,   5922,
#          5746,   7128,   8891,  11257,  13104,  15248,  18331,  19423,
#         19986,  19661,  20530,  19958,  20678,  20319,  20564,  17949,
#         19837,  18393,  19033,  17925,  18133,  16128,  15254,  12081,
#         12109,  10600,   9199,   9991,  10104,   8941,   9556,  10894,
#          9596,  10101,  10599,  11544,  11527,  11353,  12144,  13985,
#         14428,  12348,  13109,  11881,  12157,  10021,  10274,  10336,
#          9454,   9486,   9475,   8894,   8788,   8852,   7725,   7224,
#          7656,   7542,   7651,   6487,   5283,   5983,   5824,   6192,
#          4510,   4377,   4734,   4651,   4467,   4303,   5054,   4830,
#          5368,   5411,   5871,   6718,   7361,   6820,   6969,   6950,
#          8786,   9200,   8433,   9771,   9655,  11592,  10907,  11906,
#         11612,  12766,  11557,  12461,  11044,  12617,  12416,  13115,
#         12705,  13387,  12384,  12313,  12308,  11419,  12297,  11020,
#         12138,  12276,  12214,  11008,  11870,  12741,  14276,  14754,
#         17036,  18311,  21427,  22613,  26807,  28737,  30684,  29785,
#         28744,  25584,  24866,  19874,  16584,  14643,  11326,   9449,
#          7823,   6658,   5404,   4777,   4855,   3900,   3581,   3268,
#          2831,   2758,   3093,   3512,   3513,   4609,   6836,  11829,
#         10314,  13640,  13863,   8784,   8243,   7448,   6128,   6740,
#          6950,   5848,   4581,   5912,   5817,   5750,   5390,   4589,
#          3964,   4541,   3315,   3705,   2955,   3002,   2223,   3061,
#          2291,   2213,   2484,   1582,   1796,   1444,   1616,   1662,
#          1099,   1728,   1256,   1645,   1735,   1160,   1282,   2103,
#          1729,   1085,   1452,   1491,    991,   1359,   1388,   1050,
#           867,   1627,   1157,   1491,    853,   1066,    977,   1725,
#          1460,   1558,   1768,   2679,   6113,  10838,   9802,  13541,
#          9834,   6324,   6435,   6287,   4811,   5867,   3922,   4029,
#          3973,   3758,   2943,   3682,   2067,   2201,   2188,   1655,
#          2338,   2375,   1957,   2146,   2294,   2480,   1594,   3065,
#          2891,   1640,   1856,   2072,   2267,   2866,   3434,   2911,
#          5342,   4511,   6401,  10551,  13887,  19140,  26288, 107923]))
                                                                          
out = trans(img)
print(out.min(), out.max())
# tensor(0.) tensor(1.)
print(torch.unique(out, return_counts=True))
# (tensor([0.0000, 0.0039, 0.0078, 0.0118, 0.0157, 0.0196, 0.0235, 0.0275, 0.0314,
#         0.0353, 0.0392, 0.0431, 0.0471, 0.0510, 0.0549, 0.0588, 0.0627, 0.0667,
#         0.0706, 0.0745, 0.0784, 0.0824, 0.0863, 0.0902, 0.0941, 0.0980, 0.1020,
#         0.1059, 0.1098, 0.1137, 0.1176, 0.1216, 0.1255, 0.1294, 0.1333, 0.1373,
#         0.1412, 0.1451, 0.1490, 0.1529, 0.1569, 0.1608, 0.1647, 0.1686, 0.1725,
#         0.1765, 0.1804, 0.1843, 0.1882, 0.1922, 0.1961, 0.2000, 0.2039, 0.2078,
#         0.2118, 0.2157, 0.2196, 0.2235, 0.2275, 0.2314, 0.2353, 0.2392, 0.2431,
#         0.2471, 0.2510, 0.2549, 0.2588, 0.2627, 0.2667, 0.2706, 0.2745, 0.2784,
#         0.2824, 0.2863, 0.2902, 0.2941, 0.2980, 0.3020, 0.3059, 0.3098, 0.3137,
#         0.3176, 0.3216, 0.3255, 0.3294, 0.3333, 0.3373, 0.3412, 0.3451, 0.3490,
#         0.3529, 0.3569, 0.3608, 0.3647, 0.3686, 0.3725, 0.3765, 0.3804, 0.3843,
#         0.3882, 0.3922, 0.3961, 0.4000, 0.4039, 0.4078, 0.4118, 0.4157, 0.4196,
#         0.4235, 0.4275, 0.4314, 0.4353, 0.4392, 0.4431, 0.4471, 0.4510, 0.4549,
#         0.4588, 0.4627, 0.4667, 0.4706, 0.4745, 0.4784, 0.4824, 0.4863, 0.4902,
#         0.4941, 0.4980, 0.5020, 0.5059, 0.5098, 0.5137, 0.5176, 0.5216, 0.5255,
#         0.5294, 0.5333, 0.5373, 0.5412, 0.5451, 0.5490, 0.5529, 0.5569, 0.5608,
#         0.5647, 0.5686, 0.5725, 0.5765, 0.5804, 0.5843, 0.5882, 0.5922, 0.5961,
#         0.6000, 0.6039, 0.6078, 0.6118, 0.6157, 0.6196, 0.6235, 0.6275, 0.6314,
#         0.6353, 0.6392, 0.6431, 0.6471, 0.6510, 0.6549, 0.6588, 0.6627, 0.6667,
#         0.6706, 0.6745, 0.6784, 0.6824, 0.6863, 0.6902, 0.6941, 0.6980, 0.7020,
#         0.7059, 0.7098, 0.7137, 0.7176, 0.7216, 0.7255, 0.7294, 0.7333, 0.7373,
#         0.7412, 0.7451, 0.7490, 0.7529, 0.7569, 0.7608, 0.7647, 0.7686, 0.7725,
#         0.7765, 0.7804, 0.7843, 0.7882, 0.7922, 0.7961, 0.8000, 0.8039, 0.8078,
#         0.8118, 0.8157, 0.8196, 0.8235, 0.8275, 0.8314, 0.8353, 0.8392, 0.8431,
#         0.8471, 0.8510, 0.8549, 0.8588, 0.8627, 0.8667, 0.8706, 0.8745, 0.8784,
#         0.8824, 0.8863, 0.8902, 0.8941, 0.8980, 0.9020, 0.9059, 0.9098, 0.9137,
#         0.9176, 0.9216, 0.9255, 0.9294, 0.9333, 0.9373, 0.9412, 0.9451, 0.9490,
#         0.9529, 0.9569, 0.9608, 0.9647, 0.9686, 0.9725, 0.9765, 0.9804, 0.9843,
#         0.9882, 0.9922, 0.9961, 1.0000]), tensor([  4194,   2373,   3516,   4392,   4672,   5215,   4912,   5922,   5746,
#           7128,   8891,  11257,  13104,  15248,  18331,  19423,  19986,  19661,
#          20530,  19958,  20678,  20319,  20564,  17949,  19837,  18393,  19033,
#          17925,  18133,  16128,  15254,  12081,  12109,  10600,   9199,   9991,
#          10104,   8941,   9556,  10894,   9596,  10101,  10599,  11544,  11527,
#          11353,  12144,  13985,  14428,  12348,  13109,  11881,  12157,  10021,
#          10274,  10336,   9454,   9486,   9475,   8894,   8788,   8852,   7725,
#           7224,   7656,   7542,   7651,   6487,   5283,   5983,   5824,   6192,
#           4510,   4377,   4734,   4651,   4467,   4303,   5054,   4830,   5368,
#           5411,   5871,   6718,   7361,   6820,   6969,   6950,   8786,   9200,
#           8433,   9771,   9655,  11592,  10907,  11906,  11612,  12766,  11557,
#          12461,  11044,  12617,  12416,  13115,  12705,  13387,  12384,  12313,
#          12308,  11419,  12297,  11020,  12138,  12276,  12214,  11008,  11870,
#          12741,  14276,  14754,  17036,  18311,  21427,  22613,  26807,  28737,
#          30684,  29785,  28744,  25584,  24866,  19874,  16584,  14643,  11326,
#           9449,   7823,   6658,   5404,   4777,   4855,   3900,   3581,   3268,
#           2831,   2758,   3093,   3512,   3513,   4609,   6836,  11829,  10314,
#          13640,  13863,   8784,   8243,   7448,   6128,   6740,   6950,   5848,
#           4581,   5912,   5817,   5750,   5390,   4589,   3964,   4541,   3315,
#           3705,   2955,   3002,   2223,   3061,   2291,   2213,   2484,   1582,
#           1796,   1444,   1616,   1662,   1099,   1728,   1256,   1645,   1735,
#           1160,   1282,   2103,   1729,   1085,   1452,   1491,    991,   1359,
#           1388,   1050,    867,   1627,   1157,   1491,    853,   1066,    977,
#           1725,   1460,   1558,   1768,   2679,   6113,  10838,   9802,  13541,
#           9834,   6324,   6435,   6287,   4811,   5867,   3922,   4029,   3973,
#           3758,   2943,   3682,   2067,   2201,   2188,   1655,   2338,   2375,
#           1957,   2146,   2294,   2480,   1594,   3065,   2891,   1640,   1856,
#           2072,   2267,   2866,   3434,   2911,   5342,   4511,   6401,  10551,
#          13887,  19140,  26288, 107923]))
                                                  
plt.imshow(out.permute(1, 2, 0))
1 Like

thanks for verifying. I must have only checked first part of the tensor which shows all 1 that reflects the white area of the image. It’s actually transformed to tensors as expected.

Thank you ptrblck.

1 Like