-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualize_embeddings.py
145 lines (121 loc) · 6.26 KB
/
visualize_embeddings.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
# -*- coding: utf-8 -*-
import torchvision.transforms as trn
import torchvision.datasets as dset
import torch
import numpy as np
import plotly.express as px
from sklearn.manifold import TSNE
from matplotlib import cm
import matplotlib.pyplot as plt
from networks.resnet_big import *
import utils.svhn_loader as svhn
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# mean and standard deviation of channels of CIFAR-10 images
mean = [0.4914, 0.4822, 0.4465]
std = [0.2023, 0.1994, 0.2010]
test_transform = trn.Compose([trn.ToTensor(), trn.Normalize(mean, std)])
model_name = '/nobackup/OOD_762/supcon_pretrained_bsz_1024_epochs_500.pth'
#uncomment this for OOD Magnets model viz and comment out subsequent "net" initialization
'''
net = MyEnsemble(SupConResNet(), LinearClassifier())
net.load_state_dict(torch.load(model_name)['model'])
net.cuda()
'''
net = SupConResNet(name='resnet50')
net.encoder = torch.nn.DataParallel(net.encoder)
net.load_state_dict(torch.load(model_name)['model'])
net.to(device)
net.eval()
data_1 = dset.CIFAR10('./cifarpy',train=False, transform=test_transform, download = True)
test_loader = torch.utils.data.DataLoader(data_1, batch_size=32, shuffle=False)
def get_data(test_loader, ood_loader):
embeddings = np.zeros(shape=(0,128))
test_targets = []
for batch, (x,y) in enumerate(test_loader):
x, y = x.to(device), y.to(device)
#uncomment this for OOD Magnets model viz and comment subsequent line
#out = net.modelA(x)
out = net(x)
embeddings = np.concatenate([embeddings, out.detach().cpu().numpy()],axis=0)
y = y.detach().cpu().tolist()
test_targets.extend(y)
print("ID Data Shape :: ",embeddings.shape)
print("Targets :: ", len(test_targets))
for batch, (x,y) in enumerate(ood_loader):
x, y = x.to(device), y.to(device)
#uncomment this for OOD Magnets model viz and comment subsequent line
#out = net.modelA(x)
out = net(x)
embeddings = np.concatenate([embeddings, out.detach().cpu().numpy()],axis=0)
y = y.detach().cpu().tolist()
test_targets.extend([10 for i in range(len(y))])
print("After OOD addition Data Shape :: ",embeddings.shape)
print("Targets :: ", len(test_targets))
return embeddings, test_targets
# Create a two dimensional t-SNE projection of the embeddings
def plot(embeddings, test_targets, dataset):
tsne = TSNE(2, verbose=1)
embeddings = np.array(embeddings)
test_targets = np.array(test_targets)
tsne_proj = tsne.fit_transform(embeddings)
# Plot those points as a scatter plot and label them based on the pred labels
cmap = cm.get_cmap('tab20')
fig, ax = plt.subplots(figsize=(8,8))
num_categories = 11
for lab in range(num_categories):
indices = test_targets==lab
ax.scatter(tsne_proj[indices,0],tsne_proj[indices,1], c=np.array(cmap(lab)).reshape(1,4), label = lab ,alpha=0.5)
ax.legend(fontsize='large', markerscale=2)
plt.savefig(f"{dataset}_tnse.png")
# /////////////// LSUN-C ///////////////
ood_data = dset.ImageFolder(root="/nobackup/lsun_c_jnakhleh/LSUN",
transform=trn.Compose([trn.ToTensor(), trn.Normalize(mean, std)]))
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=1, pin_memory=True)
print('\n\nLSUN_C Detection')
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "lsun_c")
# /////////////// LSUN-R ///////////////
ood_data = dset.ImageFolder(root="/nobackup/lsun_r_jnakhleh/LSUN_resize",
transform=trn.Compose([trn.ToTensor(), trn.Normalize(mean, std)]))
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=1, pin_memory=True)
print('\n\nLSUN_Resize Detection')
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "lsun_r")
# /////////////// SVHN /////////////// # cropped and no sampling of the test set
print('\n\nSVHN Detection')
ood_data = svhn.SVHN(root='/nobackup/svhn_jnakhleh/', split="test",
transform=trn.Compose(
[#trn.Resize(32),
trn.ToTensor(), trn.Normalize(mean, std)]), download=True)
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=2, pin_memory=True)
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "SVHN")
# /////////////// Textures ///////////////
ood_data = dset.ImageFolder(root="/nobackup/dtd/images",
transform=trn.Compose([trn.Resize(32), trn.CenterCrop(32),
trn.ToTensor(), trn.Normalize(mean, std)]))
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=4, pin_memory=True)
print('\n\n Texture Detection')
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "dtd")
# # /////////////// Places365 ///////////////
ood_data = dset.ImageFolder(root="/nobackup/dataset_myf/places365",
transform=trn.Compose([trn.Resize(32), trn.CenterCrop(32),
trn.ToTensor(), trn.Normalize(mean, std)]))
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=2, pin_memory=True)
print('\n\nPlaces365 Detection')
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "dtd")
# # /////////////// iSUN ///////////////
ood_data = dset.ImageFolder(root="/nobackup/isun_jnakhleh/iSUN",
transform=trn.Compose([trn.ToTensor(), trn.Normalize(mean, std)]))
ood_loader = torch.utils.data.DataLoader(ood_data, batch_size=32, shuffle=True,
num_workers=1, pin_memory=True)
print('\n\niSUN Detection')
embeddings, test_targets = get_data(test_loader, ood_loader)
plot(embeddings, test_targets, "isun")