Colab - Introduction to Object Detection using TensorFlow Hub

Colab - Introduction to Object Detection using TensorFlow Hub

  • 1. 源由
  • 2. TensorFlow Hub
  • 3. 目标检测
    • 3.1 举例 - EfficientDet/D4 COCO 2017
    • 3.2 下载示例图像
      • 3.2.1 显示部分样本
      • 3.2.2 定义一个将类别ID映射到类别名称和颜色的字典
    • 3.3 加载模型
    • 3.4 单张照片执行推理
      • 3.4.1 推理第一张照片
      • 3.4.2 检查结果
      • 3.4.3 后处理和显示检测结果
        • 3.4.3.1 显示结果(min_det_thresh=0)
        • 3.4.3.1.2 显示结果(min_det_thresh=0.3)
    • 3.5 完整代码实现
      • 3.5.1 run_inference()
      • 3.5.2 对示例图像进行预测
      • 3.5.3 展示示例图像预测结果
  • 4. 总结
  • 5. 参考资料

1. 源由

如《Colab - Tensorflow & Keras Get Started: 005 Keras Fine Tune Pre-Trained Models GTSRB》存在类似内存资源不足的问题,仍然采用Colab来进行研读。
在这里插入图片描述接下来,将学习如何使用TensorFlow Hub预训练模型进行目标检测,这也是当下行业里面最热门的计算机视觉应用基础。

但是从应用的角度,其实不然,更多偏向物理世界规律的多因素预测其实当前的一些算力已经能够解决很多现实问题,尤其是对于多维因素空间下一种部件生命期监测方法的应用。

2. TensorFlow Hub

  • TensorFlow Hub, tensorflow是一个存储着经过训练的机器学习模型的仓库,可以随时进行微调,并可在任何地方部署。只需几行代码就可以重用像 BERT 和 Faster R-CNN 这样的经过训练的模型。
  • TensorFlow Hub, kaggle 也是一个用于共享、发现和重用预训练机器学习模型的库和平台。

TensorFlow Hub 的主要目标是简化重用现有模型的过程,从而促进协作、减少重复工作,并加速机器学习领域的研究和开发。

用户可以搜索由社区贡献或由谷歌提供的预训练模型,称为模块。这些模块可以轻松地集成到用户自己的机器学习项目中,只需几行代码。

3. 目标检测

目标检测是计算机视觉的一个子领域,专注于识别和定位数字图像或视频中特定的对象。它不仅涉及对图像中存在的对象进行分类,还通过在它们周围放置边界框或其他空间编码来确定它们的精确位置和大小。请注意几个关键词:

  • 对象分类
  • 边界框
  • 位置 //在三维空间是一个坐标,在四维空间是一个预测
  • 大小

3.1 举例 - EfficientDet/D4 COCO 2017

在这个例子中,将使用 EfficientDet/d4 模型,它是 EfficientDet 模型系列中的一员。

这个系列在 TensorFlow Hub 上可用的预训练模型都是在 COCO 2017 数据集上训练的。该系列中的不同模型,从 D0 到 D7,根据复杂性和输入图像尺寸的不同而异。最紧凑的模型是 D0,接受 512×512 像素的输入尺寸,并提供最快的推理速度。在另一端,我们有 D7,它需要 1536×1536 的输入尺寸,并且执行推理需要相对较长的时间。

import os
import numpy as np
import cv2
import zipfile
import requests
import glob as glob

import tensorflow_hub as hub
import matplotlib
import matplotlib.pyplot as plt
import warnings
import logging
import absl

# Filter absl warnings
warnings.filterwarnings("ignore", module="absl")

# Capture all warnings in the logging system
logging.captureWarnings(True)

# Set the absl logger level to 'error' to suppress warnings
absl_logger = logging.getLogger("absl")
absl_logger.setLevel(logging.ERROR)

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

3.2 下载示例图像

def download_file(url, save_name):
    url = url
    file = requests.get(url)

    open(save_name, 'wb').write(file.content)
def unzip(zip_file=None):
    try:
        with zipfile.ZipFile(zip_file) as z:
            z.extractall("./")
            print("Extracted all")
    except:
        print("Invalid file")
download_file( 
    'https://www.dropbox.com/s/h7l1lmhvga6miyo/object_detection_images.zip?dl=1',
    'object_detection_images.zip'
)
    
unzip(zip_file='object_detection_images.zip')

3.2.1 显示部分样本

image_paths = sorted(glob.glob('object_detection_images' + '/*.png'))

