-
Notifications
You must be signed in to change notification settings - Fork 14
/
infer.py
52 lines (38 loc) · 1.41 KB
/
infer.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
import numpy as np
import os
import torch
from PIL import Image
from torch.autograd import Variable
from torchvision import transforms
from config import testing_root
from misc import check_mkdir
from misc import crf_refine
from model import DAF
torch.manual_seed(2018)
torch.cuda.set_device(0)
ckpt_path = './ckpt'
exp_name = 'DAF'
args = {
'snapshot': ''
}
img_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
to_pil = transforms.ToPILImage()
def main():
net = DAF().cuda()
if len(args['snapshot']) > 0:
print('training resumes from ' + args['snapshot'])
net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, args['snapshot'] + '.pth')))
net.eval()
for idx, img_name in enumerate(os.listdir(os.path.join(testing_root, 'us'))):
print 'predicting %d' % (idx + 1)
check_mkdir(os.path.join(ckpt_path, exp_name, 'prediction_' + args['snapshot']))
img = Image.open(os.path.join(testing_root, 'us', img_name)).convert('RGB')
img_var = Variable(img_transform(img).unsqueeze(0)).cuda()
prediction = np.array(to_pil(net(img_var).data.squeeze(0).cpu()))
prediction = crf_refine(np.array(img), prediction)
Image.fromarray(prediction).save(os.path.join(ckpt_path, exp_name, 'prediction_' + args['snapshot'], img_name))
if __name__ == '__main__':
main()