File size: 3,840 Bytes
b910c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import torch
import torch.nn as nn

from torchmetrics import CatMetric
from torchmetrics import SumMetric  # sum over devices
from torchmetrics.image.fid import FrechetInceptionDistance
from torchmetrics.multimodal.clip_score import CLIPScore

from jutils.nn import DINOv2, preprocess_for_dinov2
from jutils.nn.metric_kid import kid_features_to_metric


def un_normalize_ims(ims):
    """Convert from [-1, 1] to [0, 255]"""
    ims = ((ims * 127.5) + 127.5).clip(0, 255).to(torch.uint8)
    return ims


class ImageMetricTracker(nn.Module):
    def __init__(self):
        super().__init__()
        self.total_samples = SumMetric()

        self.fid = FrechetInceptionDistance(
            feature=2048, reset_real_features=True, normalize=False, sync_on_compute=True
        )

    def __call__(self, target, pred):
        """Assumes target and pred in [-1, 1] range"""
        bs = target.shape[0]
        real_ims = un_normalize_ims(target)
        fake_ims = un_normalize_ims(pred)

        self.fid.update(real_ims, real=True)
        self.fid.update(fake_ims, real=False)

        self.total_samples.update(bs)

    def reset(self):
        self.fid.reset()
        self.total_samples.reset()

    def aggregate(self):
        """Compute the final metrics (automatically synced across devices)"""
        n_total_samples = int(self.total_samples.compute())
        return {
            f"fid-{n_total_samples}": self.fid.compute(),
            "n_metric_samples": n_total_samples,
        }


class Text2ImageMetricTracker(nn.Module):
    def __init__(self, kid_subsets: int = 100, kid_subset_size: int = 200):
        super().__init__()
        self.total_samples = SumMetric()
        self.fid = FrechetInceptionDistance(
            feature=2048, reset_real_features=True, normalize=False, sync_on_compute=True
        )

        self.clip = CLIPScore(model_name_or_path="openai/clip-vit-base-patch16")

        self.dino = DINOv2(pretrained=True).eval()
        self.dino_features_real = CatMetric()
        self.dino_features_fake = CatMetric()
        self.kid_subsets = kid_subsets
        self.kid_subset_size = kid_subset_size

        for p in self.parameters():
            p.requires_grad = False

    @torch.no_grad()
    def __call__(self, target, pred, txt):
        """Assumes target and pred in [-1, 1] range"""
        bs = target.shape[0]
        real_ims = un_normalize_ims(target)
        fake_ims = un_normalize_ims(pred)

        self.fid.update(real_ims, real=True)
        self.fid.update(fake_ims, real=False)

        txt = [t.decode() if isinstance(t, bytes) else t for t in txt]
        self.clip.update(fake_ims, list(txt))

        real_fts = self.dino(preprocess_for_dinov2(target, safe_mode=False))
        fake_fts = self.dino(preprocess_for_dinov2(pred, safe_mode=False))
        self.dino_features_real.update(real_fts)
        self.dino_features_fake.update(fake_fts)

        self.total_samples.update(bs)

    def reset(self):
        self.fid.reset()
        self.clip.reset()
        self.dino_features_real.reset()
        self.dino_features_fake.reset()
        self.total_samples.reset()

    def aggregate(self):
        """Compute the final metrics (automatically synced across devices)"""
        n_total_samples = int(self.total_samples.compute())

        # compute KDD
        real_fts = self.dino_features_real.compute()
        fake_fts = self.dino_features_fake.compute()
        kdd = kid_features_to_metric(
            real_fts,
            fake_fts,
            kid_subsets=self.kid_subsets,
            kid_subset_size=self.kid_subset_size,
            verbose=False,
        )

        return {
            f"fid-{n_total_samples}": self.fid.compute(),
            f"clip": self.clip.compute(),
            **kdd,
            "n_metric_samples": n_total_samples,
        }