for idx in range(len(image_paths)):
    print(image_paths[idx])
def load_image(path):

    image = cv2.imread(path)
    
    # Convert image in BGR format to RGB.
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Add a batch dimension which is required by the model.
    image = np.expand_dims(image, axis=0)
    
    return image
images = []
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 15))

idx=0
for axis in ax.flat:
    image = load_image(image_paths[idx])
    images.append(image)
    axis.imshow(image[0])
    axis.axis('off')
    idx+=1

在这里插入图片描述

3.2.2 定义一个将类别ID映射到类别名称和颜色的字典

class_index 是一个字典,将 COCO 数据集中的 90 个类别的类别 ID 映射到类别名称。

class_index =  \
{
         1: 'person',
         2: 'bicycle',
         3: 'car',
         4: 'motorcycle',
         5: 'airplane',
         6: 'bus',
         7: 'train',
         8: 'truck',
         9: 'boat',
         10: 'traffic light',
         11: 'fire hydrant',
         13: 'stop sign',
         14: 'parking meter',
         15: 'bench',
         16: 'bird',
         17: 'cat',
         18: 'dog',
         19: 'horse',
         20: 'sheep',
         21: 'cow',
         22: 'elephant',
         23: 'bear',
         24: 'zebra',
         25: 'giraffe',
         27: 'backpack',
         28: 'umbrella',
         31: 'handbag',
         32: 'tie',
         33: 'suitcase',
         34: 'frisbee',
         35: 'skis',
         36: 'snowboard',
         37: 'sports ball',
         38: 'kite',
         39: 'baseball bat',
         40: 'baseball glove',
         41: 'skateboard',
         42: 'surfboard',
         43: 'tennis racket',
         44: 'bottle',
         46: 'wine glass',
         47: 'cup',
         48: 'fork',
         49: 'knife',
         50: 'spoon',
         51: 'bowl',
         52: 'banana',
         53: 'apple',
         54: 'sandwich',
         55: 'orange',
         56: 'broccoli',
         57: 'carrot',
         58: 'hot dog',
         59: 'pizza',
         60: 'donut',
         61: 'cake',
         62: 'chair',
         63: 'couch',
         64: 'potted plant',
         65: 'bed',
         67: 'dining table',
         70: 'toilet',
         72: 'tv',
         73: 'laptop',
         74: 'mouse',
         75: 'remote',
         76: 'keyboard',
         77: 'cell phone',
         78: 'microwave',
         79: 'oven',
         80: 'toaster',
         81: 'sink',
         82: 'refrigerator',
         84: 'book',
         85: 'clock',
         86: 'vase',
         87: 'scissors',
         88: 'teddy bear',
         89: 'hair drier',
         90: 'toothbrush'
}

在这里,我们将使用 COLOR_IDS 将每个类别映射到一个唯一的 RGB 颜色。

R = np.array(np.arange(96, 256, 32))
G = np.roll(R, 1)
B = np.roll(R, 2)

COLOR_IDS = np.array(np.meshgrid(R, G, B)).T.reshape(-1, 3)

3.3 加载模型

TensorFlow Hub 包含许多不同的预训练目标检测模型。在这里,我们将使用在 COCO 2017 数据集上训练的 EfficientDet 类目标检测模型。

EfficientDet 模型有几个版本。EfficientDet 类目标检测器系列包括几个不同复杂度和性能水平的模型,从 D0 到 D7 不等。EfficientDet 家族中各种模型之间的区别主要在于它们的架构、输入图像大小、计算需求和性能。

EfficientDet  = {'EfficientDet D0 512x512'   : 'https://tfhub.dev/tensorflow/efficientdet/d0/1',
                 'EfficientDet D1 640x640'   : 'https://tfhub.dev/tensorflow/efficientdet/d1/1',
                 'EfficientDet D2 768x768'   : 'https://tfhub.dev/tensorflow/efficientdet/d2/1',
                 'EfficientDet D3 896x896'   : 'https://tfhub.dev/tensorflow/efficientdet/d3/1',
                 'EfficientDet D4 1024x1024' : 'https://tfhub.dev/tensorflow/efficientdet/d4/1',
                 'EfficientDet D5 1280x1280' : 'https://tfhub.dev/tensorflow/efficientdet/d5/1',
                 'EfficientDet D6 1280x1280' : 'https://tfhub.dev/tensorflow/efficientdet/d6/1',
                 'EfficientDet D7 1536x1536' : 'https://tfhub.dev/tensorflow/efficientdet/d7/1'
                }

这里使用D4模型:

