File size: 2,350 Bytes
d670799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Dict, List, Union

import torch
import torch.nn as nn

from mmaction.registry import MODELS
from .base import BaseHead


@MODELS.register_module()
class GCNHead(BaseHead):
    """The classification head for GCN.



    Args:

        num_classes (int): Number of classes to be classified.

        in_channels (int): Number of channels in input feature.

        loss_cls (dict): Config for building loss.

            Defaults to ``dict(type='CrossEntropyLoss')``.

        dropout (float): Probability of dropout layer. Defaults to 0.

        init_cfg (dict or list[dict]): Config to control the initialization.

            Defaults to ``dict(type='Normal', layer='Linear', std=0.01)``.

    """

    def __init__(self,

                 num_classes: int,

                 in_channels: int,

                 loss_cls: Dict = dict(type='CrossEntropyLoss'),

                 dropout: float = 0.,

                 average_clips: str = 'prob',

                 init_cfg: Union[Dict, List[Dict]] = dict(

                     type='Normal', layer='Linear', std=0.01),

                 **kwargs) -> None:
        super().__init__(
            num_classes,
            in_channels,
            loss_cls=loss_cls,
            average_clips=average_clips,
            init_cfg=init_cfg,
            **kwargs)
        self.dropout_ratio = dropout
        if self.dropout_ratio != 0:
            self.dropout = nn.Dropout(p=self.dropout_ratio)
        else:
            self.dropout = None

        self.pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(self.in_channels, self.num_classes)

    def forward(self, x: torch.Tensor, **kwargs) -> torch.Tensor:
        """Forward features from the upstream network.



        Args:

            x (torch.Tensor): Features from the upstream network.



        Returns:

            torch.Tensor: Classification scores with shape (B, num_classes).

        """

        N, M, C, T, V = x.shape
        x = x.view(N * M, C, T, V)
        x = self.pool(x)
        x = x.view(N, M, C)
        x = x.mean(dim=1)
        assert x.shape[1] == self.in_channels

        if self.dropout is not None:
            x = self.dropout(x)

        cls_scores = self.fc(x)
        return cls_scores