导入:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
报错:
Cannot find reference 'keras' in '__init__.py'
Unresolved reference 'ImageDataGenerator'
原因:
PyCharm支持的导入方式是----->import 包名.模块名或者import 包名或者import 模块名,超过该长度那么就会提示错误
解决:
按照PyCharm支持的导入方式进行导入,即超过长度分开进行导入即可
from tensorflow import keras
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
问题2
使用:
model.fit_generator()函数
报错:
UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
line 2525, in apply_affine_transform
if scipy is None:
NameError: name 'scipy' is not defined
原因:
应该是tensorflow2.x版本即将来的版本已经舍弃fit_generator函数,导致该函数内部缺失相关变量定义,例如:会出现‘scipy’是未被定义。
解决:
第一种:
可以使用tensorflow2.x支持的fit函数或者是train_on_batch函数来训练自己的模型
第二种:
可以提前下载专门用于机器学习的包:scipy(注:本人是在PyCharm里install机器学习包scipy,该包里有很多算法:聚类、傅里叶变换、积分和常微分变换等等)
问题3
使用:
model.fit()函数
报错:
line 995, in __init__
raise ValueError(
ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
原因:
该函数在训练图片时,不支持导入y参数,即不支持导入训练集图片的标签(不清楚的可以去查看一下fit函数的原型)
解决:
在使用该函数时,y参数可以舍弃不用,包括后面的验证集图片的标签参数也舍弃不用,例如:model.fit(train_image,epochs=10,steps_per_epoch=100,validation_data=validation_image,validation_steps=10,verbose=2)
问题4
使用:
image.load_img()函数,前提是已经导入image模块----->from keras.preprocessing import image,还提示模块里没有“load_image”
报错:
AttributeError: module 'keras.preprocessing.image' has no attribute 'load_image'
原因:
经测试在jupyter notebook里导入image模块,然后使用image.load_img()没有提示错误,但是在PyCharm里导入image模块,然后使用image.load_img()提示该image里没有load_img()函数或者img_to_array()函数,这是因为环境不同或者是keras的版本不同导致会出现该问题,keras的高级版本已经将load_img()函数、img_to_array()函数等移植到keras.utils包中的image_utils中
解决:
首先需要导入:
from keras.utils import image_utils
然后再使用:
img=image_utils.load_img(img_road,.....)
img_array=image_utils.img_to_array(img)
问题5
使用:
tensorflow.keras.Sequential()函数
报错:
This TensorFlow binary is optimized with oneAPI Deep Neural Network Library(oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
原因:
tensorflow代码的二进制文件被oneDNN进行了优化,由于是在PyCharm环境下导入tensorflow,并且每次重建工程时没有将虚拟环境设置为上一次的环境所以需要重新导入tensorflow,以致于解释器或编译器认为用户不小心修改了tensorflow的二进制文件,所以会弹出重新生成tensorflow代码的警告或错误
解决:
第一种:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #目的是告诉编译器或解释器用户已经知道日志输出是ERROR并屏蔽它,故解释器在输出时不会弹出重新生成tensorflow代码的警告或错误
第二种:
新建PyCharm工程,重新导入tensorflow并运行
问题6
使用:
model.predict(validation_image[0])#再进行预测时,预测的图片的维度与神经网络训练之前的维度不一致
报错:
ValueError: Exception encountered when calling layer 'sequential'(type Sequential).
Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 784, but received input with shape(None,28)
原因:
像这种问题第一行提示错误在‘Sequantial’中,第二行提示维度不正确,那就说明训练的时候的维度和预测的维度不一致。
解决:
由于之前搭建神经网络时,使用了一个函数“tf.keras.layers.Flatten()”,即降维或者扁平化处理函数,所以后面在预测时需要进行扩维处理,即使用numpy里提供的expand_dims函数进行扩维后再进行预测,如:x=np.expand_dims(validation_image[49],axis=0),model.predict(x)