• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

LR233

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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即可。

 

posted on 2022-10-31 10:06  LR233  阅读(342)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3