-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
504 lines (394 loc) · 17.4 KB
/
modules.py
File metadata and controls
504 lines (394 loc) · 17.4 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import torch
import torch.nn as nn
from torchvision import models as modules
import logging
# Use logger configured in main application
logger = logging.getLogger("modules")
class AlexNet(nn.Module):
"""
AlexNet architecture for image classification.
Historic CNN architecture that demonstrated the effectiveness of deep learning
on ImageNet in 2012. Features large convolutional kernels and aggressive
max pooling for computational efficiency. Suitable for basic classification
tasks with moderate accuracy requirements.
Args:
classes: Number of output classification classes
channels: Input image channels (3 for RGB, 1 for grayscale)
weights: Whether to initialize with ImageNet pre-trained weights
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing AlexNet: {classes} classes, {channels} input channels")
# Load base AlexNet architecture
self.module = modules.alexnet(
weights=modules.AlexNet_Weights.DEFAULT if weights else None
)
# Modify input layer for non-RGB images
if channels != 3:
self.module.features[0] = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=11,
stride=4,
padding=2,
)
# Replace final classification layer
features = self.module.classifier[6].in_features
self.module.classifier[6] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute forward pass through AlexNet architecture."""
return self.module(x)
class ResNet50(nn.Module):
"""
ResNet-50 architecture with residual connections.
50-layer convolutional network utilizing skip connections to address
vanishing gradient problems in deep networks. Widely adopted architecture
that serves as strong baseline for image classification and feature
extraction tasks.
Args:
classes: Number of target classification classes
channels: Input image channels (typically 3 for RGB)
weights: Whether to use ImageNet pre-trained initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing ResNet-50: {classes} classes, {channels} input channels")
# Load ResNet-50 with optional pre-trained weights
self.module = modules.resnet50(
weights=modules.ResNet50_Weights.DEFAULT if weights else None
)
# Adjust input layer for non-standard channel counts
if channels != 3:
self.module.conv1 = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=7,
stride=2,
padding=3,
bias=False,
)
# Configure final classification layer
features = self.module.fc.in_features # Standard ResNet-50 has 2048 features
self.module.fc = nn.Linear(in_features=features, out_features=classes)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute ResNet-50 forward propagation."""
return self.module(x)
class ResNet18(nn.Module):
"""
ResNet-18 lightweight architecture.
Compact 18-layer residual network offering faster training and lower
memory requirements compared to deeper variants. Maintains residual
connection benefits while providing practical efficiency for resource-
constrained environments.
Args:
classes: Number of output classification classes
channels: Input image channels (3 for RGB)
weights: Whether to use pre-trained weight initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing ResNet-18: {classes} classes, {channels} input channels")
# Load lightweight ResNet variant
self.module = modules.resnet18(
weights=modules.ResNet18_Weights.DEFAULT if weights else None
)
# Handle non-RGB input configurations
if channels != 3:
self.module.conv1 = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=7,
stride=2,
padding=3,
bias=False,
)
# Configure classification head
features = self.module.fc.in_features # ResNet-18 has 512 features
self.module.fc = nn.Linear(in_features=features, out_features=classes)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute ResNet-18 forward pass."""
return self.module(x)
class MobileNetLarge(nn.Module):
"""
MobileNet-V3 Large for efficient mobile deployment.
Optimized architecture using depthwise separable convolutions, squeeze-
and-excitation blocks, and hard-swish activations. Designed for mobile
and edge computing with balanced accuracy-efficiency trade-offs.
Args:
classes: Number of classification classes
channels: Input image channels
weights: Whether to use pre-trained weights
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing MobileNet-V3 Large: {classes} classes, {channels} input channels")
# Load mobile-optimized architecture
self.module = modules.mobilenet_v3_large(
weights=modules.MobileNet_V3_Large_Weights.DEFAULT if weights else None
)
# Modify initial convolution for custom channel counts
if channels != 3:
self.module.features[0][0] = nn.Conv2d(
in_channels=channels,
out_channels=16,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
# Update classification layer
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute MobileNet-V3 Large forward propagation."""
return self.module(x)
class MobileNetSmall(nn.Module):
"""
MobileNet-V3 Small for ultra-efficient deployment.
Most compact MobileNet variant optimized for extremely resource-constrained
environments. Prioritizes computational efficiency over accuracy for
real-time applications on limited hardware.
Args:
classes: Number of output classification classes
channels: Input image channels (typically 3)
weights: Whether to use pre-trained initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing MobileNet-V3 Small: {classes} classes, {channels} input channels")
# Load most compact mobile variant
self.module = modules.mobilenet_v3_small(
weights=modules.MobileNet_V3_Small_Weights.DEFAULT if weights else None
)
# Configure input layer for custom channels
if channels != 3:
self.module.features[0][0] = nn.Conv2d(
in_channels=channels,
out_channels=16,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
# Update final classifier
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute MobileNet-V3 Small forward pass."""
return self.module(x)
class VGG11(nn.Module):
"""
VGG-11 basic convolutional architecture.
Straightforward CNN design using uniform 3x3 convolutions and max pooling.
Simple architecture that serves as educational baseline for understanding
convolutional neural network fundamentals.
Args:
classes: Number of classification classes
channels: Input image channels
weights: Whether to use ImageNet pre-trained weights
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing VGG-11: {classes} classes, {channels} input channels")
# Load basic VGG architecture
self.module = modules.vgg11(
weights=modules.VGG11_Weights.DEFAULT if weights else None
)
# Modify input layer for different channel configurations
if channels != 3:
self.module.features[0] = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=3,
padding=1
)
# Configure classification layer
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute VGG-11 forward propagation."""
return self.module(x)
class VGG16(nn.Module):
"""
VGG-16 deep convolutional architecture.
Standard 16-layer VGG implementation using small receptive fields and
deep structure. More complex feature learning capability compared to
VGG-11 while maintaining architectural simplicity.
Args:
classes: Number of output classification classes
channels: Input image channels (3 for RGB)
weights: Whether to use pre-trained initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing VGG-16: {classes} classes, {channels} input channels")
# Load standard VGG-16 architecture
self.module = modules.vgg16(
weights=modules.VGG16_Weights.DEFAULT if weights else None
)
# Handle non-standard input channels
if channels != 3:
self.module.features[0] = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=3,
padding=1
)
# Configure final classification layer
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute VGG-16 forward pass."""
return self.module(x)
class EfficientNetB0(nn.Module):
"""
EfficientNet-B0 compound scaling baseline.
Optimally balanced architecture using compound scaling methodology
to jointly optimize network depth, width, and resolution. Provides
strong accuracy-efficiency trade-offs through systematic scaling.
Args:
classes: Number of classification classes to predict
channels: Input image channels
weights: Whether to use pre-trained initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing EfficientNet-B0: {classes} classes, {channels} input channels")
# Load baseline EfficientNet architecture
self.module = modules.efficientnet_b0(
weights=modules.EfficientNet_B0_Weights.DEFAULT if weights else None
)
# Modify stem convolution for custom channels
if channels != 3:
self.module.features[0][0] = nn.Conv2d(
in_channels=channels,
out_channels=32,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
# Configure classification head
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute EfficientNet-B0 forward propagation."""
return self.module(x)
class EfficientNetB3(nn.Module):
"""
EfficientNet-B3 scaled architecture for enhanced accuracy.
Compound-scaled version of EfficientNet-B0 with increased depth, width,
and resolution parameters. Provides improved classification performance
while maintaining computational efficiency relative to conventional scaling.
Args:
classes: Number of output classification classes
channels: Input image channels (typically 3)
weights: Whether to use pre-trained weights
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing EfficientNet-B3: {classes} classes, {channels} input channels")
# Load scaled EfficientNet variant
self.module = modules.efficientnet_b3(
weights=modules.EfficientNet_B3_Weights.DEFAULT if weights else None
)
# Handle custom input channel configurations
if channels != 3:
self.module.features[0][0] = nn.Conv2d(
in_channels=channels,
out_channels=40, # Wider stem due to compound scaling
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
# Update classification layer
features = self.module.classifier[-1].in_features
self.module.classifier[-1] = nn.Linear(
in_features=features, out_features=classes
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute EfficientNet-B3 forward pass."""
return self.module(x)
class DenseNet121(nn.Module):
"""
DenseNet-121 with dense connectivity patterns.
Architecture where each layer receives feature maps from all preceding
layers, promoting feature reuse and gradient flow. The 121-layer
configuration provides strong performance with parameter efficiency
through dense connections.
Args:
classes: Number of classification classes
channels: Input image channels
weights: Whether to use ImageNet pre-trained weights
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing DenseNet-121: {classes} classes, {channels} input channels")
# Load DenseNet with dense connectivity
self.module = modules.densenet121(
weights=modules.DenseNet121_Weights.DEFAULT if weights else None
)
# Configure input layer for different channel counts
if channels != 3:
self.module.features.conv0 = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=7,
stride=2,
padding=3,
bias=False,
)
# Update classification layer
features = self.module.classifier.in_features
self.module.classifier = nn.Linear(in_features=features, out_features=classes)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute DenseNet-121 forward propagation."""
return self.module(x)
class DenseNet169(nn.Module):
"""
DenseNet-169 deep dense architecture.
Extended dense connectivity network with 169 layers providing enhanced
feature learning capability. Maintains parameter efficiency through
feature reuse while offering increased model capacity compared to
DenseNet-121.
Args:
classes: Number of output classification classes
channels: Input image channels (3 for RGB)
weights: Whether to use pre-trained initialization
"""
def __init__(self, classes: int, channels: int = 3, weights: bool = True) -> None:
super().__init__()
logger.info(f"Initializing DenseNet-169: {classes} classes, {channels} input channels")
# Load deeper DenseNet variant
self.module = modules.densenet169(
weights=modules.DenseNet169_Weights.DEFAULT if weights else None
)
# Modify input layer for custom channel configurations
if channels != 3:
self.module.features.conv0 = nn.Conv2d(
in_channels=channels,
out_channels=64,
kernel_size=7,
stride=2,
padding=3,
bias=False,
)
# Configure final classifier
features = self.module.classifier.in_features
self.module.classifier = nn.Linear(in_features=features, out_features=classes)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Execute DenseNet-169 forward pass."""
return self.module(x)