Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes. End-to-End Pipeline Example on Azure #787

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified pipelines/azurepipeline/code/deploy/Dockerfile
100644 → 100755
Empty file.
4 changes: 0 additions & 4 deletions pipelines/azurepipeline/code/deploy/aksdeploymentconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion pipelines/azurepipeline/code/deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ while getopts "m:n:i:d:s:p:u:r:w:t:b:" option;
esac
done
az login --service-principal --username ${SERVICE_PRINCIPAL_ID} --password ${SERVICE_PRINCIPAL_PASSWORD} -t $TENANT_ID
az ml model deploy -n $MODEL_NAME -m ${MODEL}:1 --ic $INFERENCE_CONFIG --pi ${BASE_PATH}/myprofileresult.json --dc $DEPLOYMENTCONFIG -w $WORKSPACE -g $RESOURCE_GROUP --overwrite -v
az ml model deploy -n $MODEL_NAME -m ${MODEL}:1 --ic $INFERENCE_CONFIG --dc $DEPLOYMENTCONFIG -w $WORKSPACE -g $RESOURCE_GROUP --overwrite -v
115 changes: 58 additions & 57 deletions pipelines/azurepipeline/code/deploy/score.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,84 @@


def init():
if Model.get_model_path('tacosandburritos'):
model_path = Model.get_model_path('tacosandburritos')
else:
model_path = '/model/latest.h5'
global model
if Model.get_model_path('tacosandburritos'):
model_path = Model.get_model_path('tacosandburritos')
else:
model_path = '/model/latest.h5'

print('Attempting to load model')
model = tf.keras.models.load_model(model_path)
model.summary()
print('Done!')
print('Attempting to load model')
model = tf.keras.models.load_model(model_path)
model.summary()
print('Done!')

print('Initialized model "{}" at {}'.format(model_path, datetime.datetime.now()))
return model
print('Initialized model "{}" at {}'.format(
model_path, datetime.datetime.now()))


def run(raw_data, model):
prev_time = time.time()
def run(raw_data):
prev_time = time.time()

post = json.loads(raw_data)
img_path = post['image']
post = json.loads(raw_data)
img_path = post['image']

current_time = time.time()
current_time = time.time()

tensor = process_image(img_path, 160)
t = tf.reshape(tensor, [-1, 160, 160, 3])
o = model.predict(t, steps=1) # [0][0]
print(o)
o = o[0][0]
inference_time = datetime.timedelta(seconds=current_time - prev_time)
payload = {
'time': inference_time.total_seconds(),
'prediction': 'burrito' if o > 0.5 else 'tacos',
'scores': str(o)
}
tensor = process_image(img_path, 160)
t = tf.reshape(tensor, [-1, 160, 160, 3])
o = model.predict(t, steps=1) # [0][0]
print(o)
o = o[0][0]
inference_time = datetime.timedelta(seconds=current_time - prev_time)
payload = {
'time': inference_time.total_seconds(),
'prediction': 'burrito' if o > 0.5 else 'tacos',
'scores': str(o)
}

print('Input ({}), Prediction ({})'.format(post['image'], payload))
print('Input ({}), Prediction ({})'.format(post['image'], payload))

return payload
return payload


def process_image(path, image_size):
# Extract image (from web or path)
if path.startswith('http'):
response = requests.get(path)
img = np.array(Image.open(BytesIO(response.content)))
else:
img = np.array(Image.open(path))
# Extract image (from web or path)
if path.startswith('http'):
response = requests.get(path)
img = np.array(Image.open(BytesIO(response.content)))
else:
img = np.array(Image.open(path))

img_tensor = tf.convert_to_tensor(img, dtype=tf.float32)
# tf.image.decode_jpeg(img_raw, channels=3)
img_final = tf.image.resize(img_tensor, [image_size, image_size]) / 255
return img_final
img_tensor = tf.convert_to_tensor(img, dtype=tf.float32)
# tf.image.decode_jpeg(img_raw, channels=3)
img_final = tf.image.resize(img_tensor, [image_size, image_size]) / 255
return img_final


