-
Notifications
You must be signed in to change notification settings - Fork 86
/
test.py
71 lines (57 loc) · 2.42 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
import argparse
import better_exceptions
from pathlib import Path
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
from torch.utils.data import DataLoader
import pretrainedmodels
import pretrainedmodels.utils
from model import get_model
from dataset import FaceDataset
from defaults import _C as cfg
from train import validate
def get_args():
model_names = sorted(name for name in pretrainedmodels.__dict__
if not name.startswith("__")
and name.islower()
and callable(pretrainedmodels.__dict__[name]))
parser = argparse.ArgumentParser(description=f"available models: {model_names}",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--data_dir", type=str, required=True, help="Data root directory")
parser.add_argument("--resume", type=str, required=True, help="Model weight to be tested")
parser.add_argument("opts", default=[], nargs=argparse.REMAINDER,
help="Modify config options using the command-line")
args = parser.parse_args()
return args
def main():
args = get_args()
if args.opts:
cfg.merge_from_list(args.opts)
cfg.freeze()
# create model
print("=> creating model '{}'".format(cfg.MODEL.ARCH))
model = get_model(model_name=cfg.MODEL.ARCH, pretrained=None)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
# load checkpoint
resume_path = args.resume
if Path(resume_path).is_file():
print("=> loading checkpoint '{}'".format(resume_path))
checkpoint = torch.load(resume_path, map_location="cpu")
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}'".format(resume_path))
else:
raise ValueError("=> no checkpoint found at '{}'".format(resume_path))
if device == "cuda":
cudnn.benchmark = True
test_dataset = FaceDataset(args.data_dir, "test", img_size=cfg.MODEL.IMG_SIZE, augment=False)
test_loader = DataLoader(test_dataset, batch_size=cfg.TEST.BATCH_SIZE, shuffle=False,
num_workers=cfg.TRAIN.WORKERS, drop_last=False)
print("=> start testing")
_, _, test_mae = validate(test_loader, model, None, 0, device)
print(f"test mae: {test_mae:.3f}")
if __name__ == '__main__':
main()