Tag Archives: python

[Solved] Python sqlite3.OperationalError: near “?“: syntax error

code

con = sqlite3.connect('db.sqlite3')
cur = con.cursor()
sql = 'SELECT id FROM ?WHERE source_id = ?'
cur.execute(sql, ('article_table', 41945,))
one = cur.fetchone()
con.close()

report errors

sqlite3.OperationalError: near "?": syntax error

reason

sqlite3 placeholder (?) Cannot be used for column or table names.
Placeholders are used to insert or retrieve values of data from the database in order to prevent SQL injection.

Solution:

con = sqlite3.connect('db.sqlite3')
cur = con.cursor()
sql = 'SELECT id FROM article_table WHERE source_id = ?'
cur.execute(sql, (41945,))
one = cur.fetchone()
con.close()

[Solved] raise KeyError(key) from err KeyError: ‘Dates‘

Description of error reporting:

Today, when reading Excel data and processing the data, an error is reported as follows:

Error reason:

The Excel table data read in by pandas is not aligned. Please check the Excel table data read in
I print out the dataframe after reading it into Excel

Solution:

Delete Sheet2 and Sheet3 in the Excel table, so that Pandas will align after reading the Excel table data

The root cause of such problems is that the data is not aligned

Debug method: 1. Print out the dataframe data and check the format of the data after it is read in. 2. Then adjust the read data

[Solved] RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm

Problem

After training to a certain number of iterations, an error is reported:
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling cublasSgemm( handle, opa, opb, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)

Possible causes

  • The shape dimension does not match
  • Variables are not on the same device
  • pytorch and cuda versions do not match

Solution

Add os.environ['CUDA_VISIBLE_DEVICES'] = '0' at the beginning of the train.py file, and set device='cuda'.
But there is a strange phenomenon: if you do not set the visible gpu, but specify device='cuda:0', it will also report an error.

[Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

yolov5 Error: RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

Solution:
In model/yolo.py file

        for mi, s in zip(m.m, m.stride):  # from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            b[:, 4] += math.log(8/(640/s) ** 2)  # obj (8 objects per 640 image)
            b[:, 5:] += math.log(0.6/(m.nc - 0.99)) if cf is None else torch.log(cf/cf.sum())  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

Add with torch.no_grad(): as follows

        for mi, s in zip(m.m, m.stride):  # from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            with torch.no_grad():
                b[:, 4] += math.log(8/(640/s) ** 2)  # obj (8 objects per 640 image)
                b[:, 5:] += math.log(0.6/(m.nc - 0.99)) if cf is None else torch.log(cf/cf.sum())  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

——> The root cause is to add:

with torch.no_grad():

[Solved] AttributeError: module ‘time‘ has no attribute ‘clock‘

preface:

When the blogger runs the previous code, he encounters this problem because time method is not available in the higher version of Python.

Running code:

Error:

Traceback (most recent call last):
  File "/media/gis/DISCK_21/code/CoANet-main/process/create_crops.py", line 139, in <module>
    main()
  File "/media/gis/DISCK_21/code/CoANet-main/process/create_crops.py", line 118, in main
    start = time.clock()
AttributeError: module 'time' has no attribute 'clock'

Solution:

start = time.time()

[Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……

Project Scenarios

Run the opencv official website example code facedetect.py

#!/usr/bin/env python

'''
face detection using haar cascades

USAGE:
    facedetect.py [--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv

# local modules
from video import create_capture
from common import clock, draw_str


def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
                                     flags=cv.CASCADE_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv.rectangle(img, (x1, y1), (x2, y2), color, 2)

def main():
    import sys, getopt

    args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
    try:
        video_src = video_src[0]
    except:
        video_src = 0
    args = dict(args)
    cascade_fn = args.get('--cascade', "data/haarcascades/haarcascade_frontalface_alt.xml")
    nested_fn = args.get('--nested-cascade', "data/haarcascades/haarcascade_eye.xml")

    
    cascade = cv.CascadeClassifier(cv.samples.findFile(cascade_fn))
    nested = cv.CascadeClassifier(cv.samples.findFile(nested_fn))
    cam = create_capture(video_src, fallback='synth:bg={}:noise=0.05'.format(cv.samples.findFile('data/lena.jpg')))#此处的data文件夹是从opencv示例simple包中复制到该项目中。

    
    while True:
        _ret, img = cam.read()
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        gray = cv.equalizeHist(gray)

        t = clock()
        rects = detect(gray, cascade)
        vis = img.copy()
        draw_rects(vis, rects, (0, 255, 0))
        if not nested.empty():
            for x1, y1, x2, y2 in rects:
                roi = gray[y1:y2, x1:x2]
                vis_roi = vis[y1:y2, x1:x2]
                subrects = detect(roi.copy(), nested)
                draw_rects(vis_roi, subrects, (255, 0, 0))
        dt = clock() - t

        draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
        cv.imshow('facedetect', vis)

        if cv.waitKey(5) == 27:
            break

    print('Done')

if __name__ == '__main__':
    print(__doc__)
    main()
    cv.destroyAllWindows()

Problem description

Running the above code reports the following error:

[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\core\src\utils\samples.cpp (61) cv::samples::findFile cv::samples::findFile('data/haarcascades/haarcascade_frontalface_alt.xml') => ''
Traceback (most recent call last):
  File "E:\Pycharm\Pycharm7\facedetect.py", line 81, in <module>
    main()
  File "E:\Pycharm\Pycharm7\facedetect.py", line 48, in main
    cascade = cv.CascadeClassifier(cv.samples.findFile(cascade_fn))
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\utils\samples.cpp:64: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: data/haarcascades/haarcascade_frontalface_alt.xml in function 'cv::samples::findFile'

Cause analysis:

According to the error location:

cascade = cv.CascadeClassifier(cv.samples.findFile(cascade_fn))

It can be found that the file paths of “haarcascade_frontalface_alt.xml” and “data/haarcascades/haarcascade_eye.xml” are not correct.


Solution:

“haarcascade_frontalface_alt.xml” and “data/haarcascades/haarcascade_eye.xml” are under the cv2 package installed in python, just change the path in the code to a relative path.

The code is changed as follows:

cascade_fn = args.get('--cascade', "D:/Python/yingyong/Lib/site-packages/cv2/data/haarcascades/haarcascade_frontalface_alt.xml")
    nested_fn  = args.get('--nested-cascade', "D:/Python/yingyong/Lib/site-packages/cv2/data/haarcascades/haarcascade_eye.xml")

Run again to run normally.

[Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation

source code

	def anim(i):
		# update SMBLD
		cur_beta_idx, cur_step = i // num_steps, i % num_steps
		val = shape_range[cur_step]
		mesh.multi_betas[0, cur_beta_idx] = val  # Update betas
		fig.suptitle(f"{name.title()}\nS{cur_beta_idx} : {val:+.2f}", fontsize=50)  # update text

		return dict(mesh=mesh.get_meshes(), equalize=False)


Modified code

Add with torch.no_grad(): will be OK!

	def anim(i):
		# update SMBLD
		cur_beta_idx, cur_step = i // num_steps, i % num_steps
		val = shape_range[cur_step]
		#print("\ncur_beta_idx:",cur_beta_idx,mesh.multi_betas[0, cur_beta_idx])
		with torch.no_grad():###添加
			mesh.multi_betas[0, cur_beta_idx] = val  # Update betas
		fig.suptitle(f"{name.title()}\nS{cur_beta_idx} : {val:+.2f}", fontsize=50)  # update text

		return dict(mesh=mesh.get_meshes(), equalize=False)

[Solved] AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘

1. Question:

When learning the selenium part of the crawler, AttributeError appears: ‘WebDriver’ object has no attribute ‘find_ element_ by_ Id ‘problem.

2. Reasons:

Because of version iteration, find_element_by_id method is not supported in the new version of selenium.

3. Solution:

Modify button = browser.find_element_by_id(‘su’) to the following codes:

button = browser.find_element(By.ID,'su')

Add the following code at the front of the code page,

from selenium.webdriver.common.by import By

[Solved] AttributeError: module ‘tensorboard.summary._tf.summary‘ has no attribute ‘merge‘

The environment is TensorFlow2.9

The code to be run on github The TensorFlow environment is 1.x

Many apis cannot be called

Original code

train_summary_op = tf.summary.merge([loss_summary])

Error reporting in method 1

train_summary_op = tf.compat.v1.summary.merge([loss_summary])

display

TypeError: Tensors in list passed to 'inputs' of 'MergeSummary' Op have types [bool] that do not match expected type string.

If it can’t be solved, I will try

import tensorflow as tf

Modified as

import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()

Finally, solve the problem successfully

[Solved] TUM associate.py Scripte Error: AttributeError: ‘dict_keys‘ object has no attribute ‘remove‘

Running the script file reports an error


Cause of error.
Version difference between python2 and python3
In Python 3, dict.keys() returns the dict_keys object without the remove method. Unlike Python 2, Python 2 dict.keys() returns the list object.
In Python 3, dict.keys() returns a dict_ keys object (a view of the dictionary) which does not have remove method; unlike Python 2, where dict.keys() returns a list object.

 

Solution:
(1) Directly run with Python 2 forcely:

python2 associate.py rgb.txt depth.txt > associate.txt

(2) Still run it in python 3. Make a small change to the original source code

	for diff, a, b in potential_matches:
        if a in first_keys and b in second_keys:
            first_keys.remove(a)
            second_keys.remove(b)
            matches.append((a, b))
    
    matches.sort()
    return matches

Add two sentences above to change dict into list

	# two new lines
	first_keys = list(first_keys)
    second_keys = list(second_keys)
    for diff, a, b in potential_matches:
        if a in first_keys and b in second_keys:
            first_keys.remove(a)
            second_keys.remove(b)
            matches.append((a, b))
    
    matches.sort()
    return matches

Run again (Python here points to python3 by default)

python associate.py rgb.txt depth.txt > associate.txt

[Solved] ERROR: URL ‘s3://‘ is supported but requires these missing dependencies: [‘s3fs‘]. To install dvc wi

0. Preface
dvc error log

1. Main text
1.1 Problems

ERROR: URL 's3://' is supported but requires these missing dependencies: ['s3fs']. To install dvc with those dependencies, run:

	pip install 'dvc[s3]'

See <https://dvc.org/doc/install> for more info.

Enter as prompted

pip install 'dvc[s3]'

It doesn’t work…

1.2 Solutions

conda install -c conda-forge mamba
mamba install -c conda-forge dvc-s3

reference resources

[1] https://blog.csdn.net/scgaliguodong123_/article/details/122781190

department