Skip to content

Commit 2da0b4d

Browse files
committed
Add inception_v3 models via torchvision, 4 different pretrained weight choices
1 parent af1a68d commit 2da0b4d

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Several (less common) features that I often utilize in my projects are included.
110110
* Mixup (as in https://arxiv.org/abs/1710.09412) - currently implementing/testing
111111
* An inference script that dumps output to CSV is provided as an example
112112

113-
### Custom Weights
113+
### Self-trained Weights
114114
I've leveraged the training scripts in this repository to train a few of the models with missing weights to good levels of performance. These numbers are all for 224x224 training and validation image sizing with the usual 87.5% validation crop.
115115

116116
|Model | Prec@1 (Err) | Prec@5 (Err) | Param # | Image Scaling |
@@ -125,8 +125,12 @@ I've leveraged the training scripts in this repository to train a few of the mod
125125
### Ported Weights
126126
|Model | Prec@1 (Err) | Prec@5 (Err) | Param # | Image Scaling | Source |
127127
|---|---|---|---|---|---|
128-
| MNASNet 1.00 (B1) | 72.398 (27.602) | 90.930 (9.070) | 4.36M | bicubic | [Google TFLite](https://github.com/tensorflow/tpu/tree/master/models/official/mnasnet) |
128+
| Gluon Inception-V3 | 78.804 (21.196) | 94.380 (5.620) | 27.16M | bicubic | [MxNet Gluon](https://gluon-cv.mxnet.io/model_zoo/classification.html) |
129+
| Tensorflow Inception-V3 | 77.856 (22.144) | 93.644 (6.356) | 27.16M | bicubic | [Tensorflow Slim](https://github.com/tensorflow/models/tree/master/research/slim) |
130+
| Adversarially trained Inception-V3 | 77.576 (22.424) | 93.724 (6.276) | 27.16M | bicubic | [Tensorflow Adv models](https://github.com/tensorflow/models/tree/master/research/adv_imagenet_models) |
129131
| SE-MNASNet 1.00 (A1) | 73.086 (26.914) | 91.336 (8.664) | 3.87M | bicubic | [Google TFLite](https://github.com/tensorflow/tpu/tree/master/models/official/mnasnet) |
132+
| MNASNet 1.00 (B1) | 72.398 (27.602) | 90.930 (9.070) | 4.36M | bicubic | [Google TFLite](https://github.com/tensorflow/tpu/tree/master/models/official/mnasnet) |
133+
130134

131135
NOTE: For some reason I can't hit the stated accuracy with my impl of MNASNet and Google's tflite weights. Using a TF equivalent to 'SAME' padding was important to get > 70%, but something small is still missing. Trying to train my own weights from scratch with these models has so far to leveled off in the same 72-73% range.
132136

models/inception_v3.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from torchvision.models import Inception3
2+
from models.helpers import load_pretrained
3+
from data import IMAGENET_DEFAULT_STD, IMAGENET_DEFAULT_MEAN, IMAGENET_INCEPTION_MEAN, IMAGENET_INCEPTION_STD
4+
5+
default_cfgs = {
6+
# original PyTorch weights, ported from Tensorflow but modified
7+
'inception_v3': {
8+
'url': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
9+
'input_size': (3, 299, 299),
10+
'crop_pct': 0.875,
11+
'interpolation': 'bicubic',
12+
'mean': IMAGENET_INCEPTION_MEAN, # also works well enough with resnet defaults
13+
'std': IMAGENET_INCEPTION_STD, # also works well enough with resnet defaults
14+
'num_classes': 1000,
15+
'first_conv': 'conv0',
16+
'classifier': 'fc'
17+
},
18+
# my port of Tensorflow SLIM weights (http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz)
19+
'tf_inception_v3': {
20+
'url': 'https://www.dropbox.com/s/xdh32bpdgqzpx8t/tf_inception_v3-e0069de4.pth?dl=1',
21+
'input_size': (3, 299, 299),
22+
'crop_pct': 0.875,
23+
'interpolation': 'bicubic',
24+
'mean': IMAGENET_INCEPTION_MEAN,
25+
'std': IMAGENET_INCEPTION_STD,
26+
'num_classes': 1001,
27+
'first_conv': 'conv0',
28+
'classifier': 'fc'
29+
},
30+
# my port of Tensorflow adversarially trained Inception V3 from
31+
# http://download.tensorflow.org/models/adv_inception_v3_2017_08_18.tar.gz
32+
'adv_inception_v3': {
33+
'url': 'https://www.dropbox.com/s/b5pudqh84gtl7i8/adv_inception_v3-9e27bd63.pth?dl=1',
34+
'input_size': (3, 299, 299),
35+
'crop_pct': 0.875,
36+
'interpolation': 'bicubic',
37+
'mean': IMAGENET_INCEPTION_MEAN,
38+
'std': IMAGENET_INCEPTION_STD,
39+
'num_classes': 1001,
40+
'first_conv': 'conv0',
41+
'classifier': 'fc'
42+
},
43+
# from gluon pretrained models, best performing in terms of accuracy/loss metrics
44+
# https://gluon-cv.mxnet.io/model_zoo/classification.html
45+
'gluon_inception_v3': {
46+
'url': 'https://www.dropbox.com/s/8uv6wrl6it6394u/gluon_inception_v3-9f746940.pth?dl=1',
47+
'input_size': (3, 299, 299),
48+
'crop_pct': 0.875,
49+
'interpolation': 'bicubic',
50+
'mean': IMAGENET_DEFAULT_MEAN, # also works well with inception defaults
51+
'std': IMAGENET_DEFAULT_STD, # also works well with inception defaults
52+
'num_classes': 1000,
53+
'first_conv': 'conv0',
54+
'classifier': 'fc'
55+
}
56+
}
57+
58+
59+
def _assert_default_kwargs(kwargs):
60+
# for imported models (ie torchvision) without capability to change these params,
61+
# make sure they aren't being set to non-defaults
62+
assert kwargs.pop('global_pool', 'avg') == 'avg'
63+
assert kwargs.pop('drop_rate', 0.) == 0.
64+
65+
66+
def inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
67+
# original PyTorch weights, ported from Tensorflow but modified
68+
default_cfg = default_cfgs['inception_v3']
69+
assert in_chans == 3
70+
_assert_default_kwargs(kwargs)
71+
model = Inception3(num_classes=num_classes, aux_logits=True, transform_input=False)
72+
if pretrained:
73+
load_pretrained(model, default_cfg, num_classes, in_chans)
74+
model.default_cfg = default_cfg
75+
return model
76+
77+
78+
def tf_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
79+
# my port of Tensorflow SLIM weights (http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz)
80+
default_cfg = default_cfgs['tf_inception_v3']
81+
assert in_chans == 3
82+
_assert_default_kwargs(kwargs)
83+
model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
84+
if pretrained:
85+
load_pretrained(model, default_cfg, num_classes, in_chans)
86+
model.default_cfg = default_cfg
87+
return model
88+
89+
90+
def adv_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
91+
# my port of Tensorflow adversarially trained Inception V3 from
92+
# http://download.tensorflow.org/models/adv_inception_v3_2017_08_18.tar.gz
93+
default_cfg = default_cfgs['adv_inception_v3']
94+
assert in_chans == 3
95+
_assert_default_kwargs(kwargs)
96+
model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
97+
if pretrained:
98+
load_pretrained(model, default_cfg, num_classes, in_chans)
99+
model.default_cfg = default_cfg
100+
return model
101+
102+
103+
def gluon_inception_v3(num_classes=1000, in_chans=3, pretrained=False, **kwargs):
104+
# from gluon pretrained models, best performing in terms of accuracy/loss metrics
105+
# https://gluon-cv.mxnet.io/model_zoo/classification.html
106+
default_cfg = default_cfgs['gluon_inception_v3']
107+
assert in_chans == 3
108+
_assert_default_kwargs(kwargs)
109+
model = Inception3(num_classes=num_classes, aux_logits=False, transform_input=False)
110+
if pretrained:
111+
load_pretrained(model, default_cfg, num_classes, in_chans)
112+
model.default_cfg = default_cfg
113+
return model

models/model_factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
semnasnet_050, semnasnet_075, semnasnet_100, semnasnet_140, tflite_semnasnet_100, mnasnet_small,\
1414
mobilenetv1_100, mobilenetv2_100, mobilenetv3_050, mobilenetv3_075, mobilenetv3_100,\
1515
fbnetc_100, chamnetv1_100, chamnetv2_100, spnasnet_100
16+
from models.inception_v3 import inception_v3, gluon_inception_v3, tf_inception_v3, adv_inception_v3
1617

1718
from models.helpers import load_checkpoint
1819

0 commit comments

Comments
 (0)