aws-samples / host-yolov8-on-sagemaker-endpoint

MIT No Attribution
35 stars 24 forks source link

Non-standard packaging of model.pt under code/ #10

Closed athewsey closed 3 months ago

athewsey commented 1 year ago

Conventionally, the actual model artefacts e.g. yolov8l.pt would be packaged directly under the model.tar.gz root and outside of the code/ subfolder - which should contain only the inference code module.

This is important because (AFAIK) re-creating PyTorchModels with a different source_dir argument will "re-pack" the source artifact to replace the contents of code/ - generating a new final model.tar.gz for deployment. Deviating from this pattern is confusing for users who start from this sample and then try to deploy a model trained on SageMaker (where the starting point will likely be a model.tar.gz with a .pt file in the root)

Additionally, the model_dir passed to the model_fn points to this extracted root folder, so a more typical model loading function might look like:

def model_fn(model_dir):
    print("Executing model_fn from inference.py ...")
    env = os.environ
    model = YOLO(os.path.join(model_dir, env['YOLOV8_MODEL']))
    return model

I'd recommend refactoring this sample to pull yolov8l.pt up to the root level of model.tar.gz... and maybe even demonstrate how an initial "raw" model.tar.gz containing only the model is "re-packed" to include the inference code module - when calling PyTorchModel(source_dir="code", entry_point="inference.py", ....)

DoctorSlimm commented 1 year ago

Bump, this is very important...

KennyTC commented 12 months ago

Could anyone explain what is the value of model_dir? And why in the original source code, the path is YOLO("/opt/ml/model/code/" + env['YOLOV8_MODEL']), but it worked?