def main(): # get workspace ws = load_workspace() model = Model.register(ws, model_name='pytorch_mnist', model_path='model.pth') # create dep file myenv = CondaDependencies() myenv.add_pip_package('numpy') myenv.add_pip_package('torch') with open('pytorchmnist.yml', 'w') as f: print('Writing out {}'.format('pytorchmnist.yml')) f.write(myenv.serialize_to_string()) print('Done!') # create image image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="pytorchmnist.yml", dependencies=['./models.py']) image = Image.create(ws, 'pytorchmnist', [model], image_config) image.wait_for_creation(show_output=True) # create service aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, description='simple MNIST digit detection') service = Webservice.deploy_from_image(workspace=ws, image=image, name='pytorchmnist-svc', deployment_config=aciconfig) service.wait_for_deployment(show_output=True)
def save_conda_dependencies(amls_config, filename): conda_dependencies = CondaDependencies() for dependency in amls_config['conda_dependencies']: conda_dependencies.add_pip_package(dependency) with open(filename, "w") as f: f.write(conda_dependencies.serialize_to_string())
def main(): ws = Workspace.from_config() conda = CondaDependencies() conda.add_conda_package("python==3.5") conda.add_pip_package("h5py==2.8.0") conda.add_pip_package("html5lib==1.0.1") conda.add_pip_package("keras==2.2.0") conda.add_pip_package("Keras-Applications==1.0.2") conda.add_pip_package("Keras-Preprocessing==1.0.1") conda.add_pip_package("matplotlib==2.2.2") conda.add_pip_package("numpy==1.14.5") conda.add_pip_package("opencv-python==3.3.0.9") conda.add_pip_package("pandas==0.23.3") conda.add_pip_package("Pillow==5.2.0") conda.add_pip_package("requests==2.19.1") conda.add_pip_package("scikit-image==0.14.0") conda.add_pip_package("scikit-learn==0.19.2") conda.add_pip_package("scipy==1.1.0") conda.add_pip_package("sklearn==0.0") conda.add_pip_package("tensorflow==1.9.0") conda.add_pip_package("urllib3==1.23") conda.add_pip_package("azureml-sdk") with open("environment.yml", "w") as f: f.write(conda.serialize_to_string()) with open("environment.yml", "r") as f: print(f.read()) image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="environment.yml", docker_file="Dockerfile", dependencies=DEPENDENCIES) webservices = ws.webservices(compute_type='ACI') image = ContainerImage.create(name="ai-bootcamp", models=[], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) webservices_list = [] for key in webservices: webservices_list.append(key) service_name = webservices_list[0] aciwebservice = AciWebservice(ws, service_name) aciwebservice.update(image=image)
def create_yaml_file(): myenv = CondaDependencies() myenv.add_conda_package("scikit-learn") myenv.add_conda_package("pandas") with open("myenv.yml", "w") as f: f.write(myenv.serialize_to_string()) with open("myenv.yml", "r") as f: print(f.read())
def create_env(): myenv = CondaDependencies() myenv.add_conda_package("pytorch") myenv.add_conda_package("numpy") myenv.add_conda_package("torchvision=0.4.1") with open("./myenv.yml", "w") as f: f.write(myenv.serialize_to_string()) return "./myenv.yml"
def deploy(self): myenv = CondaDependencies() myenv.add_pip_package("azureml-sdk") myenv.add_pip_package("joblib") myenv.add_pip_package("tensorflow") myenv.add_pip_package("Pillow") myenv.add_pip_package("azureml-dataprep[pandas,fuse]>=1.1.14") with open("diagnoz_env.yml", "w") as f: f.write(myenv.serialize_to_string()) huml_env = Environment.from_conda_specification( name="diagnoz_env", file_path="diagnoz_env.yml") inference_config = InferenceConfig(entry_script="score.py", source_directory='.', environment=huml_env) print("file deployement : ") for root, dir_, files in os.walk(os.getcwd()): print("dir_", dir_) for filename in files: print("filename :", filename) aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={ "data": "cancer-data", "method": "tensorflow" }, description='Predicting cancer with tensorflow') try: AciWebservice(self.ws, self.config.DEPLOY_SERVICE_NAME).delete() print("webservice deleted") except WebserviceException: pass model = self.ws.models[self.config.MODEL_NAME] service = Model.deploy(workspace=self.ws, name=self.config.DEPLOY_SERVICE_NAME, models=[model], inference_config=inference_config, deployment_config=aciconfig) service.wait_for_deployment(show_output=True) print("success deployement")
def deploy(self): try: AciWebservice(self.ws, self.DEPLOY_SERVICE_NAME).delete() print("webservice deleted") except WebserviceException: pass conda_dep = CondaDependencies() conda_dep.add_pip_package("joblib") conda_dep.add_pip_package("torch") conda_dep.add_pip_package("torchvision") conda_dep.add_pip_package("azureml-sdk") conda_dep.add_pip_package("azure-storage-blob") conda_dep.add_pip_package("PyYAML") conda_dep.add_pip_package("scikit-learn") conda_dep.add_pip_package("matplotlib") conda_dep.add_pip_package("opencensus-ext-azure") shoes_designer_env_file = "shoes_designer_env.yml" with open(shoes_designer_env_file,"w") as f: f.write(conda_dep.serialize_to_string()) shoes_designer_env = Environment.from_conda_specification(name="shoes_designer_env", file_path=shoes_designer_env_file) inference_config = InferenceConfig(entry_script="score.py", environment=shoes_designer_env) aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=2, tags={"method" : "torch"}, description='Generate shoes with torch') model = self.ws.models[self.MODEL_NAME] service = Model.deploy(workspace=self.ws, name=self.DEPLOY_SERVICE_NAME, models=[model], inference_config=inference_config, deployment_config=aciconfig, overwrite=True) service.wait_for_deployment(show_output=True) print("success deployement") return service
description=MODEL_DESCRIPTION) print(model.name, model.id, model.version, sep='\t') #Set the image aciconfig = AciWebservice.deploy_configuration(cpu_cores=CPU_CORES, memory_gb=MEMORY_GB, description=SERVICE_DESCRIPTION, auth_enabled=AUTH_ENABLED) if CONDA_FILE_URL == '' and DOCKER_FILE_URL == '': from azureml.core.conda_dependencies import CondaDependencies myenv = CondaDependencies() myenv.add_conda_package("scikit-learn") #myenv.add_pip_package("joblib") with open("myenv.yml", "w") as f: f.write(myenv.serialize_to_string()) # configure the image image_config = ContainerImage.image_configuration( execution_script=EXECUTION_SCRIPT_PATH, runtime="python", conda_file="myenv.yml") elif CONDA_FILE_URL is not '' and DOCKER_FILE_URL == '': wget.download(CONDA_FILE_URL, CONDA_FILE_PATH) image_config = ContainerImage.image_configuration( execution_script=EXECUTION_SCRIPT_PATH, runtime="python", conda_file=CONDA_FILE_PATH) elif DOCKER_FILE_URL is not '': wget.download(DOCKER_FILE_URL, DOCKER_FILE_PATH) image_config = ContainerImage.image_configuration( execution_script=EXECUTION_SCRIPT_PATH,
model = Model.register(model_path = filename, model_name = "ta_model", tags = {"key": "1"}, description = "TextBlob Prediction", workspace = ws) ta_env = CondaDependencies() ta_env.add_conda_package("scikit-learn") ta_env.add_conda_package("dill") with open("ta_env.yml","w") as f: f.write(ta_env.serialize_to_string()) with open("ta_env.yml","r") as f: print(f.read()) %%time image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="ta_env.yml") aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "textanalytics", "method" : "textblob"}, description='Predict Sentiment of Sentences')
aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "sentiment", "method" : "textblob"}, description='Predict Sentiment Score') textblobenv = CondaDependencies() textblobenv.add_conda_package("scikit-learn") # textblobenv.add_conda_package("textblob") # textblobenv.add_conda_package("pickle") # textblobenv.add_conda_package("dill") with open("textblobenv.yml","w") as f: f.write(textblobenv.serialize_to_string()) with open("textblobenv.yml","r") as f: print(f.read()) ############################# %%writefile score.py import json import numpy as np import os from sklearn.externals import joblib from sklearn.naive_bayes import MultinomialNB from sklearn.feature_extraction.text import CountVectorizer from textblob import TextBlob
def generate_yaml( directory: str, ref_filename: str, needed_libraries: list, conda_filename: str, ): """ Creates a deployment-specific yaml file as a subset of the image classification environment.yml Also adds extra libraries, if not present in environment.yml Args: directory (string): Directory name of reference yaml file ref_filename (string): Name of reference yaml file needed_libraries (list of strings): List of libraries needed in the Docker container conda_filename (string): Name of yaml file to be deployed in the Docker container Returns: Nothing """ with open(os.path.join(directory, ref_filename), "r") as f: yaml_content = yaml.load(f, Loader=yaml.FullLoader) # Extract libraries to be installed using conda extracted_libraries = [ depend for depend in yaml_content["dependencies"] if any(lib in depend for lib in needed_libraries) ] # Extract libraries to be installed using pip if any(isinstance(x, dict) for x in yaml_content["dependencies"]): # if the reference yaml file contains a "pip" section, # find where it is in the list of dependencies ind = [ yaml_content["dependencies"].index(depend) for depend in yaml_content["dependencies"] if isinstance(depend, dict) ][0] extracted_libraries += [ depend for depend in yaml_content["dependencies"][ind]["pip"] if any(lib in depend for lib in needed_libraries) ] # Check whether additional libraries are needed not_found = [ lib for lib in needed_libraries if not any(lib in ext for ext in extracted_libraries) ] # Create the deployment-specific yaml file conda_env = CondaDependencies() for ch in yaml_content["channels"]: conda_env.add_channel(ch) for library in extracted_libraries + not_found: conda_env.add_conda_package(library) # Display the environment print(conda_env.serialize_to_string()) # Save the file to disk conda_env.save_to_file(base_directory=os.getcwd(), conda_file_path=conda_filename)
# you can return any data type as long as it is JSON-serializable return result.tolist() #%% create environment file for deployment from azureml.core.conda_dependencies import CondaDependencies mymodelenv = CondaDependencies() mymodelenv.add_conda_package("scikit-learn") mymodelenv.add_conda_package("pandas") mymodelenv.add_conda_package("statsmodels") mymodelenv.add_conda_package("scipy=1.2") mymodelenv.add_conda_package("numpy") with open("mymodelenv.yml","w") as f: f.write(mymodelenv.serialize_to_string()) with open("mymodelenv.yml","r") as f: print(f.read()) print('Complete') #%% create container image from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "dengue-fever", "method" : "sklearn"}, description='Predict dengue load')
print( 'Model picked: {} \nModel Description: {} \nModel Version: {}'.format( model.name, model.description, model.version)) dependencies = CondaDependencies() dependencies.add_conda_package("numpy") dependencies.add_conda_package("matplotlib") dependencies.add_conda_package("scikit-learn") dependencies.add_conda_package("tensorflow") dependencies.add_conda_package("keras") dependencies.add_conda_package("scikit-image") dependencies.add_pip_package("pynacl==1.2.1") os.makedirs("./score/", exist_ok=True) with open("./score/dependencies.yml", "w") as f: f.write(dependencies.serialize_to_string()) original_dir = os.getcwd() # Change directory since the docker container is expecting thing at the TLD os.chdir("./score") image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="dependencies.yml", description="Image with Uploaded Model") # Image Name can only include alphanumeric or '.' and '-' image = ContainerImage.create( name=container_image_name, models=[model], # this is the registered model object image_config=image_config,
tags = {"key": "1"}, description = "Salary Prediction", workspace = ws) # Define Azure ML Deploymemt configuration aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "Salary", "method" : "sklearn"}, description='Predict Stackoverflow Salary') # Create enviroment configuration file salenv = CondaDependencies() salenv.add_conda_package("scikit-learn") with open("salenv.yml","w") as f: f.write(salenv.serialize_to_string()) with open("salenv.yml","r") as f: print(f.read()) # Create Azure ML Scoring file ''' %%writefile score.py import json import numpy as np import os import pickle from sklearn.externals import joblib from sklearn.linear_model import LogisticRegression from azureml.core.model import Model
for m in l_models: print(m.name, m.version) model = l_models[0] # --- Create image ----------------------------------------------------------------------------------- # Define image contents env = CondaDependencies() if backend == "python": env.add_conda_package("pandas") env.add_conda_package("sklearn") env.add_conda_package("xgboost") env.add_conda_package("seaborn") else: env.add_pip_package("rpy2") with open("code/conda_file.yml", "w") as file: file.write(env.serialize_to_string()) print(env.serialize_to_string()) ''' # DOES NOT WORK # Create Basis image tmp = os.getcwd() try: os.chdir(tmp + "/code") image_config = ContainerImage.image_configuration( execution_script="dummy.py", # must be in cwd runtime="python", conda_file="conda_file.yml", docker_file="docker_file", dependencies=["init.py"] if backend == "python" else ["install_package.R", "hmsPM"]) image = ContainerImage.create(workspace=ws, name="base-image",