DOC: issue with opencv altering smallest subnormal for <class 'numpy.float32'>
See original GitHub issueDescribe the issue:
There appears to be some kind of issue where opencv when combined with numpy 1.22 generates lots of warning messages.
UserWarning: The value of the smallest subnormal for <class ‘numpy.float32’> type is zero.
During opencv importation it calls to numpy and it generates an object and puts it into the cache with the correct smallest_subnormal array(1.e-45, dtype=float32). However checking the numpy cache immediately after it appears as if the smallest_subnormal value has no changed to 0. I have no explanation for how or why this would happen. (last reproduction code example)
The workaround to avoid seeing the warnings appears to be to import numpy and attempt to read values from the cache for those types that are generating a warning before importing opencv. I’m not sure why this works but it appears to stop the warnings. In my case this looks like the following.
import numpy as np
np.finfo(np.dtype("float32"))
np.finfo(np.dtype("float64"))
import cv2
Reproduce the code example:
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import numpy
>>> numpy.finfo(numpy.dtype('float32')).smallest_subnormal
/usr/local/lib/python3.8/dist-packages/numpy/core/getlimits.py:499: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
setattr(self, word, getattr(machar, word).flat[0])
/usr/local/lib/python3.8/dist-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
return self._float_to_str(self.smallest_subnormal)
0.000000000000000000000000000000000000000000001
>>> exit()
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import cv2
>>> numpy.finfo(numpy.dtype('float32')).smallest_subnormal
/usr/local/lib/python3.8/dist-packages/numpy/core/getlimits.py:499: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
setattr(self, word, getattr(machar, word).flat[0])
/usr/local/lib/python3.8/dist-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
return self._float_to_str(self.smallest_subnormal)
0.000000000000000000000000000000000000000000001
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.finfo(numpy.dtype('float32')).smallest_subnormal
1e-45
>>> import cv2
>>> numpy.finfo(numpy.dtype('float32')).smallest_subnormal
0.000000000000000000000000000000000000000000001
>>> exit()
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> from numpy.core.getlimits import _get_machar
>>> x = _get_machar(numpy.float32)
>>> x
<numpy.core.getlimits.MachArLike object at 0x7fdd79c9ddc0>
>>> x.smallest_subnormal
array(1.e-45, dtype=float32)
>>> import cv2
>>> x = _get_machar(numpy.float32)
>>> x
<numpy.core.getlimits.MachArLike object at 0x7fdd79c9ddc0>
>>> x.smallest_subnormal
<stdin>:1: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
array(0., dtype=float32)
Error message:
UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
NumPy/Python version information:
import sys, numpy; print(numpy.version, sys.version) 1.22.0 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0] cv2.version ‘4.5.5’
The opencv used was built as part of the docker image available here so this may be related. (However I’ve found 2 reports of this same issue online in the last 2 weeks already) https://github.com/datamachines/cuda_tensorflow_opencv
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Sorry @seberg by “confirmed what’s expected”, I meant the expression
subnormal == 0
was always false beforegevent
import and always true afterwards.FYI I also see this behavior when importing
gevent
. I guess my question is: If I importnumpy
first and callnumpy.finfo(...)
to hydrate the cache before importinggevent
, am I changing numpy’s computational behavior or just suppressing the warning?