-
Notifications
You must be signed in to change notification settings - Fork 3
/
EmbeddingHelper.py
152 lines (136 loc) · 6.96 KB
/
EmbeddingHelper.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
import numpy as np
import os
from typing import Union, Sequence
import tensorflow as tf
import tensorflow.compat.v1 as tf1
from tensorboard.plugins.projector import ProjectorConfig, visualize_embeddings
from PIL import Image
from sklearn.manifold import TSNE as tsne
from matplotlib.lines import Line2D
from matplotlib import pyplot as plt
class EmbeddingHelper():
def __init__(self, encoder, data_generator, embeddings_dir):
self.encoder = encoder
self.data_generator = data_generator
self.embeddings_dir = embeddings_dir
self.metadata_filename = None
self.sprite_filename = None
self.embeddings = None
self.data_array = None
self.labels = None
self.sprite_shape = None
print("Generating embedding, and metadata...")
self._generate_embeddings() # initialize the embeddings, data, and labels
self._create_metadata()
print("Completed generating embedding, and metadata")
return
def _generate_embeddings(self) -> None:
embeddings_list = []
data_array = None
label_list = []
counter = 0
for train_data, label in self.data_generator:
if data_array is None:
data_array = train_data.numpy()
else:
data_array = np.append(data_array, train_data.numpy(), axis=0)
embeddings = self.encoder.predict(train_data)
for i, embedding in enumerate(embeddings):
embeddings_list.append(embedding)
label_list.append(label[i])
counter += 1
self.embeddings = np.array(embeddings_list)
self.data_array = data_array
self.labels = label_list
def _create_metadata(self, filename="metadata.tsv") -> None:
self.metadata_filename = filename
metadata_filename = os.path.join(self.embeddings_dir, self.metadata_filename)
with open(metadata_filename, 'w') as f:
f.write("Index\tLabel\n")
for index, label in enumerate(self.labels):
f.write(f"{index}\t{label}\n")
return
def _preprocess_data(self) -> Union[np.ndarray, Sequence[np.ndarray]]:
"""
Function for preprocessing image data.
For now, it normalizes the image, and reverses black and white
Feel free to change this function for your own use
:return: Processed image
"""
# normalizing the image into 0-1
copy = self.data_array / np.max(self.data_array)
# reversing black and white to have white background and black content
copy = 1 - copy
return copy
def create_sprite(self, filename="sprites.png") -> None:
self.sprite_filename = filename
n_data = self.data_array.shape[0]
height = self.data_array.shape[1]
width = self.data_array.shape[2]
channel = self.data_array.shape[-1]
n_plots = int(np.ceil(np.sqrt(n_data)))
data_list_processed = self._preprocess_data()
sprite_canvas = np.ones(((height * n_plots), (width * n_plots), channel))
for row in range(n_plots):
for col in range(n_plots):
data_idx = row * n_plots + col
if data_idx < n_data:
img = data_list_processed[data_idx, ...]
sprite_canvas[row * height:(row + 1) * height, col * width:(col + 1) * width, ...] = img
alpha_channel = np.zeros_like(sprite_canvas)[..., [0]]
alpha_channel[np.nonzero(sprite_canvas < 1)] = 1
sprites_filename = os.path.join(self.embeddings_dir, self.sprite_filename)
if channel == 0 or channel == 1: # if it is grayscale, stack the images on top of each other
sprite_png = np.concatenate([sprite_canvas, sprite_canvas, sprite_canvas, alpha_channel], axis=-1)
sprite_png = (sprite_png * 255).astype(np.uint8)
elif channel == 3: # if it is RGB
sprite_png = np.concatenate([sprite_canvas, alpha_channel], axis=-1)
else:
raise ValueError("Invalid image channel size. It must be none, 1 or 3")
Image.fromarray(sprite_png).save(sprites_filename, "PNG")
self.sprite_shape = (height, width, 4)
return
def to_tensorboard(self, checkpoint_filename="my-model.ckpt", embedding_name="z_tf") -> None:
tf1.reset_default_graph()
z = tf.Variable(self.embeddings, name=embedding_name)
saver = tf.compat.v1.train.Saver(var_list=[z])
ckpt_filename = os.path.join(self.embeddings_dir, checkpoint_filename)
projector_config = ProjectorConfig()
projector_config.model_checkpoint_path = ckpt_filename
embeddings = projector_config.embeddings.add()
embeddings.tensor_name = embedding_name
# metadata_filename = os.path.join(self.embeddings_dir, self.metadata_filename)
embeddings.metadata_path = self.metadata_filename
# only add sprite if the the sprite is created in the first place
if self.sprite_filename is not None:
embeddings.sprite.image_path = os.path.join(self.embeddings_dir, self.sprite_filename)
embeddings.sprite.image_path = self.sprite_filename
embeddings.sprite.single_image_dim.extend(self.sprite_shape)
visualize_embeddings(logdir=self.embeddings_dir, config=projector_config)
saver.save(sess=None, global_step=None, save_path=ckpt_filename)
def tsne_plot(self, labels, colors, filename="tsne.png", show=False, n_components=2, perplexity=30.0,
early_exaggeration=12.0, learning_rate=200.0, n_iter=1000, n_iter_without_progress=300,
min_grad_norm=1e-07, metric='euclidean', init='random', verbose=0, random_state=None,
method='barnes_hut', angle=0.5, n_jobs=None) -> None:
if len(labels) != len(colors):
raise ValueError("The list of labels and colours should be the same!")
filename_abs = os.path.join(self.embeddings_dir, filename)
X_embedded = tsne(n_components=n_components, perplexity=perplexity, early_exaggeration=early_exaggeration,
learning_rate=learning_rate, n_iter=n_iter,
n_iter_without_progress=n_iter_without_progress, min_grad_norm=min_grad_norm, metric=metric,
init=init,
verbose=verbose, random_state=random_state, method=method, angle=angle,
n_jobs=n_jobs).fit_transform(
self.embeddings)
plt.figure(figsize=(6, 5))
for i, label in enumerate(self.labels):
label = label.numpy()
plt.scatter(X_embedded[i, 0], X_embedded[i, 1], c=colors[label], label=int(label))
lines = []
for i, color in enumerate(colors):
line = Line2D(range(1), range(1), color="white", marker='o', markerfacecolor=color)
lines.append(line)
plt.legend(lines, labels, numpoints=1, loc=1)
plt.savefig(fname=filename_abs, format='png')
if show:
plt.show()