model_url = EfficientDet['EfficientDet D4 1024x1024' ]

print('loading model: ', model_url)
od_model = hub.load(model_url)

print('\nmodel loaded!')

3.4 单张照片执行推理

正式编写代码处理多个图像并对结果进行后处理之前,让我们先看看如何对单个图像进行推理,并研究模型的输出。

3.4.1 推理第一张照片

# Call the model. # The model returns the detection results in the form of a dictionary.
results = od_model(images[0])

3.4.2 检查结果

目标检测模型以字典形式返回检测结果,其中包含几种不同的类型。

# Convert the dictionary values to numpy arrays.
results = {key:value.numpy() for key, value in results.items()}
# Print the keys from the results dictionary.
for key in results:
    print(key) 
raw_detection_scores
raw_detection_boxes
num_detections
detection_scores
detection_classes
detection_anchor_indices
detection_boxes
detection_multiclass_scores

原始检测结果包含大量多余的、重叠的或具有低置信度分数目标。为了获得有意义的结果,在模型内部应用了后处理技术来过滤和精炼这些原始检测结果。

在接下来的代码单元中,展示了数千个原始检测结果,而最终检测结果只有 16 个。这些最终检测结果中的每一个都有一个关联的置信度分数,我们可能根据我们的应用程序的性质进一步进行过滤。

print('Num Raw Detections: ', (len(results['raw_detection_scores'][0])))
print('Num Detections:     ', (results['num_detections'][0]).astype(int))

现在让我们检查所有 16 个检测结果的一些检测数据。请注意,检测结果按置信度从高到低排序。

# Print the Scores, Classes and Bounding Boxes for the detections.
num_dets = (results['num_detections'][0]).astype(int)

print('\nDetection Scores: \n\n', results['detection_scores'][0][0:num_dets])
print('\nDetection Classes: \n\n', results['detection_classes'][0][0:num_dets])
print('\nDetection Boxes: \n\n', results['detection_boxes'][0][0:num_dets])

Detection Scores: 

 [0.9053347  0.87894064 0.72029626 0.35475957 0.280573   0.17851691
 0.15169649 0.14905997 0.1445412  0.13583934 0.1268262  0.11745103
 0.10781787 0.10152435 0.10052282 0.09746176 0.09339274 0.09328632
 0.09095493 0.08784384 0.0829455  0.08292609 0.08103823 0.07869378
 0.07779253 0.07525648 0.07296266 0.07162358 0.07000387 0.06878356
 0.06795825 0.06612433 0.06536039 0.06445681 0.06108232 0.05956981
 0.05944339 0.05919976 0.05844869 0.05824129 0.05798095 0.05783109
 0.05774955 0.05753423 0.057473   0.05707671 0.05703654 0.05582817
 0.05510496 0.05509546 0.05461833 0.05444406 0.05370587 0.05335438
 0.05323494 0.05099036 0.05098379 0.04960342 0.04953238 0.04781205
 0.04760257 0.04725712 0.04687678 0.0467659  0.04627746 0.04582401
 0.04572169 0.04525106 0.04518513 0.0449252  0.04447877 0.04326633
 0.04299332 0.04221777 0.04141288 0.04104272 0.04087303 0.04061542
 0.04059404 0.04027912 0.0401063  0.03926978 0.03919847 0.03858848
 0.03826131 0.03815732 0.03803976 0.03789702 0.03788551 0.0375124
 0.0373402  0.03672962 0.0366619  0.03631061 0.03627428 0.03595008
 0.03587668 0.03499278 0.0349467  0.0348866 ]

Detection Classes: 

 [ 2. 18.  8.  3. 64. 64.  2. 18. 64. 64. 64.  4. 64. 44. 64. 77. 64. 15.
  6.  3. 64. 15. 64. 17. 28. 64. 64. 64. 64.  3. 64.  1. 64.  1. 34. 44.
 64. 44. 15.  3. 47.  1. 43. 14.  2. 64.  3. 64. 64. 41. 44. 64.  9.  3.
 47. 44. 64. 51.  3.  3.  3. 72. 64. 20. 64. 64. 64. 11. 64. 15. 72. 44.
 64.  3. 85. 33. 44. 64. 33. 64. 64. 64. 64. 19. 64. 64. 64.  4. 44. 64.
 64. 21. 64. 64. 44. 64.  3.  3. 41. 18.]

