question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DOC: issue with opencv altering smallest subnormal for <class 'numpy.float32'>

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
adamhamlincommented, Apr 21, 2022

Sorry @seberg by “confirmed what’s expected”, I meant the expression subnormal == 0 was always false before gevent import and always true afterwards.

1reaction
adamhamlincommented, Apr 21, 2022

FYI I also see this behavior when importing gevent. I guess my question is: If I import numpy first and call numpy.finfo(...) to hydrate the cache before importing gevent, am I changing numpy’s computational behavior or just suppressing the warning?

Read more comments on GitHub >

github_iconTop Results From Across the Web

UserWarning: The value of the smallest subnormal for <class ...
The Numpy warning here is a cryptic way of saying that the current CPU mode is set to round denormal floats to zero....
Read more >
numpy warning with django 4 - 'numpy.float64'> type is zero
That value may not be zero, but it is treated as zero due to cv2 import messing with NumPy. So using the smallest...
Read more >
NumPy User Guide
Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all...
Read more >
Maximum and minimum float values in Python - nkmk note
In Python, the floating-point number type float is a 64-bit representation of a double-precision floating-point number, equivalent to double ...
Read more >
Basic Operations on Images - OpenCV
Goal. Learn to: Access pixel values and modify them; Access image properties; Set a Region of Interest (ROI); Split and merge images.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found