动作识别任务中基于子类预测父类Diagnosis的方案合理性问询
动作识别任务中基于子类预测父类的方案合理性分析
我正在开展一项动作识别任务,希望基于子类action_name和priority的预测结果来预测父类Diagnosis。参考多输出分类相关方案后,修改实现了如下模型:
import torch import torch.nn as nn import torch.nn.functional as F import torchvision.models as models class MultiOutputModel(nn.Module): def __init__(self, n_action_classes, n_priority_classes, n_diagnosis_classes): super().__init__() self.base_model = models.mobilenet_v2().features # take the model without classifier last_channel = models.mobilenet_v2().last_channel # size of the layer before classifier # the input for the classifier should be two-dimensional, but we will have # [batch_size, channels, width, height] # so, let's do the spatial averaging: reduce width and height to 1 self.pool = nn.AdaptiveAvgPool2d((1, 1)) # create separate classifiers for our outputs self.action = nn.Sequential( nn.Dropout(p=0.2), nn.Linear(in_features=last_channel, out_features=n_action_classes) ) self.priority = nn.Sequential( nn.Dropout(p=0.2), nn.Linear(in_features=last_channel, out_features=n_priority_classes) ) self.diagnosis = nn.Sequential( nn.Dropout(p=0.2), nn.Linear(in_features=n_action_classes + n_priority_classes, out_features=n_diagnosis_classes) ) def forward(self, x): x = self.base_model(x) x = self.pool(x) # reshape from [batch, channels, 1, 1] to [batch, channels] to put it into classifier x = torch.flatten(x, 1) # Subclass predictions action = self.action(x) priority = self.priority(x) # Concatenate subclass outputs for parent class prediction combined_action_priority_outputs = torch.cat([action, priority], dim=1) diagnosis = self.diagnosis(combined_action_priority_outputs) return { 'action': action, 'priority': priority, 'diagnosis': diagnosis }
目前模型输出的准确率为1,请问我当前的问题处理方式是否正确?
核心结论与分析
整体思路(多输出任务架构+基于子类结果推导父类)具备合理性,但细节处理存在优化空间,且准确率为1的异常情况需优先排查。
一、当前方案的潜在问题
父类预测的输入特征选择不当:
直接拼接子类的logits(未激活的原始输出)来预测父类,logits包含未归一化的噪声,若子类任务已过拟合,会将偏差直接传递给父类预测。应该使用子类的概率输出(经Softmax/Sigmoid激活后),这才是模型对子类类别的置信度表示,更适合作为父类预测的输入。准确率为1的异常需警惕:
训练集准确率达1通常是过拟合信号,测试集准确率达1则需排查数据问题:- 是否存在训练/测试集数据泄露(如测试集样本混入训练集);
- 数据集类别分布是否极度失衡(如某类占比100%);
- 标签是否存在错误或冗余。
特征融合逻辑过于简单:
仅拼接子类输出未考虑base model的原始特征,也未捕捉子类间的关联。若父类Diagnosis与子类的依赖关系复杂,简单拼接无法充分利用有效信息。
二、针对性优化建议
调整父类预测的输入:
将子类输出转为概率后再拼接,示例修改如下:def forward(self, x): x = self.base_model(x) x = self.pool(x) x = torch.flatten(x, 1) # 子类预测并转为概率 action = self.action(x) action_prob = F.softmax(action, dim=1) priority = self.priority(x) priority_prob = F.softmax(priority, dim=1) # 用概率拼接预测父类 combined = torch.cat([action_prob, priority_prob], dim=1) diagnosis = self.diagnosis(combined) return { 'action': action, 'priority': priority, 'diagnosis': diagnosis }排查过拟合与数据问题:
- 验证训练/测试集划分逻辑,确保无交叉污染;
- 统计数据集类别分布,若失衡需做重采样或调整损失函数;
- 增加正则化:提升Dropout比例、添加L2正则、引入图像/视频数据增强(如随机裁剪、翻转、时序变换)。
优化特征融合方式:
可以将base model的全局特征与子类概率特征融合,让父类预测同时利用原始视觉特征和子类任务的语义信息:# forward中修改拼接逻辑 combined = torch.cat([x, action_prob, priority_prob], dim=1) diagnosis = self.diagnosis(combined)也可引入注意力机制,让模型自动学习子类特征对父类预测的权重占比。
内容的提问来源于stack exchange,提问作者anya
相关产品推荐
相关产品推荐

