Torchvision error while saving images in image segmentation

I am working on image segmentation, on Cityscape dataset, and whenever i uses more than 7 classes torchvision doesn’t save correct coloured image. however when I tried to save it using numpy and PIL it was saved as expected. can someone explain what am I missing here.
Thanks in advance.

Could you post the code you’ve used to save the output using torchvision and PIL?

Screenshot%20from%202019-07-05%2012-46-50 Screenshot%20from%202019-07-05%2012-47-01

and i am using the standard method provided by torchvision to save images here :
torchvision.utils.save_image(target_rgb, filename_target)
torchvision.utils.save_image(y_threshed, filename_output)
torchvision.utils.save_image(image, filename_input)

and using PIL :
def saving_func(batch_tensor, path):
for i in range(batch_tensor.shape[0]):
b = np.asarray(batch_tensor.cpu().numpy())
a = b[i,:,:,:]
a =np.swapaxes(a,0,2)
a =np.swapaxes(a,1,0)
a = Image.fromarray(a.astype(‘uint8’)) + str("_") + str(i) + str(".jpg"))