|
| 1 | +from timm.models import ByobCfg, BlocksCfg, ByobNet |
| 2 | + |
| 3 | +from ._base import EncoderMixin |
| 4 | +import torch.nn as nn |
| 5 | + |
| 6 | + |
| 7 | +class GERNetEncoder(ByobNet, EncoderMixin): |
| 8 | + def __init__(self, out_channels, depth=5, **kwargs): |
| 9 | + super().__init__(**kwargs) |
| 10 | + self._depth = depth |
| 11 | + self._out_channels = out_channels |
| 12 | + self._in_channels = 3 |
| 13 | + |
| 14 | + del self.head |
| 15 | + |
| 16 | + def get_stages(self): |
| 17 | + return [ |
| 18 | + nn.Identity(), |
| 19 | + self.stem, |
| 20 | + self.stages[0], |
| 21 | + self.stages[1], |
| 22 | + self.stages[2], |
| 23 | + nn.Sequential(self.stages[3], self.stages[4], self.final_conv) |
| 24 | + ] |
| 25 | + |
| 26 | + def forward(self, x): |
| 27 | + stages = self.get_stages() |
| 28 | + |
| 29 | + features = [] |
| 30 | + for i in range(self._depth + 1): |
| 31 | + x = stages[i](x) |
| 32 | + features.append(x) |
| 33 | + |
| 34 | + return features |
| 35 | + |
| 36 | + def load_state_dict(self, state_dict, **kwargs): |
| 37 | + state_dict.pop("head.fc.weight") |
| 38 | + state_dict.pop("head.fc.bias") |
| 39 | + super().load_state_dict(state_dict, **kwargs) |
| 40 | + |
| 41 | + |
| 42 | +regnet_weights = { |
| 43 | + 'timm-gernet_s': { |
| 44 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_s-756b4751.pth', |
| 45 | + }, |
| 46 | + 'timm-gernet_m': { |
| 47 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_m-0873c53a.pth', |
| 48 | + }, |
| 49 | + 'timm-gernet_l': { |
| 50 | + 'imagenet': 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-ger-weights/gernet_l-f31e2e8d.pth', |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +pretrained_settings = {} |
| 55 | +for model_name, sources in regnet_weights.items(): |
| 56 | + pretrained_settings[model_name] = {} |
| 57 | + for source_name, source_url in sources.items(): |
| 58 | + pretrained_settings[model_name][source_name] = { |
| 59 | + "url": source_url, |
| 60 | + 'input_range': [0, 1], |
| 61 | + 'mean': [0.485, 0.456, 0.406], |
| 62 | + 'std': [0.229, 0.224, 0.225], |
| 63 | + 'num_classes': 1000 |
| 64 | + } |
| 65 | + |
| 66 | +timm_gernet_encoders = { |
| 67 | + 'timm-gernet_s': { |
| 68 | + 'encoder': GERNetEncoder, |
| 69 | + "pretrained_settings": pretrained_settings["timm-gernet_s"], |
| 70 | + 'params': { |
| 71 | + 'out_channels': (3, 13, 48, 48, 384, 1920), |
| 72 | + 'cfg': ByobCfg( |
| 73 | + blocks=( |
| 74 | + BlocksCfg(type='basic', d=1, c=48, s=2, gs=0, br=1.), |
| 75 | + BlocksCfg(type='basic', d=3, c=48, s=2, gs=0, br=1.), |
| 76 | + BlocksCfg(type='bottle', d=7, c=384, s=2, gs=0, br=1 / 4), |
| 77 | + BlocksCfg(type='bottle', d=2, c=560, s=2, gs=1, br=3.), |
| 78 | + BlocksCfg(type='bottle', d=1, c=256, s=1, gs=1, br=3.), |
| 79 | + ), |
| 80 | + stem_chs=13, |
| 81 | + num_features=1920, |
| 82 | + ) |
| 83 | + }, |
| 84 | + }, |
| 85 | + 'timm-gernet_m': { |
| 86 | + 'encoder': GERNetEncoder, |
| 87 | + "pretrained_settings": pretrained_settings["timm-gernet_m"], |
| 88 | + 'params': { |
| 89 | + 'out_channels': (3, 32, 128, 192, 640, 2560), |
| 90 | + 'cfg': ByobCfg( |
| 91 | + blocks=( |
| 92 | + BlocksCfg(type='basic', d=1, c=128, s=2, gs=0, br=1.), |
| 93 | + BlocksCfg(type='basic', d=2, c=192, s=2, gs=0, br=1.), |
| 94 | + BlocksCfg(type='bottle', d=6, c=640, s=2, gs=0, br=1 / 4), |
| 95 | + BlocksCfg(type='bottle', d=4, c=640, s=2, gs=1, br=3.), |
| 96 | + BlocksCfg(type='bottle', d=1, c=640, s=1, gs=1, br=3.), |
| 97 | + ), |
| 98 | + stem_chs=32, |
| 99 | + num_features=2560, |
| 100 | + ) |
| 101 | + }, |
| 102 | + }, |
| 103 | + 'timm-gernet_l': { |
| 104 | + 'encoder': GERNetEncoder, |
| 105 | + "pretrained_settings": pretrained_settings["timm-gernet_l"], |
| 106 | + 'params': { |
| 107 | + 'out_channels': (3, 32, 128, 192, 640, 2560), |
| 108 | + 'cfg': ByobCfg( |
| 109 | + blocks=( |
| 110 | + BlocksCfg(type='basic', d=1, c=128, s=2, gs=0, br=1.), |
| 111 | + BlocksCfg(type='basic', d=2, c=192, s=2, gs=0, br=1.), |
| 112 | + BlocksCfg(type='bottle', d=6, c=640, s=2, gs=0, br=1 / 4), |
| 113 | + BlocksCfg(type='bottle', d=5, c=640, s=2, gs=1, br=3.), |
| 114 | + BlocksCfg(type='bottle', d=4, c=640, s=1, gs=1, br=3.), |
| 115 | + ), |
| 116 | + stem_chs=32, |
| 117 | + num_features=2560, |
| 118 | + ) |
| 119 | + }, |
| 120 | + }, |
| 121 | +} |
0 commit comments