-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
59 lines (47 loc) · 1.96 KB
/
eval.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
import torch
import argparse
from tqdm import tqdm
from torch.cuda.amp import autocast
from utils.data.dataloader import create_dataloader
from utils.misc import load_config, build_model
from utils.metrics import ConfusionMatrix
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--cfg', type=str, required=True,
help="config file")
parser.add_argument('--csv', type=str, required=True,
help="dataset CSV file")
parser.add_argument('--pth', type=str, required=True,
help="checkpoint")
parser.add_argument('--workers', type=int, default=4,
help="number of dataloader workers")
parser.add_argument('--no_amp', action='store_true',
help="disable automatic mix precision")
args = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
cfg = load_config(args.cfg)
model = build_model(cfg)
model.to(device)
model.eval()
model.load_state_dict(torch.load(args.pth)['model_state_dict'])
dataloader = create_dataloader(args.csv,
batch_size=cfg.batch_size,
image_size=cfg.input_size,
num_workers=args.workers)
metric = ConfusionMatrix(cfg.num_classes)
metric.reset()
pbar = tqdm(dataloader, bar_format="{l_bar}{bar:30}{r_bar}")
with torch.no_grad():
for (images, annos) in pbar:
images = images.to(device)
annos = annos.to(device)
with autocast(enabled=(not args.no_amp)):
logits = model(images)
preds = torch.argmax(logits, axis=1)
metric.update(preds, annos)
mIoU = metric.IoUs.mean()
accuracy = metric.accuracy
print("mIoU: %.3f, accuracy: %.3f" % (mIoU, accuracy))
if __name__ == '__main__':
main()