In this lab we are going the train a PyTorch Model that can classify Simpsons using the resources we have created in the previous lab.
1. Connect to your resources
Import dependencies
Start with importing dependencies. If you are using a Notebook in Azure Machine Learning Studio, you have all the latest versions install. If you are running your own Jupyter notebook then you have to install the azureml-sdk (pip install azureml-sdk).
Paste the code below in the first cell and run this cell.
import os, random
import azureml
import shutil
import urllib.request
from pathlib import Path
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import cv2
import urllib3
import zipfile
from azureml.core.model import Model, InferenceConfig
from azureml.core import Workspace, Datastore, Experiment, Run, Environment, ScriptRunConfig
from azureml.core.compute import ComputeTarget, AmlCompute, AksCompute, ComputeTarget
from azureml.train.dnn import PyTorch
from azureml.widgets import RunDetails
from azureml.core.webservice import Webservice, AksWebservice, AciWebservice
from azureml.core.dataset import Dataset
from azureml.core.resource_configuration import ResourceConfiguration
from azureml.core.conda_dependencies import CondaDependencies
# check core SDK version number
print("Azure ML SDK Version: ", azureml.core.VERSION)
Connect to workspace
Create a new code cell by clicking on the '+ Code' button
Paste the code below
ws = Workspace.from_config()
print("Connected to workspace: ",ws.name)
Run the cell
Performing the interactive authentication using the link and code provide
Run the cell again
It should say: "Connected to workspace: "
Connect to Azure Machine Learning Compute Cluster
compute_target = ComputeTarget(workspace=ws, name="gpu-cluster")
print("Connected to compute target:",compute_target.name)
Connect to the default datastore
ds = Datastore.get_default(ws)
print("Connected to datastore:",ds.name)
Choose the refresh button above your folder structure to see data files
Preview the dataset
To take a peak at the images in the dataset paste and the run the code below.
from mpl_toolkits.axes_grid1 import AxesGrid
import random
import cv2
import matplotlib.pyplot as plt
path = r"data/train"
random_filenames = []
for tag in os.listdir(path):
random_filenames.append(path+"/"+tag+"/"+random.choice([
x for x in os.listdir(os.path.join(path,tag))
if os.path.isfile(os.path.join(path,tag, x))
]))
grid = AxesGrid(plt.figure(1, (20,20)), 111, nrows_ncols=(4, 5), axes_pad=0, label_mode="1")
i = 0
for img_name in random_filenames[0:10]:
image = cv2.imread(img_name)
image = cv2.resize(image, (352, 352))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Show image in grid
grid[i].imshow(image)
i = i+1