AssertionError: size of input tensor and input format are different. - Tensorboard add image

Viewed 634

I have tried to add an image to the tensorboard.

img = Image.open(image_path)

img = (np.asarray(img) / 255.0)
img = torch.from_numpy(img).float()
img = img.cuda().permute(2, 0, 1)
img = img.cuda().unsqueeze(0)

writer.add_image('Original', img)

but there is an error.

tensor shape: {}, input_format: {}".format(tensor.shape, input_format) AssertionError: size of input tensor and input format are different. tensor shape: (1, 3, 420, 629), input_format: CHW

1 Answers

looks like your using the function to add a single image but you are adding a batch of images of size 1. if you don't want to reshape/flatten/mess with the img try specifying the dataformats parameter and use add_images:

writer.add_images('Original', img, dataformats='NCHW')

img_tensor: Default is (N, 3, H, W)(N,3,H,W). If dataformats is specified, other shape will be accepted. e.g. NCHW or NHWC.

source SummaryWriter.add_image

Related