I wonder why the Code Example for resizing a file uses “transforms.Resize(XXX, XXX)“, while the lecture mentions “transforms.Resize(XXX)” as the “better approach”, i.e. resizing the shorter edge to XXX (and then crop)? So what are the trade-offs of each of these approaches? And which one is ultimately the best (more recommended)?
Thank you!
Hi @DAResaid,
When you do transforms.Resize((H, W)), you are forcing specific height and width dimensions. This guarantees all your images come out the exact same shape (e.g., 100x100), which is required for batching.
However, if you use transforms.Resize(S), PyTorch only resizes the shorter edge to that number S and scales the other edge automatically to keep the aspect ratio.
So, if you have inputs of 100x100 and 100x250, and you do Resize(50), you will end up with one image that is 50x50 and another that is 50x125. Since they are now different sizes, you won’t be able to stack them together into a batch for training (your code will crash).
To fix the size mismatch without squishing the image, this is often followed by Resize(S) with a Center Crop. This works well if your images have large background areas you aren’t concerned with, and the main object is central. The crop ensures the resulting images are all the same size and that the main object takes up most of the frame.
If the transforms I mentioned here are not familiar to you, don’t worry, you’ll learn more about them in Course 2 ![]()
Best,
Mubsi
My question was not about the difference between the approaches (I understanded them as well as the fact, that transforms.Resize(XXX) should go along with transforms.CenterCrop(XXX), because it was literally mentioned in the lecture
). My questions was about the trade-offs of these approaches (and why one was recommended in the lecture but another one was used in the Code Example), but it’s Ok, I can wait till the Course 2. Thank you.
Hi @DAResaid,
Can you specify exactly in which lecture this was mentioned? So that I can help better understand your query.
Thanks,
Mubsi
Sure! The “C1_M3_Data_Management” Module, the “Transform Pipelines“ video, starting at 1:36, and specifically at 1:43 “… A better approach is to use resize with just a single value, and that will scale the shorter edge while keeping the aspect ratio.”
Hi @DAResaid,
Thanks for sharing the lecture name! It helps clarify exactly where the difference lies.
After reviewing the lecture vs. the code, here is the full picture regarding your question: “Why was one recommended in the lecture but another used in the Code Example?”
As you might have noted, the lecture specifically asks: “But wait, what if your image is a rectangle?” This is the key context for the video. The instructor is focused on a scenario where your input images are rectangular (e.g., 100×250).
- If you use
Resize((H, W))on a rectangle, you force it into a square, which “squishes” or distorts the image. - To avoid this distortion, the lecture recommends
Resize(S)(scaling the short edge) followed byCenterCrop. This keeps the aspect ratio correct.
Now, I see the code uses:
transforms.Resize((256, 256))
transforms.CenterCrop(224)
If the input is a rectangle, this code will squish it into a 256×256 square before cropping. However, if your data were already square (e.g., 1000×1000), this approach would work perfectly without distortion.
To be transparent with you, the notebook was prepared before the lecture video was recorded, so the author of this notebook probably didn’t see the specific “rectangle” emphasis from the instructor.
For the purpose of this course, which is educational, both approaches are valid. Since the goal here is to learn the mechanics of the transformation pipeline at this point rather than train a State-of-the-Art model on complex rectangular data, the code example works fine despite the theoretical imperfection regarding aspect ratios.
You did a great job spotting this nuance. Recognizing how different transforms handle geometry is a great skill to have!
Again, there’s no one right answer here that fits every dataset scenario. Whether you use transforms.Resize((H, W)) or transforms.Resize(S) + transforms.CenterCrop(C) depends on the dimensions of the images in the dataset, and what you want them to be like.
Best,
Mubsi
Got it! Thank you very much!