日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【小白学习tensorflow教程】四、使用 tfhub中的模型EfficientDet-Lite2 进行对象检测

發布時間:2024/10/8 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【小白学习tensorflow教程】四、使用 tfhub中的模型EfficientDet-Lite2 进行对象检测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@Author:Runsen

tfhub是tensorflow官方提供訓練好的模型的一個倉庫。今天,我使用 tfhub中的模型EfficientDet-Lite2 進行對象檢測

選擇的模型是EfficientDet-Lite2 對象檢測模型。它在具有 91 個不同標簽的 COCO17 數據集上進行了訓練,并針對 TFLite 應用程序進行了優化。

EfficientDet-Lite 是一系列對移動/物聯網友好的對象檢測模型。

目前暫時無法直接正常訪問 https://tfhub.dev,可以通過鏡像 https://hub.tensorflow.google.cn

加載TensorFlow 模型

圖片輸入:大小可變的三通道圖像。輸入張量是一個 tf.uint8具有形狀張量[None, height, width, 3]。


加載圖像并將其處理為 TensorFlow 模型的tensor格式。

import tensorflow_hub as hub import cv2 import numpy import pandas as pd import tensorflow as tf import matplotlib.pyplot as pltwidth = 1028 height = 1028img = cv2.imread('image.jpg') inp = cv2.resize(img, (width , height )) rgb = cv2.cvtColor(inp, cv2.COLOR_BGR2RGB) rgb_tensor = tf.convert_to_tensor(rgb, dtype=tf.uint8) rgb_tensor = tf.expand_dims(rgb_tensor , 0)

加載EfficientDet-Lite2,官方給出加載代碼

由于下載模型網速不行,將開發環境轉到colab中。

detector = hub.load("https://hub.tensorflow.google.cn/tensorflow/efficientdet/lite2/detection/1") boxes, scores, classes, num_detections = detector(boxes, scores, classes, num_detections = detector(rgb_tensor))

輸出字典包含:

  • detection_boxes:一個tf.float32形狀的張量[N, 4]含有以下列順序邊界框坐標:[ymin, xmin, ymax, xmax]。
  • detection_scores:包含檢測分數tf.float32的形狀張量[N]。
  • detection_classes:包含標簽文件中檢測類索引tf.int的形狀張量[N]。
  • num_detections:一個tf.int只有一個值的張量,檢測次數[N]。

下面是檢測的scores和class


物體編號:47,72,77檢測的置信度比較高。

label = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', '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','couch', 'potted plant', 'bed', '-', 'dining table', '-', '-','toilet', '-', 'tv', 'laptop', 'mouse', 'remote', 'keyboard','cell phone', 'microwave', 'oven', 'toaster', 'sink','refrigerator', '-', 'book', 'clock', 'vase', 'scissors','teddy bear', 'hair drier', 'toothbrush', '-']

下面將tensor轉換為numpy()

pred_labels = classes.numpy().astype('int')[0] pred_labels = [labels[i] for i in pred_labels] pred_boxes = boxes.numpy()[0].astype('int') pred_scores = scores.numpy()[0]

最后設置一個閾值,將檢測框加到圖片上。

for score, (ymin,xmin,ymax,xmax), label in zip(pred_scores, pred_boxes, pred_labels):if score < 0.5:continuescore_txt = f'{100 * round(score)}%'img_boxes = cv2.rectangle(rgb,(xmin, ymax),(xmax, ymin),(0,255,0),2) font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img_boxes, label,(xmin, ymax-10), font, 0.5, (255,0,0), 2, cv2.LINE_AA)cv2.putText(img_boxes,score_txt,(xmax, ymax-10), font, 0.5, (255,0,0), 2, cv2.LINE_AA) plt.figure(figsize=(10,10)) plt.imshow(img_boxes) plt.savefig('image_pred.jpg',transparent=True, )

總結

以上是生活随笔為你收集整理的【小白学习tensorflow教程】四、使用 tfhub中的模型EfficientDet-Lite2 进行对象检测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。