Back to Blog
MediumImported

Understand the YOLOv7 Prediction Method and Deploy YOLOv7 Model as a Service

💡 本文同步發表於 Medium:原文連結

https://medium.com/@simon3458/yolov7-model-deployment-in-primehub-deployment-eng-version-945dcf9ae08e

點擊查看連結詳情...

https://medium.com/@simon3458/yolov7-model-deployment-in-primehub-deployment-eng-version-945dcf9ae08e

Understand the YOLOv7 Prediction Method and Deploy YOLOv7 Model as a Service

Understand the YOLOv7 Prediction Method and Deploy YOLOv7 Model as a Service

This article is translated from my Chinese blog post. If you want to know the article in the Chinese version, you can click the following link to get the information.

https://blog.infuseai.io/yolov7-model-deployment-in-primehub-deployment-99b377227447

點擊查看連結詳情...

https://blog.infuseai.io/yolov7-model-deployment-in-primehub-deployment-99b377227447

Introduction

I want to write an article introducing the YOLOv7 model when it is released on Github. After I read the article and understood the model, I finished the content. In this article, I will tell you about the method of predicting the YOLOv7 model and deploying the YOLOv7 model by PrimeHub deployment. I hope you can understand the YOLOv7 model when you read this article.

In 2020, researcher Wang, Chien Yao, from Academia Sinica, released the YOLOv4 model. It raises the accuracy of predicting image results. In September 2022, they released the new YOLO-base model, the YOLOv7 model. It reduces 40% of variables and 50% of computing power in the real-time object detection SOTA model. It can be improved in structure, speed of computing, and accuracy. You can check the paper to know the detail of the YOLOv7 model.

YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors

YOLOv7 surpasses all known object detectors in both speed and accuracy in the range from 5 FPS to 160 FPS and has the highest accuracy 56.8% AP among all known real-time object detectors with 30 FPS or higher on GPU V100. YOLOv7-E6 object detector (56 FPS V100, 55.9% AP) outperforms both transformer-based detector SWIN-L Cascade-Mask R-CNN (9.2 FPS A100, 53.9% AP) by 509% in speed and 2% in accuracy, and convolutional-based detector ConvNeXt-XL Cascade-Mask R-CNN (8.6 FPS A100, 55.2% AP) by 551% in speed and 0.7% AP in accuracy, as well as YOLOv7 outperforms: YOLOR, YOLOX, Scaled-YOLOv4, YOLOv5, DETR, Deformable DETR, DINO-5scale-R50, ViT-Adapter-B and many other object detectors in speed and accuracy. Moreover, we train YOLOv7 only on MS COCO dataset from scratch without using any other datasets or pre-trained weights. Source code is released in https://github.com/WongKinYiu/yolov7.

https://arxiv.org/abs/2207.02696

The author also provides the information on Github, which is contained:

  1. How to use the command line to train and predict the model.
  2. Use the docker environment and Nvidia-docker to develop the model.
  3. Provide the YOLOv7 pre-trained model and fine-tune the model.
  4. Provide the third-party tool. For example, Colab, HuggingFace, and State-of-the-art.

We can see the detailed information in the README.md file.

GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors - WongKinYiu/yolov7

https://github.com/WongKinYiu/yolov7

Deploy the YOLOv7 Model

If we want to use the simple python code to predict the YOLOv7 model, we need to understand the model function and code. When I found some information on the Kaggle website, I found the sharing code which can predict the YOLOv7 model when the notebook imports the YOLOv7 function.

yolov7_quick_start

Explore and run AI code with Kaggle Notebooks | Using data from multiple data sources

https://www.kaggle.com/code/parapapapam/yolov7-quick-start
CODE
from models.experimental import attempt_load  
from utils.general import non_max_suppression

Now, we can use the PrimeHub Deployment to deploy the YOLOv7 model on the platform. Primehub Deployments is based on the Seldon core server. We can use the simple python code and Seldon core environment to let our model as the RestAPI service. It is a convenient tool for deploying the machine learning model.

GitHub - SeldonIO/seldon-core: An MLOps framework to package, deploy, monitor and manage thousands of production machine learning models

An MLOps framework to package, deploy, monitor and manage thousands of production machine learning models - SeldonIO/seldon-core

https://github.com/SeldonIO/seldon-core

Compared to the traditional Restful API service, the model as a service has some special characteristics:

  1. The model inference service needs a GPU device to predict the result faster. The larger the model is, the larger the computing power is.
  2. The machine learning inference service needs some logic of preprocessing and postprocessing. In some duplicated scenarios, it might be related to multiple models.

Therefore, we need a better tool to do this thing. PrimeHub integrates the Seldon Core to exert the benefit.

After we understand why we need to use the Seldon core, we write the class wrapper of the model service. The following code is aimed at the YOLOv7 input and output logic. You can modify it to fit your requirement.

After we finish writing the python wrapper code, we will build the model docker image. We can use the following command line to build and push the docker images.

CODE
$ make build  
$ make push

Now we can deploy the model to the Seldon core service. In PrimeHub, we can use the Web UI interface to deploy the model. The detailed information is shown below. Please be aware that Model URI needs to fill in the correct file path. In my showcase, I put the pre-trained model, which can be gotten on the Github website, into PrimeHub shared Files folder.

PrimeHub Deployment Setting

After we finish deploying the model, we can use the command line to test and confirm the model service can get the correct answer.

CODE
$ curl -F '[email protected]' <model-endpoint-url>
CODE
{"data":{"names":["t:0","t:1","t:2","t:3","t:4","t:5"],"tensor":{"shape":[3,6],"values":[123.073974609375,199.46484375,1123.720458984375,718.7385864257812,0.9323439598083496,0.0,748.6097412109375,42.15193176269531,1160.1611328125,717.9664916992188,0.8973875641822815,0.0,432.42529296875,437.1168212890625,519.785888671875,719.8562622070312,0.626276433467865,27.0]}},"meta":{"requestPath":{"model":"Infuseaidev/yolov7-prepackaged:v0.1.0-dev"}}}

Demo the Result on the Streamlit Service Web UI

In addition, I use Streamlit, which can be created on PrimeHub Apps, to produce the simple web UI interface and predict the image on the web page. Streamlit is a special framework for machine learning and data science. They do not need knowledge of front-end programming and use python code to create the dashboard and app. It is very easy to demo the result to the user.

Predict the image by the YOLOv7 model in the Streamlit app

The following table is the config of the Streamlit in PrimeHub apps. We can also configure it on the PrimeHub web UI page.

PrimeHub Streamlit App Setting

The status of the PrimeHub Streamlit apps

Conclusion

In this article, I provide some links and content to understand the YOLOv7 prediction method. Also, I use PrimeHub deployments to deploy the model. The users can use this method to build the ML serving environment and create new business value. Welcome to give me your suggestion and thoughts. Thanks!

I am Simon

Hi, I am Simon, the Customer Success Engineer at InfuseAI. If you think the article is helpful to you, please give me applause. Welcome to provide some suggestions and discuss with me in InfuseAI Discord.
Linkedin: <https://www.linkedin.com/in/simonliuyuwei/>

We are InfuseAI

InfuseAI provides MLOps tools:

  1. PrimeHub— Our end-to-end MLOps platform, is the entire machine learning lifecycle in one platform.
  2. Piperider— The data quality toolkit for data professionals.

Join our machine learning community to discover more: InfuseAI Discord

Join our community

這篇文章有幫上忙嗎?

留言

載入留言中…