创建一个感应流物体探测器算法胶囊¶
引言¶
在本教程中,我们将介绍如何使用一个 已有模型使用[Tensorflow对象检测 API] 您可以在 我们的 GitHub存储库.
设置环境¶
看到 上一个教程 关于 建立一个发展环境.
感应器流 面部检测算法胶囊¶
文件结构¶
像上一篇教程一样,我们将开始创建一个新的文件,
detector_face, a meta.conf 并且 capsule.py.
您还需要将现有的TensorFlow模型和元数据放入
对于这本教程,它们将被命名为 detector.pb 其他
dataset_metadata.json下载 detector.pb 其他 dataset_metadata.json
根据 在这里其他 TensorFlow预训练
模型可以在 [Tensorflow 1 对象检测模型动物园]中找到
[探测器流1检测模型动物园]和[Tensorflow2对象检测模型]
动物园
探测模型动物园
文件结构将像这样:
your_working_directory
├── docker-compose.yml
└── capsules
└── detector_face
├── meta.conf
├── capsule.py
├── detector.pb
└── dataset_metadata.json
算法胶囊子的转录数据¶
像上一篇教程一样,将版本信息放入
meta.conf:
[about]
api_compatibility_version = 0.3
算法胶囊¶
首先,进口依赖性:
# Import dependencies
import numpy as np
from typing import Dict
from vcap import (
BaseCapsule,
NodeDescription,
DetectionNode,
FloatOption,
DETECTION_NODE_TYPE,
OPTION_TYPE,
BaseStreamState,
rect_to_coords,
)
from vcap_utils import TFObjectDetector
算法胶囊的定义将比前一个更复杂.
另外,由于我们在算法胶囊中可以选择值,
我们使用一个真正的后端,我们将通过一个兰布达 backend_loader我们
在下面的后端部分中,我们会更多地讨论这个问题.
# Define the Capsule class
class Capsule(BaseCapsule):
# Metadata of this capsule
name = "face_detector"
description = "This is an example of how to wrap a TensorFlow Object " \
"Detection API model"
version = 1
# Define the input type. Since this is an object detector, and doesn't
# require any input from other capsules, the input type will be a
# NodeDescription with size=NONE.
input_type = NodeDescription(size=NodeDescription.Size.NONE)
# Define the output type. In this case, as we are going to return a list of
# bounding boxes, the output type will be size=ALL. The type of detection
# will be "face", and we will place the detection confidence in extra_data.
output_type = NodeDescription(
size=NodeDescription.Size.ALL,
detections=["face"],
extra_data=["detection_confidence"]
)
# Define the backend_loader
backend_loader = lambda capsule_files, device: Backend(
device=device,
model_bytes=capsule_files["detector.pb"],
metadata_bytes=capsule_files["dataset_metadata.json"])
# The options for this capsule. In this example, we will allow the user to
# set a threshold for the minimum detection confidence. This can be adjusted
# using the BrainFrame client or through REST API.
options = {
"threshold": FloatOption(
description="Filter out bad detections",
default=0.5,
min_val=0.0,
max_val=1.0,
)
}
后端¶
因为我们使用了向量流模型,我们将使用一个子类的
TFObjectDetector 没有 BaseBackend现在,我们要去. TFObjectDetector 课程
方便地为我们做以下事情:
- 输入模型字节到内存中
- 执行批量推断
- 完成后关闭模型,清理记忆
TFObjectDetector 已经定义了构造者, batch_process() 其他
close() 我们需要一个方法,我们可以自己设定它们.
处理 process_frame() 方法.
# Define the Backend Class
class Backend(TFObjectDetector):
def process_frame(self, frame: np.ndarray,
detection_node: None,
options: Dict[str, OPTION_TYPE],
state: BaseStreamState) -> DETECTION_NODE_TYPE:
"""
:param frame: A numpy array of shape (height, width, 3)
:param detection_node: None
:param options: Example: {"threshold": 0.5}. Defined in Capsule class above.
:param state: (Unused in this capsule)
:return: A list of detections
"""
# Send the frame to the BrainFrame backend. This function will return a
# queue. BrainFrame will batch_process() received frames and populate
# the queue with the results.
prediction_output_queue = self.send_to_batch(frame)
# Wait for predictions
predictions = prediction_output_queue.get()
# Iterate through all the predictions received in this frame
detection_nodes = []
for prediction in predictions:
# Filter out detections that is not a face.
if prediction.name != "face":
continue
# Filter out detection with low confidence.
if prediction.confidence < options["threshold"]:
continue
# Create a DetectionNode for the prediction. It will be reused by
# any other capsules that require a face DetectionNode in their
# input type. An age classifier capsule would be an example of such
# a capsule.
new_detection = DetectionNode(
name=prediction.name,
# convert [x1, y1, x2, y2] to [[x1,y1], [x1, y2]...]
coords=rect_to_coords(prediction.rect),
extra_data={"detection_confidence": prediction.confidence}
)
detection_nodes.append(new_detection)
return detection_nodes
当您重新启动 BrainFrame 时,您的算法胶囊将被包装成一个 .cap 文件
您将看到其信息在"BrainFrame"客户端上.

一旦你加载了流量,你就能看到推断结果.
