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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import tqdm
from typing import List, Dict, Any
from dataclasses import dataclass
from abc import ABC, abstractmethod
from PIL import Image
import numpy as np
import cv2
from typing import Tuple
import os
import json
import argparse

import torch
from transformers import (AutoModel, AutoModelForCausalLM, AutoTokenizer,
                          BitsAndBytesConfig, CLIPImageProcessor,
                          CLIPVisionModel, GenerationConfig)

def exact_match_accuracy(predictions: List[str], references: List[str]) -> float:
    correct = 0
    for pred, ref in zip(predictions, references):
        if isinstance(ref, str):
            ref = [ref]
        is_match_this_turn = False
        for r in ref:
            if pred.strip() == r.strip():
                is_match_this_turn = True
        if is_match_this_turn:
            correct += 1
    return correct / len(predictions) if predictions else 0.0


def bbox_to_corners(bbox):
    """将(x_min, y_min, w, h)格式转换为(x_min, y_min, x_max, y_max)格式"""
    x_min, y_min, w, h = bbox
    return (x_min, y_min, x_min + w, y_min + h)


def calculate_iou(bbox1, bbox2):
    """计算两个边界框的交并比(IoU/Jaccard Index)"""
    # 转换为对角坐标格式
    bbox1 = bbox_to_corners(bbox1)
    bbox2 = bbox_to_corners(bbox2)

    # 计算交集区域的坐标
    x1 = max(bbox1[0], bbox2[0])
    y1 = max(bbox1[1], bbox2[1])
    x2 = min(bbox1[2], bbox2[2])
    y2 = min(bbox1[3], bbox2[3])

    # 计算交集面积
    intersection_area = max(0, x2 - x1) * max(0, y2 - y1)

    # 计算两个边界框的面积
    bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
    bbox2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])

    # 计算并集面积
    union_area = bbox1_area + bbox2_area - intersection_area

    # 计算IoU
    if union_area == 0:
        return 0.0
    return intersection_area / union_area


def calculate_j_metric(pred_bboxes, gt_bboxes):
    """计算J指标(Jaccard Index)"""
    if len(pred_bboxes) != len(gt_bboxes):
        raise ValueError("预测边界框和真实边界框数量不一致")

    iou_values = []
    for pred, gt in zip(pred_bboxes, gt_bboxes):
        iou = calculate_iou(pred, gt)
        iou_values.append(iou)

    # 返回平均Jaccard Index
    return sum(iou_values) / len(iou_values) if iou_values else 0.0


def calculate_f1_score(pred_bboxes, gt_bboxes, threshold=0.5):
    """计算F1 Score(F指标)"""
    if len(pred_bboxes) == 0 and len(gt_bboxes) == 0:
        return 1.0  # 特殊情况:没有检测也没有真实目标,视为完全正确

    true_positives = 0
    false_positives = 0
    false_negatives = 0

    # 标记已匹配的真实边界框
    gt_matched = [False] * len(gt_bboxes)

    # 计算每对边界框的IoU
    iou_matrix = []
    for i, pred in enumerate(pred_bboxes):
        row = []
        for j, gt in enumerate(gt_bboxes):
            row.append(calculate_iou(pred, gt))
        iou_matrix.append(row)

    # 贪心匹配:将每个预测边界框匹配到IoU最高的真实边界框
    for i in range(len(pred_bboxes)):
        if not iou_matrix:
            break

        # 找到当前行的最大值及其索引
        max_iou = max(iou_matrix[i]) if iou_matrix[i] else 0
        j = iou_matrix[i].index(max_iou) if iou_matrix[i] else -1

        if max_iou >= threshold:
            true_positives += 1
            gt_matched[j] = True
        else:
            false_positives += 1

    # 计算假阴性
    false_negatives = sum(1 for matched in gt_matched if not matched)

    # 计算精确率和召回率
    precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0
    recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0

    # 计算F1 Score
    f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
    return f1


def calculate_j_and_f_metrics(pred_bboxes, gt_bboxes, iou_threshold=0.5):
    """计算J指标和F指标"""
    # 计算J指标
    j_metric = calculate_j_metric(pred_bboxes, gt_bboxes)

    # 计算F指标
    f_metric = calculate_f1_score(pred_bboxes, gt_bboxes, threshold=iou_threshold)

    return {
        "J_metric": j_metric,
        "F_metric": f_metric
    }

def read_flow(file_path: str) -> np.ndarray:
    if file_path.endswith('.flo'):
        return read_flow_flo(file_path)
    elif file_path.endswith(('.png', '.jpg', '.jpeg')):
        return read_flow_png(file_path)
    else:
        raise NotImplementedError


def read_flow_flo(file_path: str) -> np.ndarray:
    with open(file_path, 'rb') as f:

        magic = np.fromfile(f, np.float32, count=1)
        if 202021.25 != magic:
            raise NotImplementedError

        w = np.fromfile(f, np.int32, count=1)[0]
        h = np.fromfile(f, np.int32, count=1)[0]

        flow = np.fromfile(f, np.float32, count=2 * w * h)
        flow = flow.reshape(h, w, 2)

    return flow


def read_flow_png(file_path: str) -> np.ndarray:
    img = cv2.imread(file_path, cv2.IMREAD_UNCHANGED).astype(np.float32)

    # 确保图像有足够的通道
    if len(img.shape) != 3 or img.shape[2] < 2:
        raise NotImplementedError

    u = (img[:, :, 2] - 32768.0) / 64.0  # R
    v = (img[:, :, 1] - 32768.0) / 64.0  # G

    flow = np.stack([u, v], axis=2)

    return flow


def calculate_epe(flow_gt: np.ndarray, flow_pred: np.ndarray) -> Tuple[float, np.ndarray]:
    if flow_gt.shape != flow_pred.shape:
        raise NotImplementedError

    diff = flow_gt - flow_pred
    epe_map = np.sqrt(np.sum(diff ** 2, axis=2))

    mean_epe = np.mean(epe_map)

    return mean_epe, epe_map

class Sa2VAModel:
    def __init__(self, model_name="ByteDance/Sa2VA-4B"):
        self.model_name = model_name

        model = AutoModel.from_pretrained(
            model_name,
            torch_dtype=torch.bfloat16,
            low_cpu_mem_usage=True,
            use_flash_attn=True,
            trust_remote_code=True,
        ).eval().cuda()

        tokenizer = AutoTokenizer.from_pretrained(
            model_name,
            trust_remote_code=True,
        )

        self.model = model
        self.tokenizer = tokenizer

    def generate(self, input_dict):
        pred_dict = self.model.predict_forward(**input_dict, tokenizer=self.tokenizer)
        if 'prediction_masks' in pred_dict.keys() and pred_dict['prediction_masks'] and len(
                pred_dict['prediction_masks']) != 0:
            masks = pred_dict['prediction_masks'][0]  # (f, h, w)
        else:
            masks = None
        text_response = pred_dict["prediction"]
        return text_response, masks

@dataclass
class Instance:
    input: Dict[str, Any]
    output: Dict[str, Any]
    id: str


class BaseTask(ABC):
    def __init__(self, task_data: Dict[str, Any], model):
        self.task_data = task_data
        self.model = model
        self.data = self._parse_data(task_data)

    @abstractmethod
    def _parse_data(self, task_data: Dict[str, Any]) -> List[Instance]:
        pass

    @abstractmethod
    def evaluate(self) -> Dict[str, float]:
        pass

    @abstractmethod
    def run_inference(self):
        pass

def get_bbox_from_mask(mask):
    if len(mask.shape) != 2:
        raise NotImplementedError

    y_indices, x_indices = np.nonzero(mask)

    if len(x_indices) == 0 or len(y_indices) == 0:
        return None

    x_min = np.min(x_indices)
    x_max = np.max(x_indices)
    y_min = np.min(y_indices)
    y_max = np.max(y_indices)

    return (x_min, y_min, x_max-x_min, y_max-y_min)

def mask2bbox(masks, video_length):
    if masks is None:
        bboxes = [[0, 0, 0, 0]] * video_length
    else:
        bboxes = []
        for mask in masks:
            bbox = get_bbox_from_mask(mask)
            if bbox is None:
                bbox = [0, 0, 0, 0]
            bboxes.append(bbox)
    return bboxes

class MatchTask(BaseTask):
    def _parse_data(self, task_data: Dict[str, Any]) -> List[Instance]:
        return [Instance(input=d["input"], output=d["output"], id=d["id"])
                for d in task_data["data"]]

    def run_inference(self):
        self.predictions = []
        self.references = []
        for inst in tqdm.tqdm(self.data):
            prompt = "<image>\n" + inst.input["prompt"]
            video_folder = inst.input["video_folder"]
            frame_files = [os.path.join(video_folder, _name) for _name in os.listdir(video_folder)]
            video = []
            for image_path in frame_files:
                video.append(Image.open(image_path).convert('RGB'))

            input_dict = {
                "video": video,
                "text": prompt,
            }

            response, _ = self.model.generate(input_dict, max_new_tokens=256)
            response = response.split("<")[0].strip()

            self.predictions.append(response)
            self.references.append(inst.output["answer"])

    def evaluate(self) -> Dict[str, float]:
        acc = exact_match_accuracy(self.predictions, self.references)
        return {"accuracy": acc}

class TrackingTask(BaseTask):
    def _parse_data(self, task_data: Dict[str, Any]) -> List[Instance]:
        return [Instance(input=d["input"], output=d["output"], id=d["id"])
                for d in task_data["data"]]

    def run_inference(self):
        self.predictions = []
        self.references = []
        for inst in tqdm.tqdm(self.data):
            prompt = "<image>\n" + inst.input["prompt"]
            video_folder = inst.input["video_folder"]
            frame_files = [os.path.join(video_folder, _name) for _name in os.listdir(video_folder)]
            video = []
            for image_path in frame_files:
                video.append(Image.open(image_path).convert('RGB'))

            input_dict = {
                "video": video,
                "text": prompt,
            }

            response, masks = self.model.generate(input_dict, max_new_tokens=256)

            bboxes = mask2bbox(masks, len(video))

            self.predictions.append(bboxes)
            self.references.append(inst.output["answer"])

    def evaluate(self) -> Dict[str, float]:
        j_f, n = 0, 1e-4
        for pred_bboxes, gt_bboxes in zip(self.predictions, self.references):
            metrics = calculate_j_and_f_metrics(pred_bboxes, gt_bboxes)
            j_f += (metrics['J_metric'] + metrics['F_metric']) / 2.0
            n += 1
        j_f = j_f / n
        return {"J&F": j_f}

class FlowTask(BaseTask):
    def _parse_data(self, task_data: Dict[str, Any]) -> List[Instance]:
        return [Instance(input=d["input"], output=d["output"], id=d["id"])
                for d in task_data["data"]]

    def run_inference(self):
        self.predictions = []
        self.references = []
        for inst in tqdm.tqdm(self.data):
            prompt = "<image>\n" + inst.input["prompt"]
            video_folder = inst.input["video_folder"]
            frame_files = [os.path.join(video_folder, _name) for _name in os.listdir(video_folder)]
            video = []
            for image_path in frame_files:
                video.append(Image.open(image_path).convert('RGB'))

            input_dict = {
                "video": video,
                "text": prompt,
            }

            response, masks = self.model.generate(input_dict, max_new_tokens=256)

            pred_flows = np.zeros(masks.shape[1], masks.shape[2], 2)

            self.predictions.append(pred_flows)
            self.references.append(read_flow(inst.output["flow"]))

    def evaluate(self) -> Dict[str, float]:
        EPE, n = 0, 1e-4
        for pred_flow, gt_flow in zip(self.predictions, self.references):
            mean_epe, _ = calculate_epe(pred_flow, gt_flow)
            EPE += mean_epe
            n += 1
        EPE = EPE / n
        return {"EPE": EPE}


def log_performance(model_name, task_name, metrics, root_path, output_file='performance_log.csv'):
    import csv
    file_exists = os.path.isfile(os.path.join(root_path, output_file))

    row_data = {
        'model': model_name,
        'task': task_name,
        'metrics': str(metrics)
    }

    with open(os.path.join(root_path, output_file), mode='a', newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, fieldnames=row_data.keys())
        if not file_exists:
            writer.writeheader()

        writer.writerow(row_data)


def log_performance_detail(model_name, task_name, metrics, root_path, output_file='performance_log.csv'):
    import csv
    file_path = os.path.join(root_path, output_file)
    file_exists = os.path.isfile(file_path)

    # 从metrics字典中获取主要指标值
    metric_value = None
    if isinstance(metrics, dict):
        # 按照优先级选择指标
        for key in ['accuracy', 'f1', 'micro_f1', 'bleu4', 'rougeL', 'code_bleu', 'MAE']:
            if key in metrics:
                metric_value = metrics[key]
                break
        if metric_value is None and len(metrics) > 0:
            # 如果没有找到优先指标,使用第一个指标
            metric_value = list(metrics.values())[0]
    else:
        metric_value = metrics

    # 简化文件名,只保留最后一部分
    model_name = model_name.split('/')[-1]

    if file_exists:
        # 读取现有数据
        rows = []
        tasks = set()
        with open(file_path, 'r', newline='', encoding='utf-8') as f:
            reader = csv.reader(f)
            header = next(reader, ['task', model_name])  # 如果文件为空,使用默认表头
            if len(header) == 1:  # 如果只有task列,添加model列
                header.append(model_name)
            rows.append(header)

            # 读取现有数据并更新
            for row in reader:
                if row[0] == task_name:  # 如果找到相同任务,更新值
                    row = [task_name, str(metric_value)]
                tasks.add(row[0])
                rows.append(row)

            # 如果是新任务,添加新行
            if task_name not in tasks:
                rows.append([task_name, str(metric_value)])
    else:
        # 创建新文件
        rows = [
            ['task', model_name],
            [task_name, str(metric_value)]
        ]

    # 写入所有数据
    with open(file_path, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--root_path", type=str, default="General-Bench-Openset/video/comprehension")
    parser.add_argument("--model_name", type=str, default="ByteDance/Sa2VA-4B")
    args = parser.parse_args()
    root_path = args.root_path
    model_name = args.model_name

    model = Sa2VAModel(model_name=model_name)

    task_files = [
        "AnimalTrack",
        "GreenWaterTrack",
        "LongVideoHumanTrack",
        "RelationMatch",
        "UAVUAVTrack",
        "BallTrack",
        "HumanPartTrack",
        "LongVideoVehicleTrack",
        "ShapeMatch",
        "UAVVehicleTrack",
        "BlueWaterTrack",
        "HumanTrack",
        "MotionMatch",
        "SizeMatch",
        "VehicleTrack",
        "ColorMatch",
        "LOGOMarkerMatch",
        "ObjectMarkerMatch",
        "SyntheticSceneFlowEstimate",
        "WhiteWaterTrack",
        "ComplexSceneFlowEstimate",
        "LongVideoAnimalTrack",
        "OtherPartTrack",
        "UAVBuildingTrack",
        "YellowWaterTrack",
        "CrowdTrack",
        "LongVideoCrowdTrack",
        "PanoramicFlowEstimate",
        "UAVGeneralObjectTrack",
        "GeneralObjectTrack",
        "LongVideoGeneralObjectTrack",
        "PositionMatch",
        "UAVHumanTrack"]

    task_files = [w + '.json' if not w.endswith('json') else w for w in task_files]

    if isinstance(task_files, str):
        task_files = [task_files]

    for idx, filename in enumerate(task_files):
        file_path = os.path.join(root_path, f"{filename.replace('.json', '')}/", filename)
        if not os.path.exists(file_path):
            continue

        with open(file_path, 'r', encoding='utf-8') as f:
            task_data = json.load(f)

        task_type = task_data["type"]
        task_name = task_data["task"]
        print(f"Running evaluation for task {idx + 1}: {task_name}")

        # 定义任务类型与任务类的映射字典
        TASK_MAPPING = {
            "AnimalTrack": TrackingTask,
            "GreenWaterTrack": TrackingTask,
            "LongVideoHumanTrack": TrackingTask,
            "RelationMatch": MatchTask,
            "UAVUAVTrack": TrackingTask,
            "BallTrack": TrackingTask,
            "HumanPartTrack": TrackingTask,
            "LongVideoVehicleTrack": TrackingTask,
            "ShapeMatch": MatchTask,
            "UAVVehicleTrack": TrackingTask,
            "BlueWaterTrack": TrackingTask,
            "HumanTrack": TrackingTask,
            "MotionMatch": MatchTask,
            "SizeMatch": MatchTask,
            "VehicleTrack": TrackingTask,
            "ColorMatch": MatchTask,
            "LOGOMarkerMatch": MatchTask,
            "ObjectMarkerMatch": MatchTask,
            "SyntheticSceneFlowEstimate": FlowTask,
            "WhiteWaterTrack": TrackingTask,
            "ComplexSceneFlowEstimate": FlowTask,
            "LongVideoAnimalTrack": TrackingTask,
            "OtherPartTrack": TrackingTask,
            "UAVBuildingTrack": TrackingTask,
            "YellowWaterTrack": TrackingTask,
            "CrowdTrack": TrackingTask,
            "LongVideoCrowdTrack": TrackingTask,
            "PanoramicFlowEstimate": FlowTask,
            "UAVGeneralObjectTrack": TrackingTask,
            "GeneralObjectTrack": TrackingTask,
            "LongVideoGeneralObjectTrack": TrackingTask,
            "PositionMatch": MatchTask,
            "UAVHumanTrack": TrackingTask,
        }

        # 根据 task_type 获取对应的任务类
        task_class = TASK_MAPPING.get(task_type)  # 使用精确匹配
        if task_class is None:
            raise NotImplementedError
        else:
            task = task_class(task_data, model)

        task.run_inference()
        metrics = task.evaluate()
        print("Task name: ", task_name, "Task type: ", task_type, "Evaluation results:", metrics)
        log_performance(model_name, task_name, metrics, root_path)