forked from inconnu11/Objective-evaluation_speech_synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsne_visualization.py
144 lines (121 loc) · 4.13 KB
/
tsne_visualization.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
import os
import sys
import numpy as np
from time import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from sklearn import (manifold, datasets, decomposition,
ensemble, random_projection)
import random
from time import time,sleep
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import NullFormatter
from sklearn import manifold, datasets
style_encoder_path = "/home/zhaoxt20/vae_tac_myself/exp_multi/Libritts_styles"
embedding_dict={}
speakers = os.listdir(style_encoder_path)
random.shuffle(speakers)
for speaker in speakers[:10]:
speaker_path = os.path.join(style_encoder_path,speaker)
sentences = os.listdir(speaker_path)
if speaker not in embedding_dict:
embedding_dict[speaker]=[]
for sentence in sentences:
sentence_path = os.path.join(speaker_path,sentence)
embeddings = os.listdir(sentence_path)
for embedding in embeddings:
embedding_path = os.path.join(sentence_path,embedding)
embedding_np = np.load(embedding_path)[0]
embedding_dict[speaker].append(embedding_np)
if len(embedding_dict[speaker])<=1:
embedding_dict.pop(speaker)
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0, verbose=1)
color_num = len(speakers) * 2
colors = cm.rainbow(np.linspace(0, 1, color_num))
Y=None
data=None
label=[]
for x in embedding_dict:
if data is None:
data = embedding_dict[x]
else:
data = data + embedding_dict[x]
#data = tsne.fit_transform(data)
# if Y is None:
# Y=np.array(data)
# else:
# Y = np.concatenate((Y,data),axis=
tmp=[]
for i in range(len(embedding_dict[x])):
tmp.append(x)
label=label+tmp
print(len(data))
Y = tsne.fit_transform(data)
def plot_embedding_2d(X, title=None, save_path=None):
# linear to [0,1]
x_min, x_max = np.min(X, axis=0), np.max(X, axis=0)
X = (X - x_min) / (x_max - x_min)
# print('plot X', X)
# print('plot Char', Char)
# at x0, x1, draw text
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(X.shape[0]):
c = plt.cm.Set1(speakers.index(label[i]) % 10 / 10.)
ax.text(X[i, 0], X[i, 1], str(label[i]), color=c,
fontdict={'weight': 'bold', 'size': 4})
if title is not None:
plt.title(title)
if save_path is not None:
plt.savefig(save_path, format='png', dpi=300)
plt.close()
def plot_embedding_2d_focus(X, focus, title=None, save_path=None):
# linear to [0,1]
x_min, x_max = np.min(X, axis=0), np.max(X, axis=0)
X = (X - x_min) / (x_max - x_min)
# print('plot X', X)
# print('plot Char', Char)
# at x0, x1, draw text
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
for i in range(X.shape[0]):
ch = get_symbol(i)
c = get_color(i)
if ch in focus:
# Log(ch)
# Log(X[i, 0])
# Log(X[i, 1])
# ax.text(X[i, 0], X[i, 1], ch, color=c,
# fontdict={'weight': 'bold', 'size': 9})
ax.text(X[i, 0]+np.random.normal(0, 0.0075), X[i, 1]+np.random.normal(0, 0.0075), ch, color=c,
fontdict={'weight': 'bold', 'size': 9})
if title is not None:
plt.title(title)
if save_path is not None:
plt.savefig(save_path, format='png', dpi=300)
plt.close()
# def plot_embedding(data, label, title):
# x_min, x_max = np.min(data, 0), np.max(data, 0)
# data = (data - x_min) / (x_max - x_min)
#
# fig = plt.figure()
# ax = plt.subplot(111)
# for i in range(data.shape[0]):
# plt.text(data[i, 0], data[i, 1], str(speakers.index(label[i])),
# ,
# fontdict={'weight': 'bold', 'size': 9})
# plt.xticks([])
# plt.yticks([])
# plt.title(title)
# return fig
#
# t0=time()
# fig = plot_embedding(Y,label,
# 't-SNE embedding of the digits (time %.2fs)'
# % (time() - t0))
# plt.show(fig)
# fig.savefig('./temp.png')
plot_embedding_2d(Y, "t-SNE 2D", style_encoder_path+'/t-sne.png')