File size: 14,531 Bytes
e2cffd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""

ISL Sign Language Translation - TechMatrix Solvers Initiative

Model definitions for body pose and hand pose estimation

Developed by: TechMatrix Solvers Team

"""

import torch
from collections import OrderedDict
import torch.nn as nn


def construct_layers(layer_config, no_relu_layers, prelu_layers=[]):
    """

    Constructs neural network layers based on configuration

    

    Args:

        layer_config: Dictionary defining layer parameters

        no_relu_layers: List of layers that shouldn't have ReLU activation

        prelu_layers: List of layers that should use PReLU instead of ReLU

    """
    layers = []
    
    for layer_name, params in layer_config.items():
        if 'pool' in layer_name:
            layer = nn.MaxPool2d(kernel_size=params[0], stride=params[1], padding=params[2])
            layers.append((layer_name, layer))
        else:
            conv2d = nn.Conv2d(
                in_channels=params[0], 
                out_channels=params[1],
                kernel_size=params[2], 
                stride=params[3],
                padding=params[4]
            )
            layers.append((layer_name, conv2d))
            
            if layer_name not in no_relu_layers:
                if layer_name not in prelu_layers:
                    layers.append(('relu_' + layer_name, nn.ReLU(inplace=True)))
                else:
                    layers.append(('prelu' + layer_name[4:], nn.PReLU(params[1])))

    return nn.Sequential(OrderedDict(layers))


def construct_multi_conv_layers(layer_config, no_relu_layers):
    """

    Constructs multiple convolution layers for complex architectures

    """
    modules = []
    for layer_name, params in layer_config.items():
        layers = []
        if 'pool' in layer_name:
            layer = nn.MaxPool2d(kernel_size=params[0], stride=params[1], padding=params[2])
            layers.append((layer_name, layer))
        else:
            conv2d = nn.Conv2d(
                in_channels=params[0], 
                out_channels=params[1],
                kernel_size=params[2], 
                stride=params[3],
                padding=params[4]
            )
            layers.append((layer_name, conv2d))
            if layer_name not in no_relu_layers:
                layers.append(('Mprelu' + layer_name[5:], nn.PReLU(params[1])))
        modules.append(nn.Sequential(OrderedDict(layers)))
    return nn.ModuleList(modules)


class BodyPose25Model(nn.Module):
    """

    Body pose estimation model using 25-point skeleton

    Developed by TechMatrix Solvers for ISL translation

    """
    
    def __init__(self):
        super(BodyPose25Model, self).__init__()
        
        # Define layers without ReLU activation
        no_relu_layers = [
            'Mconv7_stage0_L1', 'Mconv7_stage0_L2',
            'Mconv7_stage1_L1', 'Mconv7_stage1_L2',
            'Mconv7_stage2_L2', 'Mconv7_stage3_L2'
        ]
        prelu_layers = ['conv4_2', 'conv4_3_CPM', 'conv4_4_CPM']
        
        # Initial feature extraction layers
        base_layers = OrderedDict([
            ('conv1_1', [3, 64, 3, 1, 1]),
            ('conv1_2', [64, 64, 3, 1, 1]),
            ('pool1_stage1', [2, 2, 0]),
            ('conv2_1', [64, 128, 3, 1, 1]),
            ('conv2_2', [128, 128, 3, 1, 1]),
            ('pool2_stage1', [2, 2, 0]),
            ('conv3_1', [128, 256, 3, 1, 1]),
            ('conv3_2', [256, 256, 3, 1, 1]),
            ('conv3_3', [256, 256, 3, 1, 1]),
            ('conv3_4', [256, 256, 3, 1, 1]),
            ('pool3_stage1', [2, 2, 0]),
            ('conv4_1', [256, 512, 3, 1, 1]),
            ('conv4_2', [512, 512, 3, 1, 1]),
            ('conv4_3_CPM', [512, 256, 3, 1, 1]),
            ('conv4_4_CPM', [256, 128, 3, 1, 1])
        ])
        self.base_model = construct_layers(base_layers, no_relu_layers, prelu_layers)
        
        # Multi-stage refinement blocks
        stage_blocks = {}
        
        # L2 branch - Stage 0
        stage_blocks['Mconv1_stage0_L2'] = OrderedDict([
            ('Mconv1_stage0_L2_0', [128, 96, 3, 1, 1]),
            ('Mconv1_stage0_L2_1', [96, 96, 3, 1, 1]),
            ('Mconv1_stage0_L2_2', [96, 96, 3, 1, 1])
        ])
        
        for i in range(2, 6):
            stage_blocks[f'Mconv{i}_stage0_L2'] = OrderedDict([
                (f'Mconv{i}_stage0_L2_0', [288, 96, 3, 1, 1]),
                (f'Mconv{i}_stage0_L2_1', [96, 96, 3, 1, 1]),
                (f'Mconv{i}_stage0_L2_2', [96, 96, 3, 1, 1])
            ])
            
        stage_blocks['Mconv6_7_stage0_L2'] = OrderedDict([
            ('Mconv6_stage0_L2', [288, 256, 1, 1, 0]),
            ('Mconv7_stage0_L2', [256, 52, 1, 1, 0])
        ])
        
        # L2 branch - Stages 1-3
        for stage in range(1, 4):
            stage_blocks[f'Mconv1_stage{stage}_L2'] = OrderedDict([
                (f'Mconv1_stage{stage}_L2_0', [180, 128, 3, 1, 1]),
                (f'Mconv1_stage{stage}_L2_1', [128, 128, 3, 1, 1]),
                (f'Mconv1_stage{stage}_L2_2', [128, 128, 3, 1, 1])
            ])
            for i in range(2, 6):
                stage_blocks[f'Mconv{i}_stage{stage}_L2'] = OrderedDict([
                    (f'Mconv{i}_stage{stage}_L2_0', [384, 128, 3, 1, 1]),
                    (f'Mconv{i}_stage{stage}_L2_1', [128, 128, 3, 1, 1]),
                    (f'Mconv{i}_stage{stage}_L2_2', [128, 128, 3, 1, 1])
                ])
            stage_blocks[f'Mconv6_7_stage{stage}_L2'] = OrderedDict([
                (f'Mconv6_stage{stage}_L2', [384, 512, 1, 1, 0]),
                (f'Mconv7_stage{stage}_L2', [512, 52, 1, 1, 0])
            ])
        
        # L1 branch configurations
        stage_blocks['Mconv1_stage0_L1'] = OrderedDict([
            ('Mconv1_stage0_L1_0', [180, 96, 3, 1, 1]),
            ('Mconv1_stage0_L1_1', [96, 96, 3, 1, 1]),
            ('Mconv1_stage0_L1_2', [96, 96, 3, 1, 1])
        ])
        
        for i in range(2, 6):
            stage_blocks[f'Mconv{i}_stage0_L1'] = OrderedDict([
                (f'Mconv{i}_stage0_L1_0', [288, 96, 3, 1, 1]),
                (f'Mconv{i}_stage0_L1_1', [96, 96, 3, 1, 1]),
                (f'Mconv{i}_stage0_L1_2', [96, 96, 3, 1, 1])
            ])
            
        stage_blocks['Mconv6_7_stage0_L1'] = OrderedDict([
            ('Mconv6_stage0_L1', [288, 256, 1, 1, 0]),
            ('Mconv7_stage0_L1', [256, 26, 1, 1, 0])
        ])
        
        stage_blocks['Mconv1_stage1_L1'] = OrderedDict([
            ('Mconv1_stage1_L1_0', [206, 128, 3, 1, 1]),
            ('Mconv1_stage1_L1_1', [128, 128, 3, 1, 1]),
            ('Mconv1_stage1_L1_2', [128, 128, 3, 1, 1])
        ])
        
        for i in range(2, 6):
            stage_blocks[f'Mconv{i}_stage1_L1'] = OrderedDict([
                (f'Mconv{i}_stage1_L1_0', [384, 128, 3, 1, 1]),
                (f'Mconv{i}_stage1_L1_1', [128, 128, 3, 1, 1]),
                (f'Mconv{i}_stage1_L1_2', [128, 128, 3, 1, 1])
            ])
            
        stage_blocks['Mconv6_7_stage1_L1'] = OrderedDict([
            ('Mconv6_stage1_L1', [384, 512, 1, 1, 0]),
            ('Mconv7_stage1_L1', [512, 26, 1, 1, 0])
        ])
        
        # Build multi-conv modules
        for block_name in stage_blocks.keys():
            stage_blocks[block_name] = construct_multi_conv_layers(stage_blocks[block_name], no_relu_layers)
        
        self.stage_models = nn.ModuleDict(stage_blocks)
        
        # Freeze parameters for efficiency
        for param in self.parameters():
            param.requires_grad = False
            
    def _multi_conv_forward(self, x, models):
        """Forward pass through multi-convolution blocks"""
        outputs = []
        current_output = x
        for model in models:
            current_output = model(current_output)
            outputs.append(current_output)
        return torch.cat(outputs, 1)
        
    def forward(self, x):
        """Forward pass through the body pose model"""
        base_features = self.base_model(x)
        
        # L2 branch processing
        current_features = base_features
        for stage in range(4):
            current_features = self._multi_conv_forward(
                current_features, self.stage_models[f'Mconv1_stage{stage}_L2']
            )
            for layer in range(2, 6):
                current_features = self._multi_conv_forward(
                    current_features, self.stage_models[f'Mconv{layer}_stage{stage}_L2']
                )
            current_features = self.stage_models[f'Mconv6_7_stage{stage}_L2'][0](current_features)
            current_features = self.stage_models[f'Mconv6_7_stage{stage}_L2'][1](current_features)
            l2_output = current_features
            current_features = torch.cat([base_features, current_features], 1)
        
        # L1 branch - Stage 0
        current_features = self._multi_conv_forward(
            current_features, self.stage_models['Mconv1_stage0_L1']
        )
        for layer in range(2, 6):
            current_features = self._multi_conv_forward(
                current_features, self.stage_models[f'Mconv{layer}_stage0_L1']
            )
        current_features = self.stage_models['Mconv6_7_stage0_L1'][0](current_features)
        current_features = self.stage_models['Mconv6_7_stage0_L1'][1](current_features)
        stage0_l1_output = current_features
        current_features = torch.cat([base_features, stage0_l1_output, l2_output], 1)
        
        # L1 branch - Stage 1
        current_features = self._multi_conv_forward(
            current_features, self.stage_models['Mconv1_stage1_L1']
        )
        for layer in range(2, 6):
            current_features = self._multi_conv_forward(
                current_features, self.stage_models[f'Mconv{layer}_stage1_L1']
            )
        current_features = self.stage_models['Mconv6_7_stage1_L1'][0](current_features)
        stage1_l1_output = self.stage_models['Mconv6_7_stage1_L1'][1](current_features)
        
        return l2_output, stage1_l1_output


class HandPoseModel(nn.Module):
    """

    Hand pose estimation model using 21-point hand landmarks

    Developed by TechMatrix Solvers for ISL translation

    """
    
    def __init__(self):
        super(HandPoseModel, self).__init__()
        
        # Layers without ReLU activation
        no_relu_layers = [
            'conv6_2_CPM', 'Mconv7_stage2', 'Mconv7_stage3',
            'Mconv7_stage4', 'Mconv7_stage5', 'Mconv7_stage6'
        ]
        
        # Stage 1 - Feature extraction
        stage1_base = OrderedDict([
            ('conv1_1', [3, 64, 3, 1, 1]),
            ('conv1_2', [64, 64, 3, 1, 1]),
            ('pool1_stage1', [2, 2, 0]),
            ('conv2_1', [64, 128, 3, 1, 1]),
            ('conv2_2', [128, 128, 3, 1, 1]),
            ('pool2_stage1', [2, 2, 0]),
            ('conv3_1', [128, 256, 3, 1, 1]),
            ('conv3_2', [256, 256, 3, 1, 1]),
            ('conv3_3', [256, 256, 3, 1, 1]),
            ('conv3_4', [256, 256, 3, 1, 1]),
            ('pool3_stage1', [2, 2, 0]),
            ('conv4_1', [256, 512, 3, 1, 1]),
            ('conv4_2', [512, 512, 3, 1, 1]),
            ('conv4_3', [512, 512, 3, 1, 1]),
            ('conv4_4', [512, 512, 3, 1, 1]),
            ('conv5_1', [512, 512, 3, 1, 1]),
            ('conv5_2', [512, 512, 3, 1, 1]),
            ('conv5_3_CPM', [512, 128, 3, 1, 1])
        ])

        stage1_prediction = OrderedDict([
            ('conv6_1_CPM', [128, 512, 1, 1, 0]),
            ('conv6_2_CPM', [512, 22, 1, 1, 0])
        ])

        stage_blocks = {}
        stage_blocks['stage1_base'] = stage1_base
        stage_blocks['stage1_prediction'] = stage1_prediction

        # Stages 2-6 refinement
        for i in range(2, 7):
            stage_blocks[f'stage{i}'] = OrderedDict([
                (f'Mconv1_stage{i}', [150, 128, 7, 1, 3]),
                (f'Mconv2_stage{i}', [128, 128, 7, 1, 3]),
                (f'Mconv3_stage{i}', [128, 128, 7, 1, 3]),
                (f'Mconv4_stage{i}', [128, 128, 7, 1, 3]),
                (f'Mconv5_stage{i}', [128, 128, 7, 1, 3]),
                (f'Mconv6_stage{i}', [128, 128, 1, 1, 0]),
                (f'Mconv7_stage{i}', [128, 22, 1, 1, 0])
            ])

        # Build all stage models
        for block_name in stage_blocks.keys():
            stage_blocks[block_name] = construct_layers(stage_blocks[block_name], no_relu_layers)

        self.stage1_base_model = stage_blocks['stage1_base']
        self.stage1_prediction_model = stage_blocks['stage1_prediction']
        self.stage2_model = stage_blocks['stage2']
        self.stage3_model = stage_blocks['stage3']
        self.stage4_model = stage_blocks['stage4']
        self.stage5_model = stage_blocks['stage5']
        self.stage6_model = stage_blocks['stage6']
        
        # Freeze parameters for efficiency
        for param in self.parameters():
            param.requires_grad = False

    def forward(self, x):
        """Forward pass through the hand pose model"""
        base_features = self.stage1_base_model(x)
        stage1_output = self.stage1_prediction_model(base_features)
        
        # Stage 2
        stage2_input = torch.cat([stage1_output, base_features], 1)
        stage2_output = self.stage2_model(stage2_input)
        
        # Stage 3
        stage3_input = torch.cat([stage2_output, base_features], 1)
        stage3_output = self.stage3_model(stage3_input)
        
        # Stage 4
        stage4_input = torch.cat([stage3_output, base_features], 1)
        stage4_output = self.stage4_model(stage4_input)
        
        # Stage 5
        stage5_input = torch.cat([stage4_output, base_features], 1)
        stage5_output = self.stage5_model(stage5_input)
        
        # Stage 6
        stage6_input = torch.cat([stage5_output, base_features], 1)
        stage6_output = self.stage6_model(stage6_input)
        
        return stage6_output


# Factory functions for easy model instantiation
def create_bodypose_model():
    """Create and return body pose detection model"""
    return BodyPose25Model()


def create_handpose_model():
    """Create and return hand pose detection model"""
    return HandPoseModel()