Category Archives: Python

Raspberry pie import opencv error: ImportError: numpy.core.multiarray failed to import

After installing opencv with PIP3, an error is reported when importing CV2 through Python 3, as follows:

 from .cv2 import *
ImportError: numpy.core.multiarray failed to import

Beacuse the version of NumPy library is too low or too high, which is not suitable for the newly installed opencv version
if we use pip to install the latest version of OpenCV, that is, the installation command:

pip3 install opencv-python
pip3 install opencv_contrib-python

It means that the numpy version is too low, so we should install the latest version:

Command: pip3 install -U numpy

After installation, you can import OpenCV

[Solved] mnn Import Error: initMNN: init numpy failed

import MNN Error:

initMNN: init numpy failed

Solution:
pip uninstall numpy
pip install numpy

Error Messages:

D:\Anaconda3\lib\site-packages\numpy\_distributor_init.py:32: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda3\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
D:\Anaconda3\lib\site-packages\numpy\.libs\libopenblas.XWYDX2IKJW2NMTWSFYNGFUWKQU3LYTCZ.gfortran-win_amd64.dll

 

mnn compile succefully:

Start to Convert Other Model Format To MNN Model...
[22:16:38] :30: ONNX Model ir version: 4
Start to Optimize the MNN Net...
inputTensors : [ images, ]
outputTensors: [ output, ]
Converted Success!

However, even so, MNN reasoning may report errors

Export failed:

Start to Convert Other Model Format To MNN Model...
[22:19:55] :30: ONNX Model ir version: 4
Start to Optimize the MNN Net...
inputTensors : [ images, ]
outputTensors: [ Transpose156, ]
Converted Success!

DLL load failed while importing rdBase [How to Solve]

At first, there was no problem with using rdkit. When deep Chem is installed later, this error will be reported,

DLL load failed while importing rdBase

The rdkit package may be damaged, so you can reinstall the rdkit. The installation code is as follows

conda install -c rdkit rdkit

However, it is recommended to install deepchem in a separate virtual environment, it will affect many of the previously installed packages, such as numpy and so on

[Solved] OpenCV Import Error: ImportError: numpy.core.multiarray failed to import

cv2 import Error:

(paddle) C:\Windows\system32>python
Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “D:\anaconda3\envs\paddle\lib\site-packages\cv2\__init__.py”, line 8, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
>>> exit()

Solution:

pip install -U numpy 

New problems,

(paddle) C:\Windows\system32>pip install -U numpy
Requirement already satisfied: numpy in d:\anaconda3\envs\paddle\lib\site-packages (1.19.3)
Collecting numpy
  Downloading numpy-1.22.3-cp38-cp38-win_amd64.whl (14.7 MB)
     |████████████████████████████████| 14.7 MB 547 kB/s
Installing collected packages: numpy
  Attempting uninstall: numpy
    Found existing installation: numpy 1.19.3
    Uninstalling numpy-1.19.3:
      Successfully uninstalled numpy-1.19.3
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
paddlepaddle-gpu 2.2.2 requires numpy<=1.19.3,>=1.13; python_version >= "3.5" and platform_system == "Windows", but you have numpy 1.22.3 which is incompatible.
openvino 2021.4.2 requires numpy<1.20,>=1.16.6, but you have numpy 1.22.3 which is incompatible.
openvino-dev 2021.4.2 requires numpy<1.20,>=1.16.6, but you have numpy 1.22.3 which is incompatible.
Successfully installed numpy-1.22.3

However, I tested it and found that this new error has not caused too many problems.

To meet compatibility, if you want to change back to the version, you can

pip install numpy==1.19.3
pip uninstall opencv-contrib-python
conda install opencv

Note that the following instructions will install (uninstall) the latest version of OpenCV,

pip (un)install opencv-contrib-python

 

Reference:

https://stackoverflow.com/questions/20518632/importerror-numpy-core-multiarray-failed-to-import

[Solved] Pandas rename Error: ValueError: operands could not be broadcast together with shapes (1,2) (3,) (1,2)

Pandas rename reported an error valueerror

error message

ValueError: operands could not be broadcast together with shapes (1,2) (3,) (1,2) 

Solution:

The initial investigation is that the pandas version is low, and the pandas version needs to be updated.初步排查是pandas版本较低导致,需要更新pandas版本。

