A drop-in replacement for Torchvision Transforms using OpenCV
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.
- 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 >0in a pytorch
DataLoader. I haven't run into this issue yet.
opencv_transforms is now a pip package! Simply use
pip install opencv_transforms
Breaking change! Please note the import syntax!
from opencv_transforms import transforms
- From here, almost everything should work exactly as the original
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
- 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.
The changes start to add up when you compose multiple transformations together.
- <input type="checkbox" checked="" disabled="" /> Initial commit with all currently implemented torchvision transforms
- <input type="checkbox" checked="" disabled="" /> Cityscapes benchmarks
- <input type="checkbox" disabled="" /> Make the
RandomAffineactually do something
- <input type="checkbox" disabled="" /> 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 history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size opencv_transforms-0.0.6.tar.gz (918.6 kB)||File type Source||Python version None||Upload date||Hashes View|
|Filename, size opencv_transforms-0.0.6-py3-none-any.whl (18.7 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
Hashes for opencv_transforms-0.0.6-py3-none-any.whl