-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevalization.py
214 lines (184 loc) · 7.86 KB
/
evalization.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
import torch
from sklearn.metrics import roc_auc_score
from torch.utils.data import DataLoader
from data_utils.dataset_PIAD import PIAD
from model.IAGNet import get_IAGNet
from utils.eval import SIM
from numpy import nan
import numpy as np
import pdb
import random
import os
import pandas as pd
import yaml
import argparse
def Evalization(dataset, data_loader, model_path, use_gpu, Seeting):
if use_gpu:
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
if(Seeting == 'Unseen'):
object_list = ['Bed', 'Dishwasher','Microwave','Scissors','Vase', 'Laptop']
Affordance_list = ['contain', 'lay', 'sit', 'wrapgrasp','open','display','stab','grasp', 'press','cut']
else:
object_list = ['Vase', 'Display', 'Bed', 'Microwave', 'Door',
'Earphone', 'Bottle', 'Bowl', 'Laptop', 'Clock', 'Scissors', 'Mug', 'Faucet',
'StorageFurniture', 'Bag', 'Chair', 'Dishwasher', 'Refrigerator',
'Table', 'Hat', 'Keyboard', 'Knife', 'TrashCan']
Affordance_list = ['grasp', 'contain', 'lift', 'open',
'lay', 'sit', 'support', 'wrapgrasp', 'pour', 'move', 'display',
'push', 'listen', 'wear', 'press', 'cut', 'stab']
for obj in object_list:
exec(f'{obj} = [[], [], [], []]')
for aff in Affordance_list:
exec(f'{aff} = [[], [], [], []]')
model = get_IAGNet(pre_train=False)
checkpoint = torch.load(model_path, map_location='cuda:0')
model.load_state_dict(checkpoint['model'])
model = model.to(device)
results = torch.zeros((len(dataset), 2048, 1))
targets = torch.zeros((len(dataset), 2048, 1))
total_point = 0
num = 0
with torch.no_grad():
model.eval()
Object = []
Affordance = []
for i,(img, point, label, img_path, point_path, sub_box, obj_box) in enumerate(data_loader):
print(f'iteration: {i} start----')
B = img.shape[0]
for iter in range(B):
object_class = point_path[iter].split('_')[-2]
affordance_cls = img_path[iter].split('_')[-2]
Object.append(object_class)
Affordance.append(affordance_cls)
point, label = point.float(), label.float()
label = label.unsqueeze(dim=-1)
if(use_gpu):
img = img.to(device)
point = point.to(device)
label = label.to(device)
sub_box, obj_box = sub_box.to(device), obj_box.to(device)
pred,_,_ = model(img, point, sub_box, obj_box)
pred_num = pred.shape[0]
print(f'num:{num}, pred_num:{pred_num}')
results[num : num+pred_num, :, :] = pred
targets[num : num+pred_num, :, :] = label
num += pred_num
results = results.detach().numpy()
targets = targets.detach().numpy()
SIM_matrix = np.zeros(targets.shape[0])
MAE_martrix = np.zeros(targets.shape[0])
for i in range(targets.shape[0]):
Sim = SIM(results[i], targets[i])
mAE = np.sum(np.absolute(results[i]-targets[i])) / 2048
SIM_matrix[i] = Sim
MAE_martrix[i] = mAE
object_cls = Object[i]
aff_cls = Affordance[i]
exec(f'{object_cls}[1].append({Sim})')
exec(f'{aff_cls}[1].append({Sim})')
exec(f'{object_cls}[3].append({mAE})')
exec(f'{aff_cls}[3].append({mAE})')
sim = np.mean(SIM_matrix)
mean_MAE = np.mean(MAE_martrix)
AUC = np.zeros((targets.shape[0], targets.shape[2]))
IOU = np.zeros((targets.shape[0], targets.shape[2]))
IOU_thres = np.linspace(0, 1, 20)
targets = targets >= 0.5
targets = targets.astype(int)
for i in range(AUC.shape[0]):
t_true = targets[i]
p_score = results[i]
object_cls = Object[i]
aff_cls = Affordance[i]
if np.sum(t_true) == 0:
AUC[i] = np.nan
IOU[i] = np.nan
obj_auc = AUC[i]
aff_auc = AUC[i]
obj_iou = IOU[i]
aff_iou = IOU[i]
exec(f'{object_cls}[2].append({obj_auc})')
exec(f'{aff_cls}[2].append({aff_auc})')
exec(f'{object_cls}[0].append({obj_iou})')
exec(f'{aff_cls}[0].append({aff_iou})')
else:
auc = roc_auc_score(t_true, p_score)
AUC[i] = auc
p_mask = (p_score > 0.5).astype(int)
temp_iou = []
for thre in IOU_thres:
p_mask = (p_score >= thre).astype(int)
intersect = np.sum(p_mask & t_true)
union = np.sum(p_mask | t_true)
temp_iou.append(1.*intersect/union)
temp_iou = np.array(temp_iou)
aiou = np.mean(temp_iou)
IOU[i] = aiou
obj_auc = AUC[i]
aff_auc = AUC[i]
obj_iou = IOU[i]
aff_iou = IOU[i]
exec(f'{object_cls}[2].append({obj_auc})')
exec(f'{aff_cls}[2].append({aff_auc})')
exec(f'{object_cls}[0].append({obj_iou})')
exec(f'{aff_cls}[0].append({aff_iou})')
AUC = np.nanmean(AUC)
IOU = np.nanmean(IOU)
print('------Object-------')
for obj in object_list:
aiou = np.nanmean(eval(obj)[0])
sim_ = np.mean(eval(obj)[1])
auc_ = np.nanmean(eval(obj)[2])
mae_ = np.mean(eval(obj)[3])
print(f'{obj} | IOU:{aiou} | SIM:{sim_} | AUC:{auc_}')
avg_mertics = [0, 0, 0, 0]
print('------Affordance-------')
for i,aff in enumerate(Affordance_list):
aiou = np.nanmean(eval(aff)[0])*100
sim_ = np.mean(eval(aff)[1])
auc_ = np.nanmean(eval(aff)[2])*100
mae_ = np.mean(eval(aff)[3])
avg_mertics[0] += aiou
avg_mertics[1] += sim_
avg_mertics[2] += auc_
avg_mertics[3] += mae_
print(f'{aff} | IOU:{aiou} | SIM:{sim_} | AUC:{auc_}, MAE:{mae_}')
num_affordance = len(Affordance_list)
avg_iou, avg_sim = avg_mertics[0] / num_affordance, avg_mertics[1] / num_affordance
avg_auc, avg_mae = avg_mertics[2] / num_affordance, avg_mertics[3] / num_affordance
print('------ALL-------')
print(f'Overall---AUC:{AUC*100} | IOU:{IOU*100} | SIM:{sim} | MAE:{mean_MAE}')
def seed_torch(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def read_yaml(path):
file = open(path, 'r', encoding='utf-8')
string = file.read()
dict = yaml.safe_load(string)
return dict
def run(opt):
dict = read_yaml(opt.yaml)
point_path = dict['point_test']
img_path = dict['img_test']
box_path = dict['box_test']
model_path = opt.checkpoint_path
val_dataset = PIAD('val', dict['Setting'], point_path, img_path, box_path)
val_loader = DataLoader(val_dataset, dict['batch_size'], num_workers=8, shuffle=True)
Evalization(val_dataset, val_loader, model_path, opt.use_gpu, Seeting=dict['Setting'])
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=str, default='cuda:0', help='gpu device id')
parser.add_argument('--use_gpu', type=str, default=True, help='whether or not use gpus')
parser.add_argument('--checkpoint_path', type=str, default='ckpt/IAG_Seen.pt', help='checkpoint path')
parser.add_argument('--yaml', type=str, default='config/config_seen.yaml', help='yaml path')
opt = parser.parse_args()
seed_torch(42)
run(opt)