Search code examples
pythonnumpyattributeerror

AttributeError: module 'numpy' has no attribute 'int' ---> ValueError: Buffer dtype mismatch, expected 'float32_t' but got 'double'


Code

I'm running a Python code which has the following statements:

            # do NMS
            dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
            keep = nms(dets, 0.3) # -> *** Error is thrown here :(
            dets = dets[keep, :]

Data

The data shown by the debugger is:

Debugger steppper

Debugger data

Error

Eventually, the code throws this error:

AttributeError: module 'numpy' has no attribute 'int'.

At this line of code:

from .nms.cpu_nms import cpu_nms, cpu_soft_nms

def nms(dets, thresh):
    """Dispatch to either CPU or GPU NMS implementations."""

    if dets.shape[0] == 0:
        return []
    return cpu_nms(dets, thresh) # -> *** Error is thrown here :(

Tried 1

I don't see any usage of np.int but I see that np.float32 is used as can be seen above. As suggested here, I replaced np.float32 with np.float32_. Then I get another error:

AttributeError: module 'numpy' has no attribute 'float32_'

Tried 2

I replaced np.float32 with np.float_, then this error is received:

File "nms/cpu_nms.pyx", line 17, in nms.cpu_nms.cpu_nms

ValueError: Buffer dtype mismatch, expected 'float32_t' but got 'double'

Question

What else I can try to resolve the error?


Solution

  • Numpy version mismatch

    I doublechecked the Numpy version on my activated Conda environment, it was 1.26.4:

    python -c "import numpy; print(numpy.version.version)"
    1.26.4
    

    It was strange since I had installed version 1.23 as suggested by the repository:

    Suggested Numpy version

    Conda not using Numpy from activated env

    So, I figured out that for some reason, the Conda environment is using Numpy from:

    /home/arisa/.local/bin/pip3
    

    Rather than from the activated env:

    /home/arisa/.conda/envs/mononphm/bin/pip3
    

    Solution

    So, I removed all the Numpy versions from the system altogether:

    pip3 uninstall numpy
    

    And re-installed the version suggested by the repository inside the Conda env:

    /home/arisa/.conda/envs/mononphm/bin/pip3 install numpy==1.23
    

    Now the error is resolved :)

    Note that I had previously run into a similar problem before: ModuleNotFoundError: No module named 'mononphm'