|
| 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 |
0 commit comments