-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.py
More file actions
230 lines (191 loc) · 6.26 KB
/
autoencoder.py
File metadata and controls
230 lines (191 loc) · 6.26 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
from pathlib import Path
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import (
Input,
Conv2D,
Conv2DTranspose,
Flatten,
Dense,
Reshape,
BatchNormalization,
Activation
)
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
import cv2
import matplotlib.pyplot as plt
class Autoencoder(Model):
def __init__(self, latent_dim, input_shape):
super().__init__()
self.latent_dim = latent_dim
self.encoder = tf.keras.Sequential(
[
Input(shape=input_shape),
Conv2D(32, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Conv2D(64, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Conv2D(128, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Flatten(),
Dense(latent_dim),
BatchNormalization(),
Activation("sigmoid"),
]
)
self.decoder = tf.keras.Sequential(
[
Dense(8 * 8 * 128),
BatchNormalization(),
Activation('relu'),
Reshape((8, 8, 128)),
Conv2DTranspose(128, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Conv2DTranspose(64, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Conv2DTranspose(32, (3, 3), 2, padding="same"),
BatchNormalization(),
Activation('relu'),
Conv2DTranspose(3, (3, 3), 1, padding="same"),
Activation("sigmoid")
]
)
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
def encode(self, x):
return self.encoder(x)
def decode(self, z):
return self.decoder(z)
def load_data(data_dir, image_size=(64, 64)):
images = []
image_names = os.listdir(data_dir)
for image_name in image_names:
if image_name.lower().endswith((".jpg", ".png", ".jpeg")):
image = cv2.imread(os.path.join(data_dir, image_name))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = preprocess_image(image, image_size=image_size[:2])
images.append(image)
return np.array(images)
def preprocess_image(image, image_size=(64, 64)):
image = cv2.resize(image, image_size)
image = image.astype("uint8") / 255.0
return image
def get_a_single_image_embedding(autoencoder, image):
"""Load and encode a single image."""
image = preprocess_image(image)
encoded_image = autoencoder.encode(image[None, ...]).numpy()[0]
return encoded_image
def dssim_loss(y_true, y_pred):
return 1 - tf.reduce_mean(tf.image.ssim(y_true, y_pred, 1.0)) / 2
def plot_original_and_reconstructed(
autoencoder, image, save_path="reconstructed_image.png"
):
test_image = preprocess_image(image)
test_image_processed = test_image[None, ...]
reconstructed_image = autoencoder.predict(test_image_processed)[0]
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.imshow(test_image)
plt.title("Original")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(reconstructed_image)
plt.title("Reconstructed")
plt.axis("off")
plt.savefig(save_path)
plt.show()
def load_autoencoder_model(model_path):
return load_model(
model_path,
custom_objects={"Autoencoder": Autoencoder, "dssim_loss": dssim_loss},
)
def train_model(
data_dir,
input_shape,
latent_dim,
batch_size,
epochs=200,
model_path=None,
):
# Load and prepare data
X = load_data(data_dir, input_shape)
X_train, X_val = train_test_split(X, test_size=0.2, random_state=42)
# Create autoencoder model
autoencoder = Autoencoder(latent_dim, input_shape)
autoencoder.compile(optimizer=Adam(1e-3), loss=dssim_loss)
# Set up callbacks
callbacks = [
ModelCheckpoint(
filepath=str(model_path),
save_best_only=True,
verbose=1,
monitor="val_loss",
),
EarlyStopping(monitor="val_loss", patience=5, verbose=1),
ReduceLROnPlateau(
monitor='val_loss',
factor=0.1,
patience=2,
verbose=1,
mode='auto',
min_delta=0.0001,
cooldown=0,
min_lr=0
),
]
# Train the model
autoencoder.fit(
x=X_train,
y=X_train,
epochs=epochs,
shuffle=True,
batch_size=batch_size,
validation_data=(X_val, X_val),
callbacks=callbacks,
)
return autoencoder
if __name__ == "__main__":
input_shape = (64, 64, 3)
latent_dim = 50
batch_size = 256
experiment_name = '3colors_2shapes'
repo_root = Path.cwd()
dataset_dir = repo_root / "data" / experiment_name
model_path = repo_root / "models" / f"{experiment_name}_{latent_dim}.tf"
if model_path.exists():
print("Loading autoencoder model...")
autoencoder = load_autoencoder_model(model_path)
else:
print("Training autoencoder model...")
autoencoder = train_model(
data_dir=dataset_dir,
input_shape=input_shape,
latent_dim=latent_dim,
batch_size=batch_size,
model_path=model_path,
)
# Test the model with a sample image
for image_path in [
"receiver_8567.png",
"receiver_8607.png",
"receiver_193.png",
"receiver_1635.png",
"receiver_2019.png",
]:
test_image_path = dataset_dir / image_path
image = cv2.imread(str(test_image_path))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plot_original_and_reconstructed(autoencoder, image)
embedding = get_a_single_image_embedding(autoencoder, image)
print('Embedding:', embedding)