-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy path2_tb_scalars.py
More file actions
144 lines (112 loc) · 4.08 KB
/
2_tb_scalars.py
File metadata and controls
144 lines (112 loc) · 4.08 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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import io
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import tensorflow_datasets as tfds
from tensorflow import keras
from tensorflow.keras import layers
# Make sure we don't get any GPU errors
physical_devices = tf.config.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_devices[0], True)
(ds_train, ds_test), ds_info = tfds.load(
"cifar10",
split=["train", "test"],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
def normalize_img(image, label):
"""Normalizes images"""
return tf.cast(image, tf.float32) / 255.0, label
AUTOTUNE = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
def augment(image, label):
if tf.random.uniform((), minval=0, maxval=1) < 0.1:
image = tf.tile(tf.image.rgb_to_grayscale(image), [1, 1, 3])
image = tf.image.random_brightness(image, max_delta=0.1)
image = tf.image.random_flip_left_right(image)
return image, label
# Setup for train dataset
ds_train = ds_train.map(normalize_img, num_parallel_calls=AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits["train"].num_examples)
ds_train = ds_train.map(augment)
ds_train = ds_train.batch(BATCH_SIZE)
ds_train = ds_train.prefetch(AUTOTUNE)
# Setup for test Dataset
ds_test = ds_test.map(normalize_img, num_parallel_calls=AUTOTUNE)
ds_test = ds_test.batch(BATCH_SIZE)
ds_test = ds_test.prefetch(AUTOTUNE)
class_names = [
"Airplane",
"Autmobile",
"Bird",
"Cat",
"Deer",
"Dog",
"Frog",
"Horse",
"Ship",
"Truck",
]
def get_model():
model = keras.Sequential(
[
layers.Input((32, 32, 3)),
layers.Conv2D(8, 3, padding="same", activation="relu"),
layers.Conv2D(16, 3, padding="same", activation="relu"),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation="relu"),
layers.Dropout(0.1),
layers.Dense(10),
]
)
return model
model = get_model()
num_epochs = 1
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optimizer = keras.optimizers.Adam(lr=0.001)
acc_metric = keras.metrics.SparseCategoricalAccuracy()
train_writer = tf.summary.create_file_writer("logs/train/")
test_writer = tf.summary.create_file_writer("logs/test/")
train_step = test_step = 0
for lr in [1e-1, 1e-2, 1e-3, 1e-4, 1e-5]:
train_step = test_step = 0
train_writer = tf.summary.create_file_writer("logs/train/" + str(lr))
test_writer = tf.summary.create_file_writer("logs/test/" + str(lr))
model = get_model()
optimizer = keras.optimizers.Adam(lr=lr)
for epoch in range(num_epochs):
# Iterate through training set
for batch_idx, (x, y) in enumerate(ds_train):
with tf.GradientTape() as tape:
y_pred = model(x, training=True)
loss = loss_fn(y, y_pred)
gradients = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(gradients, model.trainable_weights))
acc_metric.update_state(y, y_pred)
with train_writer.as_default():
tf.summary.scalar("Loss", loss, step=train_step)
tf.summary.scalar(
"Accuracy", acc_metric.result(), step=train_step,
)
train_step += 1
# Reset accuracy in between epochs (and for testing and test)
acc_metric.reset_states()
# Iterate through test set
for batch_idx, (x, y) in enumerate(ds_test):
y_pred = model(x, training=False)
loss = loss_fn(y, y_pred)
acc_metric.update_state(y, y_pred)
with test_writer.as_default():
tf.summary.scalar("Loss", loss, step=test_step)
tf.summary.scalar(
"Accuracy", acc_metric.result(), step=test_step,
)
test_step += 1
acc_metric.reset_states()
# Reset accuracy in between epochs (and for testing and test)
acc_metric.reset_states()