-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmnist.py
More file actions
274 lines (228 loc) · 7.77 KB
/
mnist.py
File metadata and controls
274 lines (228 loc) · 7.77 KB
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
from learning_orchestra_client.dataset.generic import DatasetGeneric
from learning_orchestra_client.function.python import FunctionPython
from learning_orchestra_client.model.tensorflow import ModelTensorflow
from learning_orchestra_client.train.tensorflow import TrainTensorflow
from learning_orchestra_client.predict.tensorflow import PredictTensorflow
from learning_orchestra_client.evaluate.tensorflow import EvaluateTensorflow
CLUSTER_IP = "http://35.247.197.191"
dataset_generic = DatasetGeneric(CLUSTER_IP)
dataset_generic.insert_dataset_async(
dataset_name="mnist_train_images",
url="https://drive.google.com/u/0/uc?"
"id=1ec6hVwvq4UPyQ7DmJVxzofE2TcKUTHNY&export=download",
)
dataset_generic.insert_dataset_async(
dataset_name="mnist_train_labels",
url="https://drive.google.com/u/0/uc?"
"id=187ID_LfQCTJOYieC-yo94jxnWiFJZ7uX&export=download",
)
dataset_generic.insert_dataset_async(
dataset_name="mnist_test_images",
url="https://drive.google.com/u/0/uc?"
"id=1ZNuiRJLKSFzmegRgIHXUl4t-UdjEPYVf&export=download",
)
dataset_generic.insert_dataset_async(
dataset_name="mnist_test_labels",
url="https://drive.google.com/u/0/uc?"
"id=1v0PRmUL8nOg3mHakjfTxOjNA9bi6XkkA&export=download",
)
dataset_generic.wait("mnist_train_images")
dataset_generic.wait("mnist_train_labels")
dataset_generic.wait("mnist_test_images")
dataset_generic.wait("mnist_test_labels")
function_python = FunctionPython(CLUSTER_IP)
mnist_datasets_treatment = '''
import numpy as np
import struct as st
import math
training_filenames = {'images': mnist_train_images,
'labels': mnist_train_labels}
test_filenames = {'images': mnist_test_images,
'labels': mnist_test_labels}
data_types = {
0x08: ('ubyte', 'B', 1),
0x09: ('byte', 'b', 1),
0x0B: ('>i2', 'h', 2),
0x0C: ('>i4', 'i', 4),
0x0D: ('>f4', 'f', 4),
0x0E: ('>f8', 'd', 8)}
def treat_dataset(dataset: dict) -> tuple:
global np, st, math, data_types
for name in dataset.keys():
if name == 'images':
images_file = dataset[name]
if name == 'labels':
labels_file = dataset[name]
images_file.seek(0)
magic = st.unpack('>4B', images_file.read(4))
data_format = data_types[magic[2]][1]
data_size = data_types[magic[2]][2]
images_file.seek(4)
content_amount = st.unpack('>I', images_file.read(4))[0]
rows_amount = st.unpack('>I', images_file.read(4))[0]
columns_amount = st.unpack('>I', images_file.read(4))[0]
labels_file.seek(8)
labels_array = np.asarray(
st.unpack(
'>' + data_format * content_amount,
labels_file.read(content_amount * data_size))).reshape(
(content_amount, 1))
n_batch = 10000
n_iter = int(math.ceil(content_amount / n_batch))
n_bytes = n_batch * rows_amount * columns_amount * data_size
images_array = np.array([])
for i in range(0, n_iter):
temp_images_array = np.asarray(
st.unpack('>' + data_format * n_bytes,
images_file.read(n_bytes))).reshape(
(n_batch, rows_amount, columns_amount))
if images_array.size == 0:
images_array = temp_images_array
else:
images_array = np.vstack((images_array, temp_images_array))
temp_images_array = np.array([])
return images_array, labels_array
train_images, train_labels = treat_dataset(training_filenames)
test_images, test_labels = treat_dataset(test_filenames)
response = {
"train_images": train_images,
"train_labels": train_labels,
"test_images": test_images,
"test_labels": test_labels,
}
'''
function_python.run_function_async(
name="mnist_datasets_treated",
parameters={
"mnist_train_images": "$mnist_train_images",
"mnist_train_labels": "$mnist_train_labels",
"mnist_test_images": "$mnist_test_images",
"mnist_test_labels": "$mnist_test_labels"
},
code=mnist_datasets_treatment)
function_python.wait("mnist_datasets_treated")
mnist_datasets_normalization = '''
test_images = test_images / 255
train_images = train_images / 255
print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
response = {
"test_images": test_images,
"test_labels": test_labels,
"train_images": train_images,
"train_labels": train_labels
}
'''
function_python.run_function_async(
name="mnist_datasets_normalized",
parameters={
"train_images": "$mnist_datasets_treated.train_images",
"train_labels": "$mnist_datasets_treated.train_labels",
"test_images": "$mnist_datasets_treated.test_images",
"test_labels": "$mnist_datasets_treated.test_labels"
},
code=mnist_datasets_normalization)
function_python.wait("mnist_datasets_normalized")
model_tensorflow = ModelTensorflow(CLUSTER_IP)
model_tensorflow.create_model_async(
name="mnist_model",
module_path="tensorflow.keras.models",
class_name="Sequential",
class_parameters={
"layers":
[
"#tensorflow.keras.layers.Flatten(input_shape=(28, 28))",
"#tensorflow.keras.layers.Dense(128, activation='relu')",
"#tensorflow.keras.layers.Dense(10, activation='softmax')",
]}
)
model_tensorflow.wait("mnist_model")
model_compilation = '''
import tensorflow as tf
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
response = model
'''
function_python.run_function_async(
name="mnist_model_compiled",
parameters={
"model": "$mnist_model"
},
code=model_compilation)
function_python.wait("mnist_model_compiled")
train_tensorflow = TrainTensorflow(CLUSTER_IP)
train_tensorflow.create_training_async(
name="mnist_model_trained",
model_name="mnist_model",
parent_name="mnist_model_compiled",
method_name="fit",
parameters={
"x": "$mnist_datasets_normalized.train_images",
"y": "$mnist_datasets_normalized.train_labels",
"validation_split": 0.1,
"epochs": 6,
}
)
train_tensorflow.wait("mnist_model_trained")
predict_tensorflow = PredictTensorflow(CLUSTER_IP)
predict_tensorflow.create_prediction_async(
name="mnist_model_predicted",
model_name="mnist_model",
parent_name="mnist_model_trained",
method_name="predict",
parameters={
"x": "$mnist_datasets_normalized.test_images"
}
)
evaluate_tensorflow = EvaluateTensorflow(CLUSTER_IP)
evaluate_tensorflow.create_evaluate_async(
name="mnist_model_evaluated",
model_name="mnist_model",
parent_name="mnist_model_trained",
method_name="evaluate",
parameters={
"x": "$mnist_datasets_normalized.test_images",
"y": "$mnist_datasets_normalized.test_labels"
}
)
predict_tensorflow.wait("mnist_model_predicted")
evaluate_tensorflow.wait("mnist_model_evaluated")
show_mnist_predict = '''
print(mnist_predicted)
response = None
'''
function_python.run_function_async(
name="mnist_model_predicted_print",
parameters={
"mnist_predicted": "$mnist_model_predicted"
},
code=show_mnist_predict
)
show_mnist_evaluate = '''
print(mnist_evaluated)
response = None
'''
function_python.run_function_async(
name="mnist_model_evaluated_print",
parameters={
"mnist_evaluated": "$mnist_model_evaluated"
},
code=show_mnist_evaluate
)
function_python.wait("mnist_model_evaluated_print")
function_python.wait("mnist_model_predicted_print")
print(function_python.search_execution_content(
name="mnist_model_predicted_print",
limit=1,
skip=1,
pretty_response=True))
print(function_python.search_execution_content(
name="mnist_model_evaluated_print",
limit=1,
skip=1,
pretty_response=True))