forked from victoresque/pytorch-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
evaluate.py
69 lines (53 loc) · 2.04 KB
/
evaluate.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
import logging
import torch
import hydra
from omegaconf import OmegaConf
from tqdm import tqdm
from srcs.utils import instantiate
logger = logging.getLogger('evaluate')
@hydra.main(config_path='conf', config_name='evaluate')
def main(config):
logger.info('Loading checkpoint: {} ...'.format(config.checkpoint))
checkpoint = torch.load(config.checkpoint)
loaded_config = OmegaConf.create(checkpoint['config'])
# setup data_loader instances
data_loader = instantiate(config.data_loader)
# restore network architecture
model = instantiate(loaded_config.arch)
logger.info(model)
# load trained weights
state_dict = checkpoint['state_dict']
if loaded_config['n_gpu'] > 1:
model = torch.nn.DataParallel(model)
model.load_state_dict(state_dict)
# instantiate loss and metrics
criterion = instantiate(loaded_config.loss, is_func=True)
metrics = [instantiate(met, is_func=True) for met in loaded_config.metrics]
# prepare model for testing
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()
total_loss = 0.0
total_metrics = torch.zeros(len(metrics))
with torch.no_grad():
for i, (data, target) in enumerate(tqdm(data_loader)):
data, target = data.to(device), target.to(device)
output = model(data)
#
# save sample images, or do something with output here
#
# computing loss, metrics on test set
loss = criterion(output, target)
batch_size = data.shape[0]
total_loss += loss.item() * batch_size
for i, metric in enumerate(metrics):
total_metrics[i] += metric(output, target) * batch_size
n_samples = len(data_loader.sampler)
log = {'loss': total_loss / n_samples}
log.update({
met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metrics)
})
logger.info(log)
if __name__ == '__main__':
# pylint: disable=no-value-for-parameter
main()