最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Tensorflow?hub完成目標(biāo)檢測(cè)過(guò)程詳解

 更新時(shí)間:2023年04月12日 11:58:14   作者:我是王大你是誰(shuí)  
這篇文章主要為大家介紹了使用Tensorflow?hub完成目標(biāo)檢測(cè)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

本文主要介紹使用 tensorflow hub 中的 CenterNet HourGlass104 Keypoints 模型來(lái)完成簡(jiǎn)單的目標(biāo)檢測(cè)任務(wù)。使用到的主要環(huán)境是:

  • tensorflow-cpu=2.10
  • tensorflow-hub=0.11.0
  • tensorflow-estimator=2.6.0
  • python=3.8
  • protobuf=3.20.1

導(dǎo)入必要的庫(kù)

首先導(dǎo)入必要的 python 包,后面要做一些復(fù)雜的安裝和配置工作,需要一點(diǎn)耐心和時(shí)間。在運(yùn)行下面代碼的時(shí)候可能會(huì)報(bào)錯(cuò):

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')

準(zhǔn)備數(shù)據(jù)和模型

(1)到 github.com/protocolbuf… 用迅雷下載對(duì)應(yīng)操作系統(tǒng)的壓縮包,我的是 win7 版本: github.com/protocolbuf…

(2)下載好之后隨便解壓到自定義目錄,我的是 “主目錄\protoc-22.1-win64”,然后將其中的 “主目錄\protoc-22.1-win64\bin” 路徑添加到用戶(hù)環(huán)境變量中的 PATH 變量中,重新打開(kāi)命令行,輸入 protoc --version ,如果能正常返回版本號(hào)說(shuō)明配置成功,可以開(kāi)始使用。

(3)進(jìn)入命令行,在和本文件同一個(gè)目錄下,執(zhí)行命令

git clone --depth 1 https://github.com/tensorflow/models 

,將 models 文件夾下載下來(lái),進(jìn)入 models/research/ 下,使用命令執(zhí)行

