resnet.py 4.77 KB
Newer Older
Paul Primus's avatar
Paul Primus committed
1
2
3
import torch.nn
import torch
from dcase2020_task2.models.custom import ACTIVATION_DICT, init_weights
Paul Primus's avatar
Paul Primus committed
4
from dcase2020_task2.models.cp_resnet_bn import Network
Paul Primus's avatar
Paul Primus committed
5
6
7
8
9
10
11
12


class ResNet(torch.nn.Module):

    def __init__(
            self,
            input_shape,
            num_outputs=1,
13
            base_channels=128,
Paul Primus's avatar
Paul Primus committed
14
            rf='normal',
Paul Primus's avatar
Paul Primus committed
15
16
17
18
19
20
21
            **kwargs

    ):
        super().__init__()

        self.input_shape = input_shape

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
        configs = {
            'normal': {
                'arch': 'cp_resnet',
                'base_channels': base_channels,
                'block_type': 'basic',
                'depth': 26,
                'input_shape': (1, *input_shape),
                'multi_label': False,
                'n_blocks_per_stage': [4, 1, 2],
                'n_classes': num_outputs,
                'prediction_threshold': 0.4,
                'stage1': {'k1s': [3, 3, 3, 3], 'k2s': [1, 3, 3, 1], 'maxpool': [1, 2, 4]},
                'stage2': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'stage3': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'use_bn': True,
                'weight_init': 'fixup',
                'pooling_padding': None,
                'use_raw_spectograms': None,
                'apply_softmax': None,
                'n_channels': None,
                'grow_a_lot': None,
                'attention_avg': None,
                'stop_before_global_avg_pooling': None,
                'use_check_point': None
            },
            'very_small': {
                'arch': 'cp_resnet',
                'base_channels': base_channels,
                'block_type': 'basic',
                'depth': 26,
                'input_shape': (1, *input_shape),
                'multi_label': False,
                'n_blocks_per_stage': [4, 1, 2],
                'n_classes': num_outputs,
                'prediction_threshold': 0.4,
                'stage1': {'k1s': [3, 3, 3, 1], 'k2s': [1, 1, 1, 1], 'maxpool': [1, 2, 4]},
                'stage2': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'stage3': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'use_bn': True,
                'weight_init': 'fixup',
                'pooling_padding': None,
                'use_raw_spectograms': None,
                'apply_softmax': None,
                'n_channels': None,
                'grow_a_lot': None,
                'attention_avg': None,
                'stop_before_global_avg_pooling': None,
                'use_check_point': None
            },
Paul Primus's avatar
Paul Primus committed
71
            'a_bit_larger': {
72
73
74
75
76
77
                'arch': 'cp_resnet',
                'base_channels': base_channels,
                'block_type': 'basic',
                'depth': 26,
                'input_shape': (1, *input_shape),
                'multi_label': False,
Paul Primus's avatar
Paul Primus committed
78
                'n_blocks_per_stage': [4, 1, 2],
79
80
                'n_classes': num_outputs,
                'prediction_threshold': 0.4,
Paul Primus's avatar
Paul Primus committed
81
                'stage1': {'k1s': [3, 3, 3, 3], 'k2s': [1, 3, 3, 3], 'maxpool': [1, 2, 4]},
82
83
84
85
86
87
88
89
90
91
92
93
                'stage2': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'stage3': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
                'use_bn': True,
                'weight_init': 'fixup',
                'pooling_padding': None,
                'use_raw_spectograms': None,
                'apply_softmax': None,
                'n_channels': None,
                'grow_a_lot': None,
                'attention_avg': None,
                'stop_before_global_avg_pooling': None,
                'use_check_point': None
Paul Primus's avatar
Paul Primus committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            },
        'a_bit_smaller': {
            'arch': 'cp_resnet',
            'base_channels': base_channels,
            'block_type': 'basic',
            'depth': 26,
            'input_shape': (1, *input_shape),
            'multi_label': False,
            'n_blocks_per_stage': [4, 1, 2],
            'n_classes': num_outputs,
            'prediction_threshold': 0.4,
            'stage1': {'k1s': [3, 3, 3, 3], 'k2s': [1, 3, 1, 1], 'maxpool': [1, 2, 4]},
            'stage2': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
            'stage3': {'k1s': [1, 1, 1, 1], 'k2s': [1, 1, 1, 1], 'maxpool': []},
            'use_bn': True,
            'weight_init': 'fixup',
            'pooling_padding': None,
            'use_raw_spectograms': None,
            'apply_softmax': None,
            'n_channels': None,
            'grow_a_lot': None,
            'attention_avg': None,
            'stop_before_global_avg_pooling': None,
            'use_check_point': None
        }
119
120
121
        }

        self.net = Network(configs[rf])
Paul Primus's avatar
Paul Primus committed
122
123
124
125

    def forward(self, batch):
        x = batch['observations']
        batch['scores'] = self.net(x).view(-1, 1)
126
        return batch