-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
63 lines (47 loc) · 1.59 KB
/
predict.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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
import torch
import cv2
import numpy as np
from torch.autograd import Variable
import torchvision.transforms as transforms
import models
import utils
import data
import random
def main():
use_cuda = torch.cuda.is_available()
path = os.path.expanduser('~/codedata/seg/')
dataset = data.VOCClassSeg(root=path,
split='val.txt',
transform=True)
model = models.FCN8(path)
model.load('SBD.pth')
model.eval()
if use_cuda:
model.cuda()
criterion = utils.CrossEntropyLoss2d(size_average=False, ignore_index=255)
for i in range(len(dataset)):
idx = random.randrange(0, len(dataset))
img, label = dataset[idx]
img_name = str(i)
img_src, _ = dataset.untransform(img, label)
cv2.imwrite(path + 'image/%s_src.jpg' % img_name, img_src)
utils.tool.labelTopng(label, path + 'image/%s_label.png' % img_name)
print(img_name)
if use_cuda:
img = img.cuda()
label = label.cuda()
img = Variable(img.unsqueeze(0), volatile=True)
label = Variable(label.unsqueeze(0), volatile=True)
out = model(img)
loss = criterion(out, label)
print('loss:', loss.data[0])
label = out.data.max(1)[1].squeeze_(1).squeeze_(0)
if use_cuda:
label = label.cpu()
utils.tool.labelTopng(label, path + 'image/%s_out.png' % img_name)
if i == 10:
break
if __name__ == '__main__':
main()