|
| 1 | +import torch.nn as nn |
| 2 | + |
| 3 | +from ._utils import Conv3DSimple, Conv3DNoTemporal |
| 4 | +from .video_stems import get_default_stem |
| 5 | +from .video_trunk import VideoTrunkBuilder, BasicBlock, Bottleneck |
| 6 | + |
| 7 | + |
| 8 | +__all__ = ["mc3_18"] |
| 9 | + |
| 10 | + |
| 11 | +def _mcX(model_depth, X=3, use_pool1=False, **kwargs): |
| 12 | + """Generate mixed convolution network as in |
| 13 | + https://arxiv.org/abs/1711.11248 |
| 14 | +
|
| 15 | + Args: |
| 16 | + model_depth (int): trunk depth - supports most resnet depths |
| 17 | + X (int): Up to which layers are convolutions 3D |
| 18 | + use_pool1 (bool, optional): Add pooling layer to the stem. Defaults to False. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + nn.Module: mcX video trunk |
| 22 | + """ |
| 23 | + assert X > 1 and X <= 5 |
| 24 | + conv_makers = [Conv3DSimple] * (X - 2) |
| 25 | + while len(conv_makers) < 5: |
| 26 | + conv_makers.append(Conv3DNoTemporal) |
| 27 | + |
| 28 | + if model_depth < 50: |
| 29 | + block = BasicBlock |
| 30 | + else: |
| 31 | + block = Bottleneck |
| 32 | + |
| 33 | + model = VideoTrunkBuilder(block=block, conv_makers=conv_makers, model_depth=model_depth, |
| 34 | + stem=get_default_stem(use_pool1=use_pool1), **kwargs) |
| 35 | + |
| 36 | + return model |
| 37 | + |
| 38 | + |
| 39 | +def _rmcX(model_depth, X=3, use_pool1=False, **kwargs): |
| 40 | + """Generate reverse mixed convolution network as in |
| 41 | + https://arxiv.org/abs/1711.11248 |
| 42 | +
|
| 43 | + Args: |
| 44 | + model_depth (int): trunk depth - supports most resnet depths |
| 45 | + X (int): Up to which layers are convolutions 2D |
| 46 | + use_pool1 (bool, optional): Add pooling layer to the stem. Defaults to False. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + nn.Module: mcX video trunk |
| 50 | + """ |
| 51 | + assert X > 1 and X <= 5 |
| 52 | + |
| 53 | + conv_makers = [Conv3DNoTemporal] * (X - 2) |
| 54 | + while len(conv_makers) < 5: |
| 55 | + conv_makers.append(Conv3DSimple) |
| 56 | + |
| 57 | + if model_depth < 50: |
| 58 | + block = BasicBlock |
| 59 | + else: |
| 60 | + block = Bottleneck |
| 61 | + |
| 62 | + model = VideoTrunkBuilder(block=block, conv_makers=conv_makers, model_depth=model_depth, |
| 63 | + stem=get_default_stem(use_pool1=use_pool1), **kwargs) |
| 64 | + |
| 65 | + return model |
| 66 | + |
| 67 | + |
| 68 | +def mc3_18(use_pool1=False, **kwargs): |
| 69 | + """Constructor for 18 layer Mixed Convolution network as in |
| 70 | + https://arxiv.org/abs/1711.11248 |
| 71 | +
|
| 72 | + Args: |
| 73 | + use_pool1 (bool, optional): Include pooling in the resnet stem. Defaults to False. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + nn.Module: MC3 Network definitino |
| 77 | + """ |
| 78 | + return _mcX(18, 3, use_pool1, **kwargs) |
0 commit comments