-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorial13_DataAugmentation.py
90 lines (70 loc) · 2.79 KB
/
Tutorial13_DataAugmentation.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"
import tensorflow as tf
import keras
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_datasets as tfds
(ds_train, ds_test) , ds_info = tfds.load(
'cifar10',
split=['train','test'],
shuffle_files=True,
as_supervised=True, # will return tuple (img, label) otherwise dict
with_info=True, # able to get info about dataset
)
def normalize_img(image, label):
'''normalize images'''
return tf.cast(image, tf.float32) / 255.0, label
AUTOTUNE = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
def augment(image, label):
new_height = new_width = 32
image = tf.image.resize(image, (new_height, new_width))
if tf.random.uniform((), minval=0, maxval=1) < 0.1:
### copy 3 times channels must checkout the 'tf.tile' function
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_contrast(image, lower=0.1, upper=0.2)
image = tf.image.random_flip_left_right(image) # 50%
# image = tf.image.random_flip_up_down(image) # 50%
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, num_parallel_calls=AUTOTUNE) ## 1 way to augmentation -> adaption technique , num_parallel_calls -> non inherently for each image
ds_train = ds_train.batch(BATCH_SIZE)
ds_train = ds_train.prefetch(AUTOTUNE)
# Setup for test Dataset
ds_test = ds_train.map(normalize_img, num_parallel_calls=AUTOTUNE)
ds_test = ds_train.batch(BATCH_SIZE)
ds_test = ds_train.prefetch(AUTOTUNE)
## 2 way to augmentation
## TF >= 2.3.0
data_augmentation = keras.Sequential(
[
layers.experimental.preprocessing.Resizing(height=32, width=32),
layers.experimental.preprocessing.RandomFlip(mode='horizontal'),
layers.experimental.preprocessing.RandomContrast(factor=0.1),
]
)
model = keras.Sequential(
[
keras.Input((32, 32, 3)),
data_augmentation, ## 2 way to data augumentation
layers.Conv2D(4, 3, padding='same', activation='relu'),
layers.Conv2D(8, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10),
]
)
model.compile(
optimizer=keras.optimizers.Adam(3e-4),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'],
)
model.fit(ds_train, epochs=5, verbose=2)
model.evaluate(ds_test)