-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
65 lines (51 loc) · 1.9 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
60
61
62
63
64
65
import time
import os
import sys
import torch
from dataloader.data_loaders import TusimpleSet
from dataloader.transformers import Rescale
from model.lanenet.LaneNet import LaneNet
from torch.utils.data import DataLoader, dataloader
from torch.autograd import Variable
from torchvision import transforms
from model.utils.cli_helper_eval import parse_args
from model.eval_function import Eval_Score
import numpy as np
from PIL import Image
import pandas as pd
import cv2
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def evaluation():
args = parse_args()
resize_height = args.height
resize_width = args.width
data_transform = transforms.Compose([
transforms.Resize((resize_height, resize_width)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
target_transforms = transforms.Compose([
Rescale((resize_width, resize_height)),
])
dataset_file = os.path.join(args.dataset, 'val.txt')
Eval_Dataset = TusimpleSet(dataset_file, transform=data_transform, target_transform=target_transforms)
eval_dataloader = DataLoader(Eval_Dataset, batch_size=1, shuffle=True)
model_path = args.model
model = LaneNet(arch=args.model_type)
state_dict = torch.load(model_path)
model.load_state_dict(state_dict)
model.eval()
model.to(DEVICE)
iou, dice = 0, 0
with torch.no_grad():
for x, target, _ in eval_dataloader:
y = model(x.to(DEVICE))
y_pred = torch.squeeze(y['binary_seg_pred'].to('cpu')).numpy()
y_true = torch.squeeze(target).numpy()
Score = Eval_Score(y_pred, y_true)
dice += Score.Dice()
iou += Score.IoU()
print('Final_IoU: %s'% str(iou/len(eval_dataloader.dataset)))
print('Final_F1: %s'% str(dice/len(eval_dataloader.dataset)))
if __name__ == "__main__":
evaluation()