|
| 1 | +""" |
| 2 | +Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + |
| 4 | +Microsoft Corporation (“Microsoft”) grants you a nonexclusive, perpetual, |
| 5 | +royalty-free right to use, copy, and modify the software code provided by us |
| 6 | +("Software Code"). You may not sublicense the Software Code or any use of it |
| 7 | +(except to your affiliates and to vendors to perform work on your behalf) |
| 8 | +through distribution, network access, service agreement, lease, rental, or |
| 9 | +otherwise. This license does not purport to express any claim of ownership over |
| 10 | +data you may have shared with Microsoft in the creation of the Software Code. |
| 11 | +Unless applicable law gives you more rights, Microsoft reserves all other |
| 12 | +rights not expressly granted herein, whether by implication, estoppel or |
| 13 | +otherwise. |
| 14 | + |
| 15 | +THE SOFTWARE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | +MICROSOFT OR ITS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 21 | +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | +ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE |
| 24 | +POSSIBILITY OF SUCH DAMAGE. |
| 25 | +""" |
| 26 | +import os, json |
| 27 | +from azureml.core import Workspace |
| 28 | +from azureml.core import Experiment |
| 29 | +from azureml.core.model import Model |
| 30 | +import azureml.core |
| 31 | +from azureml.core import Run |
| 32 | +import argparse |
| 33 | + |
| 34 | + |
| 35 | +# Get workspace |
| 36 | +# ws = Workspace.from_config() |
| 37 | +run = Run.get_context() |
| 38 | +exp = run.experiment |
| 39 | +ws = run.experiment.workspace |
| 40 | + |
| 41 | + |
| 42 | +parser = argparse.ArgumentParser("evaluate") |
| 43 | +parser.add_argument( |
| 44 | + "--config_suffix", type=str, help="Datetime suffix for json config files" |
| 45 | +) |
| 46 | +parser.add_argument( |
| 47 | + "--json_config", |
| 48 | + type=str, |
| 49 | + help="Directory to write all the intermediate json configs", |
| 50 | +) |
| 51 | +args = parser.parse_args() |
| 52 | + |
| 53 | +print("Argument 1: %s" % args.config_suffix) |
| 54 | +print("Argument 2: %s" % args.json_config) |
| 55 | + |
| 56 | +if not (args.json_config is None): |
| 57 | + os.makedirs(args.json_config, exist_ok=True) |
| 58 | + print("%s created" % args.json_config) |
| 59 | +# Paramaterize the matrics on which the models should be compared |
| 60 | +# Add golden data set on which all the model performance can be evaluated |
| 61 | + |
| 62 | +# Get the latest run_id |
| 63 | +# with open("aml_config/run_id.json") as f: |
| 64 | +# config = json.load(f) |
| 65 | + |
| 66 | +train_run_id_json = "run_id_{}.json".format(args.config_suffix) |
| 67 | +train_output_path = os.path.join(args.json_config, train_run_id_json) |
| 68 | +with open(train_output_path) as f: |
| 69 | + config = json.load(f) |
| 70 | + |
| 71 | +# parser = argparse.ArgumentParser() |
| 72 | +# parser.add_argument('--train_run_id',type=str,default='',help='Run id of the newly trained model') |
| 73 | +# #parser.add_argument('--model_assets_path',type=str,default='outputs',help='Location of trained model.') |
| 74 | + |
| 75 | + |
| 76 | +new_model_run_id = config["run_id"] # args.train_run_id |
| 77 | +experiment_name = config["experiment_name"] |
| 78 | +# exp = Experiment(workspace=ws, name=experiment_name) |
| 79 | + |
| 80 | + |
| 81 | +try: |
| 82 | + # Get most recently registered model, we assume that is the model in production. Download this model and compare it with the recently trained model by running test with same data set. |
| 83 | + model_list = Model.list(ws) |
| 84 | + production_model = next( |
| 85 | + filter( |
| 86 | + lambda x: x.created_time == max(model.created_time for model in model_list), |
| 87 | + model_list, |
| 88 | + ) |
| 89 | + ) |
| 90 | + production_model_run_id = production_model.tags.get("run_id") |
| 91 | + run_list = exp.get_runs() |
| 92 | + # production_model_run = next(filter(lambda x: x.id == production_model_run_id, run_list)) |
| 93 | + |
| 94 | + # Get the run history for both production model and newly trained model and compare mse |
| 95 | + production_model_run = Run(exp, run_id=production_model_run_id) |
| 96 | + new_model_run = Run(exp, run_id=new_model_run_id) |
| 97 | + |
| 98 | + production_model_mse = production_model_run.get_metrics().get("mse") |
| 99 | + new_model_mse = new_model_run.get_metrics().get("mse") |
| 100 | + print( |
| 101 | + "Current Production model mse: {}, New trained model mse: {}".format( |
| 102 | + production_model_mse, new_model_mse |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + promote_new_model = False |
| 107 | + if new_model_mse < production_model_mse: |
| 108 | + promote_new_model = True |
| 109 | + print("New trained model performs better, thus it will be registered") |
| 110 | +except: |
| 111 | + promote_new_model = True |
| 112 | + print("This is the first model to be trained, thus nothing to evaluate for now") |
| 113 | + |
| 114 | +run_id = {} |
| 115 | +run_id["run_id"] = "" |
| 116 | +# Writing the run id to /aml_config/run_id.json |
| 117 | +if promote_new_model: |
| 118 | + run_id["run_id"] = new_model_run_id |
| 119 | + # register new model |
| 120 | + # new_model_run.register_model(model_name='',model_path='outputs/sklearn_regression_model.pkl') |
| 121 | + |
| 122 | +run_id["experiment_name"] = experiment_name |
| 123 | +filename = "run_id_{}.json".format(args.config_suffix) |
| 124 | +output_path = os.path.join(args.json_config, filename) |
| 125 | +with open(output_path, "w") as outfile: |
| 126 | + json.dump(run_id, outfile) |
0 commit comments