-
Notifications
You must be signed in to change notification settings - Fork 86
/
cluster.py
167 lines (135 loc) · 6.1 KB
/
cluster.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
import sys
sys.path.append("..")
import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
import matplotlib.cm as cm
plt.switch_backend('agg')
import torch
from dataset import MixamoDatasetForFull
from common import config
from model import get_autoencoder
import cv2
import time
import argparse
import os
def tsne_on_pca(arr, is_PCA=True):
"""
visualize through t-sne on pca reduced data
:param arr: (nr_examples, nr_features)
:return:
"""
if is_PCA:
pca_50 = PCA(n_components=50)
arr = pca_50.fit_transform(arr)
tsne_2 = TSNE(n_components=2)
res = tsne_2.fit_transform(arr)
return res
def cluster_body(net, cluster_data, device, save_path):
data, characters = cluster_data[0], cluster_data[2]
data = data[:, :, 0, :, :]
# data = data.reshape(-1, data.shape[2], data.shape[3], data.shape[4])
nr_mv, nr_char = data.shape[0], data.shape[1]
labels = np.arange(0, nr_char).reshape(1, -1)
labels = np.tile(labels, (nr_mv, 1)).reshape(-1)
if hasattr(net, 'static_encoder'):
features = net.static_encoder(data.contiguous().view(-1, data.shape[2], data.shape[3])[:, :-2, :].to(device))
else:
features = net.body_encoder(data.contiguous().view(-1, data.shape[2], data.shape[3])[:, :-2, :].to(device))
features = features.detach().cpu().numpy().reshape(features.shape[0], -1)
features_2d = tsne_on_pca(features, is_PCA=False)
features_2d = features_2d.reshape(nr_mv, nr_char, -1)
plt.figure(figsize=(7, 4))
colors = cm.rainbow(np.linspace(0, 1, nr_char))
for i in range(nr_char):
x = features_2d[:, i, 0]
y = features_2d[:, i, 1]
plt.scatter(x, y, c=colors[i], label=characters[i])
plt.legend(bbox_to_anchor=(1.04, 1), borderaxespad=0)
plt.tight_layout(rect=[0,0,0.75,1])
plt.savefig(save_path)
def cluster_view(net, cluster_data, device, save_path):
data, views = cluster_data[0], cluster_data[3]
idx = np.random.randint(data.shape[1] - 1) # np.linspace(0, data.shape[1] - 1, 4, dtype=int).tolist()
data = data[:, idx, :, :, :]
nr_mc, nr_view = data.shape[0], data.shape[1]
labels = np.arange(0, nr_view).reshape(1, -1)
labels = np.tile(labels, (nr_mc, 1)).reshape(-1)
if hasattr(net, 'static_encoder'):
features = net.static_encoder(data.contiguous().view(-1, data.shape[2], data.shape[3])[:, :-2, :].to(device))
else:
features = net.view_encoder(data.contiguous().view(-1, data.shape[2], data.shape[3])[:, :-2, :].to(device))
features = features.detach().cpu().numpy().reshape(features.shape[0], -1)
features_2d = tsne_on_pca(features, is_PCA=False)
features_2d = features_2d.reshape(nr_mc, nr_view, -1)
plt.figure(figsize=(7, 4))
colors = cm.rainbow(np.linspace(0, 1, nr_view))
for i in range(nr_view):
x = features_2d[:, i, 0]
y = features_2d[:, i, 1]
plt.scatter(x, y, c=colors[i], label=views[i])
plt.legend(bbox_to_anchor=(1.04, 1), borderaxespad=0)
plt.tight_layout(rect=[0, 0, 0.75, 1])
plt.savefig(save_path)
def cluster_motion(net, cluster_data, device, save_path, nr_anims=15, mode='both'):
data, animations = cluster_data[0], cluster_data[1]
idx = np.linspace(0, data.shape[0] - 1, nr_anims, dtype=int).tolist()
data = data[idx]
animations = animations[idx]
if mode == 'body':
data = data[:, :, 0, :, :].reshape(nr_anims, -1, data.shape[3], data.shape[4])
elif mode == 'view':
data = data[:, 3, :, :, :].reshape(nr_anims, -1, data.shape[3], data.shape[4])
else:
data = data[:, :4, ::2, :, :].reshape(nr_anims, -1, data.shape[3], data.shape[4])
nr_anims, nr_cv = data.shape[:2]
labels = np.arange(0, nr_anims).reshape(-1, 1)
labels = np.tile(labels, (1, nr_cv)).reshape(-1)
features = net.mot_encoder(data.contiguous().view(-1, data.shape[2], data.shape[3]).to(device))
features = features.detach().cpu().numpy().reshape(features.shape[0], -1)
features_2d = tsne_on_pca(features)
features_2d = features_2d.reshape(nr_anims, nr_cv, -1)
if features_2d.shape[1] < 5:
features_2d = np.tile(features_2d, (1, 2, 1))
plt.figure(figsize=(8, 4))
colors = cm.rainbow(np.linspace(0, 1, nr_anims))
for i in range(nr_anims):
x = features_2d[i, :, 0]
y = features_2d[i, :, 1]
plt.scatter(x, y, c=colors[i], label=animations[i])
plt.legend(bbox_to_anchor=(1.04, 1), borderaxespad=0)
plt.tight_layout(rect=[0,0,0.8,1])
plt.savefig(save_path)
def test():
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--name', type=str, choices=['skeleton', 'view', 'full'], required=True,
help='which structure to use.')
parser.add_argument('-p', '--model_path', type=str, default="model/pretrained_view.pth")
parser.add_argument('--phase', type=str, default="test", choices=['train', 'test'])
parser.add_argument('-g', '--gpu_ids', type=int, default=0, required=False, help="specify gpu ids")
args = parser.parse_args()
# set config
config.initialize(args)
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu_ids)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# load trained model
net = get_autoencoder(config)
net.load_state_dict(torch.load(args.model_path))
net.to(config.device)
net.eval()
# get dataset
train_ds = MixamoDatasetForFull(args.phase, config)
cluster_data = train_ds.get_cluster_data()
# score, img = cluster_body(net, cluster_data, device, './cluster_body.png')
if args.name == 'view':
cluster_view(net, cluster_data, device, './cluster_view.png')
cluster_motion(net, cluster_data, device, './cluster_motion.png')
elif args.name == 'skeleton':
cluster_body(net, cluster_data, device, './cluster_body.png')
cluster_motion(net, cluster_data, device, './cluster_motion.png', mode='body')
else:
cluster_motion(net, cluster_data, device, './cluster_motion.png')
if __name__ == '__main__':
test()