forked from tensorpack/tensorpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist-embeddings.py
More file actions
executable file
·234 lines (185 loc) · 7.73 KB
/
mnist-embeddings.py
File metadata and controls
executable file
·234 lines (185 loc) · 7.73 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
231
232
233
234
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: mnist-embeddings.py
# Author: PatWie <mail@patwie.com>
import numpy as np
import os
from tensorpack import *
import tensorpack.tfutils.symbolic_functions as symbf
from tensorpack.tfutils.summary import add_moving_summary
import argparse
import tensorflow as tf
import tensorflow.contrib.slim as slim
from embedding_data import get_test_data, MnistPairs, MnistTriplets
MATPLOTLIB_AVAIBLABLE = False
try:
import matplotlib
from matplotlib import offsetbox
import matplotlib.pyplot as plt
MATPLOTLIB_AVAIBLABLE = True
except ImportError:
MATPLOTLIB_AVAIBLABLE = False
class EmbeddingModel(ModelDesc):
def embed(self, x, nfeatures=2):
"""Embed all given tensors into an nfeatures-dim space. """
list_split = 0
if isinstance(x, list):
list_split = len(x)
x = tf.concat(x, 0)
# pre-process MNIST dataflow data
x = tf.expand_dims(x, 3)
x = x * 2 - 1
# the embedding network
net = slim.layers.conv2d(x, 20, 5, scope='conv1')
net = slim.layers.max_pool2d(net, 2, scope='pool1')
net = slim.layers.conv2d(net, 50, 5, scope='conv2')
net = slim.layers.max_pool2d(net, 2, scope='pool2')
net = slim.layers.flatten(net, scope='flatten3')
net = slim.layers.fully_connected(net, 500, scope='fully_connected4')
embeddings = slim.layers.fully_connected(net, nfeatures, activation_fn=None, scope='fully_connected5')
# if "x" was a list of tensors, then split the embeddings
if list_split > 0:
embeddings = tf.split(embeddings, list_split, 0)
return embeddings
def _get_optimizer(self):
lr = symbf.get_scalar_var('learning_rate', 1e-4, summary=True)
return tf.train.GradientDescentOptimizer(lr)
class SiameseModel(EmbeddingModel):
@staticmethod
def get_data():
ds = MnistPairs('train')
ds = BatchData(ds, 128 // 2)
return ds
def _get_inputs(self):
return [InputDesc(tf.float32, (None, 28, 28), 'input'),
InputDesc(tf.float32, (None, 28, 28), 'input_y'),
InputDesc(tf.int32, (None,), 'label')]
def _build_graph(self, inputs):
# get inputs
x, y, label = inputs
# embed them
x, y = self.embed([x, y])
# tag the embedding of 'input' with name 'emb', just for inference later on
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
tf.identity(self.embed(inputs[0]), name="emb")
# compute the actual loss
cost, pos_dist, neg_dist = symbf.contrastive_loss(x, y, label, 5., extra=True, scope="loss")
self.cost = tf.identity(cost, name="cost")
# track these values during training
add_moving_summary(pos_dist, neg_dist, self.cost)
class CosineModel(SiameseModel):
def _build_graph(self, inputs):
x, y, label = inputs
x, y = self.embed([x, y])
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
tf.identity(self.embed(inputs[0]), name="emb")
cost = symbf.siamese_cosine_loss(x, y, label, scope="loss")
self.cost = tf.identity(cost, name="cost")
add_moving_summary(self.cost)
class TripletModel(EmbeddingModel):
@staticmethod
def get_data():
ds = MnistTriplets('train')
ds = BatchData(ds, 128 // 3)
return ds
def _get_inputs(self):
return [InputDesc(tf.float32, (None, 28, 28), 'input'),
InputDesc(tf.float32, (None, 28, 28), 'input_p'),
InputDesc(tf.float32, (None, 28, 28), 'input_n')]
def loss(self, a, p, n):
return symbf.triplet_loss(a, p, n, 5., extra=True, scope="loss")
def _build_graph(self, inputs):
a, p, n = inputs
a, p, n = self.embed([a, p, n])
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
tf.identity(self.embed(inputs[0]), name="emb")
cost, pos_dist, neg_dist = self.loss(a, p, n)
self.cost = tf.identity(cost, name="cost")
add_moving_summary(pos_dist, neg_dist, self.cost)
class SoftTripletModel(TripletModel):
def loss(self, a, p, n):
return symbf.soft_triplet_loss(a, p, n, scope="loss")
def get_config(model, algorithm_name):
extra_display = ["cost"]
if not algorithm_name == "cosine":
extra_display = extra_display + ["loss/pos-dist", "loss/neg-dist"]
return TrainConfig(
dataflow=model.get_data(),
model=model(),
callbacks=[
ModelSaver(),
ScheduledHyperParamSetter('learning_rate', [(10, 1e-5), (20, 1e-6)])
],
extra_callbacks=[
MovingAverageSummary(),
ProgressBar(extra_display),
MergeAllSummaries(),
RunUpdateOps()
],
max_epoch=20,
)
def visualize(model_path, model, algo_name):
if not MATPLOTLIB_AVAIBLABLE:
logger.error("visualize requires matplotlib package ...")
return
pred = OfflinePredictor(PredictConfig(
session_init=get_model_loader(model_path),
model=model(),
input_names=['input'],
output_names=['emb']))
NUM_BATCHES = 6
BATCH_SIZE = 128
images = np.zeros((BATCH_SIZE * NUM_BATCHES, 28, 28)) # the used digits
embed = np.zeros((BATCH_SIZE * NUM_BATCHES, 2)) # the actual embeddings in 2-d
# get only the embedding model data (MNIST test)
ds = get_test_data()
ds.reset_state()
for offset, dp in enumerate(ds.get_data()):
digit, label = dp
prediction = pred([digit])[0]
embed[offset * BATCH_SIZE:offset * BATCH_SIZE + BATCH_SIZE, ...] = prediction
images[offset * BATCH_SIZE:offset * BATCH_SIZE + BATCH_SIZE, ...] = digit
offset += 1
if offset == NUM_BATCHES:
break
plt.figure()
ax = plt.subplot(111)
ax_min = np.min(embed, 0)
ax_max = np.max(embed, 0)
ax_dist_sq = np.sum((ax_max - ax_min)**2)
ax.axis('off')
shown_images = np.array([[1., 1.]])
for i in range(embed.shape[0]):
dist = np.sum((embed[i] - shown_images)**2, 1)
if np.min(dist) < 3e-4 * ax_dist_sq: # don't show points that are too close
continue
shown_images = np.r_[shown_images, [embed[i]]]
imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(np.reshape(images[i, ...], [28, 28]),
zoom=0.6, cmap=plt.cm.gray_r), xy=embed[i], frameon=False)
ax.add_artist(imagebox)
plt.axis([ax_min[0], ax_max[0], ax_min[1], ax_max[1]])
plt.xticks([]), plt.yticks([])
plt.title('Embedding using %s-loss' % algo_name)
plt.savefig('%s.jpg' % algo_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', help='comma separated list of GPU(s) to use.')
parser.add_argument('--load', help='load model')
parser.add_argument('-a', '--algorithm', help='used algorithm', type=str,
choices=["siamese", "cosine", "triplet", "softtriplet"])
parser.add_argument('--visualize', help='export embeddings into an image', action='store_true')
args = parser.parse_args()
ALGO_CONFIGS = {"siamese": SiameseModel,
"cosine": CosineModel,
"triplet": TripletModel,
"softtriplet": SoftTripletModel}
logger.auto_set_dir(name=args.algorithm)
with change_gpu(args.gpu):
if args.visualize:
visualize(args.load, ALGO_CONFIGS[args.algorithm], args.algorithm)
else:
config = get_config(ALGO_CONFIGS[args.algorithm], args.algorithm)
if args.load:
config.session_init = SaverRestore(args.load)
else:
SimpleTrainer(config).train()