|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | +import torch.nn.functional as F |
| 4 | +from anomalib.models.components import NetworkFeatureAggregator |
| 5 | +import math |
| 6 | + |
| 7 | +def init_weight(m): |
| 8 | + if isinstance(m, torch.nn.Linear): |
| 9 | + torch.nn.init.xavier_normal_(m.weight) |
| 10 | + if isinstance(m, torch.nn.BatchNorm2d): |
| 11 | + m.weight.data.normal_(1.0, 0.02) |
| 12 | + m.bias.data.fill_(0) |
| 13 | + elif isinstance(m, torch.nn.Conv2d): |
| 14 | + m.weight.data.normal_(0.0, 0.02) |
| 15 | + |
| 16 | +class Preprocessing(torch.nn.Module): |
| 17 | + def __init__(self, input_dims, output_dim): |
| 18 | + super(Preprocessing, self).__init__() |
| 19 | + self.input_dims = input_dims |
| 20 | + self.output_dim = output_dim |
| 21 | + |
| 22 | + self.preprocessing_modules = torch.nn.ModuleList() |
| 23 | + for _ in input_dims: |
| 24 | + module = MeanMapper(output_dim) |
| 25 | + self.preprocessing_modules.append(module) |
| 26 | + |
| 27 | + def forward(self, features): |
| 28 | + _features = [] |
| 29 | + for module, feature in zip(self.preprocessing_modules, features): |
| 30 | + _features.append(module(feature)) |
| 31 | + return torch.stack(_features, dim=1) |
| 32 | + |
| 33 | + |
| 34 | +class MeanMapper(torch.nn.Module): |
| 35 | + def __init__(self, preprocessing_dim): |
| 36 | + super(MeanMapper, self).__init__() |
| 37 | + self.preprocessing_dim = preprocessing_dim |
| 38 | + |
| 39 | + def forward(self, features): |
| 40 | + features = features.reshape(len(features), 1, -1) |
| 41 | + return F.adaptive_avg_pool1d(features, self.preprocessing_dim).squeeze(1) |
| 42 | + |
| 43 | + |
| 44 | +class Aggregator(torch.nn.Module): |
| 45 | + def __init__(self, target_dim): |
| 46 | + super(Aggregator, self).__init__() |
| 47 | + self.target_dim = target_dim |
| 48 | + |
| 49 | + def forward(self, features): |
| 50 | + """Returns reshaped and average pooled features.""" |
| 51 | + features = features.reshape(len(features), 1, -1) |
| 52 | + features = F.adaptive_avg_pool1d(features, self.target_dim) |
| 53 | + return features.reshape(len(features), -1) |
| 54 | + |
| 55 | +class Projection(torch.nn.Module): |
| 56 | + def __init__(self, in_planes, out_planes=None, n_layers=1, layer_type=0): |
| 57 | + super(Projection, self).__init__() |
| 58 | + |
| 59 | + if out_planes is None: |
| 60 | + out_planes = in_planes |
| 61 | + self.layers = torch.nn.Sequential() |
| 62 | + _in = None |
| 63 | + _out = None |
| 64 | + for i in range(n_layers): |
| 65 | + _in = in_planes if i == 0 else _out |
| 66 | + _out = out_planes |
| 67 | + self.layers.add_module(f"{i}fc", torch.nn.Linear(_in, _out)) |
| 68 | + if i < n_layers - 1: |
| 69 | + if layer_type > 1: |
| 70 | + self.layers.add_module(f"{i}relu", torch.nn.LeakyReLU(.2)) |
| 71 | + self.apply(init_weight) |
| 72 | + |
| 73 | + def forward(self, x): |
| 74 | + |
| 75 | + x = self.layers(x) |
| 76 | + return x |
| 77 | + |
| 78 | +class Discriminator(torch.nn.Module): |
| 79 | + def __init__(self, in_planes, n_layers=2, hidden=None): |
| 80 | + super(Discriminator, self).__init__() |
| 81 | + |
| 82 | + _hidden = in_planes if hidden is None else hidden |
| 83 | + self.body = torch.nn.Sequential() |
| 84 | + for i in range(n_layers - 1): |
| 85 | + _in = in_planes if i == 0 else _hidden |
| 86 | + _hidden = int(_hidden // 1.5) if hidden is None else hidden |
| 87 | + self.body.add_module('block%d' % (i + 1), |
| 88 | + torch.nn.Sequential( |
| 89 | + torch.nn.Linear(_in, _hidden), |
| 90 | + torch.nn.BatchNorm1d(_hidden), |
| 91 | + torch.nn.LeakyReLU(0.2) |
| 92 | + )) |
| 93 | + self.tail = torch.nn.Sequential(torch.nn.Linear(_hidden, 1, bias=False), |
| 94 | + torch.nn.Sigmoid()) |
| 95 | + self.apply(init_weight) |
| 96 | + |
| 97 | + def forward(self, x): |
| 98 | + x = self.body(x) |
| 99 | + x = self.tail(x) |
| 100 | + return x |
| 101 | + |
| 102 | +class PatchMaker: |
| 103 | + def __init__(self, patchsize, top_k=0, stride=None): |
| 104 | + self.patchsize = patchsize |
| 105 | + self.stride = stride |
| 106 | + self.top_k = top_k |
| 107 | + |
| 108 | + def patchify(self, features, return_spatial_info=False): |
| 109 | + padding = int((self.patchsize - 1) / 2) |
| 110 | + unfolder = torch.nn.Unfold(kernel_size=self.patchsize, stride=self.stride, padding=padding, dilation=1) |
| 111 | + unfolded_features = unfolder(features) |
| 112 | + number_of_total_patches = [] |
| 113 | + for s in features.shape[-2:]: |
| 114 | + n_patches = (s + 2 * padding - 1 * (self.patchsize - 1) - 1) / self.stride + 1 |
| 115 | + number_of_total_patches.append(int(n_patches)) |
| 116 | + unfolded_features = unfolded_features.reshape( |
| 117 | + *features.shape[:2], self.patchsize, self.patchsize, -1 |
| 118 | + ) |
| 119 | + unfolded_features = unfolded_features.permute(0, 4, 1, 2, 3) |
| 120 | + |
| 121 | + if return_spatial_info: |
| 122 | + return unfolded_features, number_of_total_patches |
| 123 | + return unfolded_features |
| 124 | + |
| 125 | + def unpatch_scores(self, x, batchsize): |
| 126 | + return x.reshape(batchsize, -1, *x.shape[1:]) |
| 127 | + |
| 128 | + def score(self, x): |
| 129 | + x = x[:, :, 0] |
| 130 | + x = torch.max(x, dim=1).values |
| 131 | + return x |
| 132 | + |
| 133 | +class GlassModel(nn.Module): |
| 134 | + def __init__( |
| 135 | + self, |
| 136 | + input_shape, |
| 137 | + pretrain_embed_dim, |
| 138 | + target_embed_dim, |
| 139 | + backbone: nn.Module, |
| 140 | + patchsize: int =3, |
| 141 | + patchstride: int =1, |
| 142 | + pre_trained: bool =True, |
| 143 | + layers: list[str] = ["layer1", "layer2", "layer3"], |
| 144 | + pre_proj: int = 1, |
| 145 | + dsc_layers=2, |
| 146 | + dsc_hidden=1024 |
| 147 | + ) -> None: |
| 148 | + super().__init__() |
| 149 | + self.backbone = backbone |
| 150 | + self.layers = layers |
| 151 | + self.input_shape = input_shape |
| 152 | + |
| 153 | + self.forward_modules = torch.ModuleDict({}) |
| 154 | + feature_aggregator = NetworkFeatureAggregator( |
| 155 | + self.backbone, self.layers, pre_trained |
| 156 | + ) |
| 157 | + feature_dimensions = feature_aggregator.feature_dimensions(input_shape) |
| 158 | + self.forward_modules["feature_aggregator"] = feature_aggregator |
| 159 | + |
| 160 | + preprocessing = Preprocessing(feature_dimensions, pretrain_embed_dim) |
| 161 | + self.forward_modules["preprocessing"] = preprocessing |
| 162 | + self.target_embed_dimension = target_embed_dim |
| 163 | + preadapt_aggregator = Aggregator(target_dim=target_embed_dim) |
| 164 | + self.forward_modules["preadapt_aggregator"] = preadapt_aggregator |
| 165 | + |
| 166 | + self.pre_trained = pre_trained |
| 167 | + |
| 168 | + self.pre_proj = pre_proj |
| 169 | + if self.pre_proj > 0: |
| 170 | + self.pre_projection = Projection(self.target_embed_dimension, self.target_embed_dimension, pre_proj) |
| 171 | + |
| 172 | + self.discriminator = Discriminator(self.target_embed_dimension, n_layers=dsc_layers, hidden=dsc_hidden) |
| 173 | + |
| 174 | + self.patch_maker = PatchMaker(patchsize, stride=patchstride) |
| 175 | + |
| 176 | + def generate_embeddings(self, images, provide_patch_shapes=False, eval=False): |
| 177 | + if not eval and not self.pre_trained: |
| 178 | + self.forward_modules["feature_aggregator"].train() |
| 179 | + features = self.forward_modules["feature_aggregator"](images, eval=eval) |
| 180 | + else: |
| 181 | + self.forward_modules["feature_aggregator"].eval() |
| 182 | + with torch.no_grad(): |
| 183 | + features = self.forward_modules["feature_aggregator"](images) |
| 184 | + |
| 185 | + features = [features[layer] for layer in self.layers_to_extract_from] |
| 186 | + for i, feat in enumerate(features): |
| 187 | + if len(feat.shape) == 3: |
| 188 | + B, L, C = feat.shape |
| 189 | + features[i] = feat.reshape(B, int(math.sqrt(L)), int(math.sqrt(L)), C).permute(0, 3, 1, 2) |
| 190 | + |
| 191 | + features = [self.patch_maker.patchify(x, return_spatial_info=True) for x in features] |
| 192 | + patch_shapes = [x[1] for x in features] |
| 193 | + patch_features = [x[0] for x in features] |
| 194 | + ref_num_patches = patch_shapes[0] |
| 195 | + |
| 196 | + for i in range(1, len(patch_features)): |
| 197 | + _features = patch_features[i] |
| 198 | + patch_dims = patch_shapes[i] |
| 199 | + |
| 200 | + _features = _features.reshape( |
| 201 | + _features.shape[0], patch_dims[0], patch_dims[1], *_features.shape[2:] |
| 202 | + ) |
| 203 | + _features = _features.permute(0, 3, 4, 5, 1, 2) |
| 204 | + perm_base_shape = _features.shape |
| 205 | + _features = _features.reshape(-1, *_features.shape[-2:]) |
| 206 | + _features = F.interpolate( |
| 207 | + _features.unsqueeze(1), |
| 208 | + size=(ref_num_patches[0], ref_num_patches[1]), |
| 209 | + mode="bilinear", |
| 210 | + align_corners=False, |
| 211 | + ) |
| 212 | + _features = _features.squeeze(1) |
| 213 | + _features = _features.reshape( |
| 214 | + *perm_base_shape[:-2], ref_num_patches[0], ref_num_patches[1] |
| 215 | + ) |
| 216 | + _features = _features.permute(0, 4, 5, 1, 2, 3) |
| 217 | + _features = _features.reshape(len(_features), -1, *_features.shape[-3:]) |
| 218 | + patch_features[i] = _features |
| 219 | + |
| 220 | + patch_features = [x.reshape(-1, *x.shape[-3:]) for x in patch_features] |
| 221 | + patch_features = self.forward_modules["preprocessing"](patch_features) |
| 222 | + patch_features = self.forward_modules["preadapt_aggregator"](patch_features) |
| 223 | + |
| 224 | + return patch_features, patch_shapes |
| 225 | + |
| 226 | + def forward(self, images, eval=False): |
| 227 | + self.forward_modules.eval() |
| 228 | + with torch.no_grad(): |
| 229 | + if self.pre_proj > 0: |
| 230 | + outputs = self.pre_proj(self.generate_embeddings(images, eval)) |
| 231 | + outputs = outputs[0] if len(outputs) == 2 else outputs |
| 232 | + else: |
| 233 | + outputs = self.generate_embeddings(images, eval)[0] |
| 234 | + outputs = outputs[0] if len(outputs) == 2 else outputs |
| 235 | + outputs = outputs.reshape(images.shape[0], -1, outputs.shape[-1]) |
| 236 | + return outputs |
| 237 | + |
0 commit comments