Impy (Images in Python)

Category :

Impy is a library used for deep learning projects that utilize image datasets. This versatile tool not only allows image data augmentation but also provides efficient methods for image preprocessing, making it an excellent choice for enhancing the performance of computer vision models.

What Impy Provides

  • Data augmentation methods for images with bounding boxes (the bounding boxes are also affected by the transformations, so you don’t have to label again).
  • Fast image preprocessing methods useful in a deep learning context (e.g., if your image is too big, you need to divide it into patches).

Installation

To get started with Impy, follow these steps:

  • Download the impy.whl file.
  • Use pip to install the wheel:
    pip install impy-0.1-py3-none-any.whl

Tutorial

Impy has multiple features that enable you to tackle several common issues with just a few lines of code. We will work with a mini-dataset of cars and pedestrians, available at this link. This dataset includes object annotations that make it suitable for solving localization problems.

Object Localization

In this section, we will tackle the issue of object localization in images.

Handling Large Images

One common problem in Computer Vision is dealing with large images. For example, consider an image with a resolution of 3840×2160 pixels – it’s too big for training your model effectively due to possible memory constraints. Resizing might degrade the quality of the image and require re-labeling.

Instead of tackling the issue inefficiently, we will slice these large images into smaller, manageable pieces, or “crops,” maximizing the bounding boxes’ representation within them. Think of it as cutting a large cake into smaller slices, ensuring each slice retains a good amount of frosting (bounding boxes).

Let’s see how to implement this using Impy:

  • Create a directory named testing_cars and enter it:
    mkdir -p $PWD/testing_cars && cd testing_cars
  • Download the cars dataset from this link:
    git clone https://github.com/lozuwacars_dataset
  • Create a file named reducing_big_images.py and insert the following code:
    import os
    from impy.ObjectDetectionDataset import ObjectDetectionDataset
    
    def main():
        images_path = os.path.join(os.getcwd(), 'cars_dataset', 'images')
        annotations_path = os.path.join(os.getcwd(), 'cars_dataset', 'annotations', 'xmls')
        
        dbName = 'CarsDataset'
        
        obda = ObjectDetectionDataset(imagesDirectory=images_path, annotationsDirectory=annotations_path, databaseName=dbName)
        
        offset = [1032, 1032] 
        images_output_path = os.path.join(os.getcwd(), 'cars_dataset', 'images_reduced')
        annotations_output_path = os.path.join(os.getcwd(), 'cars_dataset', 'annotations_reduced', 'xmls')
        
        obda.reduceDatasetByRois(offset=offset, outputImageDirectory=images_output_path, outputAnnotationDirectory=annotations_output_path)
    
    if __name__ == '__main__':
        main()
    

Make sure to create the paths for the reduced images if they don’t already exist:

  • mkdir -p $PWD/cars_dataset/images_reduced && mkdir -p $PWD/cars_dataset/annotations_reduced/xmls

Now you can run the script to reduce the images:

  • python reducing_big_images.py

Impy will create a new set of images and annotations that maintain the maximum number of bounding boxes. This way, you’ll have the optimal number of data points without overwhelming your system.

Data Augmentation for Bounding Boxes

Another common task in Computer Vision is data augmentation. Instead of creating custom scripts for augmentations like scaling, cropping, or rotating, Impy streamlines the process. Let’s create a configuration file for augmentations:

  • Create a JSON file named augmentation_configuration.json:
    touch augmentation_configuration.json
  • Insert the following code into the augmentation_configuration.json:
    {
      "multiple_image_augmentations": {
        "Sequential": [
          {
            "image_color_augmenters": {
              "Sequential": [
                {"sharpening": {"weight": 2.0, "save": true, "restartFrame": false, "randomEvent": false}}
              ]
            }
          },
          {
            "bounding_box_augmenters": {
              "Sequential": [
                {"scale": {"size": [1.2, 1.2], "zoom": true, "interpolationMethod": 1, "save": true, "restartFrame": false, "randomEvent": false}},
                {"verticalFlip": {"save": true, "restartFrame": false, "randomEvent": true}}
              ]
            }
          },
          {
            "image_color_augmenters": {
              "Sequential": [
                {"histogramEqualization": {"equalizationType": 1, "save": true, "restartFrame": false, "randomEvent": false}}
              ]
            }
          },
          {
            "bounding_box_augmenters": {
              "Sequential": [
                {"horizontalFlip": {"save": true, "restartFrame": false, "randomEvent": false}},
                {"crop": {"save": true, "restartFrame": true, "randomEvent": false}}
              ]
            }
          }
        ]
      }
    }

To apply the data augmentation pipeline, create a file named apply_bounding_box_augmentations.py and insert the following code:

  • import os
    from impy.ObjectDetectionDataset import ObjectDetectionDataset
    
    def main():
        images_path = os.path.join(os.getcwd(), 'cars_dataset', 'images')
        annotations_path = os.path.join(os.getcwd(), 'cars_dataset', 'annotations', 'xmls')
    
        dbName = 'CarsDataset'
    
        obda = ObjectDetectionDataset(imagesDirectory=images_path, annotationsDirectory=annotations_path, databaseName=dbName)
    
        configuration_file = os.path.join(os.getcwd(), 'augmentation_configuration.json')
        images_output_path = os.path.join(os.getcwd(), 'cars_dataset', 'images_augmented')
        annotations_output_path = os.path.join(os.getcwd(), 'cars_dataset', 'annotations_augmented', 'xmls')
    
        obda.applyDataAugmentation(configurationFile=configuration_file, outputImageDirectory=images_output_path, outputAnnotationDirectory=annotations_output_path)
    
    if __name__ == '__main__':
        main()
    

Execute the script:

  • python apply_bounding_box_augmentations.py

You can then view the resulting augmented images without altering the bounding boxes!

Troubleshooting

If you run into issues while using Impy, consider the following troubleshooting ideas:

  • Ensure your Python environment has the necessary dependencies installed.
  • Check that the paths for images and annotations are correctly set and that the files exist.
  • If you encounter memory errors, consider reducing the size of your images further or utilizing a machine with more resources.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox

Latest Insights

© 2024 All Rights Reserved

×