# opencv_transforms
This repository is intended as a faster drop-in replacement for [Pytorch's Torchvision augmentations](https://github.com/pytorch/vision/). 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
```python
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](https://www.cityscapes-dataset.com/).
* 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](benchmarks/benchmarking_Resize.png)
![random crop](benchmarks/benchmarking_Random_crop_quarter_size.png)
![change brightness](benchmarks/benchmarking_Color_brightness_only.png)
![change brightness and contrast](benchmarks/benchmarking_Color_constrast_and_brightness.png)
![change contrast only](benchmarks/benchmarking_Color_contrast_only.png)
![random horizontal flips](benchmarks/benchmarking_Random_horizontal_flip.png)
The changes start to add up when you compose multiple transformations together.
![composed transformations](benchmarks/benchmarking_Resize_flip_brightness_contrast_rotate.png)
## TODO
- [x] Initial commit with all currently implemented torchvision transforms
- [x] 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
Raw data
{
"_id": null,
"home_page": "https://github.com/jbohnslav/opencv_transforms",
"name": "opencv-transforms",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "pytorch image augmentations",
"author": "Jim Bohnslav",
"author_email": "JBohnslav@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/62/a9/1526cf7e8365ce3bf3bb3a3adbfe2054570d4e177ae052f1ab1c2a2e81dd/opencv_transforms-0.0.6.tar.gz",
"platform": "",
"description": "# opencv_transforms\n\nThis repository is intended as a faster drop-in replacement for [Pytorch's Torchvision augmentations](https://github.com/pytorch/vision/). 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.\n\n## Requirements\n* A working installation of OpenCV. **Tested with OpenCV version 3.4.1, 4.1.0**\n* 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. \n\n## Installation\nopencv_transforms is now a pip package! Simply use\n* `pip install opencv_transforms`\n\n## Usage\n**Breaking change! Please note the import syntax!** \n* `from opencv_transforms import transforms`\n* From here, almost everything should work exactly as the original `transforms`.\n#### Example: Image resizing \n```python\nimport numpy as np\nimage = np.random.randint(low=0, high=255, size=(1024, 2048, 3))\nresize = transforms.Resize(size=(256,256))\nimage = resize(image)\n```\nShould be 1.5 to 10 times faster than PIL. See benchmarks\n\n## Performance\n* Most transformations are between 1.5X and ~4X faster in OpenCV. Large image resizes are up to 10 times faster in OpenCV.\n* To reproduce the following benchmarks, download the [Cityscapes dataset](https://www.cityscapes-dataset.com/). \n* 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. \n\n![resize](benchmarks/benchmarking_Resize.png)\n![random crop](benchmarks/benchmarking_Random_crop_quarter_size.png)\n![change brightness](benchmarks/benchmarking_Color_brightness_only.png)\n![change brightness and contrast](benchmarks/benchmarking_Color_constrast_and_brightness.png)\n![change contrast only](benchmarks/benchmarking_Color_contrast_only.png)\n![random horizontal flips](benchmarks/benchmarking_Random_horizontal_flip.png)\n\nThe changes start to add up when you compose multiple transformations together.\n![composed transformations](benchmarks/benchmarking_Resize_flip_brightness_contrast_rotate.png)\n\n## TODO\n- [x] Initial commit with all currently implemented torchvision transforms\n- [x] Cityscapes benchmarks\n- [ ] Make the `resample` flag on `RandomRotation`, `RandomAffine` actually do something\n- [ ] 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\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "A drop-in replacement for Torchvision Transforms using OpenCV",
"version": "0.0.6",
"project_urls": {
"Homepage": "https://github.com/jbohnslav/opencv_transforms"
},
"split_keywords": [
"pytorch",
"image",
"augmentations"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f20a3b2539c66fad91081e3a04d549818f2d06f0ba662213fab78d46a4b01167",
"md5": "8d9e7c70d75f61e2fe5bb7aa7a099ade",
"sha256": "eb8775a2f2b82f63c01b69b8fc5e0b013eaf612a347ad5142daf3715a157031d"
},
"downloads": -1,
"filename": "opencv_transforms-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8d9e7c70d75f61e2fe5bb7aa7a099ade",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 18655,
"upload_time": "2021-05-03T14:27:09",
"upload_time_iso_8601": "2021-05-03T14:27:09.895224Z",
"url": "https://files.pythonhosted.org/packages/f2/0a/3b2539c66fad91081e3a04d549818f2d06f0ba662213fab78d46a4b01167/opencv_transforms-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "62a91526cf7e8365ce3bf3bb3a3adbfe2054570d4e177ae052f1ab1c2a2e81dd",
"md5": "84f7a107c2f6935235b0dc5f19de1198",
"sha256": "332b6cfb6199ed273addba38c798f8abe65f4d2922380b12cdfdaf896ad0308b"
},
"downloads": -1,
"filename": "opencv_transforms-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "84f7a107c2f6935235b0dc5f19de1198",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 918574,
"upload_time": "2021-05-03T14:27:11",
"upload_time_iso_8601": "2021-05-03T14:27:11.453539Z",
"url": "https://files.pythonhosted.org/packages/62/a9/1526cf7e8365ce3bf3bb3a3adbfe2054570d4e177ae052f1ab1c2a2e81dd/opencv_transforms-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-05-03 14:27:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jbohnslav",
"github_project": "opencv_transforms",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "opencv-transforms"
}