|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn.functional as F |
| 5 | + |
| 6 | +from pretrainedmodels.models.dpn import DPN |
| 7 | + |
| 8 | + |
| 9 | +class DPNEncorder(DPN): |
| 10 | + |
| 11 | + def __init__(self, feature_blocks, *args, **kwargs): |
| 12 | + super().__init__(*args, **kwargs) |
| 13 | + self.feature_blocks = np.cumsum(feature_blocks) |
| 14 | + |
| 15 | + def forward(self, x): |
| 16 | + |
| 17 | + features = [] |
| 18 | + |
| 19 | + input_block = self.features[0] |
| 20 | + |
| 21 | + x = input_block.conv(x) |
| 22 | + x = input_block.bn(x) |
| 23 | + x = input_block.act(x) |
| 24 | + features.append(x) |
| 25 | + |
| 26 | + x = input_block.pool(x) |
| 27 | + |
| 28 | + for i, module in enumerate(self.features[1:], 1): |
| 29 | + |
| 30 | + x = module(x) |
| 31 | + if i in self.feature_blocks: |
| 32 | + features.append(x) |
| 33 | + |
| 34 | + out_features = [ |
| 35 | + features[4], |
| 36 | + F.relu(torch.cat(features[3], dim=1), inplace=True), |
| 37 | + F.relu(torch.cat(features[2], dim=1), inplace=True), |
| 38 | + F.relu(torch.cat(features[1], dim=1), inplace=True), |
| 39 | + features[0], |
| 40 | + ] |
| 41 | + |
| 42 | + shapes = [f.shape[1] for f in out_features] |
| 43 | + print(tuple(shapes)) |
| 44 | + |
| 45 | + return out_features |
| 46 | + |
| 47 | + |
| 48 | +dpn_encoders = { |
| 49 | + 'dpn68': { |
| 50 | + 'encoder': DPNEncorder, |
| 51 | + 'out_shapes': (832, 704, 320, 144, 10), |
| 52 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn68-4af7d88d2.pth', |
| 53 | + 'params': { |
| 54 | + 'feature_blocks': (3, 4, 12, 4), |
| 55 | + 'groups': 32, |
| 56 | + 'inc_sec': (16, 32, 32, 64), |
| 57 | + 'k_r': 128, |
| 58 | + 'k_sec': (3, 4, 12, 3), |
| 59 | + 'num_classes': 1000, |
| 60 | + 'num_init_features': 10, |
| 61 | + 'small': True, |
| 62 | + 'test_time_pool': True |
| 63 | + }, |
| 64 | + }, |
| 65 | + |
| 66 | + 'dpn68b': { |
| 67 | + 'encoder': DPNEncorder, |
| 68 | + 'out_shapes': (832, 704, 320, 144, 10), |
| 69 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn68b_extra-363ab9c19.pth', |
| 70 | + 'params': { |
| 71 | + 'feature_blocks': (3, 4, 12, 4), |
| 72 | + 'b': True, |
| 73 | + 'groups': 32, |
| 74 | + 'inc_sec': (16, 32, 32, 64), |
| 75 | + 'k_r': 128, |
| 76 | + 'k_sec': (3, 4, 12, 3), |
| 77 | + 'num_classes': 1000, |
| 78 | + 'num_init_features': 10, |
| 79 | + 'small': True, |
| 80 | + 'test_time_pool': True, |
| 81 | + }, |
| 82 | + }, |
| 83 | + |
| 84 | + 'dpn92': { |
| 85 | + 'encoder': DPNEncorder, |
| 86 | + 'out_shapes': (2688, 1552, 704, 336, 64), |
| 87 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn92_extra-fda993c95.pth', |
| 88 | + 'params': { |
| 89 | + 'feature_blocks': (3, 4, 20, 4), |
| 90 | + 'groups': 32, |
| 91 | + 'inc_sec': (16, 32, 24, 128), |
| 92 | + 'k_r': 96, |
| 93 | + 'k_sec': (3, 4, 20, 3), |
| 94 | + 'num_classes': 1000, |
| 95 | + 'num_init_features': 64, |
| 96 | + 'test_time_pool': True |
| 97 | + }, |
| 98 | + }, |
| 99 | + |
| 100 | + 'dpn98': { |
| 101 | + 'encoder': DPNEncorder, |
| 102 | + 'out_shapes': (2688, 1728, 768, 336, 96), |
| 103 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn98-722954780.pth', |
| 104 | + 'params': { |
| 105 | + 'feature_blocks': (3, 6, 20, 4), |
| 106 | + 'groups': 40, |
| 107 | + 'inc_sec': (16, 32, 32, 128), |
| 108 | + 'k_r': 160, |
| 109 | + 'k_sec': (3, 6, 20, 3), |
| 110 | + 'num_classes': 1000, |
| 111 | + 'num_init_features': 96, |
| 112 | + 'test_time_pool': True, |
| 113 | + }, |
| 114 | + }, |
| 115 | + |
| 116 | + 'dpn107': { |
| 117 | + 'encoder': DPNEncorder, |
| 118 | + 'out_shapes': (2688, 2432, 1152, 376, 128), |
| 119 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn107_extra-b7f9f4cc9.pth', |
| 120 | + 'params': { |
| 121 | + 'feature_blocks': (4, 8, 20, 4), |
| 122 | + 'groups': 50, |
| 123 | + 'inc_sec': (20, 64, 64, 128), |
| 124 | + 'k_r': 200, |
| 125 | + 'k_sec': (4, 8, 20, 3), |
| 126 | + 'num_classes': 1000, |
| 127 | + 'num_init_features': 128, |
| 128 | + 'test_time_pool': True |
| 129 | + }, |
| 130 | + }, |
| 131 | + |
| 132 | + 'dpn131': { |
| 133 | + 'encoder': DPNEncorder, |
| 134 | + 'out_shapes': (2688, 1984, 832, 352, 128), |
| 135 | + 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/dpn131-7af84be88.pth', |
| 136 | + 'params': { |
| 137 | + 'feature_blocks': (4, 8, 28, 4), |
| 138 | + 'groups': 40, |
| 139 | + 'inc_sec': (16, 32, 32, 128), |
| 140 | + 'k_r': 160, |
| 141 | + 'k_sec': (4, 8, 28, 3), |
| 142 | + 'num_classes': 1000, |
| 143 | + 'num_init_features': 128, |
| 144 | + 'test_time_pool': True |
| 145 | + }, |
| 146 | + }, |
| 147 | + |
| 148 | +} |
0 commit comments