Step 02: Detecting Objects Using resnet50
Features:
Models:
This tutorial and the assets can be downloaded as part of the Wallaroo Tutorials repository.
Step 02: Detecting Objects Using resnet50
The following tutorial demonstrates how to use a trained mobilenet model deployed in Wallaroo to detect objects. This process will use the following steps:
- Create a Wallaroo workspace and pipeline.
- Upload a trained resnet50 ML model and add it as a pipeline step.
- Deploy the pipeline.
- Perform an inference on a sample image.
- Draw the detected objects, their bounding boxes, their classifications, and the confidence of the classifications on the provided image.
- Review our results.
Steps
Import Libraries
The first step will be to import our libraries. Please check with Step 00: Introduction and Setup and verify that the necessary libraries and applications are added to your environment.
# preload needed libraries
import wallaroo
from wallaroo.object import EntityNotFoundError
from wallaroo.framework import Framework
from IPython.display import display
from IPython.display import Image
import pandas as pd
import json
import datetime
import time
import cv2
import matplotlib.pyplot as plt
import string
import random
import pyarrow as pa
import sys
import asyncio
pd.set_option('display.max_colwidth', None)
import sys
import utils
Connect to the Wallaroo Instance
The first step is to connect to Wallaroo through the Wallaroo client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.
This is accomplished using the wallaroo.Client()
command, which provides a URL to grant the SDK permission to your specific Wallaroo environment. When displayed, enter the URL into a browser and confirm permissions. Store the connection into a variable that can be referenced later.
If logging into the Wallaroo instance through the internal JupyterHub service, use wl = wallaroo.Client()
. For more information on Wallaroo Client settings, see the Client Connection guide.
# Login through local service
wl = wallaroo.Client()
Set Variables
The following variables and methods are used later to create or connect to an existing workspace, pipeline, and model. This example has both the resnet model, and a post process script.
workspace_name = f'resnetworkspace'
pipeline_name = f'resnetnetpipeline'
model_name = f'resnet50'
model_file_name = 'models/frcnn-resnet.pt.onnx'
Create Workspace
The workspace will be created or connected to, and set as the default workspace for this session. Once that is done, then all models and pipelines will be set in that workspace.
workspace = wl.get_workspace(name=workspace_name, create_if_not_exist=True)
wl.set_current_workspace(workspace)
wl.get_current_workspace()
{'name': 'resnetworkspace', 'id': 17, 'archived': False, 'created_by': 'fb2916bc-551e-4a76-88e8-0f7d7720a0f9', 'created_at': '2024-07-30T21:02:24.058201+00:00', 'models': [], 'pipelines': []}
Create Pipeline and Upload Model
We will now create or connect to an existing pipeline as named in the variables above.
pipeline = wl.build_pipeline(pipeline_name)
resnet_model = wl.upload_model(model_name, model_file_name, framework=Framework.ONNX).configure(batch_config="single",
tensor_fields=["tensor"])
Upload Post Processing Module
The following module takes the results from the CV model output, and averages the confidence values from all detected objects. This is used for observability and model drift tracking.
input_schema = pa.schema([
pa.field('boxes', pa.list_(pa.float32())),
pa.field('classes', pa.list_(pa.int64())),
pa.field('confidences', pa.list_(pa.float32()))
]
)
output_schema = pa.schema([
pa.field('boxes', pa.list_(pa.float32())),
pa.field('classes', pa.list_(pa.int64())),
pa.field('confidences', pa.list_(pa.float32())),
pa.field('avg_confidence', pa.float32()),
])
module_post_process_model = wl.upload_model("cv-post-process-drift-detection",
"./models/post-process-drift-detection.zip",
framework=Framework.PYTHON,
input_schema=input_schema,
output_schema=output_schema
)
Waiting for model loading - this will take up to 10.0min.
Model is pending loading to a container runtime.
Model is attempting loading to a container runtime...successful
Ready
Deploy Pipeline
With the model uploaded, we can add it is as a step in the pipeline, then deploy it. Once deployed, resources from the Wallaroo instance will be reserved and the pipeline will be ready to use the model to perform inference requests.
pipeline.undeploy()
pipeline.clear()
pipeline.add_model_step(resnet_model)
pipeline.add_model_step(module_post_process_model)
deploy_config = wallaroo.DeploymentConfigBuilder().replica_count(1).cpus(1).memory("1Gi").build()
pipeline.deploy(deployment_config=deploy_config)
name | resnetnetpipeline |
---|---|
created | 2024-07-30 21:02:25.030607+00:00 |
last_updated | 2024-07-30 21:04:00.811720+00:00 |
deployed | True |
workspace_id | 17 |
workspace_name | resnetworkspace |
arch | x86 |
accel | none |
tags | |
versions | 2d4e6200-a998-4bb9-987b-5035f7c08718, 42d9339c-aa62-412d-ab7c-d31341129afa |
steps | resnet50 |
published | False |
Test the pipeline by running inference on a sample image
Prepare input image
Next we will load a sample image and resize it to the width and height required for the object detector.
We will convert the image to a numpy ndim array and add it do a dictionary
image = cv2.imread('./data/images/input/example/dairy_bottles.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12,8))
plt.grid(False)
plt.imshow(image)
plt.show()
width, height = 640, 480
dfImage, resizedImage = utils.loadImageAndConvertToDataframe('./data/images/input/example/dairy_bottles.png', width, height)
Run Inference
With that done, we can have the model detect the objects on the image by running an inference through the pipeline, and storing the results for the next step.
IMPORTANT NOTE: If necessary, add timeout=60
to the infer
method if more time is needed to upload the data file for the inference request.
startTime = time.time()
infResults = pipeline.infer(dfImage, timeout=300)
endTime = time.time()
infResults.loc[:, ['time', 'out.avg_confidence']]
time | out.avg_confidence | |
---|---|---|
0 | 2024-07-30 21:04:24.123 | 0.358804 |
Draw the Inference Results
With our inference results, we can take them and use the Wallaroo CVDemo class and draw them onto the original image. The bounding boxes and the confidence value will only be drawn on images where the model returned a 50% confidence rate in the object’s identity.
elapsed = 1.0
results = {
'model_name' : model_name,
'pipeline_name' : pipeline_name,
'width': width,
'height': height,
'image' : resizedImage,
'inf-results' : infResults,
'confidence-target' : 0.50,
'inference-time': (endTime-startTime),
'onnx-time' : int(elapsed) / 1e+9,
'classes_file': "./models/coco_classes.pickle",
'color': 'BLUE'
}
image = utils.drawDetectedObjectsFromInference(results)
Undeploy the Pipeline
With the inference complete, we can undeploy the pipeline and return the resources back to the Wallaroo instance.
pipeline.undeploy()
name | resnetnetpipeline |
---|---|
created | 2024-07-30 21:02:25.030607+00:00 |
last_updated | 2024-07-30 21:04:00.811720+00:00 |
deployed | False |
workspace_id | 17 |
workspace_name | resnetworkspace |
arch | x86 |
accel | none |
tags | |
versions | 2d4e6200-a998-4bb9-987b-5035f7c08718, 42d9339c-aa62-412d-ab7c-d31341129afa |
steps | resnet50 |
published | False |