resize images with bboxes for training

#3
by abhishek HF staff - opened
Competitions org
โ€ข
edited Mar 3, 2023

Using the package chitra, you can easily resize images and bounding boxes at the same time:

Example:

from chitra.image import Chitra
import pandas as pd
import matplotlib.pyplot as plt

train_df = pd.read_csv("train.csv")
id = 73
train_df = train_df[train_df["id"] == f"{id}.png"].reset_index(drop=True)
bboxes = [k for k in train_df[['xmin', 'ymin', 'xmax', 'ymax']].values]
image = Chitra(f"train/{id}.png", bboxes=bboxes, labels=[0 for _ in range(len(bboxes))])

image.resize_image_with_bbox((2048, 2048))
plt.imshow(image.draw_boxes())

Please note that for your submission file, you must submit bboxes for the original image sizes in the test set.

Alternatively, we can use the albumentations library and provide it the bounding box parameters to resize images along with their bounding boxes.

Example:

import albumentations as A
from albumentations.pytorch import ToTensorV2

def get_transforms(cfg, data):
    """
    Function to obtain the Training and Validation Transforms
    Args:
        cfg (dict): Configuration File
        data (str): "train" for Training data,
                    "valid " for Validation data
    Returns:
        Augmentations : Transforms to be applied
    """

    # Train Augmentations
    if data == "train":
        return A.Compose(
            [   
                A.Resize(cfg['MODEL']['IMAGE_SIZE'], cfg['MODEL']['IMAGE_SIZE']),
                A.HorizontalFlip(p=0.5),
                A.RandomBrightnessContrast(p=0.5),
                ToTensorV2()
            ],
        p=1.,
        bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']}
    )

    # Validation Augmentations
    if data == "valid":
        return A.Compose(
            [
                A.Resize(cfg['MODEL']['IMAGE_SIZE'], cfg['MODEL']['IMAGE_SIZE']),
                ToTensorV2()
            ], 
        p=1.,
        bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']}
    )

def get_test_transform():
    return A.Compose([
        ToTensorV2(p=1.0)
    ], 
    )

@abhishek is there any difference between using this method and using chitra which you may have observed?

This dataset has tiny ships as well - EDA - not sure how many such cases are there.

So I wonder if this official code repo would work better , otherwise I'm afraid resizing will cause those tiny ships to just disappear.

@ishandutta I am using your method for resizing the images.
And then I am using this for applying transforms to the images and bbox

if self.transform is not None:
            transformed = self.transform(image=img, bboxes=target['boxes'], class_labels=target['labels'])
            image = transformed['image']
            target['boxes'] = transformed['bboxes'] 
            target['labels'] = transformed['labels']

But I am getting this error
image.png

Can you share how you are applying the transform

Sign up or log in to comment