summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2023-04-11 22:36:05 +0200
committerVolpeon <git@volpeon.ink>2023-04-11 22:36:05 +0200
commitf39286fa5c5840b67dadf8e85f5f5d7ff1414aab (patch)
treeb56956444af8439404bb8eb9b82508ac5e2194b9 /models
parentStore sample images in Tensorboard as well (diff)
downloadtextual-inversion-diff-f39286fa5c5840b67dadf8e85f5f5d7ff1414aab.tar.gz
textual-inversion-diff-f39286fa5c5840b67dadf8e85f5f5d7ff1414aab.tar.bz2
textual-inversion-diff-f39286fa5c5840b67dadf8e85f5f5d7ff1414aab.zip
Experimental convnext discriminator support
Diffstat (limited to 'models')
-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