如何将yolov3转成tensorflow的.pb模型

如何将yolov3转成tensorflow的.pb模型

前言

参考资料:
https://blog.csdn.net/heiheiya/article/details/91437196
这里首先感谢上面的大佬提供的源码以及转换方法,本文只是搬运总结一下。
最近想把yolov3部署到移动端,然后用android的npu接口加载模型跑下试试性能。但网上大多提供的yolov3都是.weights模型,而最终npu接口所需要的是.tflite。所以我需要先把.weights转成.pb,然后再把.pb通过转换工具转成.tflite。本文先介绍如何将.weights转成.pb。
运行环境
python 3.6.7
tensorflow 1.9.0

具体步骤

按照参考资料中博客所写,即可轻松进行模型转换。
1.首先下载coco.namesyolov3.weights两个文件。
这里方便起见,可以直接新建个coco.names的文件,然后把下面内容复制进去。

person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush

2.下载https://github.com/mystic123/tensorflow-yolo-v3.git代码到本地。这个大神写的代码还是很好用的,很适合单纯的进行模型转换,没有冗余的东西。

git clone https://github.com/mystic123/tensorflow-yolo-v3.git

下面是成功clone的log。
在这里插入图片描述

3.将下载好的coco.names、yolov3.weights放入步骤2的代码同级目录下。如下图所示。
在这里插入图片描述

4.开启终端,或pycharm的终端。进入tensorflow-yolo-v3的路径下,运行

python convert_weights_pb.py --class_names coco.names --data_format NHWC --weights_file yolov3.weights

即可得到名为 frozen_darknet_yolov3_model.pb文件。
运行log如下:
在这里插入图片描述

5.生成 frozen_darknet_yolov3_model.pb文件后,我们运行下列代码,看下转换后的.pb文件各层的名字是否对应。

import tensorflow as tf
import os

model_dir = './'
model_name = 'frozen_darknet_yolov3_model.pb'

def create_graph():
    with tf.gfile.FastGFile(os.path.join(model_dir, model_name), 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')

create_graph()
tensor_name_list = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
for tensor_name in tensor_name_list:
    print(tensor_name, '\n')

运行完之后会出现一大坨层的名字,结果如下:
在这里插入图片描述

后记

至此,yolov3.weights转成.pb格式完成,希望接下来能坚持把.pb转.tflite的具体过程探索完成,并记录下来。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在TensorFlow框架加载yolov3_tiny模型进行物品识别,需要进行以下步骤: 1. 下载yolov3_tiny模型的权重文件和配置文件,可以在Darknet官网或GitHub上找到。 2. 将权重文件和配置文件转换成TensorFlow可用的格式,可以使用如下命令行: ``` python convert_weights.py --weights_file yolov3-tiny.weights --output_graph yolov3-tiny.pb --config_file yolov3-tiny.cfg ``` 3. 在TensorFlow中加载模型,可以使用如下代码: ``` import tensorflow as tf # 加载模型 model = tf.keras.models.load_model('yolov3-tiny.pb') # 进行物品识别 predictions = model.predict(images) ``` 其中,images为待识别的图像数据。需要根据模型配置文件中定义的输入尺寸对图像进行预处理,并将其转换为模型所需的输入格式。 4. 根据模型输出,解析出物品识别结果。yolov3_tiny模型的输出是一个Tensor,需要进行后处理才能得到物品的位置和类别信息。可以使用如下代码: ``` def post_process(predictions, conf_threshold, iou_threshold): # 对预测结果进行后处理 boxes, confidences, class_ids = decode_predictions(predictions, conf_threshold, iou_threshold) return boxes, confidences, class_ids def decode_predictions(predictions, conf_threshold, iou_threshold): # 解码预测结果 boxes, confidences, class_ids = [], [], [] for prediction in predictions: # 对每个预测结果进行解码 box, confidence, class_id = decode_prediction(prediction, conf_threshold, iou_threshold) if box is not None: boxes.append(box) confidences.append(confidence) class_ids.append(class_id) return boxes, confidences, class_ids def decode_prediction(prediction, conf_threshold, iou_threshold): # 解码单个预测结果 boxes = prediction[..., :4] confidences = prediction[..., 4] class_ids = prediction[..., 5:] max_confidence = tf.reduce_max(confidences, axis=-1) mask = max_confidence >= conf_threshold boxes = tf.boolean_mask(boxes, mask) confidences = tf.boolean_mask(max_confidence, mask) class_ids = tf.boolean_mask(class_ids, mask) indices = tf.image.non_max_suppression(boxes, confidences, max_output_size=100, iou_threshold=iou_threshold) boxes = tf.gather(boxes, indices) confidences = tf.gather(confidences, indices) class_ids = tf.gather(class_ids, indices) if tf.shape(boxes)[0] > 0: box = boxes[0].numpy().tolist() confidence = confidences[0].numpy().tolist() class_id = tf.argmax(class_ids[0]).numpy().tolist() return box, confidence, class_id else: return None, None, None ``` 其中,conf_threshold和iou_threshold分别为置信度阈值和非极大值抑制阈值,可以根据实际应用进行调整。 5. 可以根据解析出的物品位置和类别信息,将其可视化并输出。可以使用如下代码: ``` def visualize(image, boxes, class_ids): # 可视化结果 for box, class_id in zip(boxes, class_ids): x1, y1, x2, y2 = box cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.putText(image, str(class_id), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow('result', image) cv2.waitKey(0) ``` 其中,image为待识别的原始图像,boxes和class_ids为解析出的物品位置和类别信息。需要使用OpenCV等库将结果可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值