1、keras报错信息
1、查看keras版本
import keras
print(keras.__version__)
# 在2.6版本后就没 load_img(), img_to_array() 这两个方法了,会发现这两个方法是 images_utils 的方法属性。
解决方案:
from keras.utils import image_utils
plt.imshow(image_utils.img_to_array(image_utils.load_img(np.random.choice(image_files))).astype('uint8'))
plt.show()
2、导包不正确
错误信息:
module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
解决方案:
2.1、首先查看keras的版本
from tensorflow.compat.v1 import keras
keras.__version__ # 2.4.0
2.2、2.4.0版本的需要这样导包
from tensorflow.compat.v1 import keras
from tensorflow.compat.v1.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.compat.v1.keras.models import Model
from tensorflow.compat.v1.keras.applications.vgg16 import VGG16
from tensorflow.compat.v1.keras.applications.vgg16 import preprocess_input
from tensorflow.compat.v1.keras.preprocessing import image
from tensorflow.compat.v1.keras.preprocessing.image import ImageDataGenerator
3、报错信息:keras.utils.to_categorical() - name keras not defined
解决方案:
import tensorflow as tf
from tensorflow.keras import utils as np_utils
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
4、报错信息:Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape r
解决方案:维度不相同,扩充维度
解决方案:
x_train = np.expand_dims(x_train, axis=3= np.expand_dims(x_test, axis=3)
5、报错:‘Tensor‘ object has no attribute ‘_keras_shape‘
将‘_keras_shape‘改成shape即可。