我是靠谱客的博主 执着洋葱,最近开发中收集的这篇文章主要介绍yolov3 tf1->tf2,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.Tensorflow 2.0 不兼容 Session()
删除session相关代码

2.把keras的相关代码改为tf2.x的代码

# from keras import backend as K
# from keras.layers import Conv2D, Add, ZeroPadding2D, UpSampling2D, Concatenate, MaxPooling2D
# from keras.layers.advanced_activations import LeakyReLU
# from keras.layers.normalization import BatchNormalization
# from keras.models import Model
# from keras.regularizers import l2

from tensorflow.keras import backend as K
from tensorflow.keras.layers import Conv2D, Add, ZeroPadding2D, UpSampling2D, Concatenate, MaxPooling2D, LeakyReLU, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.regularizers import l2

TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
tf2.x数据为tensor,与tf1.x数据格式不兼容
修改数据格式

target_box[(predicted_class, score.numpy())] = (left, top, right, bottom)

3.模型预测代码修改

# out_boxes, out_scores, out_classes = self.sess.run(
#     [self.boxes, self.scores, self.classes],
#     feed_dict={
#         self.yolo_model.input: image_data,
#         self.input_image_shape: [image.size[1], image.size[0]],
#         K.learning_phase(): 0
#     })

input_image_shape = tf.constant([image.size[1], image.size[0]]) 

out_boxes, out_scores, out_classes = yolo_eval(self.yolo_model(image_data), self.anchors,   len(self.class_names), input_image_shape,   score_threshold=self.score, iou_threshold=self.iou)

tensorflow.python.framework.errors_impl.InvalidArgumentError: Subshape must have computed start >= end since stride is negative, but is 0 and 2 (computed from start 0 and end 9223372036854775807 over shape with rank 2 and stride-1)

张量切片问题

#     box_xy = (K.sigmoid(feats[..., :2]) + grid) / tf.cast(grid_shape[::-1], feats.dtype)
#     box_wh = K.exp(feats[..., 2:4]) * anchors_tensor / tf.cast(input_shape[::-1], feats.dtype)
    
    box_xy = (K.sigmoid(feats[..., :2]) + grid) / tf.cast(grid_shape[..., ::-1], feats.dtype)
    box_wh = K.exp(feats[..., 2:4]) * anchors_tensor / tf.cast(input_shape[..., ::-1], feats.dtype)

最后

以上就是执着洋葱为你收集整理的yolov3 tf1->tf2的全部内容,希望文章能够帮你解决yolov3 tf1->tf2所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部