summaryrefslogtreecommitdiffstats
path: root/models/convnext
diff options
context:
space:
mode:
Diffstat (limited to 'models/convnext')
-rw-r--r--models/convnext/discriminator.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/models/convnext/discriminator.py b/models/convnext/discriminator.py
new file mode 100644
index 0000000..7dbbe3a
--- /dev/null
+++ b/models/convnext/discriminator.py
@@ -0,0 +1,35 @@
1import torch
2from timm.models import ConvNeXt
3from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
4
5from torch.nn import functional as F
6
7
8class ConvNeXtDiscriminator():
9 def __init__(self, model: ConvNeXt, input_size: int) -> None:
10 self.net = model
11
12 self.input_size = input_size
13
14 self.img_mean = torch.tensor(IMAGENET_DEFAULT_MEAN).view(1, -1, 1, 1)
15 self.img_std = torch.tensor(IMAGENET_DEFAULT_STD).view(1, -1, 1, 1)
16
17 def get_score(self, img):
18 img_mean = self.img_mean.to(device=img.device, dtype=img.dtype)
19 img_std = self.img_std.to(device=img.device, dtype=img.dtype)
20
21 img = ((img+1.)/2.).sub(img_mean).div(img_std)
22
23 img = F.interpolate(img, size=(self.input_size, self.input_size), mode='bicubic', align_corners=True)
24 pred = self.net(img)
25 return torch.softmax(pred, dim=-1)[:, 1]
26
27 def get_all(self, img):
28 img_mean = self.img_mean.to(device=img.device, dtype=img.dtype)
29 img_std = self.img_std.to(device=img.device, dtype=img.dtype)
30
31 img = ((img + 1.) / 2.).sub(img_mean).div(img_std)
32
33 img = F.interpolate(img, size=(self.input_size, self.input_size), mode='bicubic', align_corners=True)
34 pred = self.net(img)
35 return pred