Reference version

Current version: Pandas 1.1.3 updated version: Pandas 1.4.1

Pytorch Run Error: BrokenPipeError [How to Solve]

The problem occurs when doing machine learning training.

Reason for the error: The problem is due to multi-threading under windows, and is related to the DataLoader class. Just change the number of num_workers to 0. This is a bug in windows.

The error code can be referred to:

trainLoader=torch.utils.data.DataLoader(trainSet,batch_size=Bach_Size,shuffle=True,num_workers=2)
testLoader=torch.utils.data.DataLoader(testSet,batch_size=Bach_Size,shuffle=True,num_workers=2)

Set The number of num_ workers to 0

trainLoader=torch.utils.data.DataLoader(trainSet,batch_size=Bach_Size,shuffle=True,num_workers=0)
testLoader=torch.utils.data.DataLoader(testSet,batch_size=Bach_Size,shuffle=True,num_workers=0)

[Solved] Torch Build Module Error: NotImplementedError

It’s probably such an error reporting method. I’ve been using torch for so many years. I first encountered this error NotImplementedError
I’m not using a nightly version

Traceback (most recent call last):

  File "xxxxx\x.py", line 268, in <module>
    print(x(y).shape)

  File "xxxxx\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)

  File "xxxxx\x.py", line 259, in forward
    x = self.features(x)

  File "xxxxx\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)

  File "xxxxx\lib\site-packages\torch\nn\modules\container.py", line 119, in forward
    input = module(input)

  File "xxxxx\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)

  File "xxxxx\lib\site-packages\torch\nn\modules\module.py", line 201, in _forward_unimplemented
    raise NotImplementedError

NotImplementedError

Call self.forward in _call_impl

result = self.forward(*input, **kwargs)

If you inherit nn.Module, and if you don’t implement self.forward, it will

raise NotImplementedError

It turns out that when I use this function, I really don’t have the forward method:

class Hswish(nn.Module):

    def __init__(self, inplace=True):
        super(Hswish, self).__init__()
        self.inplace = inplace

    def __swish(self, x, beta, inplace=True):
        # But this swish is not used by H-swish
        # The reason it's called H-swish is to make the sigmoid hard
        # approximated by Relu6(x+3)/6
        # Reduced computational effort for embedded deployment
        return x * F.sigmoid(beta * x, inplace)

    @staticmethod
    def Hsigmoid(x, inplace=True):
        return F.relu6(x + 3, inplace=inplace)/6

    def foward(self, x):
        return x * self.Hsigmoid(x, self.inplace)

forward Write as foward

Python Kmeans Error: ConvergenceWarning: Number of distinct clusters (99) found smaller than n_clusters (100).

The following errors occur when using the kmeans method for clustering. My default setting is 100 categories, and there are more than 100 data. The following errors are reported in the clustering process:

ConvergenceWarning: Number of distinct clusters (99) found smaller than n_clusters (100). Possibly due to duplicate points in X.

First locate the error code:

kmeans = KMeans(n_clusters=k, random_state=2018)
kmeans.fit(XData)
 pre_y = kmeans.predict(XData)

It is probably one of these three items. Finally, it is found that the prompt appears in the fit() function. Use the try method to grab the error, locate the breakpoint for debugging, and grab the ConvergenceWarning error:

    # Build the clustering model object
    kmeans = KMeans(n_clusters=k, random_state=2018)
    # Train the clustering model
    try:
        kmeans.fit(XData) # If the data has duplicates, the data will be smaller than the K value when fitting, so the K value needs to be updated
    except ConvergenceWarning:
        print("Catch ConvergenceWarning,k={}\n".format(k))
    except:
        print("k={}\n".format(k))

    # Predictive Clustering Model
    pre_y = kmeans.predict(XData)

After checking, the outgoing line is caused by repeated XData data, so the K value is determined in the early stage, and the problem can be solved by repeating it here
previous K value determination Code:

XData = np.array(X)
if(XData.shape[0]<=k and XData.shape[0]!=0):
        print("XData size:",XData.shape[0])
        k=XData.shape[0]//2
        print("K:",k)
        if(k<=0):
            return result

The changed code is de duplicated by using set

