A drop-in replacement for Torchvision Transforms using OpenCV
Project description
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 pytorchDataLoader
. 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.
The changes start to add up when you compose multiple transformations together.
TODO
- Initial commit with all currently implemented torchvision transforms
- Cityscapes benchmarks
- Make the
resample
flag onRandomRotation
,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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
opencv_transforms-0.0.3.post3.tar.gz
(917.9 kB
view hashes)
Built Distribution
Close
Hashes for opencv_transforms-0.0.3.post3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e4d06de45bcf474d9f96834c39f5ffaccf34c050909f263fd0eeff4d036ae58 |
|
MD5 | 2574c831a20266111acdb11057d36685 |
|
BLAKE2b-256 | 1c5e31ace69aaa9abdd72ab9850c516f86de3a55a328a5955e55ea1f1df5a617 |
Close
Hashes for opencv_transforms-0.0.3.post3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 035fc57beefc1e6197010676f6023b2b77042f71a73df67d850f3c501da03032 |
|
MD5 | 8d5c9c2165cf641fbe8743b863df27b1 |
|
BLAKE2b-256 | f5a0b9172675ab4ee269016d23e5bb162be330757e15e5268bdf2448aeac48d0 |