-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtriplet_loss_mnist.py
151 lines (137 loc) · 6.32 KB
/
triplet_loss_mnist.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
"""
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.
"""
import io
import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa
import tensorflow_datasets as tfds
from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, Lambda, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import TensorBoard
from keras_fsl.losses.gram_matrix_losses import triplet_loss
from keras_fsl.layers import GramMatrix
from keras_fsl.utils.tensors import get_dummies
from keras_fsl.metrics.gram_matrix_metrics import classification_accuracy
#%% Build model
encoder = Sequential(
[
Conv2D(filters=64, kernel_size=2, padding="same", activation="relu", input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=2),
Dropout(0.3),
Conv2D(filters=32, kernel_size=2, padding="same", activation="relu"),
MaxPooling2D(pool_size=2),
Dropout(0.3),
Flatten(),
Dense(256, activation=None), # No activation on final dense layer
Lambda(lambda x: tf.math.l2_normalize(x, axis=1)), # L2 normalize embeddings
]
)
encoder.save_weights("initial_encoder.h5")
support_layer = GramMatrix(kernel=Lambda(lambda inputs: tf.math.reduce_sum(tf.square(inputs[0] - inputs[1]), axis=1)))
model = Sequential([encoder, support_layer])
#%% Build datasets
def preprocessing(input_tensor):
return tf.cast(input_tensor, tf.float32) / 255
train_dataset, test_dataset = tfds.load(name="mnist", split=["train", "test"], as_supervised=True)
train_dataset = train_dataset.shuffle(1024).batch(32)
test_dataset = test_dataset.batch(32)
#%% Save test labels for later visualization in projector https://projector.tensorflow.org/
out_m = io.open("meta.tsv", "w", encoding="utf-8")
for img, labels in tfds.as_numpy(test_dataset):
[out_m.write(str(x) + "\n") for x in labels]
out_m.close()
#%% Train with tfa triplet loss
encoder.load_weights("initial_encoder.h5")
encoder.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=tfa.losses.TripletSemiHardLoss(),
)
encoder.fit(train_dataset.map(lambda x, y: (preprocessing(x), y)), epochs=5, callbacks=[TensorBoard("tfa_loss")])
encoder.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), y)))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), y)))
np.savetxt("tfa_embeddings.tsv", results, delimiter="\t")
#%% Train with keras_fsl triplet loss
encoder.load_weights("initial_encoder.h5")
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=triplet_loss(), metrics=[classification_accuracy(ascending=True)]
)
model.fit(
train_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])), epochs=5, callbacks=[TensorBoard("keras_fsl_loss")]
)
model.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
np.savetxt("keras_fsl_embeddings.tsv", results, delimiter="\t")
#%% Try with l1 norm
support_layer.kernel = Lambda(lambda inputs: tf.math.reduce_sum(tf.abs(inputs[0] - inputs[1]), axis=1))
encoder.load_weights("initial_encoder.h5")
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=triplet_loss(), metrics=[classification_accuracy(ascending=True)]
)
model.fit(
train_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])),
epochs=5,
callbacks=[TensorBoard("keras_fsl_l1_loss")],
)
model.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
np.savetxt("keras_fsl_l1_embeddings.tsv", results, delimiter="\t")
#%% Try with cosine similarity
support_layer.kernel = Lambda(
lambda inputs: tf.reduce_sum(1 - tf.nn.l2_normalize(inputs[0], axis=1) * tf.nn.l2_normalize(inputs[1], axis=1), axis=1)
)
encoder.load_weights("initial_encoder.h5")
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=triplet_loss(0.1), metrics=[classification_accuracy(ascending=True)]
)
model.fit(
train_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])),
epochs=5,
callbacks=[TensorBoard("keras_fsl_cosine_similarity")],
)
model.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
np.savetxt("keras_fsl_cosine_similarity_embeddings.tsv", results, delimiter="\t")
#%% Try with learnt norm
support_layer = GramMatrix(
kernel={
"name": "MixedNorms",
"init": {"activation": "relu", "norms": [lambda x: tf.math.abs(x[0] - x[1]), lambda x: tf.math.square(x[0] - x[1])]},
}
)
encoder.load_weights("initial_encoder.h5")
model = Sequential([encoder, support_layer])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=triplet_loss(), metrics=[classification_accuracy(ascending=True)]
)
model.fit(
train_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])),
epochs=5,
callbacks=[TensorBoard("keras_fsl_learnt_norm")],
)
model.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
np.savetxt("keras_fsl_learnt_norm_embeddings.tsv", results, delimiter="\t")
#%% Try with learnt similarity
support_layer = GramMatrix(
kernel={
"name": "MixedNorms",
"init": {
"activation": "sigmoid",
"norms": [lambda x: tf.math.abs(x[0] - x[1]), lambda x: tf.math.square(x[0] - x[1])],
},
}
)
encoder.load_weights("initial_encoder.h5")
model = Sequential([encoder, support_layer])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001), loss=triplet_loss(0.1), metrics=[classification_accuracy(ascending=True)]
)
model.fit(
train_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])),
epochs=5,
callbacks=[TensorBoard("keras_fsl_learnt_similarity")],
)
model.evaluate(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
results = encoder.predict(test_dataset.map(lambda x, y: (preprocessing(x), get_dummies(y)[0])))
np.savetxt("keras_fsl_learnt_similarity_embeddings.tsv", results, delimiter="\t")