def info(msg, char="#", width=75):
print("")
print(char * width)
print(char + " %0*s" % ((-1 * width) + 5, msg) + char)
print(char * width)
print("")
print(char * width)
print(char + " %0*s" % ((-1 * width) + 5, msg) + char)
print(char * width)


if __name__ == "__main__":
images = {
'tacos': 'https://c1.staticflickr.com/5/4022/4401140214_f489c708f0_b.jpg',
'burrito': 'https://www.exploreveg.org/files/2015/05/sofritas-burrito.jpeg'
}
images = {
'tacos': 'https://c1.staticflickr.com/5/4022/4401140214_f489c708f0_b.jpg', # noqa: E501
'burrito': 'https://www.exploreveg.org/files/2015/05/sofritas-burrito.jpeg' # noqa: E501
}

my_model = init()
init()

for k, v in images.items():
print('{} => {}'.format(k, v))
for k, v in images.items():
print('{} => {}'.format(k, v))

info('Taco Test')
taco = json.dumps({'image': images['tacos']})
print(taco)
run(taco, my_model)
info('Taco Test')
taco = json.dumps({'image': images['tacos']})
print(taco)
run(taco)

info('Burrito Test')
burrito = json.dumps({'image': images['burrito']})
print(burrito)
run(burrito, my_model)
info('Burrito Test')
burrito = json.dumps({'image': images['burrito']})
print(burrito)
run(burrito)
31 changes: 5 additions & 26 deletions pipelines/azurepipeline/code/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def tacosandburritos_train(
# preprocess data
operations['preprocess'] = dsl.ContainerOp(
name='preprocess',
image='insert your image here',
image='insert image name:tag',
command=['python'],
arguments=[
'/scripts/data.py',
Expand All @@ -48,7 +48,7 @@ def tacosandburritos_train(
# train
operations['training'] = dsl.ContainerOp(
name='training',
image='insert your image here',
image='insert image name:tag',
command=['python'],
arguments=[
'/scripts/train.py',
Expand All @@ -67,7 +67,7 @@ def tacosandburritos_train(
# register model
operations['register'] = dsl.ContainerOp(
name='register',
image='insert your image here',
image='insert image name:tag',
command=['python'],
arguments=[
'/scripts/register.py',
Expand All @@ -84,30 +84,9 @@ def tacosandburritos_train(
)
operations['register'].after(operations['training'])

operations['profile'] = dsl.ContainerOp(
name='profile',
image='insert your image here',
command=['sh'],
arguments=[
'/scripts/profile.sh',
'-n', profile_name,
'-m', model_name,
'-i', '/scripts/inferenceconfig.json',
'-d', '{"image":"https://www.exploreveg.org/files/2015/05/sofritas-burrito.jpeg"}',
'-t', tenant_id,
'-r', resource_group,
'-w', workspace,
'-s', service_principal_id,
'-p', service_principal_password,
'-u', subscription_id,
'-b', persistent_volume_path
]
)
operations['profile'].after(operations['register'])

operations['deploy'] = dsl.ContainerOp(
name='deploy',
image='insert your image here',
image='insert image name:tag',
command=['sh'],
arguments=[
'/scripts/deploy.sh',
Expand All @@ -124,7 +103,7 @@ def tacosandburritos_train(
'-b', persistent_volume_path
]
)
operations['deploy'].after(operations['profile'])
operations['deploy'].after(operations['register'])
for _, op_1 in operations.items():
op_1.container.set_image_pull_policy("Always")
op_1.add_volume(
Expand Down
2 changes: 1 addition & 1 deletion pipelines/azurepipeline/code/preprocess/Dockerfile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM tensorflow/tensorflow:2.0.0a0-gpu-py3
FROM tensorflow/tensorflow:2.0.0a0-py3

# pip install
COPY requirements.txt /scripts/requirements.txt
Expand Down
Loading