您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用Tensorflow hub完成目標檢測的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
使用到的主要環境是:
tensorflow-cpu=2.10
tensorflow-hub=0.11.0
tensorflow-estimator=2.6.0
python=3.8
protobuf=3.20.1
首先導入必要的 python 包,后面要做一些復雜的安裝和配置工作,需要一點耐心和時間。在運行下面代碼的時候可能會報錯:
TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
你只需要重新使用 pip 安裝,將 protobuf 降低到 3.20.x 版本即可。
import os import pathlib import matplotlib import matplotlib.pyplot as plt import io import scipy.misc import numpy as np from six import BytesIO from PIL import Image, ImageDraw, ImageFont from six.moves.urllib.request import urlopen import tensorflow as tf import tensorflow_hub as hub tf.get_logger().setLevel('ERROR')
(1)到 github.com/protocolbuf… 用迅雷下載對應操作系統的壓縮包,我的是 win7 版本
(2)下載好之后隨便解壓到自定義目錄,我的是 “主目錄\protoc-22.1-win64”,然后將其中的 “主目錄\protoc-22.1-win64\bin” 路徑添加到用戶環境變量中的 PATH 變量中,重新打開命令行,輸入 protoc --version ,如果能正常返回版本號說明配置成功,可以開始使用。
(3)進入命令行,在和本文件同一個目錄下,執行命令
git clone --depth 1 https://github.com/tensorflow/models
,將 models 文件夾下載下來,進入 models/research/ 下,使用命令執行
protoc object_detection/protos/*.proto --python_out=.
將 models/research/object_detection/packages/tf2/setup.py 拷貝到和 models/research/ 下,然后使用執行本文件的 python 對應的 pip 去執行安裝包操作
..\Anaconda3\envs\tfcpu2.10_py38\Scripts\pip.exe install . -i https://pypi.tuna.tsinghua.edu.cn/simple
中間可能會報錯“error: netadata-generation-failed”,一般都是某個包安裝的時候出問題了,我們只需要看詳細的日志,單獨用 pip 進行安裝即可,單獨安裝完之后,再去執行上面的根據 setup.py 的整裝操作,反復即可,過程有點麻煩但還是都可以安裝成功的。
(4)這里的模型本來在:
https://tfhub.dev/tensorflow/centernet/hourglass\_512x512\_kpts/1
但是由于網絡問題無法獲取,所以我們可以改為從
https://storage.googleapis.com/tfhub-modules/tensorflow/centernet/hourglass\_512x512\_kpts/1.tar.gz
獲取模型。
from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as viz_utils from object_detection.utils import ops as utils_ops PATH_TO_LABELS = './models/research/object_detection/data/mscoco_label_map.pbtxt' category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True) model_path = 'https://storage.googleapis.com/tfhub-modules/tensorflow/centernet/hourglass_512x512_kpts/1.tar.gz' print('TensorFlow Hub 中的模型地址: {}'.format(model_path)) print('加載模型...') hub_model = hub.load(model_path) print('加載成功!')
打印結果:
TensorFlow Hub 中的模型地址: https://storage.googleapis.com/tfhub-modules/tensorflow/centernet/hourglass_512x512_kpts/1.tar.gz 加載模型... WARNING:absl:Importing a function (__inference_batchnorm_layer_call_and_return_conditional_losses_42408) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:absl:Importing a function (__inference_batchnorm_layer_call_and_return_conditional_losses_209416) with ops with custom gradients. Will likely fail if a gradient is requested. ... WARNING:absl:Importing a function (__inference_batchnorm_layer_call_and_return_conditional_losses_56488) with ops with custom gradients. Will likely fail if a gradient is requested. 加載成功!
(5)在這里我們主要定義了一個函數 load_image_into_numpy_array 來加載從網上下載圖片的圖片,并將其轉換為模型可以適配的輸入類型。
(6)IMAGES_FOR_TEST 字典中記錄了多個可以用來測試的圖片,但是這些都是在網上,用的使用需要調用 load_image_into_numpy_array 函數。
(7)COCO17_HUMAN_POSE_KEYPOINTS 記錄了人體姿態關鍵點。
(8)我們這里展示了 dogs 這張圖片,可以看到兩條可愛的小狗。
def load_image_into_numpy_array(path): image = None if(path.startswith('http')): response = urlopen(path) image_data = response.read() image_data = BytesIO(image_data) image = Image.open(image_data) else: image_data = tf.io.gfile.GFile(path, 'rb').read() image = Image.open(BytesIO(image_data)) (im_width, im_height) = image.size return np.array(image.getdata()).reshape((1, im_height, im_width, 3)).astype(np.uint8) IMAGES_FOR_TEST = { 'Beach' : 'models/research/object_detection/test_images/image2.jpg', 'Dogs' : 'models/research/object_detection/test_images/image1.jpg', 'Naxos Taverna' : 'https://upload.wikimedia.org/wikipedia/commons/6/60/Naxos_Taverna.jpg', 'Beatles' : 'https://upload.wikimedia.org/wikipedia/commons/1/1b/The_Coleoptera_of_the_British_islands_%28Plate_125%29_%288592917784%29.jpg', 'Phones' : 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Biblioteca_Maim%C3%B3nides%2C_Campus_Universitario_de_Rabanales_007.jpg/1024px-Biblioteca_Maim%C3%B3nides%2C_Campus_Universitario_de_Rabanales_007.jpg', 'Birds' : 'https://upload.wikimedia.org/wikipedia/commons/0/09/The_smaller_British_birds_%288053836633%29.jpg', } COCO17_HUMAN_POSE_KEYPOINTS = [(0, 1), (0, 2),(1, 3),(2, 4),(0, 5),(0, 6),(5, 7),(7, 9),(6, 8),(8, 10),(5, 6),(5, 11), (6, 12),(11, 12),(11, 13),(13, 15),(12, 14),(14, 16)] %matplotlib inline selected_image = 'Dogs' image_path = IMAGES_FOR_TEST[selected_image] image_np = load_image_into_numpy_array(image_path) plt.figure(figsize=(24,32)) plt.imshow(image_np[0]) plt.show()
我們這里將經過處理的小狗的圖片傳入模型中,會返回結果,我們只要使用結果來繪制出所檢測目標的框,以及對應的類別,分數,可以看出來結果是相當的準確的,甚至通過人的腿就能識別出人的框。
results = hub_model(image_np) result = {key:value.numpy() for key,value in results.items()} label_id_offset = 0 image_np_with_detections = image_np.copy() keypoints, keypoint_scores = None, None if 'detection_keypoints' in result: keypoints = result['detection_keypoints'][0] keypoint_scores = result['detection_keypoint_scores'][0] viz_utils.visualize_boxes_and_labels_on_image_array( image_np_with_detections[0], result['detection_boxes'][0], (result['detection_classes'][0] + label_id_offset).astype(int), result['detection_scores'][0], category_index, use_normalized_coordinates=True, max_boxes_to_draw=200, min_score_thresh=.30, agnostic_mode=False, keypoints=keypoints, keypoint_scores=keypoint_scores, keypoint_edges=COCO17_HUMAN_POSE_KEYPOINTS) plt.figure(figsize=(24,32)) plt.imshow(image_np_with_detections[0]) plt.show()
以上就是“怎么使用Tensorflow hub完成目標檢測”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。