نویسنده(های): جنارو دانیله آچیارو
در ابتدا منتشر شد به سمت هوش مصنوعی.
در زندگی یک یادگیری ماشینی مهندس، آموزش یک مدل تنها نیمی از نبرد است.
در واقع، پس از به دست آوردن یک شبکه عصبی که به طور دقیق تمام داده های آزمایش را پیش بینی می کند، بی فایده باقی می ماند مگر اینکه در دسترس جهانیان قرار گیرد.
استقرار مدل فرآیندی است که یک مدل را در دسترس و قابل استفاده در محیطهای تولیدی قرار میدهد، جایی که میتواند پیشبینیهایی ایجاد کند و بینشهای بیدرنگ را برای کاربران نهایی ارائه دهد و این یک مهارت ضروری برای هر فرد است. ML یا مهندس هوش مصنوعی
در این راهنما، فرآیند استقرار یک مدل سفارشی آموزشدیده با استفاده از چارچوب Detectron2 را مرور خواهیم کرد.
🤖 Detectron2 چیست؟
Detectron2 یک کتابخانه قدرتمند برای تشخیص شی و تقسیم بندی، ساخته شده بر روی PyTorch و توسعه یافته توسط Meta. این یک چارچوب عالی برای آموزش و استقرار مدل های سفارشی شما فراهم می کند. با Detectron2 میتوانید به راحتی شبکههای عصبی بسازید و تنظیم کنید تا اشیاء را در تصاویر و ویدیوها بهطور دقیق شناسایی و بخشبندی کنید.
این کتابخانه بسیاری از مدلهای از پیش آموزشدیده و الگوریتمهای پیشرفته را ارائه میکند که آن را به انتخابی محبوب در میان مهندسان و محققان یادگیری ماشین تبدیل میکند. چه در حال کار بر روی وظایف بینایی کامپیوتری یا ساختن برنامه هایی که نیاز دارند تشخیص شی قابلیتها، Detectron2 ابزارها و انعطافپذیری لازم برای دستیابی به نتایج دقیق و کارآمد را فراهم میکند.
برای اطلاعات بیشتر، به مخزن رسمی GitHub مراجعه کنید: Detectron2 GitHub
یک مدل Detectron2 را دقیق کنید
ما فرض می کنیم که شما از قبل یک مدل تنظیم شده آماده برای استقرار دارید. اگر هنوز مدلی ندارید، نگران نباشید! تیم شگفت انگیز Detectron2 یک آموزش رسمی Colab برای کمک به شما ارائه کرده است: آموزش Detectron2 Colab
در این مقاله، ما به طور خاص بر نحوه استقرار مدل آموزشدیده با استفاده از آن Colab که بر روی بالونهای تقسیمبندی دقیق تنظیم شده است تمرکز خواهیم کرد.
⚠️ مهم است: باید دو فایل (class_names.txt و config.yaml) را از مدل آموزش دیده استخراج کنیم. ما به زودی به این فایل ها نیاز خواهیم داشت.
بنابراین، پس از اجرای آموزش مدل، این قطعه را اجرا کنید:
واردات سیستم عامل
از pathlib واردات مسیر # نام کلاس ها را ذخیره کنید
class_names = MetadataCatalog.get(cfg.مجموعه داده هاقطار[0]).thing_classes
class_names_path: خ = cfg.OUTPUT_DIR + "/class_names.txt"
با باز کردن(مسیر_نام_کلاس، "w") به عنوان f:
برای مورد در class_names:
f.write("%s\n" % مورد)
چاپ کنید(f"\033[92m 🎉 Class names file saved in: {class_names_path} 🎉 \033[00m")
# Save the config yaml
config_yaml_path: str = cfg.OUTPUT_DIR + "/config.yaml"
with open(config_yaml_path, 'w') as file:
file.write(cfg.dump())
print(f"\033[92m 🎉 Config file saved in: {config_yaml_path} 🎉 \033[00m")
Now you can download the following files (located in the /output folder) from Colab:
class_names.txt
config.yaml
model_final.pth
Deploying your model with Torchserve
In this guide, we will use TorchServe to implement our model, you will end up having a fully functional REST API that can deliver predictions from your custom Detectron2 model, ready to be integrated into your applications.
What is Torchserve?
TorchServe is an open-source tool designed to facilitate the efficient deployment of PyTorch models. It streamlines the process of exposing models via a REST API, manages models, handles inference requests, and collects logs and metrics. TorchServe supports simultaneous serving of multiple models, dynamic batching, and adapts to different deployment environments, making it an optimal choice for serving models at production scale.
More details here: TorchServe GitHub , TorchServe Website.
An overview of the deployment process
The deployment process involves the following steps:
- Installation of required packages for Torchserve.
- Review of the handler: this component is responsible for processing inference requests through the API.
- Generation of the .mar package for the model.
- Deployment of the model: we use the Torchserve CLI commands to complete the deployment.
Step 0: Installing the requirements
Please note: In this guide we assume that you already have Detectron2 installed, if not, you can follow the official guide: https://detectron2.readthedocs.io/en/latest/tutorials/install.html
After installing Detectron2 we can install everything we need to distribute the models with the following commands:
pip install torch-model-archiver
pip install pyyaml torch torchvision captum nvgpu
pip install torchserve
Step 1: The handle
One of the key components of TorchServe is the handler.
This is a Python file responsible for loading the model into memory and managing the entire inference pipeline, including preprocessing, inference, and postprocessing.
For our tutorial, you can find the handler file at the following link: medium-repo/deploying_custom_detectron2_models_with_torchserve/model_handler.py at main · vargroup-datascience/medium-repo
If you’re deploying your own model, don’t forget to change the parameters 😊.
Copy and paste this handler into a new file named model_handler.py
.
Step 2: The .mar file
Torchserve requires a .mar file to function, which contains the model weights, inference code and necessary dependencies.
It is interesting to note that uploading a .mar file to a server can be done even if the server is already running, without restarting the server.This allows us to update our models in production without interrupting service. This aspect will not be discussed in detail in this manual.
At this point then we now have our four files:
class_names.txt
config.yaml
model_final.pth
model_handler.py
Next, let’s create the archive that will contain all the files. This archive, named my_d2_model.mar
, can be created using the following command:
torch-model-archiver --model-name my_d2_model --version 1.0 --handler model_handler.py --serialized-file model_final.pth --extra-files config.yaml,class_names.txt -f
This command creates a my_d2_model.mar
file containing all the files needed for the model; if a model with the same name and version already exists, you will need to increment the version.
Once executed, you should see a my_d2_model.mar
file in the current folder.
Step 3: Deploy the model
To use the server, we need to create a model_store folder where the models will be stored.
mkdir model_store
Then we copy the file my_d2_model.mar into the folder model_store:
Linux/Mac OS:
cp my_d2_model.mar model_store
Windows:
copy my_d2_model.mar model_store
At this point then we can start the server with the following command:
torchserve --start --model-store model_store --models my_d2_model=my_d2_model.mar --disable-token-auth
To test whether the server has started correctly, we ping the server with the command:
$ curl http://localhost:8080/ping
If all goes well, we will have this answer:
{
"status": "Healthy"
}
Futhermore, to test if the deployment was successful, we need to run this command, which will return the models currently active on the server.
$ curl http://localhost:8081/models
The output that we expect is as follows:
{
"models": [
{
"modelName": "my_d2_model",
"modelUrl": "my_d2_model.mar"
}
]
}
در این مرحله ما آماده استفاده از مدل خود هستیم.
مرحله 4: به API دسترسی پیدا کنید
اکنون که سرور در حال اجرا است و مدل مستقر شده است، می توانید با استفاده از نقطه پایانی API ارائه شده با مدل تعامل داشته باشید.
برای دریافت پیش بینی از مدل مستقر شده، می توانید از دستور curl زیر استفاده کنید. این دستور یک فایل تصویری را به مدل ارسال می کند و سرور نتایج پیش بینی را برمی گرداند.
$ curl http://127.0.0.1:8080/predictions/my_d2_model -T img.jpg
از طرف دیگر، می توانید از هر مشتری HTTP به انتخاب خود استفاده کنید (مانند Postman، Python’s requests
کتابخانه یا حتی یک برنامه سفارشی) برای تعامل با API و ارسال تصویر برای پیش بینی.
در صورت موفقیت آمیز بودن درخواست، سرور تصویر را با استفاده از مدل مستقر شده پردازش می کند و یک پاسخ JSON را با نتایج پیش بینی برمی گرداند.
نتیجه گیری
در پایان، این راهنما فرآیند گام به گام استقرار یک مدل Detectron2 سفارشی را با استفاده از یک REST API از طریق TorchServe نشان داده است.
ما با نصب وابستگی های لازم شروع کردیم، سپس یک handler سفارشی برای مدیریت خط لوله استنتاج ایجاد کردیم و در نهایت یک فایل .mar حاوی مدل و وابستگی های آن تولید کردیم.
با استفاده از TorchServe، میتوان سروری را راهاندازی کرد که بتواند مدل را از طریق API نشان دهد و امکان استنتاج بلادرنگ را فراهم کند. این روش نه تنها اجازه می دهد تا مدل های یادگیری ماشین را به سرعت وارد تولید کنید، بلکه می توانید مدل ها را بدون وقفه در سرویس به روز کنید.
همه اینها میتوانند برای ادغام مدلهای یادگیری ماشین در برنامههای خود مفید باشند و از استقرار قوی و مقیاسپذیر اطمینان حاصل کنند.
منتشر شده از طریق به سمت هوش مصنوعی