-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
290 lines (241 loc) · 10.8 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
import argparse
import torch
from torch import nn
import torch.backends.cudnn as cudnn
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import DataLoader
import numpy as np
from models.hrnet import HRNet
from models.danet import DANet
from models.deeplabv3_res50 import DeepLabV3 as DeepLabV3_res50
from models.deeplabv3_res101 import DeepLabV3 as DeepLabV3_res101
from dataset import potsdam
from seg_metric import SegmentationMetric
import cv2
from mutil_scale_test import MultiEvalModule_Fullimg
import logging
# os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
os.environ.setdefault('RANK', '0')
os.environ.setdefault('WORLD_SIZE', '1')
os.environ.setdefault('MASTER_ADDR', '127.0.0.1')
os.environ.setdefault('MASTER_PORT', '29555')
def get_world_size():
if not torch.distributed.is_initialized():
return 1
return torch.distributed.get_world_size()
def get_rank():
if not torch.distributed.is_initialized():
return 0
return torch.distributed.get_rank()
def reduce_tensor(inp):
"""
Reduce the loss from all processes so that
process with rank 0 has the averaged results.
"""
world_size = get_world_size()
if world_size < 2:
return inp
with torch.no_grad():
reduced_inp = inp
torch.distributed.reduce(reduced_inp, dst=0)
return reduced_inp
class params():
def __init__(self):
self.number_of_classes = 8
self.TRAIN_BATCH_SIZE_PER_GPU = 64
self.VAL_BATCH_SIZE_PER_GPU = 64
"hrnet48"
self.STAGE2 = {'NUM_MODULES': 1,
'NUM_BRANCHES': 2,
'NUM_BLOCKS': [4,4],
'NUM_CHANNELS': [48,96],
'BLOCK':'BASIC',
'FUSE_METHOD': 'SUM'}
self.STAGE3 = {'NUM_MODULES': 4,
'NUM_BRANCHES': 3,
'NUM_BLOCKS': [4, 4, 4],
'NUM_CHANNELS': [48, 96, 192],
'BLOCK': 'BASIC',
'FUSE_METHOD': 'SUM'}
self.STAGE4 = {'NUM_MODULES': 3,
'NUM_BRANCHES': 4,
'NUM_BLOCKS': [4, 4, 4, 4],
'NUM_CHANNELS': [48, 96, 192, 384],
'BLOCK': 'BASIC',
'FUSE_METHOD': 'SUM'}
def parse_args():
parser = argparse.ArgumentParser(description='Train segmentation network')
parser.add_argument("--local_rank", type=int, default=0)
parser.add_argument("--models", type=str, default='DeepLabV3_res50', choices=['HRNet','DANet','DeepLabV3_res50','DeepLabV3_res101'])
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
args2 = parser.parse_args()
return args2
def get_model():
models = args2.models
assert models in ['HRNet', 'DANet', 'DeepLabV3_res50', 'DeepLabV3_res101']
if models == 'HRNet':
model = HRNet(args)
if models == 'DANet':
model = DANet(nclass=8)
if models == 'DeepLabV3_res50':
model = DeepLabV3_res50(nclass=8)
if models == 'DeepLabV3_res101':
model = DeepLabV3_res101(nclass=8)
model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = model.to(device)
model = nn.parallel.DistributedDataParallel(
model, device_ids=[args2.local_rank], output_device=args2.local_rank)
return model
args = params()
args2 = parse_args()
dataset_dir = "../dataset"
cudnn.benchmark = True
cudnn.deterministic = False
cudnn.enabled = True
distributed = True
device = torch.device(('cuda:{}').format(args2.local_rank))
if distributed:
torch.cuda.set_device(args2.local_rank)
torch.distributed.init_process_group(
backend="nccl", init_method="env://",
)
potsdam_val = potsdam(state='val', mean_std=True)
if distributed:
val_sampler = DistributedSampler(potsdam_val)
else:
val_sampler = None
dataloader_val = DataLoader(
potsdam_val,
batch_size=args.VAL_BATCH_SIZE_PER_GPU,
shuffle=False,
num_workers=4,
pin_memory=True,
sampler=val_sampler)
potsdam_test = potsdam(state='test', mean_std=True)
if distributed:
test_sampler = DistributedSampler(potsdam_test)
else:
test_sampler = None
dataloader_test = DataLoader(
potsdam_test,
batch_size=args.VAL_BATCH_SIZE_PER_GPU,
shuffle=False,
num_workers=4,
pin_memory=True,
sampler=test_sampler)
def val(model):
model.eval()
metric = SegmentationMetric(8)
with torch.no_grad():
if args2.models == 'HRNet':
model_state_file = "weights/NAIC_hrnet48_usually_mean_std_lr0.01125_epoch750_batchsize24_vaihingen3463_xzy_750.pkl"
if args2.models == 'DANet':
model_state_file = "weights/NAIC_danet_cutmix_lr0.0075_epoch700_batchsize16_vaihingen3463_best_result_699.pkl"
if args2.models == 'DeepLabV3_res50':
model_state_file = "weights/NAIC_deeplabv3_attention_lr0.0075_epoch850_batchsize16_vaihingen3463_best_result_849.pkl"
if args2.models == 'DeepLabV3_res101':
model_state_file = "weights/NAIC_deeplabv3_res101_labelsmooth_lr0.01_epoch470_batchsize16_vaihingen3463_best_result_469.pkl"
if os.path.isfile(model_state_file):
print('loading checkpoint successfully')
logging.info("=> loading checkpoint '{}'".format(model_state_file))
checkpoint = torch.load(model_state_file, map_location=lambda storage, loc: storage)
checkpoint = {k: v for k, v in checkpoint.items() if not 'loss' in k}
checkpoint = {k.replace('model.', ''): v for k, v in checkpoint.items()}
model.load_state_dict(checkpoint)
for i, sample in enumerate(dataloader_val):
if args2.models == 'HRNet':
images, labels = sample['image_mean_std'], sample['label']
else:
images, labels = sample['image'], sample['label']
images = images.cuda()
labels = labels.long().squeeze(1)
logits = model(images)
print("test:{}/{}".format(i, len(dataloader_test)))
logits = logits.argmax(dim=1)
logits = logits.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
metric.addBatch(logits, labels)
fwiou = metric.Frequency_Weighted_Intersection_over_Union()
print('FWIOU:{}'.format(fwiou))
fwiou = reduce_tensor(torch.from_numpy(np.array(fwiou)).to(device))
print('FWIOUx:{}'.format(fwiou))
def mutil_scale_val(model):
model = MultiEvalModule_Fullimg(model, nclass=8)
model.eval()
metric = SegmentationMetric(8)
with torch.no_grad():
if args2.models == 'HRNet':
model_state_file = "weights/NAIC_hrnet48_usually_mean_std_lr0.01125_epoch750_batchsize24_vaihingen3463_xzy_750.pkl"
if args2.models == 'DANet':
model_state_file = "weights/NAIC_danet_cutmix_lr0.0075_epoch700_batchsize16_vaihingen3463_best_result_699.pkl"
if args2.models == 'DeepLabV3_res50':
model_state_file = "weights/NAIC_deeplabv3_attention_lr0.0075_epoch850_batchsize16_vaihingen3463_best_result_849.pkl"
if args2.models == 'DeepLabV3_res101':
model_state_file = "weights/NAIC_deeplabv3_res101_labelsmooth_lr0.01_epoch470_batchsize16_vaihingen3463_best_result_469.pkl"
if os.path.isfile(model_state_file):
print('loading checkpoint successfully')
logging.info("=> loading checkpoint '{}'".format(model_state_file))
checkpoint = torch.load(model_state_file, map_location=lambda storage, loc: storage)
checkpoint = {k: v for k, v in checkpoint.items() if not 'loss' in k}
checkpoint = {k.replace('model.', ''): v for k, v in checkpoint.items()}
model.load_state_dict(checkpoint)
for i, sample in enumerate(dataloader_val):
if args2.models == 'HRNet':
images, labels = sample['image_mean_std'], sample['label']
else:
images, labels = sample['image'], sample['label']
images = images.cuda()
labels = labels.long().squeeze(1)
logits = model(images)
print("test:{}/{}".format(i, len(dataloader_test)))
logits = logits.argmax(dim=1)
# logits = method1(logits)
logits = logits.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
metric.addBatch(logits, labels)
fwiou = metric.Frequency_Weighted_Intersection_over_Union()
print('FWIOU:{}'.format(fwiou))
fwiou = reduce_tensor(torch.from_numpy(np.array(fwiou)).to(device))
print('FWIOUx:{}'.format(fwiou))
def test(model):
model = MultiEvalModule_Fullimg(model, nclass=8)
model.eval()
with torch.no_grad():
if args2.models == 'HRNet':
model_state_file = "weights/NAIC_hrnet48_usually_mean_std_lr0.01125_epoch750_batchsize24_vaihingen3463_xzy_750.pkl"
if args2.models == 'DANet':
model_state_file = "weights/NAIC_danet_cutmix_lr0.0075_epoch700_batchsize16_vaihingen3463_best_result_699.pkl"
if args2.models == 'DeepLabV3_res50':
model_state_file = "weights/NAIC_deeplabv3_attention_lr0.0075_epoch850_batchsize16_vaihingen3463_best_result_849.pkl"
if args2.models == 'DeepLabV3_res101':
model_state_file = "weights/NAIC_deeplabv3_res101_labelsmooth_lr0.01_epoch470_batchsize16_vaihingen3463_best_result_469.pkl"
if os.path.isfile(model_state_file):
print('loading checkpoint successfully')
logging.info("=> loading checkpoint '{}'".format(model_state_file))
checkpoint = torch.load(model_state_file, map_location=lambda storage, loc: storage)
checkpoint = {k: v for k, v in checkpoint.items() if not 'loss' in k}
checkpoint = {k.replace('model.', ''): v for k, v in checkpoint.items()}
model.load_state_dict(checkpoint)
for i, sample in enumerate(dataloader_test):
if args2.models == 'HRNet':
images, name = sample['image_mean_std'], sample['name']
else:
images, name = sample['image'], sample['name']
images = images.cuda()
logits = model(images)
print("test:{}/{}".format(i, len(dataloader_test)))
logits = logits.argmax(dim=1)
logits = logits.cpu().detach().numpy().astype(np.uint16)
logits = (logits + 1) * 100
for j in range(logits.shape[0]):
cv2.imwrite("results/" + name[j] + '.png', logits[j])
if __name__ == '__main__':
logging.basicConfig(filename='NAIC_test.log', level=logging.INFO)
model = get_model()
val(model)
# mutil_scale_val(model)
# test(model)