-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·85 lines (67 loc) · 2.66 KB
/
main.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
from common import set_seed
from common import get_logger
from common import get_session
from model import Distiller
from model import set_model
import numpy as np
import tensorflow as tf
def main():
set_seed()
get_session('2')
logger = get_logger("MyLogger")
##########################
# Prepare the dataset
##########################
logger.info('##### Build Dataset #####')
batch_size = 64
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype("float32") / 255.0
x_train = np.reshape(x_train, (-1, 28, 28, 1))
x_test = x_test.astype("float32") / 255.0
x_test = np.reshape(x_test, (-1, 28, 28, 1))
##########################
# Build models
##########################
logger.info('##### Build Models #####')
teacher, student, student_scratch = set_model()
##########################
# Train the teacher
##########################
teacher.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
logger.info('##### Train Teacher #####')
teacher.fit(x_train, y_train, epochs=5)
logger.info('##### Evaluate Teacher #####')
teacher.evaluate(x_test, y_test)
##########################
# Distill teacher to student
##########################
distiller = Distiller(teacher=teacher, student=student)
distiller.compile(
optimizer=tf.keras.optimizers.Adam(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
student_loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
distillation_loss=tf.keras.losses.KLDivergence(),
alpha=.1,
temperature=10)
logger.info('##### Distillation #####')
distiller.fit(x_train, y_train, epochs=3)
logger.info('##### Evaluate Distillation #####')
distiller.evaluate(x_test, y_test)
##########################
# Train student from scratch for comparison
##########################
student_scratch.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
logger.info('##### Student Scratch #####')
student_scratch.fit(x_train, y_train, epochs=3)
logger.info('##### Evaluate Student Scratch #####')
student_scratch.evaluate(x_test, y_test)
if __name__ == '__main__':
main()