From 371d79fc6cb6f7e7d31e9a79ce81384111f0c012 Mon Sep 17 00:00:00 2001 From: risha Date: Thu, 28 Sep 2023 15:15:12 -0700 Subject: [PATCH 1/3] add batch scoring for other od models --- ...ction-batch-scoring-non-mlflow-model.ipynb | 2 +- .../scripts/batch_scoring.py | 2 +- .../scripts/batch_scoring_yolo.py | 54 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb index cfdf057e2a..9daca5f1ff 100644 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb @@ -555,7 +555,7 @@ "image_object_detection_job.set_limits(timeout_minutes=60)\n", "\n", "# Pass the fixed settings or parameters\n", - "image_object_detection_job.set_training_parameters(model_name=\"yolov5\")" + "image_object_detection_job.set_training_parameters(model_name=\"fasterrcnn_resnet50_fpn\")" ] }, { diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py index 70543715ea..6702c3fa6d 100644 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py @@ -13,7 +13,7 @@ load_model, run_inference_batch, ) -from azureml.automl.dnn.vision.object_detection_yolo.writers.score import ( +from azureml.automl.dnn.vision.object_detection.writers.score import ( _score_with_model, ) from azureml.automl.dnn.vision.common.utils import _set_logging_parameters diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py new file mode 100644 index 0000000000..70543715ea --- /dev/null +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py @@ -0,0 +1,54 @@ +# Copyright (c) Microsoft. All rights reserved. +# Licensed under the MIT license. + +import os +import argparse +import json + +from azureml.core.model import Model +from azureml.automl.core.shared import logging_utilities + +from azureml.automl.dnn.vision.common.logging_utils import get_logger +from azureml.automl.dnn.vision.common.model_export_utils import ( + load_model, + run_inference_batch, +) +from azureml.automl.dnn.vision.object_detection_yolo.writers.score import ( + _score_with_model, +) +from azureml.automl.dnn.vision.common.utils import _set_logging_parameters + +TASK_TYPE = "image-object-detection" +logger = get_logger("azureml.automl.core.scoring_script_images") + + +def init(): + global model + global batch_size + + # Set up logging + _set_logging_parameters(TASK_TYPE, {}) + + batch_size = os.getenv("batch_size", None) + batch_size = int(batch_size) if batch_size is not None else batch_size + print(f"args inference batch size is {batch_size}") + + model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "model.pt") + + print(model_path) + + try: + logger.info(f"Loading model from path: {model_path}.") + model_settings = {} + model = load_model(TASK_TYPE, model_path, **model_settings) + logger.info("Loading successful.") + except Exception as e: + logging_utilities.log_traceback(e, logger) + raise + + +def run(mini_batch): + logger.info("Running inference.") + result = run_inference_batch(model, mini_batch, _score_with_model, batch_size) + logger.info("Finished inferencing.") + return result From 98e2591793d86d3c12d0efdfdc631b36ff37412c Mon Sep 17 00:00:00 2001 From: risha Date: Thu, 28 Sep 2023 15:17:47 -0700 Subject: [PATCH 2/3] fix doc --- .../image-object-detection-batch-scoring-non-mlflow-model.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb index 9daca5f1ff..d2650215ee 100644 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb @@ -522,7 +522,7 @@ "- `optimizer` - Type of optimizer to use in training. It must be either sgd, adam, adamw (default is sgd).\n", "- `distributed` - It enable distributed training if compute target contain multiple GPUs. It must be boolean value (default is True).\n", "\n", - "If you wish to use the default hyperparameter values for a given algorithm (say `yolov5`), you can specify the job for your AutoML Image runs as follows:" + "If you wish to use the default hyperparameter values for a given algorithm (say `fasterrcnn_resnet50_fpn`), you can specify the job for your AutoML Image runs as follows:" ] }, { From 85aa50042986a5ead7287ed38bbec1d3d4f1e082 Mon Sep 17 00:00:00 2001 From: risha Date: Thu, 28 Sep 2023 20:38:59 -0700 Subject: [PATCH 3/3] remove duplicate scoring script --- ...ction-batch-scoring-non-mlflow-model.ipynb | 4 +- .../scripts/batch_scoring.py | 14 +++-- .../scripts/batch_scoring_yolo.py | 54 ------------------- 3 files changed, 11 insertions(+), 61 deletions(-) delete mode 100644 sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb index d2650215ee..cfdf057e2a 100644 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/image-object-detection-batch-scoring-non-mlflow-model.ipynb @@ -522,7 +522,7 @@ "- `optimizer` - Type of optimizer to use in training. It must be either sgd, adam, adamw (default is sgd).\n", "- `distributed` - It enable distributed training if compute target contain multiple GPUs. It must be boolean value (default is True).\n", "\n", - "If you wish to use the default hyperparameter values for a given algorithm (say `fasterrcnn_resnet50_fpn`), you can specify the job for your AutoML Image runs as follows:" + "If you wish to use the default hyperparameter values for a given algorithm (say `yolov5`), you can specify the job for your AutoML Image runs as follows:" ] }, { @@ -555,7 +555,7 @@ "image_object_detection_job.set_limits(timeout_minutes=60)\n", "\n", "# Pass the fixed settings or parameters\n", - "image_object_detection_job.set_training_parameters(model_name=\"fasterrcnn_resnet50_fpn\")" + "image_object_detection_job.set_training_parameters(model_name=\"yolov5\")" ] }, { diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py index 6702c3fa6d..5591c6e82c 100644 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py +++ b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring.py @@ -2,10 +2,7 @@ # Licensed under the MIT license. import os -import argparse -import json -from azureml.core.model import Model from azureml.automl.core.shared import logging_utilities from azureml.automl.dnn.vision.common.logging_utils import get_logger @@ -13,7 +10,13 @@ load_model, run_inference_batch, ) -from azureml.automl.dnn.vision.object_detection.writers.score import ( + +# Uncomment line 16-18 and comment line 19-21 to run batch scoring +# for models other than yolo. +# from azureml.automl.dnn.vision.object_detection.writers.score import ( +# _score_with_model, +# ) +from azureml.automl.dnn.vision.object_detection_yolo.writers.score import ( _score_with_model, ) from azureml.automl.dnn.vision.common.utils import _set_logging_parameters @@ -49,6 +52,7 @@ def init(): def run(mini_batch): logger.info("Running inference.") - result = run_inference_batch(model, mini_batch, _score_with_model, batch_size) + result = run_inference_batch( + model, mini_batch, _score_with_model, batch_size) logger.info("Finished inferencing.") return result diff --git a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py b/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py deleted file mode 100644 index 70543715ea..0000000000 --- a/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items-batch-scoring/scripts/batch_scoring_yolo.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright (c) Microsoft. All rights reserved. -# Licensed under the MIT license. - -import os -import argparse -import json - -from azureml.core.model import Model -from azureml.automl.core.shared import logging_utilities - -from azureml.automl.dnn.vision.common.logging_utils import get_logger -from azureml.automl.dnn.vision.common.model_export_utils import ( - load_model, - run_inference_batch, -) -from azureml.automl.dnn.vision.object_detection_yolo.writers.score import ( - _score_with_model, -) -from azureml.automl.dnn.vision.common.utils import _set_logging_parameters - -TASK_TYPE = "image-object-detection" -logger = get_logger("azureml.automl.core.scoring_script_images") - - -def init(): - global model - global batch_size - - # Set up logging - _set_logging_parameters(TASK_TYPE, {}) - - batch_size = os.getenv("batch_size", None) - batch_size = int(batch_size) if batch_size is not None else batch_size - print(f"args inference batch size is {batch_size}") - - model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "model.pt") - - print(model_path) - - try: - logger.info(f"Loading model from path: {model_path}.") - model_settings = {} - model = load_model(TASK_TYPE, model_path, **model_settings) - logger.info("Loading successful.") - except Exception as e: - logging_utilities.log_traceback(e, logger) - raise - - -def run(mini_batch): - logger.info("Running inference.") - result = run_inference_batch(model, mini_batch, _score_with_model, batch_size) - logger.info("Finished inferencing.") - return result