-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_framework_visulize_important.py
374 lines (298 loc) · 13.4 KB
/
test_framework_visulize_important.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from __future__ import print_function
import sys
sys.path.append('../')
sys.path.append('/')
from argparse import ArgumentParser
import os
import h5py
# os.environ["CUDA_VISIBLE_DEVICES"] = '0'
import random
import torch
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
from tqdm import tqdm
import numpy as np
import pdb
# from torch.utils.tensorboard import SummaryWriter
from glob import glob
import pandas as pd
from metrics_manager import metrics_manager
import time
import wandb
from collections import OrderedDict
import random
from BigredDataSet import BigredDataSet
from kornia.utils.metrics import mean_iou,confusion_matrix
import pandas as pd
import importlib
# import ckpt
# importlib.import_module
# MODEL = importlib.import_module(args.model)
# shutil.copy('models/%s.py' % args.model, str(experiment_dir))
# shutil.copy('models/pointnet_util.py', str(experiment_dir))
def setSeed(seed = 2):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def convert_state_dict(state_dict):
if not next(iter(state_dict)).startswith("module."):
return state_dict # abort if dict is not a DataParallel model_state
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
return new_state_dict
def visualize_wandb(points,pred,target,index_important):
# points [B,N,C]->[B*N,C]
# pred,target [B,N,1]->[B*N,1]
points = points.view(-1,5).numpy()
pred = pred.view(-1,1).numpy()
target = target.view(-1,1).numpy()
index_important = index_important.view(-1,)
temp_arr = np.zeros(len(target))
temp_arr[index_important] = 1
temp_arr = temp_arr.reshape(-1,1)
points_gt =np.concatenate((points[:,[0,1,2]],target),axis=1)
points_pd =np.concatenate((points[:,[0,1,2]],pred),axis=1)
points_important =np.concatenate((points[:,[0,1,2]],temp_arr),axis=1)
wandb.log({"Ground_truth": wandb.Object3D(points_gt)})
wandb.log({"Prediction": wandb.Object3D(points_pd)})
wandb.log({"important points": wandb.Object3D(points_important)})
class tag_getter(object):
def __init__(self,file_dict):
self.sorted_keys = np.array(sorted(file_dict.keys()))
self.file_dict = file_dict
def get_difficulty_location_isSingle(self,j):
temp_arr = self.sorted_keys<=j
index_for_keys = sum(temp_arr)
_key = self.sorted_keys[index_for_keys-1]
file_name = self.file_dict[_key]
file_name = file_name[:-3]
difficulty,location,isSingle = file_name.split("_")
return(difficulty,location,isSingle,file_name)
def opt_global_inti():
parser = ArgumentParser()
parser.add_argument('--conda_env', type=str, default='some_name')
parser.add_argument('--notification_email', type=str, default='will@email.com')
parser.add_argument('--num_gpu', type=int,default=1 ,help="num_gpu")
parser.add_argument('--dataset_root', type=str, default='../bigRed_h5_pointnet_sorted', help="dataset path")
parser.add_argument('--num_workers', type=int, help='number of data loading workers', default=32)
parser.add_argument("--batch_size", type=int, default=1, help="size of the batches")
parser.add_argument('--phase', type=str,default='test' ,help="root load_pretrain")
os.environ["CUDA_VISIBLE_DEVICES"] = '1'
parser.add_argument('--num_channel', type=int,default=5,help="num_channel")
parser.add_argument('--num_points', type=int,default=20000 ,help="use feature transform")
parser.add_argument('--debug', type=bool,default=False ,help="is task for debugging?False for load entire dataset")
parser.add_argument('--load_pretrain', type=str,default='ckpt/pointnet_5c_simple',help="root load_pretrain")
parser.add_argument('--model', type=str,default='Pointnet_ring_light' ,help="[pointnet,pointnetpp,deepgcn,dgcnn]")
parser.add_argument('--including_ring', type=lambda x: (str(x).lower() == 'true'),default=False ,help="is task for debugging?False for load entire dataset")
args = parser.parse_args()
return args
def save_model(package,root):
torch.save(package,root)
def generate_report(summery_dict,package):
save_sheet=[]
save_sheet.append(['name',package['name']])
save_sheet.append(['validation_miou',package['Miou_validation_ave']])
save_sheet.append(['test_miou',summery_dict['Miou']])
save_sheet.append(['Biou',summery_dict['Biou']])
save_sheet.append(['Fiou',summery_dict['Fiou']])
save_sheet.append(['time_complexicity(f/s)',summery_dict['time_complexicity']])
save_sheet.append(['storage_complexicity',summery_dict['storage_complexicity']])
save_sheet.append(['number_channel',package['num_channel']])
save_sheet.append(['Date',package['time']])
save_sheet.append(['Training-Validation-Testing','0.7-0.9-1'])
for name in summery_dict:
if(name!='Miou'
and name!='storage_complexicity'
and name!='time_complexicity'
and name!='Biou'
and name!='Fiou'
):
save_sheet.append([name,summery_dict[name]])
print(name+': %2f' % summery_dict[name])
# pdb.set_trace()
save_sheet.append(['para',''])
f = pd.DataFrame(save_sheet)
f.to_csv('testReport.csv',index=False,header=None)
def main():
setSeed(10)
opt = opt_global_inti()
print('----------------------Load ckpt----------------------')
pretrained_model_path = os.path.join(opt.load_pretrain,'best_model.pth')
package = torch.load(pretrained_model_path)
para_state_dict = package['state_dict']
opt.num_channel = package['num_channel']
opt.time = package['time']
opt.epoch_ckpt = package['epoch']
# opt.val_miou = package['validation_mIoU']
# package.pop('validation_mIoU')
# package['Validation_ave_miou'] = opt.val_miou
# num_gpu = package['gpuNum']
# package.pop('gpuNum')
# package['num_gpu'] = num_gpu
# save_model(package,pretrained_model_path)
state_dict = convert_state_dict(para_state_dict)
ckpt_,ckpt_file_name = opt.load_pretrain.split("/")
module_name = ckpt_+'.'+ckpt_file_name+'.'+'model'
MODEL = importlib.import_module(module_name)
# print('opt.num_channel: ',opt.num_channel)
model = MODEL.get_model(input_channel = opt.num_channel)
Model_Specification = MODEL.get_model_name(input_channel = opt.num_channel)
print('----------------------Test Model----------------------')
print('Root of prestrain model: ', pretrained_model_path)
print('Model: ', opt.model)
print('Pretrained model name: ', Model_Specification)
print('Trained Date: ',opt.time)
print('num_channel: ',opt.num_channel)
name = input("Edit the name or press ENTER to skip: ")
if(name!=''):
opt.model_name = name
else:
opt.model_name = Model_Specification
print('Pretrained model name: ', opt.model_name)
package['name'] = opt.model_name
save_model(package,pretrained_model_path)
# pdb.set_trace()
# save_model(package,root,name)
# if(model == 'pointnet'):
# #add args
# model = pointnet.Pointnet_sem_seg(k=2,num_channel=opt.num_channel)
# elif(model == 'pointnetpp'):
# print()
# elif(model == 'deepgcn'):
# print()
# elif(model == 'dgcnn'):
# print()
model.load_state_dict(state_dict)
model.cuda()
print('----------------------Load Dataset----------------------')
print('Root of dataset: ', opt.dataset_root)
print('Phase: ', opt.phase)
print('debug: ', opt.debug)
test_dataset = BigredDataSet(
root=opt.dataset_root,
is_train=False,
is_validation=False,
is_test=True,
num_channel = opt.num_channel,
test_code = opt.debug,
including_ring = opt.including_ring)
result_sheet = test_dataset.result_sheet
file_dict= test_dataset.file_dict
tag_Getter = tag_getter(file_dict)
testloader = torch.utils.data.DataLoader(
test_dataset,
batch_size=opt.batch_size,
shuffle=False,
pin_memory=True,
drop_last=True,
num_workers=int(opt.num_workers))
print('num_frame: ',len(test_dataset))
print('batch_size: ', opt.batch_size)
print('num_batch: ', int(len(testloader) / opt.batch_size))
print('----------------------Testing----------------------')
metrics_list = ['Miou','Biou','Fiou','test_loss','OA','time_complexicity','storage_complexicity']
for name in result_sheet:
metrics_list.append(name)
manager = metrics_manager(metrics_list)
model.eval()
wandb.init(project="Test",name=package['name'])
wandb.config.update(opt)
points_gt_list =[]
points_pd_list =[]
points_important_list =[]
with torch.no_grad():
for j, data in tqdm(enumerate(testloader), total=len(testloader), smoothing=0.9):
points, target = data
#target.shape [B,N]
#points.shape [B,N,C]
points, target = points.cuda(), target.cuda()
tic = time.perf_counter()
pred_mics = model(points)
toc = time.perf_counter()
#pred_mics[0] is pred
#pred_mics[1] is feat [only pointnet and pointnetpp has it]
#compute loss
test_loss = 0
#pred.shape [B,N,2] since pred returned pass F.log_softmax
pred, target,points = pred_mics[0].cpu(), target.cpu(),points.cpu()
imp_glob = pred_mics[2].cpu()
#pred:[B,N,2]->[B,N]
# pdb.set_trace()
pred = pred.data.max(dim=2)[1]
#compute confusion matrix
cm = confusion_matrix(pred,target,num_classes =2).sum(dim=0)
#compute OA
overall_correct_site = torch.diag(cm).sum()
overall_reference_site = cm.sum()
assert overall_reference_site == opt.batch_size * opt.num_points,"Confusion_matrix computing error"
oa = float(overall_correct_site/overall_reference_site)
#compute iou
Biou,Fiou = mean_iou(pred,target,num_classes =2).mean(dim=0)
miou = (Biou+Fiou)/2
#compute inference time complexity
time_complexity = toc - tic
#compute inference storage complexsity
num_device = torch.cuda.device_count()
assert num_device == opt.num_gpu,"opt.num_gpu NOT equals torch.cuda.device_count()"
temp = []
for k in range(num_device):
temp.append(torch.cuda.memory_allocated(k))
RAM_usagePeak = torch.tensor(temp).float().mean()
#writeup logger
# metrics_list = ['test_loss','OA','Biou','Fiou','Miou','time_complexicity','storage_complexicity']
manager.update('test_loss',test_loss)
manager.update('OA',oa)
manager.update('Biou',Biou.item())
manager.update('Fiou',Fiou.item())
manager.update('Miou',miou.item())
manager.update('time_complexicity',float(1/time_complexity))
manager.update('storage_complexicity',RAM_usagePeak.item())
#get tags,compute the save miou for corresponding class
difficulty,location,isSingle,file_name=tag_Getter.get_difficulty_location_isSingle(j)
manager.update(file_name,miou.item())
manager.update(difficulty,miou.item())
manager.update(isSingle,miou.item())
dim_num = points.shape[2]
points = points.view(-1,dim_num).numpy()
pred = pred.view(-1,1).numpy()
target = target.view(-1,1).numpy()
imp_glob = imp_glob.view(-1,)
number_sheet,_,bin_sheet = torch.unique(imp_glob, sorted=True, return_inverse=True, return_counts=True, dim=None)
temp_arr = np.zeros(len(target))
temp_arr[number_sheet] = bin_sheet
temp_arr = temp_arr.reshape(-1,1)
points_gt =np.concatenate((points[:,[0,1,2]],target),axis=1)
points_pd =np.concatenate((points[:,[0,1,2]],pred),axis=1)
points_important =np.concatenate((points[:,[0,1,2]],temp_arr),axis=1)
if(opt.including_ring):
temp_arr2 = np.zeros(len(target))
imp_ring = pred_mics[3].cpu()
imp_ring = imp_ring.view(-1,)
number_sheet,_,bin_sheet = torch.unique(imp_ring, sorted=True, return_inverse=True, return_counts=True, dim=None)
temp_arr2[number_sheet] = bin_sheet
temp_arr2 = temp_arr2.reshape(-1,1)
points_important =np.concatenate((points_important,temp_arr2),axis=1)
points_gt_list.append(points_gt)
points_pd_list.append(points_pd)
points_important_list.append(points_important)
# visualize_wandb(points,pred,target,index_important)
# pdb.set_trace()
f = h5py.File('resluts.h5','w')
f.create_dataset('points_gt_list',data = np.array(points_gt_list))
f.create_dataset('points_pd_list',data =np.array(points_pd_list))
f.create_dataset('points_important_list',data = np.array(points_important_list))
f.close()
summery_dict = manager.summary()
generate_report(summery_dict,package)
wandb.log(summery_dict)
# wandb.save('model.h5')
if __name__ == '__main__':
main()