Azure / MachineLearningNotebooks

Python notebooks with ML and deep learning examples with Azure Machine Learning Python SDK | Microsoft
https://docs.microsoft.com/azure/machine-learning/service/
MIT License
4.01k stars 2.49k forks source link

Trouble Deploying Yolov5 (pt file) Pytorch model #1780

Open VirajVaitha123 opened 2 years ago

VirajVaitha123 commented 2 years ago

Hi there,

I am trying to deploy a model by following the tutorial.

Before reading the full details I would like to address something I think is causing the problem.

When I run print(os.getenv('AZUREML_MODEL_DIR')) it returns None. Should I be able to see the variable?, I don't think the environment variable is being created when I registered my model.

Anyway here is my entry script, deployment code and error message.

Here is my entry script:

from azureml.core.model import Model
from PIL import Image
import json
import numpy as np
import os
import torch

def init():
    """
    Load registered YOLO v5 model from the Azure ML model Registry
    """
    global model
    print(os.getenv('AZUREML_MODEL_DIR'))
    model = torch.hub.load(os.getenv('AZUREML_MODEL_DIR'), 'best.pt')

def run(request):
    """
    Code to execute upon receiving a request
    """
    print(request)
    file_bytes = request.files["image"]
    image = Image.open(file_bytes).convert('RGB')
    img = Image.open(io.BytesIO(img_bytes))

    # inference
    results = model(img, size=640)
    # inference
    return results

def preprocess():
    return None

Here is all the code relating to the deployment, bare in mind I have already registered a model in AzureML workspace.

from azureml.core import Environment
from azureml.core.model import InferenceConfig

from azureml.core.webservice import LocalWebservice

env = Environment(name="Covid Detector Project")

dummy_inference_config = InferenceConfig(
    environment=myenv,
    source_directory="./source_dir",
    entry_script="entry_script.py",
)

deployment_config = LocalWebservice.deploy_configuration(port=6789)

service = Model.deploy(
    ws,
    "myservice",
    [model],
    dummy_inference_config,
    deployment_config,
    overwrite=True,
)
service.wait_for_deployment(show_output=True)

This is the error I am receiving:

File "/var/azureml-app/source_dir/entry_script.py", line 16, in init
    model = torch.hub.load(os.getenv('AZUREML_MODEL_DIR'), 'best.pt')
  File "/azureml-envs/azureml_20ec05239403d2f40e2df6a2b8665783/lib/python3.6/site-packages/torch/hub.py", line 397, in load
    repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, verbose, skip_validation)
  File "/azureml-envs/azureml_20ec05239403d2f40e2df6a2b8665783/lib/python3.6/site-packages/torch/hub.py", line 165, in _get_cache_or_reload
    repo_owner, repo_name, branch = _parse_repo_info(github)
  File "/azureml-envs/azureml_20ec05239403d2f40e2df6a2b8665783/lib/python3.6/site-packages/torch/hub.py", line 112, in _parse_repo_info
    repo_owner, repo_name = repo_info.split('/')
ValueError: too many values to unpack (expected 2)

The first thing I think I need to understand is how to retrieve the os.getenv('AZUREML_MODEL_DIR') environment variable.

Thanks,

Viraj

Mohand995 commented 1 year ago

can you share the full source code please

abhi0928 commented 5 months ago

Hi @VirajVaitha123 , did you successfully deploy your model? if so, can you please tell me the steps , I am also having issues with deploying my yolov5 model on azure,

thanks