-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_rpr.py
executable file
·49 lines (39 loc) · 1.45 KB
/
get_rpr.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
import os
import argparse
import numpy as np
import torch
import torch.nn as nn
import torchvision.datasets as dst
import torchvision.transforms as tfs
from torch.utils.data import DataLoader
import model
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--target_layer', type=str, default='22')
parser.add_argument('--batchSize', type=int, default=2500)
params = parser.parse_args()
model_path = 'models/vgg16.pth'
data_dir = 'data/cifar10/'
resp_dir = 'resps/vgg16/'
if not os.path.exists(resp_dir):
os.makedirs(resp_dir)
device = torch.device('cuda')
# model
model1 = model.VGG()
model1.load_state_dict(torch.load(model_path))
model1.to(device)
model1.eval()
model2 = model.VGG2(model1, params.target_layer)
for state in ['test', 'train']:
# data
transform = tfs.Compose([tfs.ToTensor()])
data = dst.CIFAR10(data_dir, download=True,
train=(state=='train'),
transform=transform)
dataloader = DataLoader(data, batch_size=params.batchSize, shuffle=False)
with torch.no_grad():
for counter, (data, target) in enumerate(dataloader):
y = model2.forward(data.to(device))
fname = resp_dir + state + '_' + str(params.target_layer) + \
'_' + str(counter)
np.save(fname, y.detach().cpu().numpy())