Python中tensorflow Import使用错误集合

cannot import name ‘BatchNormalization’ from 'keras.layers.normalization’

  • 错误
    拉取一份代码,报错cannot import name ‘BatchNormalization’ from ‘keras.layers.normalization’
  • 解决
    将原导入from keras.layers.normalization import BatchNormalization
    修改为from keras.layers.normalization.batch_normalization_v1 import BatchNormalization
from keras.layers.normalization.batch_normalization_v1 import BatchNormalization

No module named ‘keras.backend.tensorflow_backend’; ‘keras.backend’ is not a package

  • 错误
    报错 No module named ‘keras.backend.tensorflow_backend’; ‘keras.backend’ is not a package
  • 解决
    from keras.backend.tensorflow_backend import set_session替换为 from keras.backend import set_session
from keras.backend import set_session

module ‘tensorflow’ has no attribute ‘ConfigProto’

  • 错误
    报错 module ‘tensorflow’ has no attribute ‘ConfigProto’
  • 解决
    使用Tensorflow 2.0会出现AttributeError: module ‘tensorflow’ has no attribute 'ConfigProto’这个问题,这个是由于现在新版本中一些1.0版本的函数被和2.0版本函数区分开的缘故,需要将tf.ConfigProto修改为tf.compat.v1.ConfigProto

Unable to open file (truncated file: eof = 18044416, sblock->base_addr = 0, stored_eof = 574710816)

  • 错误
    报错 Unable to open file (truncated file: eof = 18044416, sblock->base_addr = 0, stored_eof = 574710816)
  • 解决
    Vgg模型未下载完成,重新下载即可,如果github下载过慢,可以手动下载,下载后复制到C:\Users\Administrator.keras\models目录下即可

module ‘keras.optimizers’ has no attribute ‘SGD’

  • 错误
    报错 module ‘keras.optimizers’ has no attribute ‘SGD’
  • 解决
    修改sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)为
    sgd = optimizers.gradient_descent_v2.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
sgd = optimizers.gradient_descent_v2.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)

module ‘keras.utils’ has no attribute 'to_categorical’

  • 错误
    报错 module ‘keras.utils’ has no attribute ‘to_categorical’
  • 解决
    导入from keras.utils import np_utils,使用np_utils
    keras.utils.to_categorical修改为 np_utils.to_categorical

Import “cv2” could not be resolved

  • 错误
    报错 Import “cv2” could not be resolved
  • 解决
    pip install opencv-python
    pip install opencv-python-headless
    如果不行,那么安装相应Python对应版本的opencv
    pip install opencv_python==3.3.1.11
    如果还是不行,在setting.json中添加
    “python.analysis.extraPaths”: [
    “你的对应路径\Python36\Lib\site-packages”
    ]

AttributeError: module ‘tensorflow’ has no attribute 'config’

  • 错误
    报错 AttributeError: module ‘tensorflow’ has no attribute ‘config’
  • 解决
    config 替换为ConfigProto(tensorflow1.0改2.0代码)

FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.

  • 错误
    报错 FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
  • 解决
    安装低版本的Numpy即可
    pip install numpy==1.16.0

google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices

  • 错误
    报错 google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices
  • 解决
    直接干掉了相关代码,用cpu跑,跑起来了再说,然后就被教育了,cpu太慢了

expected predictions to have shape (5,) but got array with shape (3,)

  • 错误
    报错 google.protobuf.pyext._message.FieldProperty’ object has no attribute 'list_physical_devices
  • 解决
    Dense输出类别错误
    x = Dense(3, activation=‘softmax’, name=‘predictions’)(x) # 输出类别

OSError: cannot write mode RGBA as JPEG

  • 错误
    报错 OSError: cannot write mode RGBA as JPEG
  • 解决
    RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度。而JPG不支持透明度,所以要么丢弃Alpha,要么保存为.png文件
    im=im.convert(‘RGB’)
    im.save(abs_path0+filename,format=“jpeg”,quality=80)

** 5 out of the last 5 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. **

  • 错误
    官方原因:
    This warning occurs when a TF function is retraced because its arguments change in shape or dtype (for Tensors) or even in value (Python or np objects or variables).
    当一个 TF 函数被回溯时会出现这个警告,因为它的参数在形状或 dtype(对于张量)甚至值(Python 或 np 对象或变量)上发生了变化.
    解决方案是1.循环中使用,2使用不重复的参数,3使用宽松灵活的参数,对这个地方理解不是很明白,但是用下面的代码确实可以解决这个警告。
  • 解决
    model.predict(image)改成model(X_test)

’NoneType’ object has no attribute 'reshape’

  • 错误
    在图像分类前期进行图像处理时报错,原因是文件中有中文路径,看了一下果然有一个文件为中文名称
  • 解决
    修改中文名称

OSError: SavedModel file does not exist at: ./model/.hdf5{saved_model.pbtxt|saved_model.pb}

  • 错误
    在flask中加载训练完成的模型时报错,使用了相对路径
  • 解决
    修改中文名称
    修改相对路径为绝对路径

你可能感兴趣的:(Python,开发通用,机器学习,python,keras,batch)