From f39286fa5c5840b67dadf8e85f5f5d7ff1414aab Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 11 Apr 2023 22:36:05 +0200 Subject: Experimental convnext discriminator support --- models/convnext/discriminator.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 models/convnext/discriminator.py (limited to 'models/convnext') 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 @@ +import torch +from timm.models import ConvNeXt +from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD + +from torch.nn import functional as F + + +class ConvNeXtDiscriminator(): + def __init__(self, model: ConvNeXt, input_size: int) -> None: + self.net = model + + self.input_size = input_size + + self.img_mean = torch.tensor(IMAGENET_DEFAULT_MEAN).view(1, -1, 1, 1) + self.img_std = torch.tensor(IMAGENET_DEFAULT_STD).view(1, -1, 1, 1) + + def get_score(self, img): + img_mean = self.img_mean.to(device=img.device, dtype=img.dtype) + img_std = self.img_std.to(device=img.device, dtype=img.dtype) + + img = ((img+1.)/2.).sub(img_mean).div(img_std) + + img = F.interpolate(img, size=(self.input_size, self.input_size), mode='bicubic', align_corners=True) + pred = self.net(img) + return torch.softmax(pred, dim=-1)[:, 1] + + def get_all(self, img): + img_mean = self.img_mean.to(device=img.device, dtype=img.dtype) + img_std = self.img_std.to(device=img.device, dtype=img.dtype) + + img = ((img + 1.) / 2.).sub(img_mean).div(img_std) + + img = F.interpolate(img, size=(self.input_size, self.input_size), mode='bicubic', align_corners=True) + pred = self.net(img) + return pred -- cgit v1.2.3-70-g09d2