-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest.py
137 lines (102 loc) · 3.53 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
import time
import os
import math
import argparse
from glob import glob
from collections import OrderedDict
import random
import warnings
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from skimage.io import imread, imsave
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
import torchvision
from torchvision import datasets, models, transforms
from dataset import Dataset
import archs
from metrics import dice_coef, batch_iou, mean_iou, iou_score
import losses
from utils import str2bool, count_params
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--name', default=None,
help='model name')
args = parser.parse_args()
return args
def main():
val_args = parse_args()
args = joblib.load('models/%s/args.pkl' %val_args.name)
if not os.path.exists('output/%s' %args.name):
os.makedirs('output/%s' %args.name)
print('Config -----')
for arg in vars(args):
print('%s: %s' %(arg, getattr(args, arg)))
print('------------')
joblib.dump(args, 'models/%s/args.pkl' %args.name)
# create model
print("=> creating model %s" %args.arch)
model = archs.__dict__[args.arch](args)
model = model.cuda()
# Data loading code
img_paths = glob('input/' + args.dataset + '/images/*')
mask_paths = glob('input/' + args.dataset + '/masks/*')
train_img_paths, val_img_paths, train_mask_paths, val_mask_paths = \
train_test_split(img_paths, mask_paths, test_size=0.2, random_state=41)
model.load_state_dict(torch.load('models/%s/model.pth' %args.name))
model.eval()
val_dataset = Dataset(args, val_img_paths, val_mask_paths)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
pin_memory=True,
drop_last=False)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
with torch.no_grad():
for i, (input, target) in tqdm(enumerate(val_loader), total=len(val_loader)):
input = input.cuda()
target = target.cuda()
# compute output
if args.deepsupervision:
output = model(input)[-1]
else:
output = model(input)
output = torch.sigmoid(output).data.cpu().numpy()
img_paths = val_img_paths[args.batch_size*i:args.batch_size*(i+1)]
for i in range(output.shape[0]):
imsave('output/%s/'%args.name+os.path.basename(img_paths[i]), (output[i,0,:,:]*255).astype('uint8'))
torch.cuda.empty_cache()
# IoU
ious = []
for i in tqdm(range(len(val_mask_paths))):
mask = imread(val_mask_paths[i])
pb = imread('output/%s/'%args.name+os.path.basename(val_mask_paths[i]))
mask = mask.astype('float32') / 255
pb = pb.astype('float32') / 255
'''
plt.figure()
plt.subplot(121)
plt.imshow(mask)
plt.subplot(122)
plt.imshow(pb)
plt.show()
'''
iou = iou_score(pb, mask)
ious.append(iou)
print('IoU: %.4f' %np.mean(ious))
if __name__ == '__main__':
main()