Course Content

Lesson 1.2: Image Width and Height Distribution

Defining Image Width and Height Distribution

Image Width and Height Distribution refers to the distribution of the dimensions (width and height) of all images in a dataset. It provides a histogram that depicts the frequency of each dimension across the dataset.

Importantly, if certain images have been rescaled or padded, the histogram represents the size after these operations.


The Importance of Image Width and Height Distribution  

Analyzing your dataset’s image width and height distribution provides insights into the diversity of image sizes in the dataset.

There’s an array of potential issues that can be revealed by inspecting the distribution of image sizes, which, if left unaddressed, could hamper your model’s performance.

  • Certain images in the training and/or validation sets may not have been appropriately scaled to the desired dimensions.
  • There may be unintentional discrepancies between the image sizes across the two datasets.
  • If your dataset consists of varied image sizes, it’s imperative to ensure that the size distribution of the validation set lies within that of the training set. Failing to do so may lead to a scenario where your model is trained on images of different dimensions than those it is expected to make predictions on.


Calculating Image Width and Height Distribution

Iterate over all the images in your dataset to calculate the image width and height distribution. 

For each image, extract the width and height. Do this separately for the training and validation sets. 

The result is a distribution of image widths and heights, allowing you to compare different datasets’ size characteristics.

Here’s how Image Width and Height Distribution is calculated in DataGradients.


Exploring Image Width and Height Distribution Through Examples

Let’s look at two examples and see what we can learn about our dataset from each. 


Example 1: A Favorable Scenario

Consider these training and validation sets:

Profiling Computer Vision Datasets: Image Width & Height Distribution

In this first example, the height and width distribution of the validation set is nestled within that of the training set. The validation set contains images of a singular size – 725×1275, a resolution that also exists in the training set. So the third pitfall described above is avoided; the model will be validated on image dimensions it encountered in training.

Example 2: A Problematic Case 

Profiling Computer Vision Datasets: Width & Height Distribution Problem (1)

In this second example, the validation set comprises height and width combinations that the training set lacks. In such a case, you would need to resize the training set images to include the size of the validation set images. This is preferable to resizing the validation set since it’s more likely that the validation set is a better representative of the data the model will see in production. 

Add Your Heading Text Here
					from transformers import AutoFeatureExtractor, AutoModelForImageClassification

extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-50")

model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")