tensorflow环境配置中遇到的问题
在运行一个手写数字识别模型之前,光环境配置就花了好长时间,记录下遇到的一些问题
- 解决ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow
该问题在导入keras库时报出
import keras
解决方法
pip list
keras==2.4.3(目前版本太高,需要降版本到2.2)
pip install keras==2.2
2. keras保存加载模型报错“AttributeError: ‘str‘ object has no attribute ‘decode‘”
在keras保存模型时报错
后面找到原因是h5py版本的问题,自己装的版本比较高一些,这可能导致了bug出现,把h5py降到2.10.0版本就行了。
(ps:有时候更新库要注意每个库之间版本的问题,可能会带来各种bug)
pip install h5py==2.10.0
3. ValueError: not enough values to unpack (expected 3, got 2)
该问题由opencv引出
原因是 cv2.findContours的opencv旧版本返回3个值
1 | im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE), |
新版本返回2个值:
1 | contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)。 |
原代码:
1 | _,contours, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
改为:
1 | contours, hierarchy = cv2.findContours(img_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
问题解决
4. python安装cv2出现如下错误:Could not find a version that satisfies the requirement cv2 (from versions: )
在使用pip install cv2时报错
查询了很久后终于找到原因,根本没有叫cv2的包,要安装的是一个叫opencv-python的包
使用pip install opencv-python 等待安装完成即可
5.cannot import name ‘get_config‘ from ‘tensorflow.python.eager.context‘
tensorflow2.X 中导入keras模块代码使用下述方法会报错:
1 | from keras.models import load_model |
修改为:
from tensorflow.keras.models import load_model
from tensorflow.keras.models import model_from_yaml
最终手写数字识别和AI写诗两个模型都成功跑起来了,可喜可贺
目前使用的环境如下
系统:centos8
python版本:3.6.8
tensorflow版本:1.14.0
Keras版本:2.2.0
h5py版本:2.10.0
opencv-python版本:4.5.2.54