-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
305 lines (227 loc) · 11.6 KB
/
Copy pathtest.py
File metadata and controls
305 lines (227 loc) · 11.6 KB
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
"""
Project: CVS-AdaptNet
-----
Copyright (c) University of Strasbourg, All Rights Reserved.
"""
import torch
import torch.nn as nn
import argparse
from torch import Tensor
import torchvision.models as models
from mmengine.config import Config
from mmengine.logging import MMLogger
from codes.datasets import build_dataset
from codes.models import build_algorithm
import json
import cv2
from tqdm import tqdm
import numpy as np
from torchvision.transforms import transforms
from torch.utils.data import ConcatDataset
from torchmetrics import AveragePrecision as AP, Precision, Recall, F1Score
from codes.models.algorithms.CVS_AdaptNet import CVSAdaptNetModel, process_text
import matplotlib.pyplot as plt
def test_both_plot_8(test_loaders, model_1, args):
class_prompt=args.class_prompt
model_1.eval()
with open(class_prompt) as f:
lines = f.readlines()
f.close()
class_texts_ = [i.replace('\n', '') for i in lines]
class_texts = process_text(class_texts_)
text_features = model_1(None, class_texts, mode='text')['text_emb'].cuda()
text_features /= text_features.norm(dim=-1, keepdim=True)
# init logger
logger: MMLogger = MMLogger.get_current_instance()
logger_info = []
eval_results = {}
probs_list = []
label_list = []
with torch.no_grad():
for test_loader in test_loaders:
for i, data in enumerate(test_loader):
probs_list_temp = []
label_list_temp = []
frames = data['video'].cuda() # (1, M, T, C, H, W)
B, C, H, W = frames.shape
frames = frames.view(-1, C, H, W)
image_features = model_1(frames, None, mode='video')['img_emb'] # (B*M*T, D)
image_features /= image_features.norm(dim=-1, keepdim=True)
probs = (image_features @ text_features.T) # (1, classes)
probabilities = probs.softmax(dim=-1) # (1, classes)
# Define the indices for each class based on your earlier specification
class1_indices = [4,5,6,7] # Class 1 corresponds to these indices
class2_indices = [2,3,6,7] # Class 2 corresponds to these indices
class3_indices = [1,3,5,7] # Class 3 corresponds to these indices
# Initialize an empty result tensor [1799, 3]
result = torch.zeros((probabilities.shape[0], 3))
# Sum the probabilities for each class
result[:, 0] = torch.sum(probabilities[:, class1_indices], axis=1) # Class 1
result[:, 1] = torch.sum(probabilities[:, class2_indices], axis=1) # Class 2
result[:, 2] = torch.sum(probabilities[:, class3_indices], axis=1) # Class 3
gts = [[float(item) for item in json.loads(value)] for value in data['label']]
probs_list = probs_list + [p.tolist() for p in result]
#print(gts)
label_list = label_list + [g for g in gts]
torch_ap = AP(task='multilabel', num_labels=3, average='none')
ds_preds = torch.stack([Tensor(p) for p in probs_list])
ds_gt = torch.stack([Tensor(g).round() for g in label_list]).long()
ds_ap = torch_ap(ds_preds, ds_gt)
# log overall
logger_info.append(f'ds_average_precision: {torch.nanmean(ds_ap):.4f}')
eval_results['ds_average_precision'] = torch.nanmean(ds_ap)
# log component-wise
for ind, i in enumerate(ds_ap):
logger_info.append(f'ds_average_precision_C{ind+1}: {i:.4f}')
eval_results['ds_average_precision_C{}'.format(ind+1)] = i
print('\n'.join(logger_info))
return ds_ap
def test_both_plot_pos_neg(test_loaders, model_1, args):
# Modify the string to create the negative class prompt path
class_prompt = args.class_prompt
class_prompt_neg = class_prompt.replace('.txt', '_no_class.txt')
# class_prompt_neg will now have '_no_class' added before the '.txt' extension
print(class_prompt_neg)
model_1.eval()
with open(class_prompt) as f:
lines = f.readlines()
f.close()
with open(class_prompt_neg) as f:
neg_lines = f.readlines()
f.close()
class_texts_ = [i.replace('\n', '') for i in lines]
class_texts_pos = process_text(class_texts_)
text_features = model_1(None, class_texts_pos, mode='text')['text_emb'].cuda()
text_features /= text_features.norm(dim=-1, keepdim=True)
class_texts_neg_ = [i.replace('\n', '') for i in neg_lines]
class_texts_neg = process_text(class_texts_neg_)
text_features_neg = model_1(None, class_texts_neg, mode='text')['text_emb'].cuda()
text_features_neg /= text_features_neg.norm(dim=-1, keepdim=True)
# init logger
logger: MMLogger = MMLogger.get_current_instance()
logger_info = []
eval_results = {}
probs_list = []
label_list = []
with torch.no_grad():
for test_loader in test_loaders:
for i, data in enumerate(test_loader):
frames = data['video'].cuda() # (1, M, T, C, H, W)
# B, M, T, C, H, W = frames.shape
B, C, H, W = frames.shape
image_name = data['image_name']
#print("image_name", image_name)
frames = frames.view(-1, C, H, W)
image_features = model_1(frames, None, mode='video')['img_emb'] # (B*M*T, D)
image_features /= image_features.norm(dim=-1, keepdim=True)
probs_pos = (image_features @ text_features.T) # (1, classes)
probs_neg = (image_features @ text_features_neg.T) # (1, classes)
# Stack the tensors along a new dimension to apply softmax
stacked_probs = torch.stack([probs_pos, probs_neg], dim=0) # Shape: (2, 1, classes)
# Apply softmax along the first dimension (dim=0) to normalize between pos and neg
softmax_probs = torch.nn.functional.softmax(stacked_probs, dim=0)
# Extract the softmax values
softmax_pos = softmax_probs[0] # Softmax for probs_pos
# Decision step: Choose probs_pos if its softmax value is greater, else use (1 - probs_pos)
final_probs = softmax_pos
## for average precision calculation
preds = final_probs.cpu()
gts = [[float(item) for item in json.loads(value)] for value in data['label']]
probs_list = probs_list + [p.tolist() for p in preds]
label_list = label_list + [g for g in gts]
torch_ap = AP(task='multilabel', num_labels=3, average='none')
ds_preds = torch.stack([Tensor(p) for p in probs_list])
#ds_gt_pred = torch.stack([Tensor(g) for g in gt_prob_list])
ds_gt = torch.stack([Tensor(g).round() for g in label_list]).long()
ds_ap = torch_ap(ds_preds, ds_gt)
#print(ds_ap)
# log overall
logger_info.append(f'ds_average_precision: {torch.nanmean(ds_ap):.4f}')
eval_results['ds_average_precision'] = torch.nanmean(ds_ap)
# log component-wise
for ind, i in enumerate(ds_ap):
logger_info.append(f'ds_average_precision_C{ind+1}: {i:.4f}')
eval_results['ds_average_precision_C{}'.format(ind+1)] = i
print('\n'.join(logger_info))
return ds_ap
def test_both(test_loaders, model_1, args):
class_prompt=args.class_prompt
model_1.eval()
with open(class_prompt) as f:
lines = f.readlines()
f.close()
class_texts_ = [i.replace('\n', '') for i in lines]
class_texts = process_text(class_texts_)
text_features = model_1(None, class_texts, mode='text')['text_emb'].cuda()
text_features /= text_features.norm(dim=-1, keepdim=True)
# init logger
logger: MMLogger = MMLogger.get_current_instance()
logger_info = []
eval_results = {}
probs_list = []
label_list = []
with torch.no_grad():
for test_loader in test_loaders:
for i, data in enumerate(test_loader):
frames = data['video'].cuda() # (1, M, T, C, H, W)
# B, M, T, C, H, W = frames.shape
B, C, H, W = frames.shape
frames = frames.view(-1, C, H, W)
image_features = model_1(frames, None, mode='video')['img_emb'] # (B*M*T, D)
# image_features = torch.mean(image_features, dim=0, keepdim=True) # (1, D)
image_features /= image_features.norm(dim=-1, keepdim=True)
probs = (image_features @ text_features.T) # (1, classes)
## for average precision calculation
preds = probs.cpu()
gts = [[float(item) for item in json.loads(value)] for value in data['label']]
#print(gts)
probs_list = probs_list + [p.tolist() for p in preds]
label_list = label_list + [g for g in gts]
torch_ap = AP(task='multilabel', num_labels=3, average='none')
ds_preds = torch.stack([Tensor(p) for p in probs_list]).sigmoid()
ds_gt = torch.stack([Tensor(g).round() for g in label_list]).long()
ds_ap = torch_ap(ds_preds, ds_gt)
logger_info.append(f'ds_average_precision: {torch.nanmean(ds_ap):.4f}')
eval_results['ds_average_precision'] = torch.nanmean(ds_ap)
# log component-wise
for ind, i in enumerate(ds_ap):
logger_info.append(f'ds_average_precision_C{ind+1}: {i:.4f}')
eval_results['ds_average_precision_C{}'.format(ind+1)] = i
print('\n'.join(logger_info))
return ds_ap
def get_args(description='CLIP'):
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--class_prompt', default='../class_prompts/class_prompt_endoscapes_manual.txt', type=str, help='prompt for standard, positive_negative, multi_class')
parser.add_argument('--checkpoint', default='../best_model.pth', type=str, help='prompt for categories')
parser.add_argument('--dataset_config', default='../config/config_cvsadaptnet.py', type=str, help='dataset config')
parser.add_argument('--batch_size', default=1, type=int, help='batch for testing')
parser.add_argument('--inference_mode', default="standard", type=str, help='mode for inference')
parser.add_argument('--save_dir', default="save", type=str, help='save directory')
args = parser.parse_args()
return args, parser
if __name__ == "__main__":
args, _ = get_args()
num_classes = 3
configs = Config.fromfile(args.dataset_config)['config']
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
test_datasets = [build_dataset(c) for c in configs.test_config]
test_loaders = [torch.utils.data.DataLoader(
test_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=0
) for test_dataset in test_datasets]
model = build_algorithm(configs.model_config).cuda()
state_dict = torch.load(args.checkpoint)['model_state_dict']
new_dict = {}
for k, v in state_dict.items():
#if 'module.' in k:
new_dict[k.replace('model.backbone_img.model.', 'backbone_img.model.').replace('model.backbone_text.model.', 'backbone_text.model.').replace('model.backbone_img.global_embedder','backbone_img.global_embedder')] = v
a, b = model.load_state_dict(new_dict, strict=False)
if args.inference_mode == "standard":
test_both(test_loaders, model, args)
elif args.inference_mode == "positive_negative":
test_both_plot_pos_neg(test_loaders, model, args)
elif args.inference_mode == "multi_class":
test_both_plot_8(test_loaders, model, args)