-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_handin.py
72 lines (50 loc) · 1.88 KB
/
test_handin.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
import os
import torch
import parser
import models
import data
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from mean_iou_evaluate import mean_iou_score, read_masks
import torchvision.utils
from PIL import Image
from sklearn.metrics import accuracy_score
def evaluate_save(model, data_loader):
''' set model to evaluate mode '''
model.eval()
preds = []
with torch.no_grad(): # do not need to caculate information for gradient during eval
for idx, (imgs, filename) in enumerate(data_loader):
imgs = imgs.cuda()
pred = model(imgs)
_, pred = torch.max(pred, dim=1)
pred = pred.cpu().numpy().squeeze()
save_imgs(pred, filename)
def save_imgs(imglist, filename):
n = 0
for i in imglist:
# torchvision.utils.save_image(i, "./outputfiles/" + filename[n])
result = Image.fromarray((i).astype(np.uint8))
result.save(os.path.join(args.save_dir + filename[n]))
n +=1
if __name__ == '__main__':
args = parser.arg_parse()
''' setup GPU '''
torch.cuda.set_device(args.gpu)
''' prepare data_loader '''
print('===> prepare data loader ...')
test_loader = torch.utils.data.DataLoader(data.DataLoaderPredict(args, mode='val'),
batch_size=args.test_batch,
num_workers=args.workers,
shuffle=True)
''' prepare mode '''
model = models.Net(args).cuda()
model_std = torch.load('./log/model_best.pth.tar',map_location='cuda:0')
model.load_state_dict(model_std)
''' resume save model '''
checkpoint = torch.load("./log/model_best.pth.tar",map_location='cuda:0')
model.load_state_dict(checkpoint)
evaluate_save(model, test_loader)
print("finished")