Category Archives: Python

[Solved] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

INSERT INTO report(NAME,keyword,refile,content,DATE,STATUS) VALUES ('%s','%s','%s','%s','%s',0)%("name", "key", "refile", "content",str(datetime.now()))

report errors

ERROR CODE: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key,refile,content,date,status) VALUES ('%s','%s','%s','%s','%s',0)%("name", "ke' at line 1

The keyword of MySQL is used. After modification

    sql="INSERT INTO report(`name`,`keyword`,`refile`,`content`,`date`,`status`) VALUES ('%s','%s','%s','%s','%s',0)"%(name,key, refile, content,str(datetime.now()))

[Solved] pycocotools Install Error: ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing

Recently playing detectron2, need pycocotools >= 2.0.2, python version 3.7, installation error

ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing command pip subprocess to install build dependencies

It is found that pycocotools updated to 2.0.4. The degraded version can be installed normally:

pip install pycocotools==2.0.2

[Solved] theano-GPU Error: pygpu.gpuarray.GpuArrayException: b‘cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY: out of memory

Project scenario:

l2t code needs to use theano library and GPU to accelerate calculation

Non root user ubuntu-18.04 + cuda8 0 (you need to downgrade GCC to match, I’ll configure it as 5.3) + cudnn V5 1+theano 1.0.4+pygpu 0.7.6


Problem description

pygpu.gpuarray.GpuArrayException: b'cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY: out of memory
Training... 2448.24 sec.
Traceback (most recent call last):
  File ".../python3.8/site-packages/theano/compile/function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\
  File "pygpu/gpuarray.pyx", line 689, in pygpu.gpuarray.pygpu_zeros
  File "pygpu/gpuarray.pyx", line 700, in pygpu.gpuarray.pygpu_empty
  File "pygpu/gpuarray.pyx", line 301, in pygpu.gpuarray.array_empty
pygpu.gpuarray.GpuArrayException: b'cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY: out of memory'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "learn.py", line 535, in <module>
    main()
  File "learn.py", line 531, in main
    trainer.train()
  File "..l2t/lib/python3.8/site-packages/smartlearner-0.1.0-py3.8.egg/smartlearner/trainer.py", line 31, in train
  File ../l2t/lib/python3.8/site-packages/smartlearner-0.1.0-py3.8.egg/smartlearner/trainer.py", line 90, in _learning
  File "..l2t/lib/python3.8/site-packages/theano/compile/function_module.py", line 914, in __call__
    gof.link.raise_with_op(
  File "../l2t/lib/python3.8/site-packages/theano/gof/link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "../l2t/lib/python3.8/site-packages/six.py", line 718, in reraise
    raise value.with_traceback(tb)
  File "../l2t/lib/python3.8/site-packages/theano/compile/function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\
  File "pygpu/gpuarray.pyx", line 689, in pygpu.gpuarray.pygpu_zeros
  File "pygpu/gpuarray.pyx", line 700, in pygpu.gpuarray.pygpu_empty
  File "pygpu/gpuarray.pyx", line 301, in pygpu.gpuarray.array_empty
pygpu.gpuarray.GpuArrayException: b'cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY: out of memory'
Apply node that caused the error: GpuAlloc<None>{memset_0=True}(GpuArrayConstant{[[0.]]}, Elemwise{Composite{((i0 * i1 * i2 * i3) // maximum(i3, i4))}}[(0, 0)].0, Shape_i{3}.0)
Toposort index: 98
Inputs types: [GpuArrayType<None>(float64, (True, True)), TensorType(int64, scalar), TensorType(int64, scalar)]
Inputs shapes: [(1, 1), (), ()]
Inputs strides: [(8, 8), (), ()]
Inputs values: [gpuarray.array([[0.]]), array(2383800), array(100)]
Outputs clients: [[forall_inplace,gpu,grad_of_scan_fn}(Shape_i{1}.0, InplaceGpuDimShuffle{0,2,1}.0, InplaceGpuDimShuffle{0,2,1}.0, InplaceGpuDimShuffle{0,2,1}.0, GpuAlloc<None>{memset_0=True}.0, GpuSubtensor{int64:int64:int64}.0, GpuSubtensor{int64:int64:int64}.0, GpuSubtensor{int64:int64:int64}.0, GpuSubtensor{int64:int64:int64}.0, GpuSubtensor{int64:int64:int64}.0, GpuAlloc<None>{memset_0=True}.0, GpuAlloc<None>{memset_0=True}.0, GpuAlloc<None>{memset_0=True}.0, GpuAlloc<None>{memset_0=True}.0, GpuAlloc<None>{memset_0=True}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, Shape_i{1}.0, GRU0_W, GRU0_U, GRU0_Uh, GRU1_W, GRU1_U, GRU1_Uh, InplaceGpuDimShuffle{x,0}.0, GpuFromHost<None>.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{Add}[(0, 1)]<gpuarray>.0, GpuReshape{2}.0, GpuFromHost<None>.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{Add}[(0, 1)]<gpuarray>.0, GpuReshape{2}.0, GpuFromHost<None>.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{add,no_inplace}.0, GpuElemwise{Add}[(0, 1)]<gpuarray>.0, GpuReshape{2}.0, InplaceGpuDimShuffle{1,0}.0, InplaceGpuDimShuffle{1,0}.0, InplaceGpuDimShuffle{x,0}.0, InplaceGpuDimShuffle{1,0}.0, InplaceGpuDimShuffle{1,0}.0, InplaceGpuDimShuffle{1,0}.0, InplaceGpuDimShuffle{1,0}.0, GpuFromHost<None>.0, InplaceGpuDimShuffle{1,0}.0, GpuAlloc<None>{memset_0=True}.0, GpuFromHost<None>.0, GpuAlloc<None>{memset_0=True}.0, GpuFromHost<None>.0, GpuAlloc<None>{memset_0=True}.0)]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

 


Solution:

Reduce gpu__preallocate, I reduced it to 0.8, and optimize it with a small amount of graphs according to the hint when the error is reported


type in the codes

Run successfully!

Raspberry pie Use PCA9685() Error: [Errno 121] Remote I/O error

After I install pca9685() according to the online method, the following errors will appear in the operation.

>>> pwm = Adafruit_PCA9685.PCA9685()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Adafruit_PCA9685' is not defined
>>> import Adafruit_PCA9685
>>> pwm = Adafruit_PCA9685.PCA9685()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685-1.0.1-py3.7.egg/Adafruit_PCA9685/PCA9685.py", line 75, in __init__
    self.set_all_pwm(0, 0)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PCA9685-1.0.1-py3.7.egg/Adafruit_PCA9685/PCA9685.py", line 111, in set_all_pwm
    self._device.write8(ALL_LED_ON_L, on & 0xFF)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_GPIO-1.0.4-py3.7.egg/Adafruit_GPIO/I2C.py", line 116, in write8
    self._bus.write_byte_data(self._address, register, value)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO-1.1.9-py3.7.egg/Adafruit_PureIO/smbus.py", line 327, in write_byte_data
OSError: [Errno 121] Remote I/O error

 

But the premise of this method is to have the Adafruit_PCA9685 file under the path /usr/local/lib/python3.7/dist-packages/”, if there is one, use the above method

My Raspberry Pi is 4B, there are three .egg files in the above path, Adafruit_PCA9685 is placed in it, as shown below

Select the middle of the Adafruit_PCA9685 .egg file, there will be PCA9685.py file, just create PCA9685.py under raspberry home/pi/ and paste the contents of the previous PCA9685.py into the newly created file, import the code to import PCA9685 instead of not import Adafruit_PCA9685

import PCA9685

Open the PCA9685 file and find that it needs to import the I2C.py file (I forgot to take a screenshot), then open the first one under the /usr/local/lib/python3.7/dist-packages/ path

Paste the contents of I2C.py and Plaform.py inside this file into a new I2C.py and Plaform.py file under home/pi, then open the PCA9685.py file in this directory and change the imported I2C in the middle position, it seems to be form Adafurit_GPIO import I2C as I2C directly to import I2C.

Then open the I2C file, found that you need to import Plaform.py, the same operation, change the previous import to import Plaform can be, as shown, and finally in the directory I2C.py PCA9685.py and Plaform.py file

You can write it directly in the raspberry pie file

import PCA9685
pwm = PCA9685.PCA9685()

See if it’s successful. I’m successful.

How to Solve word2vec Module Error: AttributeError & UnicodeDecodeError

1. Decode Issues
1. Error Messages: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xd7 in position 1
2. Error Codes:

Insert # -*- coding: utf8 -*-

def write_review(review, stop_words_address, stop_address, no_stop_address):
    """Write out the processed data"""
    no_stop_review = remove_stop_words(review, stop_words_address)
    with open(no_stop_address, 'a') as f:
        for row in no_stop_review:
            f.write(row[-1])
            f.write("\n")
    f.close()


if __name__ == '__main__':
    segment_text = cut_words('audito_whole.csv')
    review_text = remove_punctuation(segment_text)
    write_review(review_text, 'stop_words.txt', 'no_stop.txt', 'stop.txt')

    # Model Training Master Program
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    sentences_1 = word2vec.LineSentence('no_stop.txt')
    model_1 = word2vec.Word2Vec(sentences_1)

    # model.wv.save_word2vec_format('test_01.model.txt', 'test_01.vocab.txt', binary=False)  # 保存模型,后面可直接调用
    # model = word2vec.Word2Vec.load("test_01.model")  # Calling the model

    # Calculate the list of related words for a word
    a_1 = model_1.wv.most_similar(u"Kongjian", topn=20)
    print(a_1)

    # Calculate the correlation of two words
    b_1 = model_1.wv.similarity(u"Kongjian", u"Houzuo")
    print(b_1)

3. Error parsing: this error means that the character encoded as 0xd7 cannot be parsed during decoding. This is because the encoding method is not specified when saving the text, so the GBK encoding may be used when saving the text
4. Solution: when writing out the data, point out that the coding format is UTF-8, as shown in the following figure

def write_review(review, stop_words_address, stop_address, no_stop_address):
    """Write out the processed data"""
    with open(stop_address, 'a', encoding='utf-8') as f:
        for row in review:
            f.write(row[-1])
            f.write("\n")
    f.close()

    no_stop_review = remove_stop_words(review, stop_words_address)
    with open(no_stop_address, 'a', encoding='utf-8') as f:
        for row in no_stop_review:
            f.write(row[-1])
            f.write("\n")
    f.close()

5. Expansion – why did this mistake happen
first of all, we click the source code file of the error report, as shown in the following figure:
open utils, and we find the following code

it can be seen that the encoding format set by word2vec is UTF-8. If it is not UTF-8 during decoding, errors = ‘strict’ will be triggered, and then UnicodeDecodeError will be reported

2. Attribute error
1 Error reporting: attributeerror: ‘word2vec’ object has no attribute ‘most_similar’
2. Wrong source code:

 # Model Training Master Program
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    sentences_1 = word2vec.LineSentence('no_stop.txt')
    model_1 = word2vec.Word2Vec(sentences_1)

    # model.wv.save_word2vec_format('test_01.model.txt', 'test_01.vocab.txt', binary=False)  # 保存模型,后面可直接调用
    # model = word2vec.Word2Vec.load("test_01.model")  # Calling the model

    # Calculate the list of related words for a word
    a_1 = model_1.most_similar(u"Kongjian", topn=20)
    print(a_1)

**3. Analysis of error reports: * * error reports refer to attribute errors because the source code structure has been updated in the new word2vec. See the official website for instructions as follows:

**4. Solution: * * replace the called object as follows:

 # Model Training Master Program
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
    sentences_1 = word2vec.LineSentence('no_stop.txt')
    model_1 = word2vec.Word2Vec(sentences_1)

    # model.wv.save_word2vec_format('test_01.model.txt', 'test_01.vocab.txt', binary=False)  # 保存模型,后面可直接调用
    # model = word2vec.Word2Vec.load("test_01.model")  # Calling the model

    # Calculate the list of related words for a word
    a_1 = model_1.wv.most_similar(u"Kongjian", topn=20)
    print(a_1)

5. Why is model_1.wv.most_similar(), I didn’t find the statement about WV definition in word2vec. Does anyone know?

[Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘map‘

AttributeError: ‘XXX’ object has no attribute ‘map’

Objects that meet the map format need to be in the form of key value pairs, so check whether the elements using the map function meet this condition.

For example:

#Create an object of DataFrame
list=[[1,1],[2,2]]
list=pd.DataFrame(list)
print(list)

#Use map to map them
for _, row in list.iterrows():
    row[0]=row[0].map(lambda a:a+1)

Output:

correct method:

list=[[1,1],[2,2]]
list=pd.DataFrame(list)
print(list)

list[0]=list[0].map(lambda a:a+1)
print(list)

Output:

[Solved] Pycharm Use pip to install package error: no such option –build-dir

Pychar reports an error when using PIP to install the package. No such option — build dir

    1. reason: the PIP version is too high.
    1. Solution: downgrade the pip version and wait for the update of pycharm

open the command line, find the location of Python and enter: 

PIP install PIP = = 20.2.4
set the PIP version to 20.2.4

if no variable is set for PIP, Check the location of Python in settings->python Interpreter

and then execute the following command on the command line </ OL>

/usr/local/bin/python3. 8 -m pip install pip==20.2.4

[Solved] Python pytesseract(tesseract OCR) Error: TesseractNotFoundError

Error when running code

Use PIP install pytesseract

pip install tesseract

It still doesn’t work after installation. The same error is reported

Subsequent error finding:

Testseract-ocr is not installed

OCR (Optical Character Recognition): Optical Character Recognition, which refers to the process of analyzing, recognizing, and acquiring text in image files.
Tesseract: An open source OCR recognition engine. The initial Tesseract engine was developed by HP Labs, and later contributed to the open source software industry, and was later improved by Google to eliminate bugs, optimize, and re-release.

http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe Can be downloaded

After installation, add before the error code

pytesseract.pytesseract.tesseract_cmd = 'E:\\software\\Tesseract-OCR\\tesseract.exe'

This is followed by the path to install Tesseract-ocr

Successfully solved.

[Solved] torchsummary Error: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.F

Source code:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torchvision.models.resnet18(pretrained=None)
model.fc = nn.Linear(512, 10)
    
summary(model, input_size=[(3, 224, 224)], batch_size=256, device="cuda")

Error Messages: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
Solution:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torchvision.models.resnet18(pretrained=None)
model.fc = nn.Linear(512, 10)

model = model.to(device)  # add this line will be OK

summary(model, input_size=[(3, 224, 224)], batch_size=256, device="cuda")

[Solved] mmdetection Error: ImportError: /home/user/repos/mmdetection/mmdet/ops/dcn/deform_conv_cuda.cpython-37m-x

Environment configuration: torch 1.11.0 + CUDA 11.3 (latest)

Use mmdetection to infer:

from mmdet.apis import init_detector, inference_detector

Errors are reported as follows:

ImportError: /home/user/repos/mmdetection/mmdet/ops/dcn/deform_conv_cuda.cpython-37m-x86_64-linux-gnu.so: undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_32E

 

The problem has been solved. The reason for the error is that the pytorch version is too new. Although openmmlab supports the latest version, it will still cause the error.

Solution:

Degrade the pytorch version to torch 1.6.0 + cu102, query the official GitHub of openmmlab, uninstall and reinstall mmcv 1.3.9, and re run the mmdetection code to solve the error.

[Solved] supervisor Error: /usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:22: UserWarning: Setuptools will stop working on Python 2

An error is reported when installing supervisor. The information is as follows

web:~# supervisorctl status
/usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:22: UserWarning: Setuptools will stop working on Python 2
************************************************************
You are running Setuptools on Python 2, which is no longer
supported and
>>> SETUPTOOLS WILL STOP WORKING <<<
in a subsequent release (no sooner than 2020-04-20).
Please ensure you are installing
Setuptools using pip 9.x or later or pin to `setuptools<45`
in your environment.
If you have done those things and are still encountering
this message, please comment in
https://github.com/pypa/setuptools/issues/1458
about the steps that led to this unsupported combination.
************************************************************
  sys.version_info < (3,) and warnings.warn(pre + "*" * 60 + msg + "*" * 60)

Solution:
uninstall and reinstall pip, setuptools and wheel to return them to the version corresponding to the system Python
python3 reference:

python3 -m pip uninstall pip setuptools wheel
sudo apt-get --reinstall install  python3-setuptools python3-wheel python3-pip

Python2 reference:

python -m pip uninstall pip setuptools wheel
sudo apt-get --reinstall install python-setuptools python-wheel python-pip 

[Solved] pytorc Error: RuntimeError: one of the variables needed for gradient computation has been modified by an

Error reported by
pytroch when modifying the network:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1600, 16, 256]], which is output 0 of CudnnConvolutionBackward, is at version 1; expected version 0 instead

 

Solution 1:

Check all operations of the tensor that is reported as an error. If there is an addition or subtraction operation of x + = m, x = x + m, all operations shall be changed to the following format:

x = x.clone() + m