【小白学习tensorflow教程】四、使用 tfhub中的模型EfficientDet-Lite2 进行对象检测
生活随笔
收集整理的這篇文章主要介紹了
【小白学习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格式。
加載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檢測的置信度比較高。
下面將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 进行对象检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 近几年,关于基于Imagenet数据集图
- 下一篇: 联想平板电脑怎么用u盘启动不了怎么办 联