-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (138 loc) · 4.5 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
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
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
print(tf.__version__)
print(np.__version__)
print(pd.__version__)
# The directory paths should match where the images are located
TRAIN_DIR = "train"
TEST_DIR = "test"
NUM_CLASSES = len(os.listdir(TRAIN_DIR))
IMAGE_SIZE = 600
BATCH_SIZE = 32
EPOCHS = 5
IMAGE_TARGET_SIZE = (IMAGE_SIZE, IMAGE_SIZE)
# Build the model
img_augmentation = tf.keras.models.Sequential(
[
tf.keras.layers.experimental.preprocessing.RandomRotation(factor=0.15),
tf.keras.layers.experimental.preprocessing.RandomTranslation(
height_factor=0.1,
width_factor=0.1,
),
tf.keras.layers.experimental.preprocessing.RandomFlip(),
tf.keras.layers.experimental.preprocessing.RandomContrast(factor=0.1),
],
name="img_augmentation",
)
def build_model() -> "tf.keras.Model":
inputs = tf.keras.layers.Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
x = img_augmentation(inputs)
model = tf.keras.applications.EfficientNetB7(
include_top=False,
input_tensor=x,
weights="imagenet",
)
# Freeze the pretrained weights
model.trainable = False
# Rebuild top
x = tf.keras.layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.2, name="top_dropout")(x)
outputs = tf.keras.layers.Dense(
NUM_CLASSES,
activation="softmax",
name="pred",
)(x)
# Compile
model = tf.keras.Model(inputs, outputs, name="EfficientNet")
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-2),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"],
)
return model
def unfreeze_model(model: "tf.keras.Model"):
# unfreeze the top 20 layers while leaving BatchNorm layers frozen
for layer in model.layers[-20:]:
if not isinstance(layer, tf.keras.layers.BatchNormalization):
layer.trainable = True
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"],
)
# Fit the Model
model = build_model()
unfreeze_model(model)
train_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=tf.keras.applications.efficientnet.preprocess_input,
validation_split=0.2,
)
train_batches = train_image_generator.flow_from_directory(
directory=TRAIN_DIR,
target_size=IMAGE_TARGET_SIZE,
batch_size=BATCH_SIZE,
subset="training",
)
valid_batches = train_image_generator.flow_from_directory(
directory=TRAIN_DIR,
target_size=IMAGE_TARGET_SIZE,
batch_size=BATCH_SIZE,
subset="validation",
)
test_batches = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=tf.keras.applications.efficientnet.preprocess_input
).flow_from_directory(
directory=TEST_DIR,
target_size=IMAGE_TARGET_SIZE,
batch_size=BATCH_SIZE,
shuffle=False,
)
history = model.fit(
train_batches,
epochs=EPOCHS,
validation_data=valid_batches,
)
# Visualize the training results
epochs_range = range(EPOCHS)
training_acc = history.history["accuracy"]
validation_acc = history.history["val_accuracy"]
training_loss = history.history["loss"]
validation_loss = history.history["val_loss"]
acc_df = pd.DataFrame(
{"Training Accuracy": training_acc, "Validation Accuracy": validation_acc},
index=epochs_range,
)
loss_df = pd.DataFrame(
{"Training Loss": training_loss, "Validation Loss": validation_loss},
index=epochs_range,
)
# Plot Accuracy
acc_df.plot()
# Plot Loss
loss_df.plot()
# Testing the Model
test_labels = test_batches.classes
print("Test Labels", test_labels)
print(test_batches.class_indices)
predictions = model.predict(test_batches, steps=len(test_batches))
acc = 0
for i in range(len(test_labels)):
actual_class = test_labels[i]
if predictions[i][actual_class] > 0.5:
acc += 1
print("Accuarcy:", (acc / len(test_labels)) * 100, "%")
# Visulize test results
values = [acc, len(test_labels) - acc]
labels = ['Correct', 'Incorrect']
fig, ax = plt.subplots()
ax.pie(values, labels=labels, autopct='%1.1f%%')
ax.set_title('Percentage of correct and incorrect predictions')
plt.show()
# Save the entire model (architecture, weights, and optimizer state)
# model.save("animal_detection_model")
# Save only the weights of the model
model.save_weights("animal_detection_model_weights.h5")