天天看點

python調試程式中遇到的不常見bug彙總

在調試程式中,除了一些常見的文法錯誤,也有各種各樣的安裝包的相容性問題。此部落格也将不斷進行更新!

Bug 1

運作檔案出現如下錯誤:

Unable to open '_objects.pyx': File not found
           
python調試程式中遇到的不常見bug彙總

初步懷疑是import h5py時出錯,檢視安裝包時發現已經安裝了h5py包,查找資料有人在安裝TensorFlow時也出現過類似問題,其中的一個包prtobuf版本不相容問題,更改版本為3.6.0即可,然後還是出現同樣的錯誤。

python調試程式中遇到的不常見bug彙總

#檢視安裝清單

最後重新利用conda重新安裝了h5py,系統自動安裝了其他的幾個包,親測可用,程式運作沒問題了。

Bug 2:

出現警告的源代碼段:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y))
           

WARNING:tensorflow:From e:\吳恩達深度學習筆記\程式設計練習\Convolution model.py:163: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.

Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow

into the labels input on backprop by default.

故障原因函數更新引起的相容性問題,根據提示,更改如下:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Z3, labels=Y))
           

Bug 3:出現大量的pylint錯誤,更改pylint檔案。方法:檔案---首選項---設定---python.linting.pylintPath,

出現如下搜尋框,原位置為pylint,更改為D:\Anaconda\pkgs\pylint-2.2.2-py36_0\Scripts(在anaconda檔案夾中尋找pylint應用程式的安裝位置)

python調試程式中遇到的不常見bug彙總

Bug 4:報錯from __future__ imports must occur at the beginning of the file,此時把

from __future__ import print_function​​​​​​​
           

放在第一行即可!

Bug 5:'float' object cannot be interpreted as an integer

num_batchs = num_samples//batch_size
    model.train()
    for k in range(num_batchs):
           

python3中用雙

//

就可以了,在python中使用單/

Bug 6 :

invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor

#原語句:  
train_loss+=loss.data[0]  
#修改後:  
train_loss+=loss.item()  
#bingo  
           

Bug 7 :

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('images\eye.jpg',0) #直接讀為灰階圖像
#opencv方法讀取-cv2.calcHist(速度最快)
#圖像,通道[0]-灰階圖,掩膜-無,灰階級,像素範圍
hist_cv = cv2.calcHist([img],[0],None,[256],[0,256])
#numpy方法讀取-np.histogram()
hist_np,bins = np.histogram(img.ravel(),256,[0,256])
#numpy的另一種方法讀取-np.bincount()(速度=10倍法2)
hist_np2 = np.bincount(img.ravel(),minlength=256)
plt.subplot(221),plt.imshow(img,'gray')
plt.subplot(222),plt.plot(hist_cv)
plt.subplot(223),plt.plot(hist_np)
plt.subplot(224),plt.plot(hist_np2)
           

運作上段代碼時報錯:MatplotlibDeprecationWarning: 

Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

  "Adding an axes using the same arguments as a previous axes "

解決方案:在最後加上plt.show()即可!

plt.show()