-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
245 lines (188 loc) · 8.51 KB
/
utils.py
File metadata and controls
245 lines (188 loc) · 8.51 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import json
import logging.config
import os
import re
import socket
from functools import cache
from typing import List, Dict, Any, Union, OrderedDict
import torch
import wandb
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator, ScalarEvent
from torch import Tensor
from torch.backends import cudnn as cudnn
from torch.nn import functional as F
from config import CONFIG
from nets import make_net
from datasets import load_dataset
CONFIG_FILE_NAME = 'config.json'
SUMMARY_FILE_NAME = 'summary_metrics.json'
def activation_loss(device: torch.device, lowp_bits: float,
highp_activations: List[torch.Tensor],
lowp_activations: List[torch.Tensor]) -> torch.Tensor:
loss = torch.as_tensor(0.0, device=device)
for ha, la in zip(highp_activations, lowp_activations):
shrink_coeff = (torch.max(torch.abs(ha)) / (2 ** lowp_bits - 1)).detach()
loss += torch.norm(F.softshrink(ha - la, shrink_coeff.item())) ** 2
return loss * (1.0 / len(highp_activations))
def test_net(net: torch.nn.Module, device: torch.device, data_loader: torch.utils.data.DataLoader) -> float:
correct = 0.0
net.eval()
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(data_loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
if len(outputs) == 2:
outputs, _ = outputs
_, predicted = outputs.max(1)
correct += predicted.eq(targets).sum().item()
return correct / len(data_loader.dataset)
def load_net_from(config, classes: int, device,
return_layer_activations: Union[bool, str] = True, transform_net: bool = False):
results_dir = config['results_dir']
name = config['net']
net_filename = os.path.join(results_dir, name + '.pth')
state_dict = torch.load(net_filename, map_location=device)
if transform_net:
new_state_dict = {}
for key in state_dict.keys():
if 'act3' in key:
continue
if re.search(r'\.conv\d$', key) and '_weight' not in key:
new_state_dict[key + '_weight'] = state_dict[key]
else:
new_state_dict[key] = state_dict[key]
else:
new_state_dict = state_dict
net = make_net(arch=name, classes=classes,
stable=config['stable'],
stability_coeff=config['stability_coeff'],
quantize_weights=config['quantize_weights'],
quantize_activations=config['quantize_activations'],
return_layer_activations=return_layer_activations).to(device)
net.load_state_dict(new_state_dict)
if device == 'cuda':
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
return net
def load_test_dataset_for(config) -> torch.utils.data.DataLoader:
dataset = config['dataset']
dataset_dir = config['dataset_dir']
batch_size = config['batch_size']
train_part, val_part = config['train_val_split']
num_workers = config['num_workers']
return load_test_dataset(dataset, dataset_dir, batch_size, train_part, val_part, num_workers)
@cache
def load_test_dataset(dataset, dataset_dir, batch_size, train_part, val_part, num_workers):
dataset_dir = os.path.join(dataset_dir, dataset)
_, _, test_loader, classes = load_dataset(dataset, batch_size, train_part, val_part,
num_workers, data_root=dataset_dir, test_only=True)
return test_loader, classes
def load_experiment(experiment_path: str) -> Dict:
config_file = os.path.join(experiment_path, CONFIG_FILE_NAME)
if not os.path.isfile(config_file):
raise ValueError(f'Invalid experiment specification {experiment_path}')
with open(config_file, 'r') as f:
return json.load(f)
def save_experiment(config: Dict, experiment_path: str) -> None:
config_file = os.path.join(experiment_path, CONFIG_FILE_NAME)
os.makedirs(experiment_path, exist_ok=True)
with open(config_file, 'w') as f:
json.dump(config, f)
def update_global_config(cmd_line_opts):
if 'experiment_label' not in cmd_line_opts:
raise ValueError('experiment_label must be set!')
CONFIG.update(cmd_line_opts)
if 'results_dir' not in cmd_line_opts:
CONFIG['results_dir'] = os.path.join(CONFIG['checkpoint_dir'], CONFIG['net_type'],
CONFIG['net'], CONFIG['experiment_type'],
CONFIG['dataset'], CONFIG['experiment_label'])
def load_summary(experiment_path: str) -> Dict[str, Any]:
summary_file = os.path.join(experiment_path, SUMMARY_FILE_NAME)
return json.load(open(summary_file, 'r'))
def save_summary(summary: Dict[str, Any], experiment_path: str) -> None:
summary_file = os.path.join(experiment_path, SUMMARY_FILE_NAME)
os.makedirs(experiment_path, exist_ok=True)
json.dump(summary, open(summary_file, 'w'))
def init_wandb():
wandb.init(project=CONFIG['wandb_project_name'], entity=CONFIG['wandb_entity'],
name=CONFIG['experiment_label'], sync_tensorboard=True,
config=CONFIG,
config_exclude_keys=['pretrained_path', 'imagenet',
'gil_imagenet_debug', 'cityscapes',
'save_statistics', 'retrain', 'save_model',
'get_stats', 'pretrained', 'activation_num',
'num_gpu', 'CUDA_VISIBLE_DEVICES'])
def upload_summary_to_wandb(mode, summary, w):
for run in w.runs(mode['wandb_project_name']):
if run.name == mode['experiment_label']:
run.summary['test/acc'] = summary['test_acc']
run.summary.update()
def load_tb_metrics(experiment_path: str) -> (List[float], List[float], List[float], List[float]):
event_acc = EventAccumulator(experiment_path)
event_acc.Reload()
train_accs = extract_values(event_acc.Scalars('train/acc'))
train_losses = extract_values(event_acc.Scalars('train/loss'))
val_accs = extract_values(event_acc.Scalars('val/acc'))
val_losses = extract_values(event_acc.Scalars('val/loss'))
return train_accs, train_losses, val_accs, val_losses,
def transform_state_dict(state_dict: OrderedDict[str, Tensor], device: torch.device) -> OrderedDict[str, Tensor]:
# Remove module if no DataParallel
if device != 'cuda':
state_dict = {key.replace('module.', ''): value for key, value in state_dict.items()}
return state_dict
def glob_dirs(glob_pattern: str):
for dir_path, dir_names, _ in os.walk(glob_pattern):
if not dir_names:
yield dir_path
def extract_values(series: List[ScalarEvent]) -> List[float]:
return [t.value for t in series]
def init_logger(log_file: str):
logging.config.dictConfig(get_logger_config_dic(log_file))
def get_logger_config_dic(log_file: str):
log_dir = os.path.dirname(os.path.abspath(log_file))
os.makedirs(log_dir, exist_ok=True)
return {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "%(asctime)s.%(msecs)03d; %(levelname)s; [%(name)s %(lineno)s]; {}; %(message)s".format(
socket.gethostname()),
'datefmt': "%Y-%m-%d %H:%M:%S",
},
'simple': {
'format': '%(asctime)s.%(msecs)03d; %(levelname)s; [%(name)s %(lineno)s]; %(message)s',
'datefmt': "%Y-%m-%d %H:%M:%S",
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'rotating_file': {
'level': 'INFO',
'formatter': 'verbose',
'class': 'logging.FileHandler',
'filename': log_file
},
'rotating_file_debug': {
'level': 'DEBUG',
'formatter': 'verbose',
'class': 'logging.FileHandler',
'filename': f"{log_file}.debug"
},
},
"root": {
"level": "DEBUG",
"handlers": ["console", "rotating_file", "rotating_file_debug"]
},
"loggers": {
"PIL": {
"level": "CRITICAL",
"handlers": ["console", "rotating_file", "rotating_file_debug"],
"propagate": False
}
}
}