-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_attr.py
221 lines (190 loc) · 8.54 KB
/
test_attr.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
import torch
from os import path as osp
import os
import numpy as np
import pandas as pd
from models.segmentation_fine_grained import get_model
from src.config import get_config,print_usage
from torchvision.transforms import functional as F
from torchvision import transforms
from PIL import Image
from src.rle import kaggle_rle_encode, rle_encode,rle_to_string
from tqdm import tqdm
import cv2
import math
import pickle
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
def load_pkl(pkl_name):
with open(pkl_name + '.pkl', 'rb') as f:
return pickle.load(f)
def _scale_image(img, long_size):
if img.shape[0] < img.shape[1]:
scale = img.shape[1] / long_size
size = (long_size, math.floor(img.shape[0] / scale))
else:
scale = img.shape[0] / long_size
size = (math.floor(img.shape[1] / scale), long_size)
return cv2.resize(img, size, interpolation=cv2.INTER_NEAREST)
def refine_masks(masks,labels, im):
# compute the areas of each mask
areas = np.sum(masks.reshape(-1, masks.shape[-1]), axis = 0)
# ordered masks from smallest to largest
mask_index = np.argsort(areas)
# one reference mask is created to be incrementally populated
union_mask = {k: np.zeros(masks.shape[:-1], dtype = bool) for k in np.unique(labels)}
for m in mask_index:
label = labels[m]
masks[:,:,m] = np.logical_and(masks[:,:, m], np.logical_not(union_mask[label]))
union_mask[label] = np.logical_or(masks[:,:,m], union_mask[label])
# reorder masks
refined = list()
for m in range(masks.shape[-1]):
mask_raw = cv2.resize(masks[:,:,m], (im.shape[1], im.shape[0]), interpolation=cv2.INTER_NEAREST)
#mask = mask_raw.ravel(order='F')
rle = kaggle_rle_encode(mask_raw)
#rle = rle_encode(mask_raw)
label = labels[m] - 1
refined.append([mask_raw, rle, label])
return refined
def refine_masksv2(masks, labels, im, attrs, attrmap_dict):
# compute the areas of each mask
areas = np.sum(masks.reshape(-1, masks.shape[-1]), axis = 0)
# ordered masks from smallest to largest
mask_index = np.argsort(areas)
# one reference mask is created to be incrementally populated
union_mask = {k: np.zeros(masks.shape[:-1], dtype = bool) for k in np.unique(labels)}
for m in mask_index:
label = labels[m]
masks[:,:,m] = np.logical_and(masks[:,:, m], np.logical_not(union_mask[label]))
union_mask[label] = np.logical_or(masks[:,:,m], union_mask[label])
# reorder masks
refined = list()
for m in range(masks.shape[-1]): # proposal number
mask_raw = cv2.resize(masks[:,:,m], (im.shape[1], im.shape[0]), interpolation=cv2.INTER_NEAREST)
#mask = mask_raw.ravel(order='F')
rle = kaggle_rle_encode(mask_raw)
#rle = rle_encode(mask_raw)
label = labels[m] - 1
### deal with attribute id ###
attr = attrs[m]
attr = attr.cpu().numpy().tolist()
### preprocess prediction attribute id ###
# remove 0 labels if len(attr) > 1
# set [] if len(attr) == 1 && attr[0]==0
if (0 in attr) and len(attr) > 1:
attr.remove(0)
elif (len(attr) == 1) and (attr[0] == 0):
attr = []
### translate to string ###
attr_str = ','.join(str(attrmap_dict[x]) for x in attr)
# set_trace()
refined.append([mask_raw, rle, label, attr_str])
return refined
def test(config):
print(config)
# test_dt = FashionDataset(config, transforms= None)
sample_df = pd.read_csv(config.sample_path)
### Map Table ###
root='/data/imaterialist2020'
map_file = root + '/attr_map'
attrmap_dict = load_pkl(map_file)
################################################################################
# create the model instance
num_classes = 46 + 1
# model_test = get_model_instance_segmentation(num_classes)
model_test = get_model(nr_class=num_classes, attr_score_thresh=config.attr_score_thresh)
#load the training weights
# load_path =osp.join(config.save_dir, '9_weights')
load_path = osp.join(config.save_dir, 'weights')
# pretrain_params = torch.load(osp.join(load_path, '{}_model.bin'.format(config.checkpoint)),map_location='cpu')
ckpt_state = torch.load(osp.join(load_path, '{}_model.bin'.format(config.checkpoint)), map_location='cpu')
pretrain_params = ckpt_state['state_dict']
# print(pretrain_params)
for k in list(pretrain_params.keys()):
if k.startswith('module.'):
pretrain_params[k[len('module.'):]] = pretrain_params[k]
del pretrain_params[k]
model_test.load_state_dict(pretrain_params)
# send the test model to gpu
model_test.to(device)
for param in model_test.parameters():
param.requires_grad = False
model_test.eval()
# for submission
sub_list = []
missing_count = 0
for i,row in tqdm(sample_df.iterrows(), total = len(sample_df)):
###modify##########################################################
# import the image
img_path = osp.join(config.test_dir,sample_df['ImageId'][i]+'.jpg')
# print(img_path)
img = Image.open(img_path).convert('RGB')
img = img.resize((config.width,config.height), resample = Image.BILINEAR)
# convert the img as tensor
img = F.to_tensor(img)
#####modify#############################################################
# labels/scores/boxes: box branch, masks: mask branch
pred = model_test([img.to(device)])[0] # {'labels:', 'masks', 'scores', 'boxes'}
masks = np.zeros((512,512, len(pred['masks'])))
for j,m in enumerate(pred['masks']):
res = transforms.ToPILImage()(m.permute(1,2,0).cpu().numpy())
res = np.asarray(res.resize((512, 512), resample=Image.BILINEAR))
masks[:,:,j] = (res[:,:] * 255. > 127).astype(np.uint8)
labels = pred['labels'].cpu().numpy() # (nr_proposals,)
scores = pred['scores'].cpu().numpy()
ascores = pred['ascores'] # .cpu().numpy()
# print("ascores: ", ascores)
# print("scores: ", scores)
# print("labels: ", labels)
# set_trace()
best_idx = 0
# print('the maximum scores is {}'.format(np.mean(scores)))
# print('the current masks is {}'.format(masks))
for _scores in scores:
if _scores > config.mask_thresh:
best_idx += 1
if best_idx == 0:
# print(masks.shape[-1])
sub_list.append([sample_df.loc[i, 'ImageId'], '1 1', 23, ''])
missing_count += 1
continue
# mask在roi_heads部分做了后处理,将maskpaste到原图输入上: masks(512,512,nr_proposals)
# 根据box branch预测得到的labels取对应mask channel的mask作为最终的mask
if masks.shape[-1]>0:
im = cv2.imread(img_path)
im = _scale_image(im, 1024)
# FIXME: refine_masks ????
# masks = refine_masks(masks[:,:,:best_idx], labels[:best_idx], im) # List([mask_raw, rle, label])
masks = refine_masksv2(masks[:,:,:best_idx], labels[:best_idx], im, ascores[:best_idx], attrmap_dict['new2attr'])
for m, rle, label, attr in masks:
# print(attr)
# set_trace()
# sub_list.append([sample_df.loc[i, 'ImageId'], rle, label, '']) # TODO: attribute assign
sub_list.append([sample_df.loc[i, 'ImageId'], rle, label, attr])
else:
sub_list.append([sample_df.loc[i, 'ImageId'], '1 1', 23, '']) # TODO: attribute assign
missing_count += 1
#if i > 2:
# break
#set_trace()
submission_df = pd.DataFrame(sub_list, columns=sample_df.columns.values)
print("Total image results: ", submission_df['ImageId'].nunique())
print("Missing Images: ", missing_count)
submission_df = submission_df[submission_df.EncodedPixels.notnull()]
# for row in range(len(submission_df)):
# line = submission_df.iloc[row, :]
# submission_df.iloc[row, 1] = line['EncodedPixels'].replace('.0', '')
# # submission_df.head()
submit_path = config.submit_path + 'submission_{}e_{}t_{}attr.csv'.format(
config.checkpoint, config.mask_thresh, config.attr_score_thresh)
print('submit_path: ',submit_path)
submission_df.to_csv(submit_path, index=False)
print('ok,finished')
if __name__ == '__main__':
# parse configuration
config, unparsed = get_config()
#print(config)
if len(unparsed)>0:
print_usage()
exit(1)
test(config)