-
Notifications
You must be signed in to change notification settings - Fork 5
/
entropy.py
193 lines (157 loc) · 7.53 KB
/
entropy.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
##----------------------------------------------------------
# written by Fei Pan
#
# to get the entropy ranking from Inter-domain adaptation process
#-----------------------------------------------------------
import sys
from tqdm import tqdm
import argparse
import os
import os.path as osp
import pprint
import torch
import numpy as np
from PIL import Image
from torch import nn
from torch.utils import data
from advent.model.deeplabv2 import get_deeplab_v2
from advent.model.discriminator import get_fc_discriminator
from advent.dataset.cityscapes import CityscapesDataSet
from advent.utils.func import prob_2_entropy
import torch.nn.functional as F
from advent.utils.func import loss_calc, bce_loss
from advent.domain_adaptation.config import cfg, cfg_from_file
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
#------------------------------------- color -------------------------------------------
palette = [0, 0, 0, 255, 255, 255, 70, 70, 70, 102, 102, 156, 190, 153, 153, 153, 153, 153, 250, 170, 30,
220, 220, 0, 107, 142, 35, 152, 251, 152, 70, 130, 180, 220, 20, 60, 255, 0, 0, 0, 0, 142, 0, 0, 70,
0, 60, 100, 0, 80, 100, 0, 0, 230, 119, 11, 32]
zero_pad = 256 * 3 - len(palette)
for i in range(zero_pad):
palette.append(0)
# The rare classes trainID from cityscapes dataset
# These classes are:
# wall, fence, pole, traffic light, trafflic sign, terrain, rider, truck, bus, train, motor.
rare_class = [3, 4, 5, 6, 7, 9, 12, 14, 15, 16, 17]
def colorize(mask):
# mask: numpy array of the mask
new_mask = Image.fromarray(mask.astype(np.uint8)).convert('P')
new_mask.putpalette(palette)
return new_mask
def colorize_save(output_pt_tensor, name):
# output_np_tensor = output_pt_tensor.cpu().data[0].numpy()
# mask_np_tensor = output_np_tensor.transpose(1,2,0)
# mask_np_tensor = np.asarray(np.argmax(mask_np_tensor, axis=2), dtype=np.uint8)
mask_Img = Image.fromarray(output_pt_tensor)
mask_color = colorize(output_pt_tensor)
name = name.split('.')[0]
mask_Img.save('./color_masks/%s.png' % (name))
mask_color.save('./color_masks/%s_color.png' % (name.split('.')[0]))
def find_rare_class(output_pt_tensor):
output_np_tensor = output_pt_tensor.cpu().data[0].numpy()
mask_np_tensor = output_np_tensor.transpose(1,2,0)
mask_np_tensor = np.asarray(np.argmax(mask_np_tensor, axis=2), dtype=np.uint8)
mask_np_tensor = np.reshape(mask_np_tensor, 512*1024)
unique_class = np.unique(mask_np_tensor).tolist()
commom_class = set(unique_class).intersection(rare_class)
return commom_class
def cluster_subdomain(entropy_list, lambda1):
entropy_list = sorted(entropy_list, key=lambda img: img[1], reverse = True)
copy_list = entropy_list.copy()
entropy_rank = [item[0] for item in entropy_list]
easy_split = entropy_rank[ : int(len(entropy_rank) * lambda1)]
hard_split = entropy_rank[int(len(entropy_rank)* lambda1): ]
with open('easy_split.txt', 'w+') as f:
for item in easy_split:
f.write('%s\n' % item)
with open('hard_split.txt', 'w+') as f:
for item in hard_split:
f.write('%s\n' % item)
return copy_list
def load_checkpoint_for_evaluation(model, checkpoint, device):
saved_state_dict = torch.load(checkpoint)
model.load_state_dict(saved_state_dict)
model.eval()
model.cuda(device)
def get_arguments():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description="Code for evaluation")
parser.add_argument('--best_iter', type=int, default=32000,
help='iteration with best mIoU')
parser.add_argument('--normalize', type=bool, default=False,
help='add normalizor to the entropy ranking')
parser.add_argument('--lambda1', type=float, default=0.7,
help='hyperparameter lambda to split the target domain')
parser.add_argument('--cfg', type=str, default='../ADVENT/advent/scripts/configs/advent.yml',
help='optional config file' )
return parser.parse_args()
def main(args):
# load configuration file
device = cfg.GPU_ID
assert args.cfg is not None, 'Missing cfg file'
cfg_from_file(args.cfg)
if not os.path.exists('./color_masks'):
os.mkdir('./color_masks')
cfg.EXP_NAME = f'{cfg.SOURCE}2{cfg.TARGET}_{cfg.TRAIN.MODEL}_{cfg.TRAIN.DA_METHOD}'
cfg.TEST.SNAPSHOT_DIR[0] = osp.join(cfg.EXP_ROOT_SNAPSHOT, cfg.EXP_NAME)
# load model with parameters trained from Inter-domain adaptation
model_gen = get_deeplab_v2(num_classes=cfg.NUM_CLASSES, multi_level=cfg.TEST.MULTI_LEVEL)
restore_from = osp.join(cfg.TEST.SNAPSHOT_DIR[0], f'model_{args.best_iter}.pth')
print("Loading the generator:", restore_from)
load_checkpoint_for_evaluation(model_gen, restore_from, device)
# load data
target_dataset = CityscapesDataSet(root=cfg.DATA_DIRECTORY_TARGET,
list_path=cfg.DATA_LIST_TARGET,
set=cfg.TRAIN.SET_TARGET,
info_path=cfg.TRAIN.INFO_TARGET,
max_iters=None,
crop_size=cfg.TRAIN.INPUT_SIZE_TARGET,
mean=cfg.TRAIN.IMG_MEAN)
target_loader = data.DataLoader(target_dataset,
batch_size=cfg.TRAIN.ENTROPY_BATCH_SIZE_TARGET,
num_workers=cfg.NUM_WORKERS,
shuffle=True,
pin_memory=True,
worker_init_fn=None)
target_loader_iter = enumerate(target_loader)
# upsampling layer
input_size_target = cfg.TRAIN.INPUT_SIZE_TARGET
interp_target = nn.Upsample(size=(input_size_target[1], input_size_target[0]), mode='bilinear',
align_corners=True)
entropy_list = []
for index in tqdm(range(len(target_loader))):
_, batch = target_loader_iter.__next__()
image, _, _, name = batch
with torch.no_grad():
_, pred_trg_main = model_gen(image.cuda(device))
pred_trg_main = interp_target(pred_trg_main)
if args.normalize == True:
normalizor = (11-len(find_rare_class(pred_trg_main))) / 11.0 + 0.5
else:
normalizor = 1
# generate binary mask
output_np_tensor = pred_trg_main.cpu().data[0].numpy()
mask_np_tensor = output_np_tensor.transpose(1, 2, 0)
mask_np_tensor = np.asarray(np.argmax(mask_np_tensor, axis=2), dtype=np.uint8)
pred_trg_main_sfmax = F.softmax(pred_trg_main)
# sf_max_np = pred_trg_main_sfmax[0,1,:,:].cpu().numpy()
# pred_trg_entropy = prob_2_entropy(pred_trg_main_sfmax)
pred_trg_sofmax_road = pred_trg_main_sfmax[0,1,:,:].cpu().numpy()
road_prediction = mask_np_tensor * pred_trg_sofmax_road
mask_sum = mask_np_tensor.sum()
if mask_sum == 0:
S_cf = 0
else:
S_cf= road_prediction.sum()/ mask_sum
# mean_item = pred_trg_entropy_road_mean.item()
entropy_list.append((name[0], S_cf * normalizor))
colorize_save(mask_np_tensor, name[0])
# split the enntropy_list into
cluster_subdomain(entropy_list, args.lambda1)
if __name__ == '__main__':
args = get_arguments()
print('Called with args:')
main(args)