-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_classification.py
executable file
·166 lines (139 loc) · 5.25 KB
/
img_classification.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
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
import os
import random
from gc import callbacks
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout
import matplotlib.pyplot as plt
parallel = True
def run():
print(f"Using TensorFlow {tf.__version__}")
#acc_str = "accuracy" if tf.__version__[:2] == "2." else "acc"
cwd = os.getcwd()
data_dir = os.path.join(cwd, "goniopin_auto_12012023")
batch_size = 32
img_height = 300
img_width = 160
image_size = (img_height, img_width)
seed = random.randint(11111111,99999999)
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=seed,
image_size=(img_height, img_width),
batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=seed,
image_size=(img_height, img_width),
batch_size=batch_size,
)
# wont work with singularity as it tries to plot image
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(int(labels[i]))
plt.axis("off")
plt.savefig(os.path.join(cwd, "un-augmented.png"))
data_augmentation = Sequential(
[
keras.layers.RandomTranslation(
height_factor=0.1, width_factor=0.2, fill_mode="nearest"
),
keras.layers.RandomContrast(factor=0.2),
keras.layers.RandomBrightness(factor=0.3),
keras.layers.RandomRotation(0.02, fill_mode="nearest"),
]
)
plt.figure(figsize=(20, 20))
for images, _ in train_ds.take(1):
for i in range(25):
augmented_images = data_augmentation(images)
ax = plt.subplot(5, 5, i + 1)
plt.imshow(augmented_images[i].numpy().astype("uint8"))
plt.axis("off")
plt.savefig(os.path.join(cwd, "augmented.png"))
def make_model(input_shape, num_classes):
inputs = keras.Input(shape=input_shape)
x = data_augmentation(inputs)
x = layers.Rescaling(1.0 / 255)(x)
x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(64, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
previous_block_activation = x # Set aside residual
for size in [128, 256, 512, 1024, 2048]:
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.SeparableConv2D(size, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, strides=2, padding="same")(x)
# Project residual
residual = layers.Conv2D(size, 1, strides=2, padding="same")(
previous_block_activation
)
x = layers.add([x, residual]) # Add back residual
previous_block_activation = x # Set aside next residual
x = layers.SeparableConv2D(1024, 3, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.GlobalAveragePooling2D()(x)
if num_classes == 2:
activation = "sigmoid"
units = 1
else:
activation = "softmax"
units = num_classes
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(units, activation=activation)(x)
return keras.Model(inputs, outputs)
model = make_model(input_shape=image_size + (3,), num_classes=2)
epochs = 100
callbacks = [
keras.callbacks.ModelCheckpoint("save_at_{epoch}.h5"),
tf.keras.callbacks.EarlyStopping(
monitor="loss", patience=3, restore_best_weights=True
),
]
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss="binary_crossentropy",
metrics=["accuracy", "mae", "categorical_accuracy"],
)
model.fit(
train_ds,
epochs=epochs,
callbacks=callbacks,
validation_data=val_ds,
)
model.save("final.h5")
strategy = tf.distribute.MirroredStrategy()
if not parallel:
run()
else:
with strategy.scope():
run()