Skip to main content

opencv_transforms

This repository is intended as a faster drop-in replacement for Pytorch's Torchvision augmentations. This repo uses OpenCV for fast image augmentation for PyTorch computer vision pipelines. I wrote this code because the Pillow-based Torchvision transforms was starving my GPU due to slow image augmentation.

Requirements

  • A working installation of OpenCV. Tested with OpenCV version 3.4.1, 4.1.0
  • Tested on Windows 10 and Ubuntu 18.04. There is evidence that OpenCV doesn't work well with multithreading on Linux / MacOS, for example num_workers >0 in a pytorch DataLoader. I haven't run into this issue yet.

Installation

opencv_transforms is now a pip package! Simply use

  • pip install opencv_transforms

Usage

Breaking change! Please note the import syntax!

  • from opencv_transforms import transforms
  • From here, almost everything should work exactly as the original transforms.

Example: Image resizing

import numpy as np
image = np.random.randint(low=0, high=255, size=(1024, 2048, 3))
resize = transforms.Resize(size=(256,256))
image = resize(image)

Should be 1.5 to 10 times faster than PIL. See benchmarks

Performance

  • Most transformations are between 1.5X and ~4X faster in OpenCV. Large image resizes are up to 10 times faster in OpenCV.
  • To reproduce the following benchmarks, download the Cityscapes dataset.
  • An example benchmarking file can be found in the notebook bencharming_v2.ipynb I wrapped the Cityscapes default directories with a HDF5 file for even faster reading.

resize random crop change brightness change brightness and contrast change contrast only random horizontal flips

The changes start to add up when you compose multiple transformations together. composed transformations

TODO

  • Initial commit with all currently implemented torchvision transforms
  • Cityscapes benchmarks
  • Make the resample flag on RandomRotation, RandomAffine actually do something
  • Speed up augmentation in saturation and hue. Currently, fastest way is to convert to a PIL image, perform same augmentation as Torchvision, then convert back to np.ndarray

Release files for opencv-transforms 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opencv-transforms 0.0.6
File Size Uploaded
opencv_transforms-0.0.6.tar.gz 918.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opencv-transforms 0.0.6
File Interpreter ABI Platform
opencv_transforms-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 937.2 kB

Release files / opencv_transforms-0.0.6.tar.gz

Download URL opencv_transforms-0.0.6.tar.gz
Size 918.6 kB
Tags Source
SHA-256 checksum
How to use checksums
332b6cfb6199ed273addba38c798f8abe65f4d2922380b12cdfdaf896ad0308b
BLAKE2b-256 checksum
How to use checksums
62a91526cf7e8365ce3bf3bb3a3adbfe2054570d4e177ae052f1ab1c2a2e81dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9

Release files / opencv_transforms-0.0.6-py3-none-any.whl

Download URL opencv_transforms-0.0.6-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb8775a2f2b82f63c01b69b8fc5e0b013eaf612a347ad5142daf3715a157031d
BLAKE2b-256 checksum
How to use checksums
f20a3b2539c66fad91081e3a04d549818f2d06f0ba662213fab78d46a4b01167
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.5.0.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page