pytorch / vision

Datasets, Transforms and Models specific to Computer Vision
https://pytorch.org/vision
BSD 3-Clause "New" or "Revised" License
16.06k stars 6.93k forks source link

ColorJitter results with OverflowError #8536

Closed shaibagon closed 2 months ago

shaibagon commented 2 months ago

🐛 Describe the bug

Using ColorJitter augmentations in torchvision 0.18.1 results in an OverflowError. This was not observed in older torchvision versions (tested with 0.15.0).

How to reproduce:

# read an image
from PIL import Image
import requests
from io import BytesIO
# I picked this image, but it actually happens with others as well. just try one that you have.
pil_img = Image.open(BytesIO(requests.get('https://www.weizmann.ac.il/math/bagon/sites/math.bagon/files/styles/pi_photo/public/ShaiBagon_8.png').content))

from torchvision import transforms
cj = transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1)
for _ in range(10):
    cj(pil_img)  # it does not happen every time, but out of 10 it will most likely happen)

This code will through:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "[...]/python3.10/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "[...]/python3.10/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "[...]/lib/python3.10/site-packages/torchvision/transforms/transforms.py", line 1280, in forward
    img = F.adjust_hue(img, hue_factor)
  File "[...]/lib/python3.10/site-packages/torchvision/transforms/functional.py", line 959, in adjust_hue
    return F_pil.adjust_hue(img, hue_factor)
  File "[...]/lib/python3.10/site-packages/torchvision/transforms/_functional_pil.py", line 114, in adjust_hue
    np_h += np.uint8(hue_factor * 255)
OverflowError: Python integer -24 out of bounds for uint8

Versions

PyTorch version: 2.3.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Red Hat Enterprise Linux 9.1 (Plow) (x86_64)
GCC version: (GCC) 11.3.1 20220421 (Red Hat 11.3.1-2)
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.34

Python version: 3.10.0 (default, Mar  3 2022, 09:58:08) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.14.0-162.6.1.el9_1.x86_64-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100 80GB PCIe
Nvidia driver version: 535.161.07
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   46 bits physical, 57 bits virtual
Byte Order:                      Little Endian
CPU(s):                          52
On-line CPU(s) list:             0-51
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
CPU family:                      6
Model:                           106
Thread(s) per core:              1
Core(s) per socket:              26
Socket(s):                       2
Stepping:                        6
CPU max MHz:                     3400.0000
CPU min MHz:                     800.0000
BogoMIPS:                        4400.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       2.4 MiB (52 instances)
L1i cache:                       1.6 MiB (52 instances)
L2 cache:                        65 MiB (52 instances)
L3 cache:                        78 MiB (2 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50
NUMA node1 CPU(s):               1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] numpy==2.0.0
[pip3] pytorch-lightning==2.3.3
[pip3] torch==2.3.1
[pip3] torchmetrics==1.4.0.post0
[pip3] torchvision==0.18.1
[pip3] triton==2.3.1
[conda] numpy                     2.0.0                    pypi_0    pypi
[conda] pytorch-lightning         2.3.3                    pypi_0    pypi
[conda] torch                     2.3.1                    pypi_0    pypi
[conda] torchmetrics              1.4.0.post0              pypi_0    pypi
[conda] torchvision               0.18.1                   pypi_0    pypi
[conda] triton                    2.3.1                    pypi_0    pypi
shaibagon commented 2 months ago

Happended using pillow version 10.4.0 and 9.5.0.

zhmiao commented 2 months ago

It is because numpy 2.0 stopped doing the auto conversion in the new versions. In version 1.24.4, when I do np.uint8(-34) I got DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of -34 to uint8 will fail in the future. While in numpy 2.0, I got OverflowError: Python integer -34 out of bounds for uint8. Pytorch should make changes to their transform functions and anywhere that require automatic uint conversion to adapt to the new numpy changes.

shaibagon commented 2 months ago

@zhmiao thank you. I downgraded to numpy==1.26.4 and the problem disappeared. However, no DeprecationWarning is shown in this version.

shaibagon commented 2 months ago

Is it possible that numpy 2 is a bigger issue? See, e.g., #8530

NicolasHug commented 2 months ago

Hi @shaibagon , thanks for the report.

TL;DR: This will be fixed in torchvision 0.19 which we'll release later this week.

A bit more detail for anyone else hitting this: as @zhmiao pointed, this is related to numpy2. That's not related to Pillow, and that's not related to torchvision's version either (for any version <= 0.18). The fix was made in https://github.com/pytorch/vision/pull/8463. Updating torchvision to >= 0.19 once it's released (in a few days) should fix the problem. Meanwhile, the workaround is to downgrade numpy to any version <2.

NicolasHug commented 2 months ago

Closing as 0.19 is out. Feel free to reopen if the issue persists.

shaibagon commented 2 months ago

@NicolasHug Thank you!