-
Notifications
You must be signed in to change notification settings - Fork 0
/
test7.py
228 lines (204 loc) · 8.45 KB
/
test7.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
from prefect import flow
import logging
import os
import shutil
import time
import numpy as np
#from kafka import KafkaConsumer
import json
from typing import List
from joblib import dump, load
from sklearn.utils import check_random_state
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from kafka import KafkaConsumer
from kafka import KafkaProducer
import queue
import time
from datetime import datetime
from prefect import flow, task
from prefect_ray import RayTaskRunner
from prefect_shell import ShellOperation
from prefect_ray.context import remote_options
import ray
current_dir = '/root/prefect/Rayflow'#os.path.dirname(__file__)
dataset_path = os.path.join(current_dir, 'dataset', 'mnist_{}.npz')
working_dir = os.path.join(current_dir, 'tmp')
trained_model_dir = os.path.join(working_dir, 'trained_models')
validated_model_dir = os.path.join(working_dir, 'validated_models')
deployed_model_dir = os.path.join(working_dir, 'deployed_models')
#model_deployed_event = queue.Queue()
producer = KafkaProducer(bootstrap_servers='localhost:9092')
consumer = KafkaConsumer('data_prepared')
def _prepare_working_dir():
for path in [trained_model_dir, validated_model_dir, deployed_model_dir]:
if not os.path.isdir(path):
os.makedirs(path)
def _get_latest_model(model_dir) -> str:
file_list = os.listdir(model_dir)
if file_list is None or len(file_list) == 0:
return None
else:
file_list.sort(reverse=True)
return os.path.join(model_dir, file_list[0])
def _preprocess_data_p(x_data, y_data):
random_state = check_random_state(0)
permutation = random_state.permutation(x_data.shape[0])
x_train = x_data[permutation]
y_train = y_data[permutation]
reshaped_x_train = x_train.reshape((x_train.shape[0], -1))
scaler_x_train = StandardScaler().fit_transform(reshaped_x_train)
return scaler_x_train, y_train
def _preprocess_data(dataset_uri):
with np.load(dataset_uri) as f:
x_data, y_data = f['x_train'], f['y_train']
return _preprocess_data_p(x_data, y_data)
def preprocess1():
_prepare_working_dir()
train_dataset = dataset_path.format('train')
try:
event_sender = AIFlowNotificationClient(NOTIFICATION_SERVER_URI)
my_topic = "test2"
consumer = KafkaConsumer(my_topic, bootstrap_servers='10.105.48.156:9092', auto_offset_reset='smallest', group_id='test')
count = 0
x_train, y_train = [], []
for msg in consumer: # 这里会监听,无限循环
count += 1
jsont = json.loads(str(msg.value,'utf-8'))
x_train.append(jsont["xdata"] )
y_train.append(jsont["ydata"] )
if count % 1000 == 0:
x_train1, y_train1 = _preprocess_data_p(np.array(x_train),np.array(y_train))
np.save(os.path.join(working_dir, f'x_train'), x_train1) # 覆盖
np.save(os.path.join(working_dir, f'y_train'), y_train1)
event_sender.send_event(key="data_prepared", value=None)
x_train, y_train = [], []
#time.sleep(30)
finally:
event_sender.close()
@task(retries=3)
def preprocess():
_prepare_working_dir()
train_dataset = dataset_path.format('train')
# try:
# event_sender = AIFlowNotificationClient(NOTIFICATION_SERVER_URI)
while True:
x_train, y_train = _preprocess_data(train_dataset)
np.save(os.path.join(working_dir, f'x_train'), x_train)
np.save(os.path.join(working_dir, f'y_train'), y_train)
# #event_sender.send_event(key="data_prepared", value=None)
# other_event.put("data_prepared")
producer.send('data_prepared', b'ok')
print("data prepared!!")
time.sleep(30)
#finally:
#event_sender.close()
@task(retries=3)
def train():
"""
See also:
https://scikit-learn.org/stable/auto_examples/linear_model/plot_sparse_logistic_regression_mnist.html
"""
_prepare_working_dir()
clf = LogisticRegression(C=50. / 5000, penalty='l1', solver='saga', tol=0.1)
x_train = np.load(os.path.join(working_dir, f'x_train.npy'))
y_train = np.load(os.path.join(working_dir, f'y_train.npy'))
clf.fit(x_train, y_train)
model_path = os.path.join(trained_model_dir, time.strftime("%Y%m%d%H%M%S", time.localtime()))
dump(clf, model_path)
@task(retries=3)
def validate():
_prepare_working_dir()
validate_dataset = dataset_path.format('evaluate')
x_validate, y_validate = _preprocess_data(validate_dataset)
to_be_validated = _get_latest_model(trained_model_dir)
clf = load(to_be_validated)
scores = cross_val_score(clf, x_validate, y_validate, scoring='precision_macro')
try:
#event_sender = AIFlowNotificationClient(NOTIFICATION_SERVER_URI)
deployed_model = _get_latest_model(deployed_model_dir)
if deployed_model is None:
logging.info(f"Generate the 1st model with score: {scores}")
shutil.copy(to_be_validated, validated_model_dir)
#event_sender.send_event(key="model_validated", value=None)
#other_event.put("model_validated")
else:
deployed_clf = load(deployed_model)
old_scores = cross_val_score(deployed_clf, x_validate, y_validate, scoring='precision_macro')
if np.mean(scores) > np.mean(old_scores):
logging.info(f"A new model with score: {scores} passes validation")
shutil.copy(to_be_validated, validated_model_dir)
#event_sender.send_event(key="model_validated", value=None)
#other_event.put("model_validated")
else:
logging.info(f"New generated model with score: {scores} is worse "
f"than the previous: {old_scores}, ignored.")
finally:
pass
#event_sender.close()
@task(retries=3)
def deploy():
_prepare_working_dir()
to_be_deployed = _get_latest_model(validated_model_dir)
return to_be_deployed
#deploy_model_path = shutil.copy(to_be_deployed, deployed_model_dir)
#model_deployed_event.put(to_be_deployed)
# try:
# event_sender = AIFlowNotificationClient(NOTIFICATION_SERVER_URI)
# event_sender.send_event(key="model_deployed", value=deploy_model_path)
# finally:
# event_sender.close()
# class ModelLoader(ListenerProcessor):
# def __init__(self):
# self.current_model = None
# logging.info("Waiting for the first model deployed...")
# def process(self, events: List[Event]):
# for e in events:
# self.current_model = e.value
@task(retries=3)
def predict(model):
_prepare_working_dir()
predict_dataset = dataset_path.format('predict')
result_path = os.path.join(working_dir, 'predict_result')
x_predict, _ = _preprocess_data(predict_dataset)
#model_loader = ModelLoader()
#while True:
#model = model_deployed_event.get()
current_model = model#model_loader.current_model
clf = load(current_model)
result = clf.predict(x_predict)
with open(result_path, 'a') as f:
f.write(f'model [{current_model}] predict result: {result}\n')
#model_deployed_event.task_done()
# try:
# event_listener = AIFlowNotificationClient(NOTIFICATION_SERVER_URI)
# event_listener.register_listener(listener_processor=model_loader,
# event_keys=["model_deployed", ])
# while True:
# if current_model != model_loader.current_model:
# current_model = model_loader.current_model
# logging.info(f"Predicting with new model: {current_model}")
# clf = load(current_model)
# result = clf.predict(x_predict)
# with open(result_path, 'a') as f:
# f.write(f'model [{current_model}] predict result: {result}\n')
# time.sleep(5)
# finally:
# event_listener.close()
@flow(name="online_machine_learning" )
def online_machine_learning():
# with remote_options(
# scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy(
# node_id = '693c57a1facb7d49bc1851c1fda9095f2408539f1f7945c8ea864585',
# soft=False)
# ):
yid = preprocess.submit()
while True:
for msg in consumer:
print(msg)
train()
validate()
d = deploy()
predict(d)
online_machine_learning()