forked from despoisj/LatentSpaceVisualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visuals.py
201 lines (162 loc) · 6.98 KB
/
visuals.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
import os
import sys
import h5py
import cv2
import math
import random, string
import numpy as np
from scipy.stats import norm
from sklearn import manifold
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from model import getModels
from config import latent_dim, imageSize, visualsPath
from datasetTools import loadDataset
# Show every image, good for picking interplation candidates
def visualizeDataset(X):
for i,image in enumerate(X):
cv2.imshow(str(i),image)
cv2.waitKey()
cv2.destroyAllWindows()
# Scatter with images instead of points
def imscatter(x, y, ax, imageData, zoom):
images = []
for i in range(len(x)):
x0, y0 = x[i], y[i]
# Convert to image
img = imageData[i]*255.
img = img.astype(np.uint8).reshape([imageSize,imageSize])
img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
# Note: OpenCV uses BGR and plt uses RGB
image = OffsetImage(img, zoom=zoom)
ab = AnnotationBbox(image, (x0, y0), xycoords='data', frameon=False)
images.append(ax.add_artist(ab))
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
# Show dataset images with T-sne projection of latent space encoding
def computeTSNEProjectionOfLatentSpace(X, encoder, display=True):
# Compute latent space representation
print("Computing latent space projection...")
X_encoded = encoder.predict(X)
# Compute t-SNE embedding of latent space
print("Computing t-SNE embedding...")
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
X_tsne = tsne.fit_transform(X_encoded)
# Plot images according to t-sne embedding
if display:
print("Plotting t-SNE visualization...")
fig, ax = plt.subplots()
imscatter(X_tsne[:, 0], X_tsne[:, 1], imageData=X, ax=ax, zoom=0.6)
plt.show()
else:
return X_tsne
# Show dataset images with T-sne projection of pixel space
def computeTSNEProjectionOfPixelSpace(X, display=True):
# Compute t-SNE embedding of latent space
print("Computing t-SNE embedding...")
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
X_tsne = tsne.fit_transform(X.reshape([-1,imageSize*imageSize*1]))
# Plot images according to t-sne embedding
if display:
print("Plotting t-SNE visualization...")
fig, ax = plt.subplots()
imscatter(X_tsne[:, 0], X_tsne[:, 1], imageData=X, ax=ax, zoom=0.6)
plt.show()
else:
return X_tsne
# Reconstructions for samples in dataset
def getReconstructedImages(X, autoencoder):
nbSamples = X.shape[0]
nbSquares = int(math.sqrt(nbSamples))
nbSquaresHeight = 2*nbSquares
nbSquaresWidth = nbSquaresHeight
resultImage = np.zeros((nbSquaresHeight*imageSize,nbSquaresWidth*imageSize/2,X.shape[-1]))
reconstructedX = autoencoder.predict(X)
for i in range(nbSamples):
original = X[i]
reconstruction = reconstructedX[i]
rowIndex = i%nbSquaresWidth
columnIndex = (i-rowIndex)/nbSquaresHeight
resultImage[rowIndex*imageSize:(rowIndex+1)*imageSize,columnIndex*2*imageSize:(columnIndex+1)*2*imageSize,:] = np.hstack([original,reconstruction])
return resultImage
# Reconstructions for samples in dataset
def visualizeReconstructedImages(X_train, X_test, autoencoder, save=False, label=False):
trainReconstruction = getReconstructedImages(X_train,autoencoder)
testReconstruction = getReconstructedImages(X_test,autoencoder)
if not save:
print("Generating 10 image reconstructions...")
result = np.hstack([trainReconstruction,np.zeros([trainReconstruction.shape[0],5,trainReconstruction.shape[-1]]),testReconstruction])
result = (result*255.).astype(np.uint8)
if save:
cv2.imwrite(visualsPath+"reconstructions_{}.png".format(label),result)
else:
cv2.imshow("Reconstructed images (train - test)",result)
cv2.waitKey()
cv2.destroyAllWindows()
# Computes A, B, C, A+B, A+B-C in latent space
def visualizeArithmetics(a, b, c, encoder, decoder):
print("Computing arithmetics...")
# Create micro batch
X = np.array([a,b,c])
# Compute latent space projection
latentA, latentB, latentC = encoder.predict(X)
add = latentA+latentB
addSub = latentA+latentB-latentC
# Create micro batch
X = np.array([latentA,latentB,latentC,add,addSub])
# Compute reconstruction
reconstructedA, reconstructedB, reconstructedC, reconstructedAdd, reconstructedAddSub = decoder.predict(X)
cv2.imshow("Arithmetics in latent space",np.hstack([reconstructedA, reconstructedB, reconstructedC, reconstructedAdd, reconstructedAddSub]))
cv2.waitKey()
# Shows linear inteprolation in image space vs latent space
def visualizeInterpolation(start, end, encoder, decoder, save=False, nbSteps=5):
print("Generating interpolations...")
# Create micro batch
X = np.array([start,end])
# Compute latent space projection
latentX = encoder.predict(X)
latentStart, latentEnd = latentX
# Get original image for comparison
startImage, endImage = X
vectors = []
normalImages = []
#Linear interpolation
alphaValues = np.linspace(0, 1, nbSteps)
for alpha in alphaValues:
# Latent space interpolation
vector = latentStart*(1-alpha) + latentEnd*alpha
vectors.append(vector)
# Image space interpolation
blendImage = cv2.addWeighted(startImage,1-alpha,endImage,alpha,0)
normalImages.append(blendImage)
# Decode latent space vectors
vectors = np.array(vectors)
reconstructions = decoder.predict(vectors)
# Put final image together
resultLatent = None
resultImage = None
if save:
hashName = ''.join(random.choice(string.lowercase) for i in range(3))
for i in range(len(reconstructions)):
interpolatedImage = normalImages[i]*255
interpolatedImage = cv2.resize(interpolatedImage,(50,50))
interpolatedImage = interpolatedImage.astype(np.uint8)
resultImage = interpolatedImage if resultImage is None else np.hstack([resultImage,interpolatedImage])
reconstructedImage = reconstructions[i]*255.
reconstructedImage = reconstructedImage.reshape([28,28])
reconstructedImage = cv2.resize(reconstructedImage,(50,50))
reconstructedImage = reconstructedImage.astype(np.uint8)
resultLatent = reconstructedImage if resultLatent is None else np.hstack([resultLatent,reconstructedImage])
if save:
cv2.imwrite(visualsPath+"{}_{}.png".format(hashName,i),np.hstack([interpolatedImage,reconstructedImage]))
result = np.vstack([resultImage,resultLatent])
if not save:
cv2.imshow("Interpolation in Image Space vs Latent Space",result)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == "__main__":
# Load dataset to test
print("Loading dataset...")
X_train, X_test = loadDataset()
visualizeDataset(X_test[:100])