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", ....)
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?
Conventionally, the actual model artefacts e.g.
yolov8l.pt
would be packaged directly under themodel.tar.gz
root and outside of thecode/
subfolder - which should contain only the inference code module.This is important because (AFAIK) re-creating
PyTorchModel
s with a differentsource_dir
argument will "re-pack" the source artifact to replace the contents ofcode/
- generating a new finalmodel.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 amodel.tar.gz
with a.pt
file in the root)Additionally, the
model_dir
passed to themodel_fn
points to this extracted root folder, so a more typical model loading function might look like:I'd recommend refactoring this sample to pull
yolov8l.pt
up to the root level ofmodel.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 callingPyTorchModel(source_dir="code", entry_point="inference.py", ....)