1
- from typing import Optional , Tuple , Type , Union , cast
1
+ from typing import Optional , Tuple , Type , Union
2
+ from warnings import warn
2
3
3
4
import torch
4
5
import torch .nn as nn
@@ -19,24 +20,37 @@ def googlenet(
19
20
) -> "InceptionV1" :
20
21
r"""GoogLeNet (also known as Inception v1 & Inception 5h) model architecture from
21
22
`"Going Deeper with Convolutions" <http://arxiv.org/abs/1409.4842>`_.
23
+
22
24
Args:
25
+
23
26
pretrained (bool, optional): If True, returns a model pre-trained on ImageNet.
27
+ Default: False
24
28
progress (bool, optional): If True, displays a progress bar of the download to
25
29
stderr
30
+ Default: True
26
31
model_path (str, optional): Optional path for InceptionV1 model file.
32
+ Default: None
27
33
replace_relus_with_redirectedrelu (bool, optional): If True, return pretrained
28
34
model with Redirected ReLU in place of ReLU layers.
35
+ Default: *True* when pretrained is True otherwise *False*
29
36
use_linear_modules_only (bool, optional): If True, return pretrained
30
37
model with all nonlinear layers replaced with linear equivalents.
38
+ Default: False
31
39
aux_logits (bool, optional): If True, adds two auxiliary branches that can
32
- improve training. Default: *False* when pretrained is True otherwise *True*
40
+ improve training.
41
+ Default: False
33
42
out_features (int, optional): Number of output features in the model used for
34
- training. Default: 1008 when pretrained is True.
43
+ training.
44
+ Default: 1008
35
45
transform_input (bool, optional): If True, preprocesses the input according to
36
- the method with which it was trained on ImageNet. Default: *False*
46
+ the method with which it was trained on ImageNet.
47
+ Default: False
37
48
bgr_transform (bool, optional): If True and transform_input is True, perform an
38
49
RGB to BGR transform in the internal preprocessing.
39
- Default: *False*
50
+ Default: False
51
+
52
+ Returns:
53
+ **InceptionV1** (InceptionV1): An Inception5h model.
40
54
"""
41
55
42
56
if pretrained :
@@ -69,6 +83,8 @@ def googlenet(
69
83
70
84
# Better version of Inception V1 / GoogleNet for Inception5h
71
85
class InceptionV1 (nn .Module ):
86
+ __constants__ = ["aux_logits" , "transform_input" , "bgr_transform" ]
87
+
72
88
def __init__ (
73
89
self ,
74
90
out_features : int = 1008 ,
@@ -78,7 +94,29 @@ def __init__(
78
94
replace_relus_with_redirectedrelu : bool = False ,
79
95
use_linear_modules_only : bool = False ,
80
96
) -> None :
81
- super (InceptionV1 , self ).__init__ ()
97
+ """
98
+ Args:
99
+
100
+ replace_relus_with_redirectedrelu (bool, optional): If True, return
101
+ pretrained model with Redirected ReLU in place of ReLU layers.
102
+ Default: False
103
+ use_linear_modules_only (bool, optional): If True, return pretrained
104
+ model with all nonlinear layers replaced with linear equivalents.
105
+ Default: False
106
+ aux_logits (bool, optional): If True, adds two auxiliary branches that can
107
+ improve training.
108
+ Default: False
109
+ out_features (int, optional): Number of output features in the model used
110
+ for training.
111
+ Default: 1008
112
+ transform_input (bool, optional): If True, preprocesses the input according
113
+ to the method with which it was trained on ImageNet.
114
+ Default: False
115
+ bgr_transform (bool, optional): If True and transform_input is True,
116
+ perform an RGB to BGR transform in the internal preprocessing.
117
+ Default: False
118
+ """
119
+ super ().__init__ ()
82
120
self .aux_logits = aux_logits
83
121
self .transform_input = transform_input
84
122
self .bgr_transform = bgr_transform
@@ -99,7 +137,6 @@ def __init__(
99
137
out_channels = 64 ,
100
138
kernel_size = (7 , 7 ),
101
139
stride = (2 , 2 ),
102
- padding = 3 ,
103
140
groups = 1 ,
104
141
bias = True ,
105
142
)
@@ -121,7 +158,6 @@ def __init__(
121
158
out_channels = 192 ,
122
159
kernel_size = (3 , 3 ),
123
160
stride = (1 , 1 ),
124
- padding = 1 ,
125
161
groups = 1 ,
126
162
bias = True ,
127
163
)
@@ -163,9 +199,18 @@ def __init__(
163
199
self .fc = nn .Linear (1024 , out_features )
164
200
165
201
def _transform_input (self , x : torch .Tensor ) -> torch .Tensor :
202
+ """
203
+ Args:
204
+
205
+ x (torch.Tensor): An input tensor to normalize and scale the values of.
206
+
207
+ Returns:
208
+ x (torch.Tensor): A transformed tensor.
209
+ """
166
210
if self .transform_input :
167
211
assert x .dim () == 3 or x .dim () == 4
168
- assert x .min () >= 0.0 and x .max () <= 1.0
212
+ if x .min () < 0.0 or x .max () > 1.0 :
213
+ warn ("Model input has values outside of the range [0, 1]." )
169
214
x = x .unsqueeze (0 ) if x .dim () == 3 else x
170
215
x = x * 255 - 117
171
216
x = x [:, [2 , 1 , 0 ]] if self .bgr_transform else x
@@ -174,6 +219,15 @@ def _transform_input(self, x: torch.Tensor) -> torch.Tensor:
174
219
def forward (
175
220
self , x : torch .Tensor
176
221
) -> Union [torch .Tensor , Tuple [torch .Tensor , torch .Tensor , torch .Tensor ]]:
222
+ """
223
+ Args:
224
+
225
+ x (torch.Tensor): An input tensor to normalize and scale the values of.
226
+
227
+ Returns:
228
+ x (torch.Tensor or tuple of torch.Tensor): A single or multiple output
229
+ tensors from the model.
230
+ """
177
231
x = self ._transform_input (x )
178
232
x = self .conv1 (x )
179
233
x = self .conv1_relu (x )
@@ -212,7 +266,7 @@ def forward(
212
266
x = self .drop (x )
213
267
x = self .fc (x )
214
268
if not self .aux_logits :
215
- return cast ( torch . Tensor , x )
269
+ return x
216
270
else :
217
271
return x , aux1_output , aux2_output
218
272
@@ -230,7 +284,25 @@ def __init__(
230
284
activ : Type [nn .Module ] = nn .ReLU ,
231
285
p_layer : Type [nn .Module ] = nn .MaxPool2d ,
232
286
) -> None :
233
- super (InceptionModule , self ).__init__ ()
287
+ """
288
+ Args:
289
+
290
+ in_channels (int, optional): The number of input channels to use for the
291
+ inception module.
292
+ c1x1 (int, optional):
293
+ c3x3reduce (int, optional):
294
+ c3x3 (int, optional):
295
+ c5x5reduce (int, optional):
296
+ c5x5 (int, optional):
297
+ pool_proj (int, optional):
298
+ activ (type of nn.Module, optional): The nn.Module class type to use for
299
+ activation layers.
300
+ Default: nn.ReLU
301
+ p_layer (type of nn.Module, optional): The nn.Module class type to use for
302
+ pooling layers.
303
+ Default: nn.MaxPool2d
304
+ """
305
+ super ().__init__ ()
234
306
self .conv_1x1 = nn .Conv2d (
235
307
in_channels = in_channels ,
236
308
out_channels = c1x1 ,
@@ -254,7 +326,6 @@ def __init__(
254
326
out_channels = c3x3 ,
255
327
kernel_size = (3 , 3 ),
256
328
stride = (1 , 1 ),
257
- padding = 1 ,
258
329
groups = 1 ,
259
330
bias = True ,
260
331
)
@@ -273,7 +344,6 @@ def __init__(
273
344
out_channels = c5x5 ,
274
345
kernel_size = (5 , 5 ),
275
346
stride = (1 , 1 ),
276
- padding = 1 ,
277
347
groups = 1 ,
278
348
bias = True ,
279
349
)
@@ -289,6 +359,14 @@ def __init__(
289
359
)
290
360
291
361
def forward (self , x : torch .Tensor ) -> torch .Tensor :
362
+ """
363
+ Args:
364
+
365
+ x (torch.Tensor): An input tensor to pass through the Inception Module.
366
+
367
+ Returns:
368
+ x (torch.Tensor): The output tensor of the Inception Module.
369
+ """
292
370
c1x1 = self .conv_1x1 (x )
293
371
294
372
c3x3 = self .conv_3x3_reduce (x )
@@ -311,31 +389,51 @@ def __init__(
311
389
out_features : int = 1008 ,
312
390
activ : Type [nn .Module ] = nn .ReLU ,
313
391
) -> None :
314
- super (AuxBranch , self ).__init__ ()
392
+ """
393
+ Args:
394
+
395
+ in_channels (int, optional): The number of input channels to use for the
396
+ auxiliary branch.
397
+ Default: 508
398
+ out_features (int, optional): The number of output features to use for the
399
+ auxiliary branch.
400
+ Default: 1008
401
+ activ (type of nn.Module, optional): The nn.Module class type to use for
402
+ activation layers.
403
+ Default: nn.ReLU
404
+ """
405
+ super ().__init__ ()
315
406
self .avg_pool = nn .AdaptiveAvgPool2d ((4 , 4 ))
316
- self .loss_conv = nn .Conv2d (
407
+ self .conv = nn .Conv2d (
317
408
in_channels = in_channels ,
318
409
out_channels = 128 ,
319
410
kernel_size = (1 , 1 ),
320
411
stride = (1 , 1 ),
321
412
groups = 1 ,
322
413
bias = True ,
323
414
)
324
- self .loss_conv_relu = activ ()
325
- self .loss_fc = nn .Linear (in_features = 2048 , out_features = 1024 , bias = True )
326
- self .loss_fc_relu = activ ()
327
- self .loss_dropout = nn .Dropout (0.699999988079071 )
328
- self .loss_classifier = nn .Linear (
329
- in_features = 1024 , out_features = out_features , bias = True
330
- )
415
+ self .conv_relu = activ ()
416
+ self .fc1 = nn .Linear (in_features = 2048 , out_features = 1024 , bias = True )
417
+ self .fc1_relu = activ ()
418
+ self .dropout = nn .Dropout (0.699999988079071 )
419
+ self .fc2 = nn .Linear (in_features = 1024 , out_features = out_features , bias = True )
331
420
332
421
def forward (self , x : torch .Tensor ) -> torch .Tensor :
422
+ """
423
+ Args:
424
+
425
+ x (torch.Tensor): An input tensor to pass through the auxiliary branch
426
+ module.
427
+
428
+ Returns:
429
+ x (torch.Tensor): The output tensor of the auxiliary branch module.
430
+ """
333
431
x = self .avg_pool (x )
334
- x = self .loss_conv (x )
335
- x = self .loss_conv_relu (x )
432
+ x = self .conv (x )
433
+ x = self .conv_relu (x )
336
434
x = torch .flatten (x , 1 )
337
- x = self .loss_fc (x )
338
- x = self .loss_fc_relu (x )
339
- x = self .loss_dropout (x )
340
- x = self .loss_classifier (x )
435
+ x = self .fc1 (x )
436
+ x = self .fc1_relu (x )
437
+ x = self .dropout (x )
438
+ x = self .fc2 (x )
341
439
return x
0 commit comments