Azure / Azure/azureml-examples
Import Error
- Dominant language
- Jupyter Notebook
- Stars
- 2k
- Forks
- 1.7k
- Avg merge
- 18h 18m
- Merged PRs (30d)
- 2
Description
## Which example? Describe the issue
Running the following codes one by one :
********************************
import azureml.core
print("This notebook was created using version 1.35.0 of the Azure ML SDK.")
print("You are currently using version", azureml.core.VERSION, "of the Azure ML SDK.")
assert (
azureml.core.VERSION >= "1.35"
), "Please upgrade the Azure ML SDK by running '!pip install --upgrade azureml-sdk' then restart the kernel."
!pip install --upgrade azureml-sdk
from azureml.core.workspace import Workspace
ws = Workspace.from_config()
from azureml.core.compute import AmlCompute, ComputeTarget
cluster_name = "gpu-cluster-nc6"
try:
compute_target = ws.compute_targets[cluster_name]
print("Found existing compute target.")
except KeyError:
print("Creating a new compute target...")
compute_config = AmlCompute.provisioning_configuration(
vm_size="Standard_NC6",
idle_seconds_before_scaledown=600,
min_nodes=0,
max_nodes=4,
)
compute_target = ComputeTarget.create(ws, cluster_name, compute_config)
# Can poll for a minimum number of nodes and for a specific timeout.
# If no min_node_count is provided, it will use the scale settings for the cluster.
compute_target.wait_for_completion(
show_output=True, min_node_count=None, timeout_in_minutes=20
)
from azureml.core import Experiment
experiment_name = "automl-image-multiclass"
experiment = Experiment(ws, name=experiment_name)
import os
import urllib
from zipfile import ZipFile
# download data
download_url = "https://cvbp-secondary.z19.web.core.windows.net/datasets/image_classification/fridgeObjects.zip"
data_file = "./fridgeObjects.zip"
urllib.request.urlretrieve(download_url, filename=data_file)
# extract files
with ZipFile(data_file, "r") as zip:
print("extracting files...")
zip.extractall()
print("done")
# delete zip file
os.remove(data_file)
#Where are the files in the MS Azure?
import json
import os
src = "./fridgeObjects/"
train_validation_ratio = 5
# Retrieving default datastore that got automatically created when we setup a workspace
workspaceblobstore = ws.get_default_datastore().name
# Path to the training and validation files
train_annotations_file = os.path.join(src, "train_annotations.jsonl")
validation_annotations_file = os.path.join(src, "validation_annotations.jsonl")
# sample json line dictionary
json_line_sample = {
"image_url": "AmlDatastore://"
+ workspaceblobstore
+ "/"
+ os.path.basename(os.path.dirname(src)),
"label": "",
}
index = 0
# Scan each sub directary and generate jsonl line
with open(train_annotations_file, "w") as train_f:
with open(validation_annotations_file, "w") as validation_f:
for className in os.listdir(src):
subDir = src + className
if not os.path.isdir(subDir):
continue
# Scan each sub directary
print("Parsing " + subDir)
for image in os.listdir(subDir):
json_line = dict(json_line_sample)
json_line["image_url"] += f"/{className}/{image}"
json_line["label"] = className
if index % train_validation_ratio == 0:
# validation annotation
validation_f.write(json.dumps(json_line) + "\n")
else:
# train annotation
train_f.write(json.dumps(json_line) + "\n")
index += 1
from azureml.automl.core.shared.constants import ImageTask
from azureml.train.automl import AutoMLImageConfig
from azureml.train.hyperdrive import GridParameterSampling, choice
image_config_vit = AutoMLImageConfig(
task=ImageTask.IMAGE_CLASSIFICATION,
compute_target=compute_target,
training_data=training_dataset,
validation_data=validation_dataset,
hyperparameter_sampling=GridParameterSampling({"model_name": choice("vitb16r224")}),
iterations=1,
)
# Retrieving default datastore that got automatically created when we setup a workspace
ds = ws.get_default_datastore()
ds.upload(src_dir="./fridgeObjects", target_path="fridgeObjects")
from azureml.core import Dataset
from azureml.data import DataType
# get existing training dataset
training_dataset_name = "fridgeObjectsTrainingDataset"
if training_dataset_name in ws.datasets:
training_dataset = ws.datasets.get(training_dataset_name)
print("Found the training dataset", training_dataset_name)
else:
# create training dataset
training_dataset = Dataset.Tabular.from_json_lines_files(
path=ds.path("fridgeObjects/train_annotations.jsonl"),
set_column_types={"image_url": DataType.to_stream(ds.workspace)},
)
training_dataset = training_dataset.register(
workspace=ws, name=training_dataset_name
)
# get existing validation dataset
validation_dataset_name = "fridgeObjectsValidationDataset"
if validation_dataset_name in ws.datasets:
validation_dataset = ws.datasets.get(validation_dataset_name)
print("Found the validation dataset", validation_dataset_name)
else:
# create validation dataset
validation_dataset = Dataset.Tabular.from_json_lines_files(
path=ds.path("fridgeObjects/validation_annotations.jsonl"),
set_column_types={"image_url": DataType.to_stream(ds.workspace)},
)
validation_dataset = validation_dataset.register(
workspace=ws, name=validation_dataset_name
)
print("Training dataset name: " + training_dataset.name)
print("Validation dataset name: " + validation_dataset.name)
training_dataset.to_pandas_dataframe()
from azureml.automl.core.shared.constants import ImageTask
from azureml.train.automl import AutoMLImageConfig
from azureml.train.hyperdrive import GridParameterSampling, choice
image_config_vit = AutoMLImageConfig(
task=ImageTask.IMAGE_CLASSIFICATION,
compute_target=compute_target,
training_data=training_dataset,
validation_data=validation_dataset,
hyperparameter_sampling=GridParameterSampling({"model_name": choice("vitb16r224")}),
iterations=1,
)
ImportError Traceback (most recent call last)
/tmp/ipykernel_18229/4251181215.py in
----> 1 from azureml.automl.core.shared.constants import ImageTask
2 from azureml.train.automl import AutoMLImageConfig
3 from azureml.train.hyperdrive import GridParameterSampling, choice
4
5 image_config_vit = AutoMLImageConfig(
ImportError: cannot import name 'ImageTask' from 'azureml.automl.core.shared.constants' (/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azureml/automl/core/shared/constants.py)
## Additional context
-
Contributor guide
Assessment
This issue has not been assessed yet.