forked from few-shot-learning/Keras-FewShotLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplet_loss_cifar10.py
284 lines (271 loc) · 11.5 KB
/
triplet_loss_cifar10.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
This notebooks borrows from https://www.tensorflow.org/addons/tutorials/losses_triplet and is intended to compare tf.addons triplet loss
implementation against this one. It is also aimed at benchmarking the impact of the distance function.
"""
from pathlib import Path
from pprint import pprint
import seaborn as sns
import matplotlib.pyplot as plt
import itertools
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import Conv2D, Dense, Dropout, GlobalMaxPooling2D, Input, Flatten, MaxPooling2D, Lambda
from tensorflow.keras.models import Sequential
from keras_fsl.layers import GramMatrix
from keras_fsl.losses.gram_matrix_losses import BinaryCrossentropy, class_consistency_loss, TripletLoss
from keras_fsl.metrics.gram_matrix_metrics import classification_accuracy
from keras_fsl.utils.tensors import get_dummies
#%% Build datasets
train_dataset, val_dataset, test_dataset = tfds.load(
name="cifar10", split=["train[:90%]", "train[90%:]", "test"], as_supervised=True
)
train_dataset = train_dataset.shuffle(1024).batch(64, drop_remainder=True)
val_dataset = val_dataset.shuffle(1024).batch(64, drop_remainder=True)
test_dataset = test_dataset.batch(64, drop_remainder=True)
train_labels = [labels.numpy().tolist() for _, labels in train_dataset]
val_labels = [labels.numpy().tolist() for _, labels in val_dataset]
test_labels = [labels.numpy().tolist() for _, labels in test_dataset]
train_steps = len(train_labels)
val_steps = len(val_labels)
test_steps = len(test_labels)
print(
pd.concat(
[
pd.DataFrame({"label": tf.nest.flatten(train_labels)}).assign(split="train"),
pd.DataFrame({"label": tf.nest.flatten(val_labels)}).assign(split="val"),
pd.DataFrame({"label": tf.nest.flatten(test_labels)}).assign(split="test"),
]
)
.groupby("split")
.apply(lambda group: pd.get_dummies(group.label).agg("sum"))
)
output_dir = Path("logs") / "triplet_loss_cifar10"
output_dir.mkdir(exist_ok=True, parents=True)
results = []
#%% Save test labels for later visualization in projector https://projector.tensorflow.org/
np.savetxt(output_dir / "meta.tsv", tf.nest.flatten(test_labels), fmt="%0d")
#%% Build model
encoder = Sequential(
[
Input(train_dataset.element_spec[0].shape[1:]),
Conv2D(filters=32, kernel_size=(3, 3), padding="same", activation="relu"),
MaxPooling2D(pool_size=2),
Dropout(0.3),
Conv2D(filters=64, kernel_size=(3, 3), padding="same", activation="relu"),
MaxPooling2D(pool_size=2),
Dropout(0.3),
Conv2D(filters=128, kernel_size=(3, 3), padding="same", activation="relu"),
MaxPooling2D(pool_size=2),
Dropout(0.3),
Conv2D(filters=256, kernel_size=(3, 3), padding="same", activation="relu"),
MaxPooling2D(pool_size=2),
Dropout(0.3),
GlobalMaxPooling2D(),
Flatten(),
]
)
encoder.save_weights(str(output_dir / "initial_encoder.h5"))
#%% Train encoder with usual cross entropy
encoder.load_weights(str(output_dir / "initial_encoder.h5"))
classifier = Sequential([encoder, Dense(10, activation="softmax")])
classifier.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["sparse_categorical_accuracy"])
classifier.fit(
train_dataset.map(
lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), y), num_parallel_calls=tf.data.experimental.AUTOTUNE
).repeat(),
epochs=100,
steps_per_epoch=train_steps,
validation_data=val_dataset.map(
lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), y), num_parallel_calls=tf.data.experimental.AUTOTUNE
).repeat(),
validation_steps=val_steps,
callbacks=[TensorBoard(str(output_dir / "sparse_categorical_crossentropy"))],
)
loss, accuracy = classifier.evaluate(
test_dataset.map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), y)), steps=test_steps
)
results += [{"experiment": "classifier", "loss": loss, "top_score_classification_accuracy": accuracy}]
embeddings = encoder.predict(test_dataset.map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), y)), steps=test_steps)
np.savetxt(str(output_dir / "classifier_embeddings.tsv"), embeddings, delimiter="\t")
#%% Train
experiments = [
{
"name": "l2_triplet_loss",
"kernel": Lambda(lambda x: tf.reduce_sum(tf.square(x[0] - x[1]), axis=1)),
"loss": TripletLoss(1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "l1_triplet_loss",
"kernel": Lambda(lambda x: tf.reduce_sum(tf.abs(x[0] - x[1]), axis=1)),
"loss": TripletLoss(1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "cosine_similarity_triplet_loss",
"kernel": Lambda(
lambda x: 1 - tf.reduce_sum(tf.nn.l2_normalize(x[0], axis=1) * tf.nn.l2_normalize(x[1], axis=1), axis=1)
),
"loss": TripletLoss(0.1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "cosine_similarity_crossentropy_loss",
"kernel": Lambda(
lambda x: (1 + tf.reduce_sum(tf.nn.l2_normalize(x[0], axis=1) * tf.nn.l2_normalize(x[1], axis=1), axis=1)) / 2
),
"loss": BinaryCrossentropy(),
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
{
"name": "cosine_similarity_consistency_loss",
"kernel": Lambda(
lambda x: (1 + tf.reduce_sum(tf.nn.l2_normalize(x[0], axis=1) * tf.nn.l2_normalize(x[1], axis=1), axis=1)) / 2
),
"loss": class_consistency_loss,
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
{
"name": "mixed_norms_triplet_loss",
"kernel": {
"name": "MixedNorms",
"init": {"activation": "relu", "norms": [lambda x: tf.square(x[0] - x[1]), lambda x: tf.abs(x[0] - x[1])]},
},
"loss": TripletLoss(1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "learnt_norms_triplet_loss",
"kernel": {"name": "LearntNorms", "init": {"activation": "relu"}},
"loss": TripletLoss(1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "mixed_similarity_triplet_loss",
"kernel": {
"name": "MixedNorms",
"init": {"activation": "sigmoid", "norms": [lambda x: tf.square(x[0] - x[1]), lambda x: tf.abs(x[0] - x[1])]},
},
"loss": TripletLoss(0.1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "learnt_similarity_triplet_loss",
"kernel": {"name": "LearntNorms", "init": {"activation": "sigmoid"}},
"loss": TripletLoss(0.1),
"metrics": [classification_accuracy(ascending=True)],
},
{
"name": "mixed_similarity_crossentropy_loss",
"kernel": {
"name": "MixedNorms",
"init": {"activation": "sigmoid", "norms": [lambda x: tf.square(x[0] - x[1]), lambda x: tf.abs(x[0] - x[1])]},
},
"loss": BinaryCrossentropy(),
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
{
"name": "learnt_similarity_crossentropy_loss",
"kernel": {"name": "LearntNorms", "init": {"activation": "sigmoid"}},
"loss": BinaryCrossentropy(),
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
{
"name": "mixed_similarity_consistency_loss",
"kernel": {
"name": "MixedNorms",
"init": {"activation": "sigmoid", "norms": [lambda x: tf.square(x[0] - x[1]), lambda x: tf.abs(x[0] - x[1])]},
},
"loss": class_consistency_loss,
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
{
"name": "learnt_similarity_consistency_loss",
"kernel": {"name": "LearntNorms", "init": {"activation": "sigmoid"}},
"loss": class_consistency_loss,
"metrics": [classification_accuracy(ascending=False), class_consistency_loss, BinaryCrossentropy()],
},
]
projectors = [
{"name": "", "projector": []},
{"name": "_l2_normalize", "projector": [Lambda(lambda x: tf.math.l2_normalize(x, axis=1))]},
{"name": "_dense_10", "projector": [Dense(10)]},
{"name": "_dense_128", "projector": [Dense(128)]},
]
for experiment, projector in itertools.product(experiments, projectors):
pprint(experiment)
pprint(projector)
for i in range(10):
encoder.load_weights(str(output_dir / "initial_encoder.h5"))
model = Sequential([encoder, *projector["projector"], GramMatrix(kernel=experiment["kernel"])])
model.compile(
optimizer="adam", loss=experiment["loss"], metrics=experiment["metrics"],
)
model.fit(
train_dataset.map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), get_dummies(y)[0])).repeat(),
epochs=100,
steps_per_epoch=train_steps,
validation_data=val_dataset.map(
lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), get_dummies(y)[0])
).repeat(),
validation_steps=val_steps,
callbacks=[TensorBoard(str(output_dir / f"{experiment['name']}{projector['name']}_{i}"))],
)
results += [
{
"experiment": experiment["name"],
"projector": projector["name"],
"iteration": i,
**dict(
zip(
model.metrics_names,
model.evaluate(
test_dataset.map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), get_dummies(y)[0])),
steps=test_steps,
),
)
),
}
]
embeddings = encoder.predict(
test_dataset.map(lambda x, y: (tf.image.convert_image_dtype(x, tf.float32), get_dummies(y)[0])), steps=test_steps
)
np.savetxt(str(output_dir / f"{experiment['name']}{projector['name']}_{i}.tsv"), embeddings, delimiter="\t")
#%% Export final stats
pd.DataFrame(results).to_csv(output_dir / "results.csv", index=False)
#%% Plot results
results = pd.read_csv(output_dir / "results.csv")
baseline = results[results.experiment == "classifier"].dropna(axis=1)
results = (
results.loc[lambda df: df.experiment != "classifier"]
.pipe(
lambda df: pd.concat(
[
df.fillna({"projector": "raw"}).filter(items=["top_score_classification_accuracy", "projector"]),
df.experiment.str.extract(
r"(?P<similarity>l1|l2|cosine_similarity|mixed_norms|mixed_similarity|learnt_norms|learnt_similarity)_"
r"(?P<loss_name>triplet_loss|consistency_loss|crossentropy_loss)"
),
],
axis=1,
)
)
.assign(projector=lambda df: df.projector.str.strip("_"))
)
chart = sns.catplot(
x="similarity",
y="top_score_classification_accuracy",
col="projector",
row="loss_name",
data=results,
legend=True,
legend_out=True,
)
chart.set_xticklabels(rotation=90)
[ax.axhline(y=baseline.top_score_classification_accuracy[0]) for ax in chart.axes.flatten()]
plt.tight_layout()
plt.savefig(output_dir / "all_losses.png")
plt.show()