1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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):
pred = self.get_all(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
|