-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsne.py
133 lines (100 loc) · 3.38 KB
/
tsne.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import json
from models import *
from utils import progress_bar
import utils
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import torchvision.models as models
import argparse
import os
import numpy as np
import pandas as pd
# from tsne import bh_sne
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import load_digits
from sklearn.manifold import TSNE
import seaborn as sns
import matplotlib.pyplot as plt
import openTSNE
parser = argparse.ArgumentParser(description='PyTorch t-SNE for STL10')
parser.add_argument('--save-dir', type=str, default='./tsne_results', help='path to save the t-sne image')
parser.add_argument('--batch-size', type=int, default=4, help='batch size (default: 128)')
parser.add_argument('--seed', type=int, default=1, help='random seed value (default: 1)')
args = parser.parse_args()
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
device = 'cuda'
#device = 'cuda' if torch.cuda.is_available() else 'cpu'
# set seed
torch.manual_seed(args.seed)
if device == 'cuda':
torch.cuda.manual_seed(args.seed)
transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
dataset = torchvision.datasets.CIFAR10(root="data/", train=True, download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch_size, shuffle=False, num_workers=4)
# set net
net = VGG('VGG19')
# net = ResNet18()
# net = PreActResNet18()
# # net = GoogLeNet()
# # net = DenseNet121()
# # net = ResNeXt29_2x64d()
# # net = MobileNet()
# # net = MobileNetV2()
# # net = DPN92()
# # net = ShuffleNetG2()
# # net = SENet18()
# # net = ShuffleNetV2(1)
# # net = EfficientNetB0()
# # net = RegNetX_200MF()
# # net = SimpleDLA()
if device == 'cuda':
net = net.to(device)
if device == 'cuda':
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
checkpoint = torch.load("checkpoint/ckpt_vgg_ip0005_x500.pth")
net.load_state_dict(checkpoint['net'])
def gen_features():
#net.eval()
targets_list = []
outputs_list = []
with torch.no_grad():
for idx, (inputs, targets) in enumerate(dataloader):
inputs = inputs.to(device)
targets = targets.to(device)
targets_np = targets.data.cpu().numpy()
outputs = net(inputs).cpu()
outputs = torch.squeeze(outputs)
outputs_np = outputs.data.numpy()
targets_list.append(targets_np)#[:, np.newaxis])
outputs_list.append(outputs_np)
if ((idx+1) % 10 == 0) or (idx+1 == len(dataloader)):
print(idx+1, '/', len(dataloader))
targets = np.concatenate(targets_list, axis=0)
outputs = np.concatenate(outputs_list, axis=0)#.astype(np.float64)
print(outputs)
return targets, outputs
targets, outputs = gen_features()
outputs = np.array(outputs)
targets = np.array(targets)
tsne = openTSNE.TSNE(
perplexity=30,
metric="euclidean",
n_jobs=8,
random_state=42,
verbose=True,
)
embedding = tsne.fit(outputs)
utils.plot(args.save_dir, embedding, targets, colors=utils.MACOSKO_COLORS)