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
- Tested on Windows 10. There is evidence that OpenCV doesn't work well with multithreading on Linux / MacOS, for example
num_workers >0in a pytorchDataLoader. I haven't tried this on those systems.
Installation
git clone https://github.com/jbohnslav/opencv_transforms.git- Add to your python path
Usage
from opencv_transforms import opencv_transforms as 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
resampleflag onRandomRotation,RandomAffineactually 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.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| opencv_transforms-0.0.1.tar.gz | 917.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| opencv_transforms-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 935.3 kB
Release files / opencv_transforms-0.0.1.tar.gz
| Download URL | opencv_transforms-0.0.1.tar.gz |
|---|---|
| Size | 917.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1e3391e6d17b90edc300985601a367a47ca2215074fbe17b98b08a09d372367
|
|
BLAKE2b-256 checksum How to use checksums |
08627bfc342298d086433526238ed71a39d9cafc36574b4673a9032992460d33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|
Release files / opencv_transforms-0.0.1-py3-none-any.whl
| Download URL | opencv_transforms-0.0.1-py3-none-any.whl |
|---|---|
| Size | 18.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f639aca4e6107f06269ac89f48ef07aa9a5fe38084080a37bfeb81703d0abcf2
|
|
BLAKE2b-256 checksum How to use checksums |
120444a8df65691e12e2e7747eaaa658df9e9eca1190ffb7ea47b6c94d7dcba7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|