Python编程中常见问题汇总

问题1:执行df.shape()报错

  • 解决方法:
    shape[0]就是读取矩阵第一维度的长度,相当于行数。它的输入参数可以是一个整数表示维度,也可以是一个矩阵。shape函数返回的是一个元组tuple,表示数组(矩阵)的维度/形状:
    w.shape[0]返回的是w的行数;
    w.shape[1]返回的是w的列数;
    df.shape():查看行数和列数。

问题2:提示找不到Sequential
Python编程中常见问题汇总_第1张图片

  • 解决方法: 重新安装pip install keras==2.6.0

问题3:windows环境jupyter的文件路径

  • 第一种方式: 在开始栏中找到jupyter Notebook

Python编程中常见问题汇总_第2张图片Python编程中常见问题汇总_第3张图片

  • 第二种方式:
    Python编程中常见问题汇总_第4张图片

问题4: size.width>0 && size.height>0 in function ‘cv::imshow’

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
  • 解决方法:经排查是,文件的路径有中文,因此去掉修改中文路径为英文路径

问题5:提示mkl-service package failed to import

C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py:143: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
  • 解决方法:
    第一步: 先配置三个环境变量: E:\Anaconda3、 E:\Anaconda3\Scripts、E:\Anaconda3\Library\bin
    第二步: 将E:\Anaconda3\Library\bin下ibcrypto-1_1-x64.dll和libssl-1_1-x64.dll,以上两个文件复制到 E:\Anaconda3\DLLs

问题6:None of the MLIR Optimization Passes are enabled (registered 2)

C:\ProgramData\Anaconda3\python.exe C:/Users/Administrator/PycharmProjects/pythonProject/单个神经网络实现预测.py
2022-06-18 17:15:58.250081: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-06-18 17:15:58.250363: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
data.shape: (24809, 3)
2022-06-18 17:16:39.556612: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2022-06-18 17:16:39.564416: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2022-06-18 17:16:39.720073: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: USER-20220309LD
2022-06-18 17:16:39.720503: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: USER-20220309LD
2022-06-18 17:16:39.861162: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
================================================================
dense (Dense)                (None, 1)                 2         
================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________
2022-06-18 17:16:45.608803: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/1000
forrtl: error (200)
  • 解决方法:重新启动电脑

问题7 :Failed to open NetParameter file

`cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\darknet\darknet_importer.cpp:210: error: (-212:Parsing error) Failed to open NetParameter file: E:\yoloyolov3.cfg in function 'cv::dnn::dnn4_v20220524::readNetFromDarknet'`
  • 解决方法:更换路径

问题8: IndexError: index 80 is out of bounds for axis 0 with size 80

Python编程中常见问题汇总_第5张图片

  • 解决方法:待补充

问题9: ERROR: No matching distribution found for numpy<1.19.0,>=1.16.0

  • 解决方法:
    pip install numpy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
    pip install numpy==1.18.3

问题10:执行U-net时,提示文件或路径不存在

在这里插入图片描述
在这里插入图片描述

  • 解决方法:修改路径。windows环境和MAC环境文件执行路径不同。
修改前
midname = imgname[imgname.rindex("/")+1:]

修改后
midname = imgname[imgname.rindex("\\")+1:]

问题11:AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’

Using TensorFlow backend.
loading data
------------------------------
load train images...
------------------------------
------------------------------
load test images...
------------------------------
loading data done
Traceback (most recent call last):
  File "E:/test/Unet-master/unet.py", line 174, in <module>
    myunet.train()
  File "E:/test/Unet-master/unet.py", line 154, in train
    model = self.get_unet()
  File "E:/test/Unet-master/unet.py", line 89, in get_unet
    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 446, in __call__
    self.assert_input_compatibility(inputs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 310, in assert_input_compatibility
    K.is_keras_tensor(x)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 695, in is_keras_tensor
    if not is_tensor(x):
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 703, in is_tensor
    return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)
AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'
  • 解决方法:查看 keras和tensorflow的版本是否匹配;keras和tensorflow都用2.6.0版本

问题12:ImportError: cannot import name ‘get_config’ from 'tensorflow.python.eager.context

C:\ProgramData\Anaconda3\python.exe E:/test/Unet-master/unet.py
Traceback (most recent call last):
  File "E:/test/Unet-master/unet.py", line 2, in <module>
    from keras.callbacks import ModelCheckpoint
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\__init__.py", line 24, in <module>
    from keras import models
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\models\__init__.py", line 18, in <module>
    from keras.engine.functional import Functional
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\functional.py", line 23, in <module>
    from keras import backend
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend.py", line 39, in <module>
    from tensorflow.python.eager.context import get_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\context.py)
  • 解决方法:执行命令
