Problem Description
环境:Windows+Python+Pytorch
When learning to use the SummaryWriter class in torch.utils.tensorboard in Pytorch to display and process image data, an error message is reported:
assert(len(tensor.shape) == len(input_format)), "size of input tensor and input format are different.
AssertionError: size of input tensor and input format are different. tensor shape: (64, 3, 32, 32), input_format: CHW The reason for the size of input tensor and input format are different mainly lies
in the add_image or add_images function:
① First check whether it is a single input image or multiple images. Add_image should be used for a single image, and add_images should be used for multiple images. ②
Check whether the formats of the input images are the same. The general function defaults to CHW. Use tensor.shape() to see Whether it meets the requirement
③ Check whether the number of channels meets the requirement
import torch
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, 64)
writer = SummaryWriter("nn_conv2d")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_image("Old Image", imgs, step)//add_image出错
step = step + 1
writer.close()
//报错:assert(len(tensor.shape) == len(input_format)), "size of input tensor and input format are different. \ AssertionError: size of input tensor and input format are different. tensor shape: (64, 3, 32, 32), input_format: CHW
Cause Analysis:
Here imgs is more than one picture, the type is [64, 3, H, W], add_image is only used for one picture
solution:
Replace add_image with add_images