-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathglassware_quality_control.py
296 lines (182 loc) · 8.1 KB
/
glassware_quality_control.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Databricks notebook source
# MAGIC %md
# MAGIC # Glassware Quality Control
# COMMAND ----------
# MAGIC %md
# MAGIC ### Objective:
# MAGIC - To predict and grade the quality of the product as they are made, prior to actual inspection process which are a day or two later.
# MAGIC - This could act as an early indicator of quality for process optimization, and to send lower quality products for additional inspection and categorization.
# MAGIC
# MAGIC ### About Data:
# MAGIC - sensor_reading - Sensor data from manufacturing equipment is streamed through IoT devices. Final aggregated data includes Temperature, and Pressure, along with process duration.
# MAGIC - product_quality - For each product id, actual quality categorization is available
# COMMAND ----------
# MAGIC %md
# MAGIC ### Architecture Reference
# MAGIC
# MAGIC <!-- <img src="https://publicimg.blob.core.windows.net/images/model_drift_architecture.png" width="1300"> -->
# MAGIC <img src="https://joelcthomas.github.io/modeldrift/img/model_drift_architecture.png" width="1300">
# COMMAND ----------
# MAGIC %md
# MAGIC ### End to End Pipeline
# MAGIC ### Data Access -> Data Prep -> Model Training -> Deployment -> Monitoring -> Action & Feedback Loop
# COMMAND ----------
# MAGIC %run ./config
# COMMAND ----------
# MAGIC %md ## <a>Data Access & Prep</a>
# COMMAND ----------
# MAGIC %md
# MAGIC __Setup access to blob storage, where data is streamed from and to, in delta lake__
# COMMAND ----------
# MAGIC %run ./data/data_access
# COMMAND ----------
# MAGIC %md
# MAGIC __Read sensor_reading & product_quality, join and prepare the data to be fed for model training__
# COMMAND ----------
# MAGIC %run ./data/data_prep
# COMMAND ----------
# MAGIC %md
# MAGIC ### Assume today is 2019-07-11
# MAGIC __Using data from (2019-07-01 - 2019-07-10) to generate models, for which sensor_reading and product_quality data are available__
# MAGIC
# MAGIC __To reproduce this demo, generate and push initial set of data using ./demo_utils/demo_data_init __
# COMMAND ----------
# MAGIC %md
# MAGIC __Prior to production workflow, typically one could setup a notebook for EDA, get familiar with dataset, and explore modeling methods__
# MAGIC __Check EDA notebook example here, for this project__
# COMMAND ----------
today_date = '2019-07-11 18:00'
# COMMAND ----------
model_data_date = {'start_date':'2019-07-01 00:00:00', 'end_date':'2019-07-10 23:59:00'}
model_df = model_data(model_data_date)
display(model_df)
# COMMAND ----------
# MAGIC %md ## <a>Model Training & Tuning</a>
# COMMAND ----------
# MAGIC %md
# MAGIC __Run various models (Random Forest, Decision Tree, XGBoost), each with its own set of hyperparameters, and log all the information to MLflow__
# COMMAND ----------
# MAGIC %run ./glassware_quality_modeler/generate_models
# COMMAND ----------
# MAGIC %md ## <a>Model Selection & Deployment</a>
# COMMAND ----------
# MAGIC %md
# MAGIC __Search MLflow to find the best model from above experiment runs across all model types__
# COMMAND ----------
mlflow_search_query = "params.model_data_date = '"+ model_data_date['start_date']+ ' - ' + model_data_date['end_date']+"'"
best_run_details = best_run(mlflow_exp_id, mlflow_search_query)
print("Best run from all trials:" + best_run_details['runid'])
print("Params:")
print(best_run_details["params"])
print("Metrics:")
print(best_run_details["metrics"])
# COMMAND ----------
# MAGIC %md
# MAGIC __Mark the best run as production in MLflow, to be used during scoring__
# COMMAND ----------
push_model_production(mlflow_exp_id, best_run_details['runid'], userid, today_date)
# COMMAND ----------
# MAGIC %md ## <a>Model Scoring</a>
# COMMAND ----------
get_model_production(mlflow_exp_id)
# COMMAND ----------
# MAGIC %run ./glassware_quality_scorer/score_quality
# COMMAND ----------
# MAGIC %md
# MAGIC __Read the sensor_reading stream, apply model scoring, and write the output stream as 'predicted_quality' delta table__
# COMMAND ----------
sensor_reading_stream = stream_sensor_reading()
predict_stream = stream_score_quality(sensor_reading_stream)
# COMMAND ----------
# MAGIC %md ## <a>Model Monitoring & Feedback</a>
# COMMAND ----------
# MAGIC %run ./model_quality/model_quality_monitor
# COMMAND ----------
predicted_quality = get_predicted_quality()
product_quality = get_product_quality()
model_quality_summary = track_model_quality(product_quality, predicted_quality)
# COMMAND ----------
display(model_quality_summary)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Assume today is 2019-07-16
# COMMAND ----------
plot_model_quality(model_quality_summary)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Assume today is 2019-07-21
# MAGIC __To reproduce this demo, push data for range 2019-07-16 - 2019-07-21 using ./demo_utils/demo_data_init __
# COMMAND ----------
today_date = '2019-07-21 01:00'
# COMMAND ----------
model_quality_summary = track_model_quality(get_product_quality(), get_predicted_quality())
plot_model_quality(model_quality_summary)
# COMMAND ----------
# MAGIC %md
# MAGIC We see drift occurs on 07/20 (based on process date)
# COMMAND ----------
# MAGIC %md ## <a>Retrain After Drift</a>
# COMMAND ----------
# MAGIC %md
# MAGIC __Read sensor_reading & product_quality, join and prepare the data to be fed for model training__
# COMMAND ----------
model_data_date = {'start_date':'2019-07-19 00:00:00', 'end_date':'2019-07-21 00:00:00'}
model_df = model_data(model_data_date)
display(model_df)
# COMMAND ----------
# MAGIC %md
# MAGIC __Run various models (Random Forest, Decision Tree, XGBoost), each with its own set of hyperparameters, and log all the information to MLflow__
# COMMAND ----------
# MAGIC %run ./glassware_quality_modeler/generate_models
# COMMAND ----------
# MAGIC %md
# MAGIC __Search MLflow to find the best model from above experiment runs across all model types__
# COMMAND ----------
mlflow_search_query = "params.model_data_date = '"+ model_data_date['start_date']+ ' - ' + model_data_date['end_date']+"'"
best_run_details = best_run(mlflow_exp_id, mlflow_search_query)
print("Best run from all trials:" + best_run_details['runid'])
print("Params:")
print(best_run_details["params"])
print("Metrics:")
print(best_run_details["metrics"])
# COMMAND ----------
# MAGIC %md
# MAGIC __Mark the best run as production in MLflow, to be used during scoring__
# COMMAND ----------
push_model_production(mlflow_exp_id, best_run_details['runid'], userid, today_date)
# COMMAND ----------
predict_stream.stop()
predict_stream = stream_score_quality(stream_sensor_reading())
# COMMAND ----------
# MAGIC %md
# MAGIC __Summary after retrain__
# COMMAND ----------
# MAGIC %md
# MAGIC ### Assume today is 2019-08-01
# MAGIC __To reproduce this demo, push data for range 2019-07-21 - 2019-08-01 using ./demo_utils/demo_data_init __
# COMMAND ----------
model_quality_summary = track_model_quality(get_product_quality(), get_predicted_quality())
plot_model_quality(model_quality_summary)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Assume today is 2019-08-12
# MAGIC __To reproduce this demo, push data for range > 2019-08-01 using ./demo_utils/demo_data_init __
# COMMAND ----------
model_quality_summary = track_model_quality(get_product_quality(), get_predicted_quality())
plot_model_quality(model_quality_summary)
# COMMAND ----------
# MAGIC %md
# MAGIC __ Let's see what would have happened if model was not updated?__
# COMMAND ----------
predicted_quality = score_quality(get_sensor_reading(), 'd45cd111bc2d49409bb9ccd5df94507d')
model_quality_summary = track_model_quality(get_product_quality(), predicted_quality)
plot_model_quality(model_quality_summary)
# COMMAND ----------
# MAGIC %md ## <a>Summary</a>
# COMMAND ----------
predicted_quality = score_quality(get_sensor_reading(), 'd45cd111bc2d49409bb9ccd5df94507d')
model_quality_summary_1 = track_model_quality(get_product_quality(), predicted_quality)
predicted_quality = score_quality(get_sensor_reading(), '34c33e631fd441a9be329a600550f4c1')
model_quality_summary_2 = track_model_quality(get_product_quality(), predicted_quality)
# COMMAND ----------
plot_summary(model_quality_summary_1, model_quality_summary_2)