XData = np.array(list(set(X)))#2022.3.22 zph comes with a de-duplication effect
if(XData.shape[0]<=k and XData.shape[0]!=0):
        print("XData size:",XData.shape[0])
        k=XData.shape[0]//2
        print("K:",k)
        if(k<=0):
            return result

The above is hereby recorded for reference by those who have the same problems.

[Solved] Arcpy: RuntimeError: ERROR 999998: Unexpected Error.

Summarize the reasons why individuals encounter such errors when using arcpy to batch process raster data:

1. Because the supporting python of ArcGIS is 2.7, sometimes the file name is read in some hexadecimal codes with many slashes, resulting in the wrong naming of the output file name and unable to output

Solution: carefully check whether the input and output file names are correct

2. There are many files with different suffixes (for example, raster data also has files with .DBF suffix) in addition to the files that can be seen in ArcMap, so it is inevitable to make mistakes when reading in and reading out some suffix files in batch processing.

Solution: when batch processing, sort out the folders in advance, and put all kinds of or similar data together. Don’t mix them.

Python Error: SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3:

Reason: Window can read files with \, but in the string \ is used as an escape character, after the escape may not find the path of the resource, for example \t will be escaped as tab key

Upper Code:

>>> def func1(path_name):
...     import os
...     if os.path.exists(path_name):
...         return "True"
...     else:
...         return "False"
...
>>> func1("C:\Users\renyc")#error
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>>
In this example: "C:\Users\renyc" is escaped and the resource of the path is not found.

Solution:

>>> def func1(path_name):
...     import os
...     if os.path.exists(path_name):
...         return "True"
...     else:
...         return "False"
...
>>> func1(r"C:\Users\renyc")#Add r to declare the string without escaping
'True'
>>> func1("C:\\Users\\\renyc")# handling of absolute paths
'True'
>>>

 

There are three ways to summarize.

One:Replace the write method with an absolute path func1(“C:\Users\renyc”)

Two: explicitly declare the string without escaping (add r) func1(r “C:\Users\renyc”)

III:Use Linux paths/ func1(“C:/Users/renyc”)

[Solved] Pycham Error: non zero exit code (2)

preface

When using pycharm to install a third-party package in the python virtual environment in Python interpreter settings, it is likely to report an error: non zero exit code (2), as shown in the figure:

even upgrading pip will report an error, as shown in the figure:

Tips are as follows:

Try to run this command from the system terminal. 
Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 
'D:\Program Files\Python\py36-basic-v\Scripts\python.exe'.

As above belongs, the official says the possible reason is that pip is not installed in the specified directory of the virtual environment, and suggests running the pip command in the terminal to install the third-party package.

The official hint has a certain degree of reasonableness, but it does not try all cases, you can check whether the pip module exists in the directory of the virtual environment, if it does not exist, the pip module can be installed in the specified directory; if it exists, it is not the problem of the pip module path, so what exactly is the reason?

After investigation, it is the problem of pip version, my Python version is 3.6, pip version is 21.3.1, this version of pip will definitely appear the problem, the solution is also very simple, the pip version can be downgraded to 20.2.4, operation method see the following program 2: Downgrade pip version. (It is not recommended to upgrade pip, because new versions of pip may have the same problem)

Solution 1 (recommended): install the third-party package using the terminal terminal

Click Terminal at the bottom of pycharm toolbar , as shown in the following figure:

enter the PIP install command to install the third-party package, as shown in the figure:

But this solution treats the symptom but not the root cause, the problem is not solved, only the installation of third-party packages by alternative methods, if you want to solve the problem at all, please see solution 2 below.

Solution 2 (fundamental solution): downgrade the PIP version

First, open a project using pycharm. If there is no Python environment, you need to create a virtual environment first, as shown in the following figure:

In file - Settings - Python interpreter, we can see that the version of PIP in Python virtual environment is 21.3.1. As shown in the figure below, we need to downgrade the version of Pip to 20.2.4

Click Terminal on the toolbar at the bottom of pychart, as shown in the following figure:

enter the following command. Note that there must be Python -m, otherwise there is no permission:

python -m pip install pip==20.2.4

After entering the command, the following prompt appears to prove that the downgrade is successful:

enter the command PIP - V to view the current PIP version, as shown in the following figure:

the problem has been successfully solved so far. The third-party package can be successfully installed in Settings, as shown in the following figure: