因为本人项目需要使用video swin transformer
默认是提供了一些evaluation metrics 但是我看了一下 我准召率 f1这些都没有,毕竟本事是值考虑多标签任务的正确性的
mmaction.apis — MMAction2 1.2.0 documentation
自定义Metric的代码可以参考这个教程
模型精度评测(Evaluation) — mmengine 0.10.2 文档
from typing import Sequence, List
from mmengine.evaluator import BaseMetric
from mmengine.registry import METRICS
import numpy as np
@METRICS.register_module() # 将 Accuracy 类注册到 METRICS 注册器
class SimpleAccuracy(BaseMetric):
""" Accuracy Evaluator
Default prefix: ACC
Metrics:
- accuracy (float): classification accuracy
"""
default_prefix = 'ACC' # 设置 default_prefix
def process(self, data_batch: Sequence[dict], data_samples: Sequence[dict]):
"""Process one batch of data and predictions. The processed
Results should be stored in `self.results`, which will be used
to compute the metrics when all batches have been processed.
Args:
data_batch (Sequence[Tuple[Any, dict]]): A batch of data
from the dataloader.
data_samples (Sequence[dict]): A batch of outputs from
the model.
"""
# 取出分类预测结果和类别标签
result = {
'pred': data_samples['pred_label'],
'gt': data_samples['data_sample']['gt_label']
}
# 将当前 batch 的结果存进 self.results
self.results.append(result)
def compute_metrics(self, results: List):
"""Compute the metrics from processed results.
Args:
results (dict): The processed results of each batch.
Returns:
Dict: The computed metrics. The keys are the names of the metrics,
and the values are corresponding results.
"""
# 汇总所有样本的分类预测结果和类别标签
preds = np.concatenate([res['pred'] for res in results])
gts = np.concatenate([res['gt'] for res in results])
# 计算分类正确率
acc = (preds == gts).sum() / preds.size
# 返回评测指标结果
return {'accuracy': acc}
给的代码在我这个版本下是不匹配的,估计是没有更新
实际上process拿到的data-samples部分是
[{'num_classes': 2,
'img_shape': (64, 64),
'pred_label': tensor([1], device='cuda:0'),
'pred_score': tensor([0.3516, 0.6484], device='cuda:0'),
'gt_label': tensor([1], device='cuda:0')}]
是一个数组,所有需要额外写一步先提取第一个元素
其他的就按教程中的自定义方法写再加上自定义的metric 添加到结果字典中返回就可以