The error originates from the ToPILImage() transform. The input should have three dimensions, i.e.: 3x256x256, for it to work. Use .squeeze() to remove the dummy dimension:
a = torch.randn((1, 3, 256, 256))

transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((224, 224)),
    transforms.ToTensor() 
])

b = transform(a.squeeze())

print(b.size()) # torch.Size([3, 224, 224])

If you want to have shape 1x3x224x224 again, simply use .unsqueeze() on the desired dimension:
b = b.unsqueeze(0)
print(b.size()) # torch.Size([1, 3, 224, 224])