-
Notifications
You must be signed in to change notification settings - Fork 10
/
compute_descriptors.py
140 lines (113 loc) · 4.06 KB
/
compute_descriptors.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
import argparse
import os
import torch
import configs
from functions.loss.pca import PCALoss
from models import get_backbone
from scripts.train_helper import prepare_dataloader
torch.multiprocessing.set_sharing_strategy('file_system')
torch.backends.cudnn.benchmark = True
parser = argparse.ArgumentParser()
parser.add_argument('--ds', default='cifar10', help='dataset')
parser.add_argument('--c10-ep', default=1, type=int)
parser.add_argument('--backbone', default='alexnet', help='backbone')
parser.add_argument('--savedir', required=True, help='save to')
parser.add_argument('--device', default='cuda:0')
parser.add_argument('--bs', default=64, type=int)
parser.add_argument('--pca-output', default=False, action='store_true')
parser.add_argument('--pca-dim', default=128, type=int)
args = parser.parse_args()
proceed = True
if os.path.exists(args.savedir):
proceed = False
if not proceed:
inp = input('Folder exists. Proceed and overwrite? (y/n)')
if inp == 'y':
proceed = True
else:
exit()
dataset_config = {
'arch': '',
'batch_size': args.bs,
'max_batch_size': args.bs,
'dataset': args.ds,
'dataset_kwargs': {
'no_augmentation': True, # turn off augmentation
'resize': configs.imagesize(args.ds),
'crop': configs.cropsize(args.ds),
'norm': 2,
'evaluation_protocol': args.c10_ep
},
'arch_kwargs': {
'nclass': configs.nclass(args.ds)
}
}
train_loader, test_loader, db_loader = prepare_dataloader(dataset_config,
train_shuffle=False,
test_shuffle=False,
gpu_transform=False,
gpu_mean_transform=False,
include_train=True,
train_drop_last=False,
workers=os.cpu_count())
data_structure = {
'train.txt': {
'codes': [],
'labels': []
},
'test.txt': {
'codes': [],
'labels': []
},
'database.txt': {
'codes': [],
'labels': []
}
}
loaders = {
'train.txt': train_loader,
'test.txt': test_loader,
'database.txt': db_loader,
}
backbone = get_backbone(backbone=args.backbone,
nbit=64, # nbit and nclass will be ignored
nclass=configs.nclass(args.ds),
pretrained=True,
freeze_weight=True)
backbone.eval()
print(backbone)
device = torch.device(args.device)
backbone.to(device)
if args.pca_output:
print('PCA enabled')
pca = PCALoss(args.pca_dim)
else:
pca = None
for filename in loaders:
print(f'Filename: {filename}')
loader = loaders[filename]
for i, (data, labels, index) in enumerate(loader):
print(f'Computing [{i}/{len(loader)}]', end='\r')
data = data.to(device)
with torch.no_grad():
codes = backbone(data)
if pca is not None and filename != 'train.txt':
pca.eval()
codes = pca(codes)
data_structure[filename]['codes'].append(codes.cpu())
data_structure[filename]['labels'].append(labels)
print()
data_structure[filename]['codes'] = torch.cat(data_structure[filename]['codes'])
data_structure[filename]['labels'] = torch.cat(data_structure[filename]['labels'])
if pca is not None and filename == 'train.txt':
print('PCA training')
pca.train()
data_structure[filename]['codes'] = pca(data_structure[filename]['codes'])[0]
print(f'Total number of data: {len(data_structure[filename]["codes"])}')
os.makedirs(args.savedir, exist_ok=True)
for filename in data_structure:
saveto = args.savedir + '/' + filename
torch.save(data_structure[filename], saveto)
fsize = os.stat(saveto).st_size / (1024 * 1024) # bytes -> Mbytes
print(saveto)
print(f'Filesize: {fsize:.4f} MB')