|
|
|
|
|
from mmengine.model.weight_init import normal_init
|
|
|
from torch import Tensor, nn
|
|
|
|
|
|
from mmaction.registry import MODELS
|
|
|
from mmaction.utils import ConfigType
|
|
|
from .base import BaseHead
|
|
|
|
|
|
|
|
|
@MODELS.register_module()
|
|
|
class I3DHead(BaseHead):
|
|
|
"""Classification head for I3D.
|
|
|
|
|
|
Args:
|
|
|
num_classes (int): Number of classes to be classified.
|
|
|
in_channels (int): Number of channels in input feature.
|
|
|
loss_cls (dict or ConfigDict): Config for building loss.
|
|
|
Default: dict(type='CrossEntropyLoss')
|
|
|
spatial_type (str): Pooling type in spatial dimension. Default: 'avg'.
|
|
|
dropout_ratio (float): Probability of dropout layer. Default: 0.5.
|
|
|
init_std (float): Std value for Initiation. Default: 0.01.
|
|
|
kwargs (dict, optional): Any keyword argument to be used to initialize
|
|
|
the head.
|
|
|
"""
|
|
|
|
|
|
def __init__(self,
|
|
|
num_classes: int,
|
|
|
in_channels: int,
|
|
|
loss_cls: ConfigType = dict(type='CrossEntropyLoss'),
|
|
|
spatial_type: str = 'avg',
|
|
|
dropout_ratio: float = 0.5,
|
|
|
init_std: float = 0.01,
|
|
|
**kwargs) -> None:
|
|
|
super().__init__(num_classes, in_channels, loss_cls, **kwargs)
|
|
|
|
|
|
self.spatial_type = spatial_type
|
|
|
self.dropout_ratio = dropout_ratio
|
|
|
self.init_std = init_std
|
|
|
if self.dropout_ratio != 0:
|
|
|
self.dropout = nn.Dropout(p=self.dropout_ratio)
|
|
|
else:
|
|
|
self.dropout = None
|
|
|
self.fc_cls = nn.Linear(self.in_channels, self.num_classes)
|
|
|
|
|
|
if self.spatial_type == 'avg':
|
|
|
|
|
|
self.avg_pool = nn.AdaptiveAvgPool3d((1, 1, 1))
|
|
|
else:
|
|
|
self.avg_pool = None
|
|
|
|
|
|
def init_weights(self) -> None:
|
|
|
"""Initiate the parameters from scratch."""
|
|
|
normal_init(self.fc_cls, std=self.init_std)
|
|
|
|
|
|
def forward(self, x: Tensor, **kwargs) -> Tensor:
|
|
|
"""Defines the computation performed at every call.
|
|
|
|
|
|
Args:
|
|
|
x (Tensor): The input data.
|
|
|
|
|
|
Returns:
|
|
|
Tensor: The classification scores for input samples.
|
|
|
"""
|
|
|
|
|
|
if self.avg_pool is not None:
|
|
|
x = self.avg_pool(x)
|
|
|
|
|
|
if self.dropout is not None:
|
|
|
x = self.dropout(x)
|
|
|
|
|
|
x = x.view(x.shape[0], -1)
|
|
|
|
|
|
cls_score = self.fc_cls(x)
|
|
|
|
|
|
return cls_score
|
|
|
|