-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinception_v3.py
63 lines (50 loc) · 1.69 KB
/
inception_v3.py
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
'''
Inception_V3 model for image embedding
Author:
Yuchen (Peter) Ge
E-mail:
Usage:
$ from inception_v3 import new_inception_v3 as icpv3
Attributes:
set_parameter_requires_grad(): freeze all parameters to extract features
new_inception_v3(): customize num of classes after image embeddings
'''
#%%
import torchvision.models as models
import torch.nn as nn
#%%
def set_parameter_requires_grad(model, feature_extracting):
'''
Set trainbale parameters.
For feature extraction, freeze all parameters but those in the last layer.
Args:
model (inception_v3): the pre-trained inception v3 model
feature_extracting (bool): set true to freeze all paraters
Returns:
None
'''
if feature_extracting:
for param in model.parameters():
param.requires_grad = False
#%%
def new_inception_v3(num_classes):
'''
Create our own inception model.
Args:
num_classes (int): number of classes in the output
Returns:
inception (inception_v3): model with replaced fully connected layers and frozen parameters
'''
inception = models.inception_v3(pretrained=True)
set_parameter_requires_grad(inception, True)
inception.AuxLogits.fc = nn.Linear(768, num_classes)
inception.fc = nn.Linear(2048, num_classes)
return inception
#%%
# Find total parameters and trainable parameters
net = new_inception_v3(100) # (2048+768)*100 + 100+100 = 281,800
total_params = sum(p.numel() for p in net.parameters())
print(f'{total_params:,} total parameters.')
total_trainable_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print(f'{total_trainable_params:,} training parameters.')