Let A be a tensor(CxHxW).

What is the difference between torchvision.transforms.ToPILImage(A) and torchvision.transforms.ToPILImage()(A) ?

transforms.ToPILImage() takes an argument mode, which defines the PIL.Image.mode of the input data:

mode (PIL.Image mode) – color space and pixel depth of input data (optional). If mode is None (default) there are some assumptions made about the input data: 1. If the input has 3 channels, the mode is assumed to be RGB. 2. If the input has 4 channels, the mode is assumed to be RGBA. 3. If the input has 1 channel, the mode is determined by the data type (i,e, int, float, short).

In the second example you are creating the transformation first and then pass your input A to it, which should work right.
However, in your first example, you are creating an instance of the transformation and pass your input data A as the mode argument. Nothing will happen in this case, since the transformation is not assigned to a variable and even if it is, it’ll fail with an TypeError.

This will thus work:

transform = transforms.ToPILImage()
x = torch.zeros(3, 24, 24).uniform_(0, 1)
x_image = transform(x)
1 Like

Thank U ptrblck…:grinning::grinning:

Please help me…