pip install tensorflow==2.6.0  -i  http://mirrors.aliyun.com/pypi/simple/ --trusted-host   mirrors.aliyun.com

参考内容:

TF1.x
tensorflow版本 keras版本
TensorFlow 1.13 Keras 2.2.4
TensorFlow 1.14 Keras 2.2.5
TensorFlow 1.15 Keras 2.2.5

TF2.x
tensorflow版本 keras版本
TensorFlow 2.0.0 Keras 2.3.1
TensorFlow 2.1.0 Keras 2.3.1
TensorFlow 2.2.0 Keras 2.3.1
TensorFlow 2.4.0 Keras 2.4.3
TensorFlow 2.6.0 Keras 2.6.0

问题13:cannot import name ‘array_to_img’ from ‘keras’

C:\ProgramData\Anaconda3\python.exe E:/test/Unet-master/unet.py
2022-06-27 23:47:40.946200: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-06-27 23:47:40.946200: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "E:/test/Unet-master/unet.py", line 6, in <module>
    from keras import array_to_img
ImportError: cannot import name 'array_to_img' from 'keras' (C:\ProgramData\Anaconda3\lib\site-packages\keras\__init__.py)
  • 解决方法:安装对应的keras包

问题14:安装依赖包报错

Python编程中常见问题汇总_第6张图片

  • 解决方法: 添加 -r ;如:pip install -r E:\test\requirements.txt

  • 加快下载的速度的方法:

 pip install  -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple  -r E:\test\requirements.txt

问题15:执行python E:\test\detect.py --source E:\test\v_test.mp4 --weights
yolov5s.pt报AttributeError: ‘Upsample’ object has no attribute
‘recompute_scale_factor’

报错具体信息:

(base) E:\test\yolov5-master>
(base) E:\test\yolov5-master>
(base) E:\test\yolov5-master>python detect.py --sourc
e E:\test\yolov5-master\data\mp_vedio\v_test.mp4 --we
ights yolov5s.pt
Downloading https://ultralytics.com/assets/Arial.ttf to C:\Users\Administrator\A
ppData\Roaming\Ultralytics\Arial.ttf...
detect: weights=['yolov5s.pt'], source=E:\test\yolov5
-master\data\mp_vedio\v_test.mp4, imgsz=[640, 640], conf_thres=0.25, iou_thres=0
.45, max_det=1000, device=cpu, view_img=False, save_txt=False, save_conf=False,
save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False,
visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, li
ne_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5  2021-10-19 torch 1.11.0+cpu CPU

Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients
video 1/1 (1/511) E:\test\yolov5-master\data\mp_vedio
\v_test.mp4: Traceback (most recent call last):
  File "detect.py", line 310, in <module>
    main(opt)
  File "detect.py", line 305, in main
    run(**vars(opt))
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\autograd\grad_mode.py",
 line 27, in decorate_context
    return func(*args, **kwargs)
  File "detect.py", line 154, in run
    pred = model(img, augment=augment, visualize=visualize)[0]
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py",
line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "E:\test\yolov5-master\models\yolo.py", line 1
26, in forward
    return self._forward_once(x, profile, visualize)  # single-scale inference,
train
  File "E:\test\yolov5-master\models\yolo.py", line 1
49, in _forward_once
    x = m(x)  # run
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py",
line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\upsampling.p
y", line 154, in forward
    recompute_scale_factor=self.recompute_scale_factor)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py",
line 1185, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'

(base) E:\test\yolov5-master\yolov5-master>

解决方法:
第一步:查看日志报错信息,找到对应的代码
Python编程中常见问题汇总_第7张图片
第二步:修改代码
修改前:

def forward(self, input: Tensor) -> Tensor:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
recompute_scale_factor=self.recompute_scale_factor)

修改后:

    def forward(self, input: Tensor) -> Tensor:
        return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)

第三步:保存后,重新执行成功

问题15:提示ss labels are 0-{nc - 1}’ AssertionError: Label class 15
exceeds nc=2 in E:\automachine\seven\datasets\my_
datasets\lfy_test.yaml. Possible class labels are 0-1

Python编程中常见问题汇总_第8张图片

  • 解决方法:
    第一步:排查数据
    在这里插入图片描述
    第二步:排查标签,发现有很多的标签,而不是两个
    在这里插入图片描述修改后的标签

    Python编程中常见问题汇总_第9张图片

第三步:修改相应的标签,然后在重新标记,重新执行成功


                    **欢迎大家在评论区多多批评指正,十分感谢!**

你可能感兴趣的:(机器学习,python,开发语言,opencv)