This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
129 lines (107 loc) · 3.68 KB
/
test.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
# -*- coding: utf-8 -*-
import argparse
import os
from typing import Any, Callable, Iterable, List, Optional
import torch
from torch.nn import Module
from torch.utils.data import DataLoader
from tqdm import tqdm
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from train import get_instance
def main(config: dict, resume: Optional[str]):
# Instantiate data loader.
data_loader: DataLoader = getattr(
module_data,
config["data_loader"]["type"]
)(
config["data_loader"]["args"]["data_dir"],
batch_size = 512,
shuffle = False,
validation_split = 0.0,
training = False,
num_workers = 2
)
# Instantiate model and print summary.
model: Module = get_instance(
module_arch, "arch", config, data_loader.dataset.feats
)
model.summary()
# Obtain function handles of loss and metrics.
loss_fn: Callable = getattr(module_loss, config["loss"]["type"])
loss_args: dict = config["loss"]["args"]
metric_fns: List[Callable] = [
getattr(module_metric, met) for met in config["metrics"]
]
metric_args: List[dict] = [
config["metrics"][met] for met in config["metrics"]
]
# Load state dict.
checkpoint: dict = torch.load(resume)
state_dict: dict = checkpoint["state_dict"]
if config["n_gpu"] > 1:
model = torch.nn.DataParallel(model)
model.load_state_dict(state_dict)
# Prepare model for testing.
device: str = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()
total_loss: float = 0.0
total_metrics: torch.Tensor = torch.zeros(len(metric_fns))
with torch.no_grad():
i: int
data: torch.Tensor
target: torch.Tensor
for i, (data, target) in enumerate(tqdm(data_loader)):
data, target = data.to(device), target.to(device)
output: torch.Tensor = model(data)
#
# save sample images, or do something with output here
#
# computing loss, metrics on test set
loss: torch.Tensor = loss_fn(output, target, **loss_args)
batch_size: int = data.shape[0]
total_loss += loss.item() * batch_size
j: int
metric: Callable
for j, metric in enumerate(metric_fns):
if metric.__name__ == "adj_rsqr":
total_metrics[j] += metric(
output, target, data.shape[1], **metric_args[j]
) * batch_size
else:
total_metrics[j] += metric(
output, target, **metric_args[j]
) * batch_size
n_samples: int = len(data_loader.sampler)
log: dict = {"loss": total_loss / n_samples}
log.update({
met.__name__: total_metrics[i].item()
/ n_samples for i, met in enumerate(metric_fns)
})
print(log)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = "Michael's adapted PyTorch Template -- Testing")
parser.add_argument(
"-r",
"--resume",
default = None,
type = str,
help = "Latest PTH checkpoint file path (default: None)"
)
parser.add_argument(
"-d",
"--device",
default = None,
type = str,
help = "Indices of GPUs to enable (default: all)"
)
args = parser.parse_args()
if args.resume:
config = torch.load(args.resume)["config"]
if args.device:
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
main(config, args.resume)