-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.py
More file actions
88 lines (81 loc) · 3.06 KB
/
Copy pathexample.py
File metadata and controls
88 lines (81 loc) · 3.06 KB
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
import torch
import torchvision.models as models
import torchvision.transforms as transforms
from dora import Dora
from dora.objectives import ChannelObjective
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
neuron_indices = [i for i in range(5, 6)]
model = models.resnet18(pretrained=True).eval().to(device)
d = Dora(model=model, device=device)
d.generate_signals(
neuron_idx=neuron_indices,
num_samples = 1,
layer=model.fc,
batch_size = 2,
image_transforms = transforms.Compose([transforms.Pad(2, fill=.5, padding_mode='constant'),
transforms.RandomAffine((-15,15),
translate=(0, 0.1),
scale=(0.85, 1.2),
shear=(-15,15),
fill=0.5),
transforms.RandomCrop((224, 224),
padding=None,
pad_if_needed=True,
fill=0,
padding_mode='constant')]),
objective_fn=ChannelObjective(),
lr=2e-2,
width=224,
height=224,
iters=100,
experiment_name="model.fc",
overwrite_experiment=True, ## will still use what already exists if generation params are same
)
from dora import SignalDataset
data = SignalDataset('/Users/kirillbykov/Documents/GitHub/dora/.dora/sAMS/model.fc',
k = 2,
n = 5,
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
)
#
#
# A = torch.zeros([2, 5, 2, 2])
# with torch.no_grad():
# for i in data:
# print(i[0].shape)
# r = i[1][0] - 5
# s = i[1][1]
# sign = 0 if i[1][2] == "+" else 1
# out = model(i[0].view([1,3,224,224]))[0][5:7]
# A[r, s, sign, :] = out
#
# print(A)
#
# from dora import compute_distance
#
# print(compute_distance(A))
#
# d.collect_encodings(layer=model.avgpool, experiment_name="model.avgpool")
#
# result = d.run_outlier_detection(
# experiment_name="model.avgpool",
# neuron_idx=neuron_indices,
# method="PCA",
# outliers_fraction=0.1,
# )
#
# print(result.embeddings.shape) ## shape:[len(neuron_idx), 2]
# print(result.outlier_neuron_idx) ## list of neuron indices which were outliers
#
# ## runs an interactive dash app on http://127.0.0.1:8050/
# result.visualize()
#
# ## get outliers
# outliers = result.get_outlier_neurons()
# print(outliers)
#
# ## get normal neurons
# normal_neurons = result.get_outlier_neurons()
# print(normal_neurons)