-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcnn.py
executable file
·196 lines (174 loc) · 7.17 KB
/
cnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Convolutional Neural Network (CNN) Pipeline"""
from kubernetes import client as k8s_client
import kfp.dsl as dsl
import kfp.compiler as compiler
from kfp.azure import use_azure_secret
import json
import os
from kubernetes.client.models import V1EnvVar
from utils.kubernetes.secret import use_azstorage_secret # noqa: E501
# Initially derived from https://github.com/kaizentm/kubemlops
TRAIN_START_EVENT = "Training Started"
TRAIN_FINISH_EVENT = "Training Finished"
@dsl.pipeline(
name='Tacos vs. Burritos',
description='Simple TF CNN'
)
def get_callback_payload(event_type):
payload = {}
payload['event_type'] = event_type
payload['sha'] = os.getenv('GITHUB_SHA')
payload['pr_num'] = os.getenv('PR_NUM')
payload['run_id'] = dsl.RUN_ID_PLACEHOLDER
if (event_type == TRAIN_FINISH_EVENT):
payload['status'] = '{{workflow.status}}'
return json.dumps(payload)
def cnn_train(
resource_group,
workspace,
dataset,
token
):
"""Pipeline steps"""
persistent_volume_path = '/mnt/azure'
data_download = dataset # noqa: E501
batch = 32
model_name = 'cnnmodel'
operations = {}
image_size = 160
training_folder = 'train'
training_dataset = 'train.txt'
model_folder = 'model'
image_repo_name = "k8scc01covidmlopsacr.azurecr.io/mlops"
callback_url = 'kubemlopsbot-svc.kubeflow.svc.cluster.local:8080'
mlflow_url = 'http://mlflow.mlflow:5000'
exit_op = dsl.ContainerOp(
name='Exit Handler',
image="curlimages/curl",
command=['curl'],
arguments=[
'-d', get_callback_payload(TRAIN_FINISH_EVENT),
callback_url
]
)
with dsl.ExitHandler(exit_op):
start_callback = \
dsl.UserContainer('callback',
'curlimages/curl',
command=['curl'],
args=['-d',
get_callback_payload(TRAIN_START_EVENT), callback_url]) # noqa: E501
operations['tensorflow preprocess'] = dsl.ContainerOp(
name='tensorflow preprocess',
init_containers=[start_callback],
image=image_repo_name + '/tensorflow-preprocess:latest',
command=['python'],
arguments=[
'/scripts/data.py',
'--base_path', persistent_volume_path,
'--data', training_folder,
'--target', training_dataset,
'--img_size', image_size,
'--zipfile', data_download
]
)
operations['tensorflow training'] = dsl.ContainerOp(
name="tensorflow training",
image=image_repo_name + '/tensorflow-training:latest',
command=['python'],
arguments=[
'/scripts/train.py',
'--base_path', persistent_volume_path,
'--data', training_folder,
'--epochs', 2,
'--batch', batch,
'--image_size', image_size,
'--lr', 0.0001,
'--outputs', model_folder,
'--dataset', training_dataset
],
output_artifact_paths={
'mlpipeline-metrics': '/mlpipeline-metrics.json',
'mlpipeline-ui-metadata': '/mlpipeline-ui-metadata.json'
}
).apply(use_azstorage_secret()).add_env_variable(V1EnvVar(name="RUN_ID", value=dsl.RUN_ID_PLACEHOLDER)).add_env_variable(V1EnvVar(name="MLFLOW_TRACKING_TOKEN", value=token)).add_env_variable(V1EnvVar(name="MLFLOW_TRACKING_URI", value=mlflow_url)).add_env_variable(V1EnvVar(name="GIT_PYTHON_REFRESH", value='quiet')) # noqa: E501
operations['tensorflow training'].after(operations['tensorflow preprocess']) # noqa: E501
operations['evaluate'] = dsl.ContainerOp(
name='evaluate',
image="busybox",
command=['sh', '-c'],
arguments=[
'echo',
'Life is Good!'
]
)
operations['evaluate'].after(operations['tensorflow training'])
operations['register kubeflow'] = dsl.ContainerOp(
name='register kubeflow',
image=image_repo_name + '/register-kubeflow-artifacts:latest',
command=['python'],
arguments=[
'/scripts/register.py',
'--base_path', persistent_volume_path,
'--model', 'latest.h5',
'--model_name', model_name,
'--data', training_folder,
'--dataset', training_dataset,
'--run_id', dsl.RUN_ID_PLACEHOLDER
]
).apply(use_azure_secret())
operations['register kubeflow'].after(operations['evaluate'])
operations['register AML'] = dsl.ContainerOp(
name='register AML',
image=image_repo_name + '/register-aml:latest',
command=['python'],
arguments=[
'/scripts/register.py',
'--base_path', persistent_volume_path,
'--model', 'latest.h5',
'--model_name', model_name,
'--tenant_id', "$(AZ_TENANT_ID)",
'--service_principal_id', "$(AZ_CLIENT_ID)",
'--service_principal_password', "$(AZ_CLIENT_SECRET)",
'--subscription_id', "$(AZ_SUBSCRIPTION_ID)",
'--resource_group', resource_group,
'--workspace', workspace,
'--run_id', dsl.RUN_ID_PLACEHOLDER
]
).apply(use_azure_secret())
operations['register AML'].after(operations['register kubeflow'])
operations['register mlflow'] = dsl.ContainerOp(
name='register mlflow',
image=image_repo_name + '/register-mlflow:latest',
command=['python'],
arguments=[
'/scripts/register.py',
'--model', 'model',
'--model_name', model_name,
'--experiment_name', 'kubeflow-mlops',
'--run_id', dsl.RUN_ID_PLACEHOLDER
]
).apply(use_azure_secret()).add_env_variable(V1EnvVar(name="MLFLOW_TRACKING_URI", value=mlflow_url)).add_env_variable(V1EnvVar(name="MLFLOW_TRACKING_TOKEN", value=token)) # noqa: E501
operations['register mlflow'].after(operations['register AML'])
operations['finalize'] = dsl.ContainerOp(
name='Finalize',
image="curlimages/curl",
command=['curl'],
arguments=[
'-d', get_callback_payload("Model is registered"),
callback_url
]
)
operations['finalize'].after(operations['register mlflow'])
for _, op_1 in operations.items():
op_1.container.set_image_pull_policy("Always")
op_1.add_volume(
k8s_client.V1Volume(
name='azure',
persistent_volume_claim=k8s_client.V1PersistentVolumeClaimVolumeSource( # noqa: E501
claim_name='azure-managed-file')
)
).add_volume_mount(k8s_client.V1VolumeMount(
mount_path='/mnt/azure', name='azure'))
if __name__ == '__main__':
compiler.Compiler().compile(cnn_train, __file__ + '.tar.gz')