protoc object_detection/protos/*.proto --python_out=.

將 models/research/object_detection/packages/tf2/setup.py 拷貝到和 models/research/ 下,然后使用執(zhí)行本文件的 python 對(duì)應(yīng)的 pip 去執(zhí)行安裝包操作

..\Anaconda3\envs\tfcpu2.10_py38\Scripts\pip.exe install . -i https://pypi.tuna.tsinghua.edu.cn/simple 

中間可能會(huì)報(bào)錯(cuò)“error: netadata-generation-failed”,一般都是某個(gè)包安裝的時(shí)候出問(wèn)題了,我們只需要看詳細(xì)的日志,單獨(dú)用 pip 進(jìn)行安裝即可,單獨(dú)安裝完之后,再去執(zhí)行上面的根據(jù) setup.py 的整裝操作,反復(fù)即可,過(guò)程有點(diǎn)麻煩但還是都可以安裝成功的。

(4)這里的模型本來(lái)在:

https://tfhub.dev/tensorflow/centernet/hourglass\_512x512\_kpts/1 

但是由于網(wǎng)絡(luò)問(wèn)題無(wú)法獲取,所以我們可以改為從

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('加載成功!')

打印結(jié)果:

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)在這里我們主要定義了一個(gè)函數(shù) load_image_into_numpy_array 來(lái)加載從網(wǎng)上下載圖片的圖片,并將其轉(zhuǎn)換為模型可以適配的輸入類(lèi)型。

(6)IMAGES_FOR_TEST 字典中記錄了多個(gè)可以用來(lái)測(cè)試的圖片,但是這些都是在網(wǎng)上,用的使用需要調(diào)用 load_image_into_numpy_array 函數(shù)。

(7)COCO17_HUMAN_POSE_KEYPOINTS 記錄了人體姿態(tài)關(guān)鍵點(diǎn)。

(8)我們這里展示了 dogs 這張圖片,可以看到兩條可愛(ài)的小狗。

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()

目標(biāo)檢測(cè)

我們這里將經(jīng)過(guò)處理的小狗的圖片傳入模型中,會(huì)返回結(jié)果,我們只要使用結(jié)果來(lái)繪制出所檢測(cè)目標(biāo)的框,以及對(duì)應(yīng)的類(lèi)別,分?jǐn)?shù),可以看出來(lái)結(jié)果是相當(dāng)?shù)臏?zhǔn)確的,甚至通過(guò)人的腿就能識(shí)別出人的框。

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完成目標(biāo)檢測(cè)過(guò)程詳解的詳細(xì)內(nèi)容,更多關(guān)于Tensorflow hub目標(biāo)檢測(cè)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Pycharm設(shè)置utf-8自動(dòng)顯示方法

    Pycharm設(shè)置utf-8自動(dòng)顯示方法

    今天小編就為大家分享一篇Pycharm設(shè)置utf-8自動(dòng)顯示方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python如何拆分ZIP文件

    Python如何拆分ZIP文件

    這篇文章主要介紹了Python如何拆分ZIP文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 利用python程序幫大家清理windows垃圾

    利用python程序幫大家清理windows垃圾

    Python 是一種面向?qū)ο?、解釋型?jì)算機(jī)程序設(shè)計(jì)語(yǔ)言,下面這篇文章主要給大家介紹了利用python程序如何實(shí)現(xiàn)清理垃圾的功能,本文只適用于windows,有需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • 在Python程序和Flask框架中使用SQLAlchemy的教程

    在Python程序和Flask框架中使用SQLAlchemy的教程

    SQLAlchemy為Python程序與SQL語(yǔ)句之間建立了映射,是Python操作數(shù)據(jù)庫(kù)的利器,這里我們將來(lái)看在Python程序和Flask框架中使用SQLAlchemy的教程,需要的朋友可以參考下
    2016-06-06
  • PyQt5的相對(duì)布局管理的實(shí)現(xiàn)

    PyQt5的相對(duì)布局管理的實(shí)現(xiàn)

    這篇文章主要介紹了PyQt5的相對(duì)布局管理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 一文詳解Python中Reduce函數(shù)輕松解決復(fù)雜數(shù)據(jù)聚合

    一文詳解Python中Reduce函數(shù)輕松解決復(fù)雜數(shù)據(jù)聚合

    這篇文章主要為大家介紹了Python中Reduce函數(shù)輕松解決復(fù)雜數(shù)據(jù)聚合示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • python實(shí)現(xiàn)文件分片上傳的接口自動(dòng)化

    python實(shí)現(xiàn)文件分片上傳的接口自動(dòng)化

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)文件分片上傳的接口自動(dòng)化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 靈活運(yùn)用Python 枚舉類(lèi)來(lái)實(shí)現(xiàn)設(shè)計(jì)狀態(tài)碼信息

    靈活運(yùn)用Python 枚舉類(lèi)來(lái)實(shí)現(xiàn)設(shè)計(jì)狀態(tài)碼信息

    在python中枚舉是一種類(lèi)(Enum,IntEnum),存放在enum模塊中。枚舉類(lèi)型可以給一組標(biāo)簽賦予一組特定的值,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo)

    Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo)

    今天小編就為大家分享一篇Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python爬取網(wǎng)頁(yè)轉(zhuǎn)換為PDF文件

    python爬取網(wǎng)頁(yè)轉(zhuǎn)換為PDF文件

    這篇文章主要為大家詳細(xì)介紹了python爬取網(wǎng)頁(yè)轉(zhuǎn)換為PDF文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06

最新評(píng)論

阿拉善盟| 五家渠市| 谢通门县| 罗城| 兴安盟| 天全县| 莫力| 行唐县| 交城县| 柳林县| 平邑县| 上饶县| 石城县| 黎川县| 双城市| 青河县| 织金县| 荆门市| 新巴尔虎左旗| 甘德县| 贵德县| 广丰县| 壶关县| 阳西县| 蚌埠市| 师宗县| 兴和县| 甘孜县| 田林县| 井陉县| 永仁县| 行唐县| 吐鲁番市| 华亭县| 郑州市| 娱乐| 孟州市| 东安县| 大埔区| 朝阳县| 阳谷县|