-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
62 lines (50 loc) · 1.86 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
import time
import os
import sys
import torch
from dataloader.transformers import Rescale
from model.lanenet.LaneNet import LaneNet
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torchvision import transforms
from model.utils.cli_helper_test import parse_args
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 load_test_data(img_path, transform):
img = Image.open(img_path)
img = transform(img)
return img
def test():
if os.path.exists('test_output') == False:
os.mkdir('test_output')
args = parse_args()
img_path = args.img
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])
])
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)
dummy_input = load_test_data(img_path, data_transform).to(DEVICE)
dummy_input = torch.unsqueeze(dummy_input, dim=0)
outputs = model(dummy_input)
input = Image.open(img_path)
input = input.resize((resize_width, resize_height))
input = np.array(input)
instance_pred = torch.squeeze(outputs['instance_seg_logits'].detach().to('cpu')).numpy() * 255
binary_pred = torch.squeeze(outputs['binary_seg_pred']).to('cpu').numpy() * 255
cv2.imwrite(os.path.join('test_output', 'input.jpg'), input)
cv2.imwrite(os.path.join('test_output', 'instance_output.jpg'), instance_pred.transpose((1, 2, 0)))
cv2.imwrite(os.path.join('test_output', 'binary_output.jpg'), binary_pred)
if __name__ == "__main__":
test()