|
| 1 | +from timm import create_model |
| 2 | +import torch.nn as nn |
| 3 | +from ._base import EncoderMixin |
| 4 | + |
| 5 | + |
| 6 | +def make_divisible(x, divisible_by=8): |
| 7 | + import numpy as np |
| 8 | + return int(np.ceil(x * 1. / divisible_by) * divisible_by) |
| 9 | + |
| 10 | + |
| 11 | +class MobileNetV3Encoder(nn.Module, EncoderMixin): |
| 12 | + def __init__(self, model, width_mult, depth=5, **kwargs): |
| 13 | + super().__init__() |
| 14 | + self._depth = depth |
| 15 | + if 'small' in str(model): |
| 16 | + self.mode = 'small' |
| 17 | + self._out_channels = (16*width_mult, 16*width_mult, 24*width_mult, 48*width_mult, 576*width_mult) |
| 18 | + self._out_channels = tuple(map(make_divisible, self._out_channels)) |
| 19 | + elif 'large' in str(model): |
| 20 | + self.mode = 'large' |
| 21 | + self._out_channels = (16*width_mult, 24*width_mult, 40*width_mult, 112*width_mult, 960*width_mult) |
| 22 | + self._out_channels = tuple(map(make_divisible, self._out_channels)) |
| 23 | + else: |
| 24 | + self.mode = 'None' |
| 25 | + raise ValueError( |
| 26 | + 'MobileNetV3 mode should be small or large, got {}'.format(self.mode)) |
| 27 | + self._out_channels = (3,) + self._out_channels |
| 28 | + self._in_channels = 3 |
| 29 | + # minimal models replace hardswish with relu |
| 30 | + model = create_model(model_name=model, |
| 31 | + scriptable=True, # torch.jit scriptable |
| 32 | + exportable=True, # onnx export |
| 33 | + features_only=True) |
| 34 | + self.conv_stem = model.conv_stem |
| 35 | + self.bn1 = model.bn1 |
| 36 | + self.act1 = model.act1 |
| 37 | + self.blocks = model.blocks |
| 38 | + |
| 39 | + def get_stages(self): |
| 40 | + if self.mode == 'small': |
| 41 | + return [ |
| 42 | + nn.Identity(), |
| 43 | + nn.Sequential(self.conv_stem, self.bn1, self.act1), |
| 44 | + self.blocks[0], |
| 45 | + self.blocks[1], |
| 46 | + self.blocks[2:4], |
| 47 | + self.blocks[4:], |
| 48 | + ] |
| 49 | + elif self.mode == 'large': |
| 50 | + return [ |
| 51 | + nn.Identity(), |
| 52 | + nn.Sequential(self.conv_stem, self.bn1, self.act1, self.blocks[0]), |
| 53 | + self.blocks[1], |
| 54 | + self.blocks[2], |
| 55 | + self.blocks[3:5], |
| 56 | + self.blocks[5:], |
| 57 | + ] |
| 58 | + else: |
| 59 | + ValueError('MobileNetV3 mode should be small or large, got {}'.format(self.mode)) |
| 60 | + |
| 61 | + def forward(self, x): |
| 62 | + stages = self.get_stages() |
| 63 | + |
| 64 | + features = [] |
| 65 | + for i in range(self._depth + 1): |
| 66 | + x = stages[i](x) |
| 67 | + features.append(x) |
| 68 | + |
| 69 | + return features |
| 70 | + |
| 71 | + def load_state_dict(self, state_dict, **kwargs): |
| 72 | + state_dict.pop('conv_head.weight') |
| 73 | + state_dict.pop('conv_head.bias') |
| 74 | + state_dict.pop('classifier.weight') |
| 75 | + state_dict.pop('classifier.bias') |
| 76 | + super().load_state_dict(state_dict, **kwargs) |
| 77 | + |
| 78 | + |
| 79 | +mobilenetv3_weights = { |
| 80 | + 'tf_mobilenetv3_large_075': { |
| 81 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_075-150ee8b0.pth' |
| 82 | + }, |
| 83 | + 'tf_mobilenetv3_large_100': { |
| 84 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_100-427764d5.pth' |
| 85 | + }, |
| 86 | + 'tf_mobilenetv3_large_minimal_100': { |
| 87 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_minimal_100-8596ae28.pth' |
| 88 | + }, |
| 89 | + 'tf_mobilenetv3_small_075': { |
| 90 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_075-da427f52.pth' |
| 91 | + }, |
| 92 | + 'tf_mobilenetv3_small_100': { |
| 93 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_100-37f49e2b.pth' |
| 94 | + }, |
| 95 | + 'tf_mobilenetv3_small_minimal_100': { |
| 96 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_minimal_100-922a7843.pth' |
| 97 | + }, |
| 98 | + |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +pretrained_settings = {} |
| 103 | +for model_name, sources in mobilenetv3_weights.items(): |
| 104 | + pretrained_settings[model_name] = {} |
| 105 | + for source_name, source_url in sources.items(): |
| 106 | + pretrained_settings[model_name][source_name] = { |
| 107 | + "url": source_url, |
| 108 | + 'input_range': [0, 1], |
| 109 | + 'mean': [0.485, 0.456, 0.406], |
| 110 | + 'std': [0.229, 0.224, 0.225], |
| 111 | + 'input_space': 'RGB', |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +timm_mobilenetv3_encoders = { |
| 116 | + 'timm-mobilenetv3_large_075': { |
| 117 | + 'encoder': MobileNetV3Encoder, |
| 118 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_075'], |
| 119 | + 'params': { |
| 120 | + 'model': 'tf_mobilenetv3_large_075', |
| 121 | + 'width_mult': 0.75 |
| 122 | + } |
| 123 | + }, |
| 124 | + 'timm-mobilenetv3_large_100': { |
| 125 | + 'encoder': MobileNetV3Encoder, |
| 126 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_100'], |
| 127 | + 'params': { |
| 128 | + 'model': 'tf_mobilenetv3_large_100', |
| 129 | + 'width_mult': 1.0 |
| 130 | + } |
| 131 | + }, |
| 132 | + 'timm-mobilenetv3_large_minimal_100': { |
| 133 | + 'encoder': MobileNetV3Encoder, |
| 134 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_large_minimal_100'], |
| 135 | + 'params': { |
| 136 | + 'model': 'tf_mobilenetv3_large_minimal_100', |
| 137 | + 'width_mult': 1.0 |
| 138 | + } |
| 139 | + }, |
| 140 | + 'timm-mobilenetv3_small_075': { |
| 141 | + 'encoder': MobileNetV3Encoder, |
| 142 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_075'], |
| 143 | + 'params': { |
| 144 | + 'model': 'tf_mobilenetv3_small_075', |
| 145 | + 'width_mult': 0.75 |
| 146 | + } |
| 147 | + }, |
| 148 | + 'timm-mobilenetv3_small_100': { |
| 149 | + 'encoder': MobileNetV3Encoder, |
| 150 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_100'], |
| 151 | + 'params': { |
| 152 | + 'model': 'tf_mobilenetv3_small_100', |
| 153 | + 'width_mult': 1.0 |
| 154 | + } |
| 155 | + }, |
| 156 | + 'timm-mobilenetv3_small_minimal_100': { |
| 157 | + 'encoder': MobileNetV3Encoder, |
| 158 | + 'pretrained_settings': pretrained_settings['tf_mobilenetv3_small_minimal_100'], |
| 159 | + 'params': { |
| 160 | + 'model': 'tf_mobilenetv3_small_minimal_100', |
| 161 | + 'width_mult': 1.0 |
| 162 | + } |
| 163 | + }, |
| 164 | +} |
0 commit comments