'Tensorboard add_image shows green image red

I encountered a quite weird problem. After running some images through my neural network and trying to display the segmentation as follows:

print(label.shape)

torch.Size([1, 3, 321, 321])

Now displaying the image with matplotlib shows everything correctly:

plt.imshow(fake_seg_tb[0].permute(1,2,0))

Label with correct colors

When I throw the image into tensorboard (pytorch API), I get a red version of the image, which is not what I have as pixel values.

writer_semisuper = SummaryWriter()
writer_semisuper.add_image('My Label: ', torchvision.utils.make_grid(label), some_step)

Label with wrong colors

The library I am importing is (PyTorch 1.5.0):

from torch.utils.tensorboard import SummaryWriter

With the tensorboard --version beeing 2.1.0.

I have no clue why in matplotlib and in the tensor the RGB values are correct but when i display it, it is just not right.



Solution 1:[1]

I was facing this problem because, in the dataloader I was modifying my output labels to have a particular standard deviation and mean. But tensorboard is unaware of this modification to your labels.
Solution
Before passing your image to writer_semisuper.add_image, you need to apply an inverse transform to your grid. For example, if your transform in your dataloader was

data_transforms = transforms.Compose([transforms.ToTensor(),transforms.Normalize([0.485, 0.485, 0.485], [0.229, 0.229, 0.229])])

You need to create a inverse transform like

inv_normalize = transforms.Normalize(
    mean=[-0.485/0.229, -0.485/0.229, -0.485/0.229],
    std=[1/0.229, 1/0.229, 1/0.229]
)

Now write your grid to tensorboard using

writer_semisuper = SummaryWriter()
writer_semisuper.add_image('My Label: ', inv_normalize(torchvision.utils.make_grid(label)), some_step)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 satinder singh