-
Notifications
You must be signed in to change notification settings - Fork 18
/
mnist.py
57 lines (40 loc) · 1.5 KB
/
mnist.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
from tensorflow import keras
import os
import tensorflow as tf
import pickle
# Download
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# Preprocessing
train_images = train_images / 255.0
test_images = test_images / 255.0
with open('/tmp/train_images.pickle', 'wb') as f:
pickle.dump(train_images, f)
with open('/tmp/train_labels.pickle', 'wb') as f:
pickle.dump(train_labels, f)
with open('/tmp/test_images.pickle', 'wb') as f:
pickle.dump(test_images, f)
with open('/tmp/test_labels.pickle', 'wb') as f:
pickle.dump(test_labels, f)
# Training
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
with open('/tmp/train_images.pickle', 'rb') as f:
train_images = pickle.load(f)
with open('/tmp/train_labels.pickle', 'rb') as f:
train_labels = pickle.load(f)
model.fit(train_images, train_labels, epochs=10)
with open('/tmp/test_images.pickle', 'rb') as f:
test_images = pickle.load(f)
with open('/tmp/test_labels.pickle', 'rb') as f:
test_labels = pickle.load(f)
# Evaluation
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
model_dir = '/home/benjamintan/dev/kubeflow-mnist/'
# Save model
tf.saved_model.save(model, model_dir)