在调试程序中,除了一些常见的语法错误,也有各种各样的安装包的兼容性问题。此博客也将不断进行更新!
Bug 1
运行文件出现如下错误:
Unable to open '_objects.pyx': File not found
初步怀疑是import h5py时出错,查看安装包时发现已经安装了h5py包,查找资料有人在安装TensorFlow时也出现过类似问题,其中的一个包prtobuf版本不兼容问题,更改版本为3.6.0即可,然后还是出现同样的错误。
最后重新利用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应用程序的安装位置)
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()