-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_miou.py
251 lines (208 loc) · 9.64 KB
/
test_miou.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import sys
sys.path.append('../')
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
import argparse
import gc
import os
import torch
import torchvision
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import glob
import numpy as np
from PIL import Image
import json
from torchvision import transforms
from Posterior.fcn import *
import random
class ImageLabelDataset(Dataset):
def __init__(
self,
img_path_list,
label_path_list,
mode,
img_size=(128, 128),
):
self.img_path_list = img_path_list
self.label_path_list = label_path_list
self.img_size = img_size
self.mode = mode
def __len__(self):
return len(self.img_path_list)
def __getitem__(self, index):
im_path = self.img_path_list[index]
lbl_path = self.label_path_list[index]
im = np.array(Image.open(im_path))
try:
lbl = np.load(lbl_path)
except:
lbl = np.array(Image.open(lbl_path))
if len(lbl.shape) == 3:
lbl = lbl[:, :, 0]
xmin, xmax, ymin, ymax = np.where(lbl!=0)[0].min(), np.where(lbl!=0)[0].max(), np.where(lbl!=0)[1].min(), np.where(lbl!=0)[1].max()
im_new = im[max(0,xmin-20):min(xmax+20,im.shape[0]),max(0,ymin-20):min(ymax+20, im.shape[1])]
lbl = lbl[max(0,xmin-20):min(xmax+20,im.shape[0]),max(0,ymin-20):min(ymax+20, im.shape[1])]
im = Image.fromarray(im_new)
if len(np.unique(lbl))==1:
print(im_path)
lbl = Image.fromarray(lbl.astype('uint8'))
im, lbl = self.transform(im, lbl)
return im, lbl, im_path
def transform(self, img, lbl):
jitter = random.random() < 0.5
if jitter and self.mode==True:
img = torchvision.transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0)(img)
hflip = random.random() < 0.5
if hflip and self.mode==True:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
lbl = lbl.transpose(Image.FLIP_LEFT_RIGHT)
lbl = np.array(lbl)
lbl_new = lbl.copy()
lbl_new[np.where(lbl==4)] = 5
lbl_new[np.where(lbl==6)] = 7
lbl_new[np.where(lbl==8)] = 9
lbl_new[np.where(lbl==5)] = 4
lbl_new[np.where(lbl==7)] = 6
lbl_new[np.where(lbl==9)] = 8
lbl = Image.fromarray(lbl_new)
img = img.resize((self.img_size[0], self.img_size[1]))
lbl = lbl.resize((self.img_size[0], self.img_size[1]), resample=Image.NEAREST)
lbl = torch.from_numpy(np.array(lbl)).long()
img = transforms.ToTensor()(img)
return img, lbl
def cross_validate(cp_path, args):
cp_list = [cp_path]
data_all = glob.glob(args.data_images + "/*")
data_all_labels = glob.glob(args.data_parts + "/*")
images = [path for path in data_all if 'npy' not in path]
labels = [path for path in data_all_labels if 'npy' not in path]
images.sort()
labels.sort()
ids = range(11)
fold_num =int( len(images) / 5)
resnet_transform = torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
classifier = fcn(pretrained=False, nparts=11)
classifier.decoder[3] = UpBlock(64, 64, upsample=True)
classifier.decoder.add_module("fc",nn.Conv2d(64,11,1,1))
cross_mIOU = []
for i in range(5):
val_image = images[fold_num * i: fold_num *i + fold_num]
val_label = labels[fold_num * i: fold_num *i + fold_num]
test_image = [img for img in images if img not in val_image]
test_label =[label for label in labels if label not in val_label]
print("Val Data length,", str(len(val_image)))
print("Testing Data length,", str(len(test_image)))
val_data = ImageLabelDataset(img_path_list=val_image,
label_path_list=val_label, mode=False,
img_size=(256, 256))
val_data = DataLoader(val_data, batch_size=1, shuffle=False, num_workers=0)
test_data = ImageLabelDataset(img_path_list=test_image,
label_path_list=test_label, mode=False,
img_size=(256, 256))
test_data = DataLoader(test_data, batch_size=1, shuffle=False, num_workers=0)
best_miou = 0
best_val_miou = 0
criterion = nn.CrossEntropyLoss().cuda()
# import pdb;pdb.set_trace()
for resume in cp_list:
checkpoint = torch.load(resume)
classifier.load_state_dict(checkpoint['model_state_dict'])
classifier.cuda()
classifier.eval()
unions = {}
intersections = {}
for target_num in ids:
unions[target_num] = 0
intersections[target_num] = 0
loss = []
with torch.no_grad():
for _, da, in enumerate(val_data):
img, mask = da[0], da[1]
if img.size(1) == 4:
img = img[:, :-1, :, :]
img = img.cuda()
mask = mask.cuda()
input_img_tensor = []
for b in range(img.size(0)):
input_img_tensor.append(resnet_transform(img[b]))
input_img_tensor = torch.stack(input_img_tensor)
y_pred = classifier(input_img_tensor)#['out']
los = criterion(y_pred, mask)
loss.append(los.item())
y_pred = torch.log_softmax(y_pred, dim=1)
_, y_pred = torch.max(y_pred, dim=1)
y_pred = y_pred.cpu().detach().numpy()
mask = mask.cpu().detach().numpy()
bs = y_pred.shape[0]
curr_iou = []
for target_num in ids:
y_pred_tmp = (y_pred == target_num).astype(int)
mask_tmp = (mask == target_num).astype(int)
intersection = (y_pred_tmp & mask_tmp).sum()
union = (y_pred_tmp | mask_tmp).sum()
unions[target_num] += union
intersections[target_num] += intersection
if not union == 0:
curr_iou.append(intersection / union)
mean_ious = []
for target_num in ids:
mean_ious.append(intersections[target_num] / (1e-8 + unions[target_num]))
mean_iou_val = np.array(mean_ious).mean()
if mean_iou_val > best_val_miou:
best_val_miou = mean_iou_val
unions = {}
intersections = {}
for target_num in ids:
unions[target_num] = 0
intersections[target_num] = 0
with torch.no_grad():
for _, da, in enumerate(test_data):
img, mask = da[0], da[1]
if img.size(1) == 4:
img = img[:, :-1, :, :]
img = img.cuda()
mask = mask.cuda()
input_img_tensor = []
for b in range(img.size(0)):
input_img_tensor.append(resnet_transform(img[b]))
input_img_tensor = torch.stack(input_img_tensor)
y_pred = classifier(input_img_tensor)#['out']
y_pred = torch.log_softmax(y_pred, dim=1)
_, y_pred = torch.max(y_pred, dim=1)
y_pred = y_pred.cpu().detach().numpy()
mask = mask.cpu().detach().numpy()
curr_iou = []
for target_num in ids:
y_pred_tmp = (y_pred == target_num).astype(int)
mask_tmp = (mask == target_num).astype(int)
intersection = (y_pred_tmp & mask_tmp).sum()
union = (y_pred_tmp | mask_tmp).sum()
unions[target_num] += union
intersections[target_num] += intersection
if not union == 0:
curr_iou.append(intersection / union)
img = img.cpu().numpy()
img = img * 255.
img = np.transpose(img, (0, 2, 3, 1)).astype(np.uint8)
test_mean_ious = []
for target_num in ids:
test_mean_ious.append(intersections[target_num] / (1e-8 + unions[target_num]))
best_test_miou = np.array(test_mean_ious).mean()
print("Best IOU ,", str(best_test_miou), "CP: ", resume)
cross_mIOU.append(best_test_miou)
print(cross_mIOU)
print(" cross validation mean:" , np.mean(cross_mIOU) )
print(" cross validation std:", np.std(cross_mIOU))
result = {"Cross validation mean": np.mean(cross_mIOU), "Cross validation std": np.std(cross_mIOU), "Cross validation":cross_mIOU }
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--resume', type=str, default="")
parser.add_argument('--data_images', metavar='DIR',
help='path to images')
parser.add_argument('--data_parts', metavar='DIR',
help='path to parts')
args = parser.parse_args()
cross_validate(args.resume, args)