-
Notifications
You must be signed in to change notification settings - Fork 43
/
validate.py
103 lines (82 loc) · 2.94 KB
/
validate.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
import argparse
import os
import numpy as np
import torch.utils.data
from PIL import Image, ImageDraw
from torch import nn
from torch.autograd import Variable
from config_tools import get_config
from data import load_data
from model import PolygonNet
from utils.utils import iou, label2vertex
from utils.utils import tensor2img
def validate(net, Dataloader):
'''
Test on validation dataset
:param net: net to evaluate
:param Dataloader: data to evaluate
:return:
'''
dtype = torch.cuda.FloatTensor
dtype_t = torch.cuda.LongTensor
dir_name = 'save_img/validate/'
if not os.path.exists(dir_name):
os.makedirs(dir_name)
len_dl = len(Dataloader)
print(len_dl)
nu = 0
de = 0
for step, data in enumerate(Dataloader):
labels = data[4].numpy()
xx = Variable(data[0].type(dtype))
re = net.module.test(xx, 60)
for i in range(len(re)):
labels_p = re.cpu().numpy()[i]
vertices1 = label2vertex(labels_p)
vertices2 = label2vertex(labels[i])
color = [np.random.randint(0, 255) for _ in range(3)]
color += [100]
color = tuple(color)
img_array = tensor2img(data[0][i])
img = Image.fromarray(img_array)
drw = ImageDraw.Draw(img, 'RGBA')
drw.polygon(vertices1, color)
img.save(
dir_name + str(step) + '_' + str(i) + '_pred.png',
'PNG')
img = Image.fromarray(img_array)
drw = ImageDraw.Draw(img, 'RGBA')
drw.polygon(vertices2, color)
img.save(
dir_name + str(step) + '_' + str(i) + '_gt.png',
'PNG')
_, nu_this, de_this = iou(vertices1, vertices2, 224, 224)
nu += nu_this
de += de_this
print('iou: {}'.format(nu * 1.0 / de))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--gpu_id', nargs='+', type=int)
parser.add_argument('--batch_size', type=int)
parser.add_argument('--num', type=int, )
parser.add_argument('--model', type=str)
parser.add_argument('--dataset', type=str)
parser.add_argument('--config', dest='config_file', help='Config File')
args = parser.parse_args()
config_from_args = args.__dict__
config_file = config_from_args.pop('config_file')
config = get_config('val', config_from_args, config_file)
devices = config['gpu_id']
batch_size = config['batch_size']
num = config['num']
dataset = config['dataset']
model = config['model']
print('gpus: {}'.format(devices))
torch.cuda.set_device(devices[0])
net = PolygonNet(load_vgg=False)
net = nn.DataParallel(net, device_ids=devices)
net.load_state_dict(torch.load(model))
net.cuda()
print('Loading completed!')
Dataloader = load_data(num, dataset, 600, batch_size)
validate(net, Dataloader)