Detection Boxes: 

 [[1.64872468e-01 1.57030821e-01 7.44122684e-01 7.44292676e-01]
 [3.53599966e-01 1.66687638e-01 9.77678120e-01 4.06754047e-01]
 [6.44268319e-02 6.11664593e-01 2.52094895e-01 8.95661056e-01]
 [6.63066134e-02 6.11912012e-01 2.51467615e-01 8.98775935e-01]
 [8.41052532e-02 6.99530840e-02 1.81532562e-01 1.31785527e-01]
 [1.37546375e-01 8.97510648e-01 2.21870631e-01 9.40171123e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [1.80238336e-01 1.55317247e-01 7.69674718e-01 7.74034619e-01]
 [8.78890008e-02 6.87568486e-02 1.87820852e-01 1.03662342e-01]
 [8.96975677e-03 1.10131532e-01 8.94228965e-02 1.57099128e-01]
 [8.78244117e-02 8.89956504e-02 1.61299452e-01 1.39885262e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [6.90699667e-02 8.94493699e-01 2.21749574e-01 9.60544229e-01]
 [3.02217782e-01 1.09277435e-01 3.30914080e-01 1.51607603e-01]
 [1.11322552e-01 9.43266004e-02 1.63035363e-01 1.29377097e-01]
 [1.33766994e-01 5.59260726e-01 1.81785822e-01 5.84418297e-01]
 [1.69360042e-01 9.05158639e-01 2.20974877e-01 9.38509583e-01]
 [1.67536631e-01 1.60059541e-01 7.52878010e-01 7.50257134e-01]
 [6.34177104e-02 6.11119270e-01 2.52539754e-01 8.96613955e-01]
 [8.30912441e-02 5.53367317e-01 1.26330957e-01 5.95886171e-01]
 [8.78340676e-02 1.08522616e-01 1.64300382e-01 1.76131040e-01]
 [6.55938685e-02 6.11329556e-01 2.51263857e-01 8.96704316e-01]
 [5.78486025e-02 1.11252695e-01 9.05974433e-02 1.43568069e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [1.36894956e-01 8.91270101e-01 2.14785576e-01 9.16019142e-01]
 [1.74802095e-02 5.26435673e-04 4.01396424e-01 1.02759115e-01]
 [1.24034390e-01 7.39476532e-02 1.86334699e-01 1.00527301e-01]
 [1.63804311e-02 9.00249243e-01 2.26505056e-01 9.86810207e-01]
 [5.93338683e-02 1.11474507e-01 8.95349160e-02 1.42564982e-01]
 [5.75893894e-02 9.44942892e-01 1.88199267e-01 9.80552971e-01]
 [1.25466585e-01 7.87266195e-01 1.57683328e-01 8.13043892e-01]
 [8.36775899e-02 1.21865049e-02 1.99123383e-01 1.27721667e-01]
 [1.19268894e-01 7.82504976e-01 1.53899118e-01 8.05798113e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [2.91318387e-01 1.11356884e-01 3.28983605e-01 1.34163916e-01]
 [6.54594824e-02 6.89840317e-02 1.74558565e-01 1.78287789e-01]
 [2.70692766e-01 1.10364988e-01 3.29678982e-01 1.47175804e-01]
 [5.81662506e-02 1.11311868e-01 8.91821533e-02 1.43789485e-01]
 [9.53361765e-02 1.95676565e-01 1.28487736e-01 2.68072605e-01]
 [1.33766994e-01 5.59260726e-01 1.81785822e-01 5.84418297e-01]
 [1.29232541e-01 7.88466334e-01 1.54698476e-01 8.04546356e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [1.32084399e-01 5.58603346e-01 1.82824224e-01 5.85762322e-01]
 [1.97583944e-01 1.73898876e-01 3.45296204e-01 3.29735219e-01]
 [9.58986953e-02 1.22943275e-01 1.53478786e-01 1.67262658e-01]
 [1.43826336e-01 8.37410629e-01 2.17136309e-01 8.97402942e-01]
 [1.97423846e-02 9.15301740e-01 4.17055339e-01 9.97946799e-01]
 [1.54386023e-02 8.48747551e-01 2.16598883e-01 9.61028039e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [3.10323000e-01 1.00097544e-01 3.30266923e-01 1.13031082e-01]
 [1.18318260e-01 8.25858340e-02 1.77379444e-01 1.25782013e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [1.12383544e-01 6.10164046e-01 1.88168332e-01 6.28713846e-01]
 [3.05876464e-01 1.08968809e-01 3.30649525e-01 1.45296082e-01]
 [2.90190130e-01 1.20972842e-01 3.30591053e-01 1.55480236e-01]
 [8.72497633e-02 6.93231225e-02 1.34429961e-01 9.59156454e-02]
 [5.93338683e-02 1.11474507e-01 8.95349160e-02 1.42564982e-01]
 [1.39585942e-01 6.10274732e-01 1.84828877e-01 6.22814715e-01]
 [9.29178968e-02 5.13639927e-01 1.23643354e-01 5.44009447e-01]
 [9.59800482e-02 5.08866668e-01 1.22083634e-01 5.29584408e-01]
 [4.01576646e-02 0.00000000e+00 3.95416975e-01 1.03089206e-01]
 [8.98193792e-02 1.35473967e-01 1.59401804e-01 1.87099367e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [4.53225970e-02 1.11479618e-01 1.05331413e-01 1.56691015e-01]
 [1.57827780e-01 8.98793697e-01 2.18541548e-01 9.25692916e-01]
 [5.82250394e-02 2.86231697e-01 3.17502648e-01 3.81842673e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [7.84504041e-02 1.05156288e-01 1.55611336e-01 1.48619071e-01]
 [3.23234797e-02 1.10831708e-01 8.94218907e-02 1.54622853e-01]
 [1.33766994e-01 5.59260726e-01 1.81785822e-01 5.84418297e-01]
 [3.07980508e-01 1.09241381e-01 3.29170287e-01 1.20673895e-01]
 [1.64561838e-01 1.70814097e-01 7.29829729e-01 7.55293548e-01]
 [9.87112299e-02 5.43558896e-01 1.22093461e-01 5.60232580e-01]
 [1.33766994e-01 5.59260726e-01 1.81785822e-01 5.84418297e-01]
 [6.55938685e-02 6.11329556e-01 2.51263857e-01 8.96704316e-01]
 [2.97420114e-01 1.28319323e-01 3.27704251e-01 1.44253701e-01]
 [3.64893563e-02 6.67656511e-02 1.64101094e-01 1.39753684e-01]
 [1.64907873e-01 1.56999767e-01 7.47540116e-01 7.41400898e-01]
 [5.84709197e-02 9.18762147e-01 2.00678051e-01 9.75903094e-01]
 [3.53834294e-02 1.32890970e-01 8.96973014e-02 1.54766202e-01]
 [8.71308893e-02 8.78557503e-01 2.17836186e-01 9.37938273e-01]
 [3.14090177e-02 0.00000000e+00 2.71784872e-01 1.43777996e-01]
 [6.34177104e-02 6.11119270e-01 2.52539754e-01 8.96613955e-01]
 [0.00000000e+00 8.18094909e-02 9.15636867e-02 1.64138213e-01]
 [1.05832808e-03 1.23680338e-01 1.10894613e-01 2.55682230e-01]
 [4.87464387e-03 6.55749664e-02 1.18604109e-01 1.46144748e-01]
 [6.55938685e-02 6.11329556e-01 2.51263857e-01 8.96704316e-01]
 [3.08386505e-01 1.06262662e-01 3.30590278e-01 1.36533350e-01]
 [9.58888978e-02 1.13962032e-01 1.52433887e-01 1.42586708e-01]
 [1.34968966e-01 9.13233101e-01 2.26271749e-01 9.62688386e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [1.69809069e-03 1.10684238e-01 9.64165926e-02 1.96017563e-01]
 [7.35746250e-02 3.10681835e-02 3.31526279e-01 1.67747080e-01]
 [3.08449388e-01 1.39392763e-01 3.29580367e-01 1.52594984e-01]
 [7.35872164e-02 6.91096112e-02 3.16760093e-01 1.35739923e-01]
 [9.68379453e-02 5.48292518e-01 1.22824632e-01 5.68545938e-01]
 [3.23234797e-02 1.10831708e-01 8.94218907e-02 1.54622853e-01]
 [3.45106423e-01 1.68578252e-01 9.71659541e-01 4.09179568e-01]
 [3.37914348e-01 9.60409939e-02 8.65746975e-01 5.68797350e-01]]

3.4.3 后处理和显示检测结果

模型返回了 16 个检测结果,然而,许多检测结果具有较低的置信度分数,因此需要进一步通过使用最小检测阈值进行筛选。

  1. 从结果字典中检索检测结果
  2. 应用最小检测阈值以筛选检测结果
  3. 对于每个经过阈值筛选的检测结果,显示边界框和一个标签,指示检测到的类别和检测的置信度。
def process_detection(image, results,  min_det_thresh=.3):

    # Extract the detection results from the results dictionary.
    scores  =  results['detection_scores'][0]
    boxes   =  results['detection_boxes'][0]
    classes = (results['detection_classes'][0]).astype(int)

    # Set a minimum detection threshold to post-process the detection results.
    min_det_thresh = min_det_thresh

    # Get the detections whose scores exceed the minimum detection threshold.
    det_indices = np.where(scores >= min_det_thresh)[0]

    scores_thresh  = scores[det_indices]
    boxes_thresh   = boxes[det_indices]
    classes_thresh = classes[det_indices]

    # Make a copy of the image to annotate.
    img_bbox = image.copy()

    im_height, im_width = image.shape[:2]

    font_scale = .6
    box_thickness = 2

    # Loop over all thresholded detections.
    for box, class_id, score in zip(boxes_thresh, classes_thresh, scores_thresh):

        # Get bounding box normalized coordiantes.
        ymin, xmin, ymax, xmax = box

        class_name = class_index[class_id]

        # Convert normalized bounding box coordinates to pixel coordinates.
        (left, right, top, bottom) = (int(xmin * im_width), 
                                      int(xmax * im_width), 
                                      int(ymin * im_height), 
                                      int(ymax * im_height))

        # Annotate the image with the bounding box.
        color = tuple(COLOR_IDS[class_id % len(COLOR_IDS)].tolist())[::-1]
        img_bbox = cv2.rectangle(img_bbox, (left, top), (right, bottom), color, thickness=box_thickness)

        #-------------------------------------------------------------------
        # Annotate bounding box with detection data (class name and score).
        #-------------------------------------------------------------------

        # Build the text string that contains the class name and score associated with this detection.
        display_txt = '{}: {:.2f}%'.format(class_name, 100 * score)
        ((text_width, text_height), _) = cv2.getTextSize(display_txt, cv2.FONT_HERSHEY_SIMPLEX, font_scale, 1)
        
        # Handle case when the label is above the image frame.
        if top < text_height:
            shift_down = int(2*(1.3*text_height))
        else:
            shift_down = 0
        
        # Draw a filled rectangle on which the detection results will be displayed.
        img_bbox = cv2.rectangle(img_bbox, 
                                 (left-1, top-box_thickness - int(1.3*text_height) + shift_down), 
                                 (left-1 + int(1.1 * text_width), top),               
                                 color, 
                                 thickness=-1)

        # Annotate the filled rectangle with text (class label and score).
        img_bbox = cv2.putText(img_bbox, 
                               display_txt,
                               (left + int(.05*text_width), top - int(0.2*text_height) + int(shift_down/2)),
                               cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), 1)
    return img_bbox
3.4.3.1 显示结果(min_det_thresh=0)

首先,使用最小检测阈值为零来处理一张图像,只是为了查看模型返回的所有 16 个检测结果。由于我们没有对结果进行过滤,我们预计可能会有一些多余和/或错误的检测结果。

# Call the model.
results = od_model(images[0])

# Convert the dictionary values to numpy arrays.
results = {key:value.numpy() for key, value in results.items()}

# Remove the batch dimension from the first image.
image = np.squeeze(images[0])

# Process the first sample image.
img_bbox = process_detection(image, results, min_det_thresh=0)

plt.figure(figsize=[15, 10])
plt.imshow(img_bbox)
plt.axis('off');

在这里插入图片描述
由于我们没有应用检测阈值来过滤结果,上述结果显示了模型返回的所有检测结果。然而,请注意,所有错误标记的检测结果也具有非常低的置信度。因此,始终建议对模型生成的结果应用最小检测阈值。

3.4.3.1.2 显示结果(min_det_thresh=0.3)

阈值的值取决于数据和应用程序,但通常,介于 0.3 和 0.5 之间是一个不错的经验法则。现在让我们应用一个检测阈值来过滤结果。

img_bbox = process_detection(image, results, min_det_thresh=.3)

plt.figure(figsize=[15, 10])
plt.imshow(img_bbox)
plt.axis('off');

在这里插入图片描述

3.5 完整代码实现

现在正式实现并创建一个方便的函数来在图像列表上迭代执行模型预测。

3.5.1 run_inference()

def run_inference(images, model):
    
    results_list = []
    for img in images:
        result = model(img)
        result = {key:value.numpy() for key,value in result.items()}

        results_list.append(result)

    return results_list

3.5.2 对示例图像进行预测

# Perform inference on each image and store the results in a list.
results_list = run_inference(images, od_model)

3.5.3 展示示例图像预测结果

for idx in range(len(images)):
      
    # Remove the batch dimension.
    image = np.squeeze(images[idx])
    
    # Generate the annotated image.
    image_bbox = process_detection(image, results_list[idx], min_det_thresh=.31)
        
    # Display annotated image.
    plt.figure(figsize=[20,10*len(images)])
    plt.subplot(len(images),1,idx+1)
    plt.imshow(image_bbox)
    plt.axis('off')

在这里插入图片描述在这里插入图片描述在这里插入图片描述

在这里插入图片描述

4. 总结

本篇使用了 EfficienDet 家族的 D4 模型进行目标识别,如果应用程序需要更快的推理速度,可以考虑使用较小的模型(从 D0 到 D3)。

测试代码:Keras-Object-Detection-TensorFlow-Hub

5. 参考资料

【1】Jammy@Jetson Orin - Tensorflow & Keras Get Started

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/592314.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

STM32入门学习之ADC

1.ADC在STM32进行数据采集时十分重要。通过ADC可以将外界的数字信号转换为模拟信号&#xff0c;以满足采样的需求。(资料参考于正点原子) STM32 拥有 1~3 个 ADC &#xff08; STM32F101/102 系列只有 1 个 ADC &#xff09;&#xff0c;这些 ADC 可以独立使用&#…

20240504在RK3588的Buildroot系统下使用i2cdetect xxxx ppppp

20240504在RK3588的Buildroot系统下使用i2cdetect 2024/5/4 10:45 rootok3588:/# rootok3588:/# i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- …

2024年第六届先进材料、机械和制造国际会议(AMMM 2024)即将召开!

2024年第六届先进材料、机械和制造国际会议&#xff08;AMMM 2024&#xff09;将于2024年9月6-8日在日本东京举行。AMMM 2024将以国际材料&#xff0c;机械和制造为主题&#xff0c;吸引到来自多个领域的研究人员和学者相聚在一起分享知识&#xff0c;讨论想法&#xff0c;并了…

【系统架构师】-选择题(十二)

1、网闸的作用&#xff1a;实现内网与互联网通信&#xff0c;但内网与互联网不是直连的 2、管理距离是指一种路由协议的路由可信度。15表示该路由信息比较可靠 管理距离越小&#xff0c;它的优先级就越高&#xff0c;也就是可信度越高。 0是最可信赖的&#xff0c;而255则意味…

Typora编辑markdown的技巧

参考视频的B站链接&#xff1a; 手把手教你撰写Typora笔记 在其中选择了常用的部分做标记。 一、标题 使用ctrl数字键&#xff0c;可以快捷的把一行文字变成n级标题 二、源代码模式 可以在下图所示进入 三、设置typora能够自动显示粘贴的图片 打开“偏好设置”&#xff0…

利用 Zstandard (zstd) 高效压缩数据

目录标题 1. Zstandard 压缩算法概述主要特性&#xff1a; 2. Zstandard 在实践中的应用应用案例&#xff1a; 3. 如何使用 Zstandard3.1 安装 Zstandard3.2 使用命令行工具3.3 集成到程序中 4. 总结 在大数据时代&#xff0c;如何高效地存储和传输数据成为了许多企业和开发者面…

Elasticsearch 数据聚合

Bucket聚合&#xff08;桶聚合&#xff09; 对文档做分组&#xff0c;aggs 按照文档字段值或日期进行分组&#xff0c;能参与分词的字段不能做聚合&#xff0c;如text类型的字段 例如&#xff1a;根据城市名称做聚合&#xff0c;也就是城市名称对数据进行分组统计。可以加qu…

OSPF优化

OSPF的优化主要目的是为了减少LSA的更新量 路由汇总-----可以减少骨干区域的LSA数量 特殊区域-----可以减少非骨干区域的LSA数量 OSPF路由汇总 域间路由汇总 域间路由汇总在ABR设备上进行操作 [GS-R2-ospf-1-area-0.0.0.1]abr-summary 192.168.0.0 255.255.224.0 [GS-R3-o…

面经总结系列(二): 面壁智能大模型算法工程师

&#x1f468;‍&#x1f4bb;作者简介&#xff1a; CSDN、阿里云人工智能领域博客专家&#xff0c;新星计划计算机视觉导师&#xff0c;百度飞桨PPDE&#xff0c;专注大数据与AI知识分享。✨公众号&#xff1a;GoAI的学习小屋 &#xff0c;免费分享书籍、简历、导图等&#xf…

设计模式之数据访问对象模式

在Java编程的浩瀚星海中&#xff0c;有一个模式低调却强大&#xff0c;它像是一位默默无闻的超级英雄&#xff0c;支撑起无数应用的数据脊梁——那就是数据访问对象&#xff08;DAO, Data Access Object&#xff09;模式&#xff01;想象一下&#xff0c;如果你能像操纵魔法一样…

网络基础(全)

协议 ”协议“就是一种约定。那么协议需要需要管理吗&#xff1f;答案是当然需要管理呀。 操作系统要进行协议管理——先描述&#xff0c;在组织协议本质就是软件&#xff0c;软件是可以进分层的协议在设计的时候&#xff0c;就是被层状的划分的为什么要划分为层状结呢&#…

综合能源系统:Modbus转IEC104网关解决方案

Modbus转IEC104网关BE102 方案概述 Modbus和IEC104是两种通信协议&#xff0c;各自适用于不同行业和场景&#xff0c;其中Modbus常见于工业自动化&#xff0c;而IEC104则主导电力行业。在某些项目中&#xff0c;需要将Modbus设备的数据传至IEC104电力平台&#xff0c;但两者协…

[嵌入式系统-65]:RT-Thread-组件:FinSH控制台, 用户与RT Thread OS实时命令行交互工具

目录 FinSH 控制台 1. FinSH 简介 2. FinSH 内置命令 - 内核代码自身提供的命令 显示线程状态 显示信号量状态 显示事件状态 显示互斥量状态 显示邮箱状态 显示消息队列状态 显示内存池状态 显示定时器状态 显示设备状态 显示动态内存状态 3. 自定义 FinSH 命令 …

【5/01-5/03】 Arxiv安全类文章速览

知识星球 首先推荐一下我们的知识星球&#xff0c;以AI与安全结合作为主题&#xff0c;包括AI在安全上的应用和AI本身的安全&#xff1b; 加入星球你将获得&#xff1a; 【Ai4sec】&#xff1a;以数据驱动增强安全水位&#xff0c;涵盖内容包括&#xff1a;恶意软件分析&…

MATLAB中功率谱密度计算pwelch函数使用详解

MATLAB中功率谱密度计算pwelch函数使用详解 目录 前言 一、pwelch函数简介 二、pwelch函数参数说明 三、pxx pwelch(x)示例 四、[pxx,f]pwelch(x,window,noverlap,nfft,fs)示例 四、[pxx,f] pwelch(x,window,noverlap,nfft,fs,freqrange,spectrumtype)示例 五、多通道功…

# cmd 报错 “npm 不是内部或外部命令,也不是可运行的程序 或批处理文件”

cmd 报错 “npm 不是内部或外部命令,也不是可运行的程序 或批处理文件” 1、报错原因分析&#xff1a; Node.js 没有安装或安装不正确。 npm 的路径没有添加到系统环境变量中。 安装 Node.js 时选择了不包含 npm 的安装选项。 2、解决方法&#xff1a; 1&#xff09;在 cm…

【房屋】租房攻略,萌新第一次租房需要考虑的要素(通勤、地段、房源)

【房屋】租房攻略&#xff0c;萌新第一次租房需要考虑的要素&#xff08;通勤、地段、房源&#xff09; 文章目录 1、位置要好&#xff08;通勤近 vs 地段好&#xff09;2、户型要好&#xff08;朝向/楼层&#xff0c;独卫/家具&#xff0c;水电费&#xff09;3、价格要便宜4、…

Github 2024-05-03 Java开源项目日报 Top9

根据Github Trendings的统计,今日(2024-05-03统计)共有9个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量Java项目9Kotlin项目1C++项目1libGDX: 跨平台Java游戏开发框架 创建周期:4284 天开发语言:Java, C++协议类型:Apache License 2.0Star数量:2…

DDD:根据maven的脚手架archetype生成ddd多模块项目目录结构

随着领域驱动的兴起&#xff0c;很多人都想学习如何进行ddd的项目开发&#xff0c;那ddd的项目结构是怎么样的&#xff1f;又是如何结合SpringBoot呢&#xff1f;那么针对这个问题&#xff0c;笔者使用maven的archetype封装一个相对通用的ddd的项目目录&#xff0c;方便一键生成…

函数模板 template

函数模板的定义和调用 注意&#xff1a; 在调用函数模板时&#xff0c;编译器会根据调用的函数的参数类型自动推导出T的类型。 优先选择普通函数 强制调用函数模板 函数模板不能对函数的参数自动强制类型转换 myPrintAll(10,b)//普通函数&#xff0c;因为普通函数将b强制转换成…
最新文章