Based on the error message the collate_fn fails to create a batch of the images returned by Dataset.__getitem__, since they have a different spatial resolution.
Often, you would resize the images to a fixed size, which would avoid this error.
To do so, you could use torchvision.transforms.Resize and add it to your transform.
Your second approach of using a custom collate_fn would also work, but note that you would have to pass the images one-by-one to the model if you are not padding or resizing them in the training loop.
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging a bit easier.