最近在学习吴恩达深度学习系列课程,做到编程作业4.3车辆识别时因为TensorFlow和Keras版本不兼容而出现各种问题,查找了一些资料后好多人都提出降低TF版本,不放弃我最后的倔强,最终还是把所有问题逐一解决了:)
查看Python、TensorFlow、Keras版本:我的Python版本3.7.0,TensorFlow版本2.3.1,Keras版本2.4.3。
import sys
import tensorflow as tf
import keras
print(sys.version) # 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
print(tf.__version__) # 之前是1.15.3,现在用的版本升级到了2.3.1
print(keras.__version__) # 2.4.3
Keras与TensorFlow关系
Traceback (most recent call last):
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 225, in <module>
yolo_model = load_model("model_data/yolov2.h5")
......
File "C:\Users\Oscar\Desktop\yad2k\yad2k\models\keras_yolo.py", line 32, in space_to_depth_x2
AttributeError: module 'tensorflow' has no attribute 'space_to_depth'
我使用的是TensorFlow2.1以上版本,源编译前的H5文件中使用了tf.space_to_depth,TF2.1之后的版本改成了tf.nn.space_to_depth,需要自己重新生成H5文件。我参考以下三篇文章自己生成了yolo.h5文件,注意要将yad2k/models/keras_yolo.py第31行改为tf.nn.space_to_depth。
tf2.1下生成yolo.h5文件
yolo.h5文件问题的解决 - 吴恩达深度学习:目标检测之YOLO算法
关于吴恩达车辆识别代码出错的问题)
Traceback (most recent call last):
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 246, in <module>
scores, boxes, classes = yolo_eval(yolo_outputs, image_shape)
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 188, in yolo_eval
scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold)
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 123, in yolo_non_max_suppression
tf.compat.v1.keras.backend.get_session().run(tf.compat.v1.variables_initializer([max_boxes_tensor])) # 初始化变量max_boxes_tensor
File "F:\Python\lib\site-packages\tensorflow\python\client\session.py", line 958, in run
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=10>
run_metadata_ptr)
File "F:\Python\lib\site-packages\tensorflow\python\client\session.py", line 1166, in _run
self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
File "F:\Python\lib\site-packages\tensorflow\python\client\session.py", line 477, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "F:\Python\lib\site-packages\tensorflow\python\client\session.py", line 263, in for_fetch
(fetch, type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>
这个错误最恶心,我找了很久才解决。看到这个错误时找了一个很全的参考文章: Tensorflow报错:TypeError: Fetch argument None has invalid type class ‘NoneType’ ,并参考了github说是大部分错误的原因都是某一个op重新赋值导致变量变成None,但是还是没有解决。仔细查看代码发现问题出在非最大值抑制函数中创建会话并运行初始化变量max_boxes_tensor这一步,后来发现TensorFlow 2.x是默认开启 eager execution 的,eager execution提供了一种命令式的编程环境,不需要构建图直接可以运行(参考文章:tensorflow 2.0系列(3):eager execution和计算图)。而在老版本中需要先构建计算图再执行。我这里还是用的老版本的代码,所有需要禁用:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
Traceback (most recent call last):
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 288, in <module>
out_scores, out_boxes, out_classes = predict(sess, "test.jpg")
File "G:/Project/PYTHON/Demo01/Deep_Learning/test4_3/车辆识别.py", line 272, in predict
draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
File "G:\Project\PYTHON\Demo01\Deep_Learning\test4_3\yolo_utils.py", line 52, in draw_boxes
font = ImageFont.truetype(font='font/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
File "F:\Python\lib\site-packages\PIL\ImageFont.py", line 648, in truetype
return freetype(font)
File "F:\Python\lib\site-packages\PIL\ImageFont.py", line 645, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "F:\Python\lib\site-packages\PIL\ImageFont.py", line 194, in __init__
font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource
这是路径问题:图中绘制边界框那行代码有个draw_boxes(),需要把这个函数中第一行的font文件路径改成你自己的(默认是在当前路径下找)。还有保存绘制图片image.save()那一行默认保存在当前路径下的out文件夹下,如果没有这个文件夹就报错。
raise TypeError Tensor is unhashable. TypeError: Tensor is unhashable. Instead, use tensor.ref as the key.
不知道该怎么复现了,当时用的是这个解决办法:FailedPreconditionError: Error while reading resource variable was uninitialized