This error is raised, if you try to pass a 4-dimensional image tensor to transforms.ToPILImage(), i.e. if it contains the batch dimension.
To avoid this you would have to call this transformation in a loop for each tensor.
Note however, that this approach would break the computation graph and your first conv layer will not get any gradients from the model output and the corresponding loss, since you are transforming the tensors to numpy arrays.
You could use F.interpolate to resize the tensors instead.