File size: 11,965 Bytes
7aefe45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from typing import Union, List, Tuple
from collections import OrderedDict
from functools import partial

import numpy as np
import torch
import torch.nn as nn
import torchvision.transforms as transforms


class CLIPScoreWrapper(nn.Module):

    def __init__(self,
                 clip_model_name: str,
                 download_root: str = None,
                 device: torch.device = "cuda" if torch.cuda.is_available() else "cpu",
                 jit: bool = False,
                 # additional params
                 visual_score: bool = False,
                 feats_loss_type: str = None,
                 feats_loss_weights: List[float] = None,
                 fc_loss_weight: float = None,
                 context_length: int = 77):
        super().__init__()

        import clip  # local import

        # check model info
        self.clip_model_name = clip_model_name
        self.device = device
        self.available_models = clip.available_models()
        assert clip_model_name in self.available_models, f"A model backbone: {clip_model_name} that does not exist"

        # load CLIP
        self.model, self.preprocess = clip.load(clip_model_name, device, jit=jit, download_root=download_root)
        self.model.eval()

        # load tokenize
        self.tokenize_fn = partial(clip.tokenize, context_length=context_length)

        # load CLIP visual
        self.visual_encoder = VisualEncoderWrapper(self.model, clip_model_name).to(device)
        self.visual_encoder.eval()

        # check loss weights
        self.visual_score = visual_score
        if visual_score:
            assert feats_loss_type in ["l1", "l2", "cosine"], f"{feats_loss_type} is not exist."
            if clip_model_name.startswith("ViT"): assert len(feats_loss_weights) == 12
            if clip_model_name.startswith("RN"): assert len(feats_loss_weights) == 5

            # load visual loss wrapper
            self.visual_loss_fn = CLIPVisualLossWrapper(self.visual_encoder, feats_loss_type,
                                                        feats_loss_weights,
                                                        fc_loss_weight)

    @property
    def input_resolution(self):
        return self.model.visual.input_resolution  # default: 224

    @property
    def resize(self):  # Resize only
        return transforms.Compose([self.preprocess.transforms[0]])

    @property
    def normalize(self):
        return transforms.Compose([
            self.preprocess.transforms[0],  # Resize
            self.preprocess.transforms[1],  # CenterCrop
            self.preprocess.transforms[-1],  # Normalize
        ])

    @property
    def norm_(self):  # Normalize only
        return transforms.Compose([self.preprocess.transforms[-1]])

    def encode_image_layer_wise(self, x: torch.Tensor) -> Tuple[torch.Tensor, List[torch.Tensor]]:
        semantic_vec, feature_maps = self.visual_encoder(x)
        return semantic_vec, feature_maps

    def encode_text(self, text: Union[str, List[str]], norm: bool = True) -> torch.Tensor:
        tokens = self.tokenize_fn(text).to(self.device)
        text_features = self.model.encode_text(tokens)
        if norm:
            text_features = text_features.mean(axis=0, keepdim=True)
            text_features_norm = text_features / text_features.norm(dim=-1, keepdim=True)
            return text_features_norm
        return text_features

    def encode_image(self, image: torch.Tensor, norm: bool = True) -> torch.Tensor:
        image_features = self.model.encode_image(image)
        if norm:
            image_features_norm = image_features / image_features.norm(dim=-1, keepdim=True)
            return image_features_norm
        return image_features

    @torch.no_grad()
    def predict(self,
                image: torch.Tensor,
                text: Union[str, List[str]]) -> Tuple[torch.Tensor, torch.Tensor, np.ndarray]:
        image_features = self.model.encode_image(image)
        text_tokenize = self.tokenize_fn(text).to(self.device)
        text_features = self.model.encode_text(text_tokenize)
        logits_per_image, logits_per_text = self.model(image, text)
        probs = logits_per_image.softmax(dim=-1).cpu().numpy()
        return image_features, text_features, probs

    def compute_text_visual_distance(
            self, image: torch.Tensor, text: Union[str, List[str]]
    ) -> torch.Tensor:
        image_features = self.model.encode_image(image)
        text_tokenize = self.tokenize_fn(text).to(self.device)
        with torch.no_grad():
            text_features = self.model.encode_text(text_tokenize)

        image_features_norm = image_features / image_features.norm(dim=-1, keepdim=True)
        text_features_norm = text_features / text_features.norm(dim=-1, keepdim=True)
        # loss = - (image_features_norm @ text_features_norm.T)
        loss = 1 - torch.cosine_similarity(image_features_norm, text_features_norm, dim=1)
        return loss.mean()

    def directional_text_visual_distance(self, src_text, src_img, tar_text, tar_img):
        src_image_features = self.model.encode_image(src_img).detach()
        tar_image_features = self.model.encode_image(tar_img)
        src_text_tokenize = self.tokenize_fn(src_text).to(self.device)
        tar_text_tokenize = self.tokenize_fn(tar_text).to(self.device)
        with torch.no_grad():
            src_text_features = self.model.encode_text(src_text_tokenize)
            tar_text_features = self.model.encode_text(tar_text_tokenize)

        delta_image_features = tar_image_features - src_image_features
        delta_text_features = tar_text_features - src_text_features

        # # avold zero divisor
        # delta_image_features_norm = delta_image_features / delta_image_features.norm(dim=-1, keepdim=True)
        # delta_text_features_norm = delta_text_features / delta_text_features.norm(dim=-1, keepdim=True)

        loss = 1 - torch.cosine_similarity(delta_image_features, delta_text_features, dim=1, eps=1e-3)
        return loss.mean()

    def compute_visual_distance(
            self, x: torch.Tensor, y: torch.Tensor, clip_norm: bool = True,
    ) -> Tuple[torch.Tensor, List]:
        # return a fc loss and the list of feat loss
        assert self.visual_score is True
        assert x.shape == y.shape
        assert x.shape[-1] == self.input_resolution and x.shape[-2] == self.input_resolution
        assert y.shape[-1] == self.input_resolution and y.shape[-2] == self.input_resolution

        if clip_norm:
            return self.visual_loss_fn(self.normalize(x), self.normalize(y))
        else:
            return self.visual_loss_fn(x, y)


class VisualEncoderWrapper(nn.Module):
    """
    semantic features and layer by layer feature maps are obtained from CLIP visual encoder.
    """

    def __init__(self, clip_model: nn.Module, clip_model_name: str):
        super().__init__()
        self.clip_model = clip_model
        self.clip_model_name = clip_model_name

        if clip_model_name.startswith("ViT"):
            self.feature_maps = OrderedDict()
            for i in range(12):  # 12 ResBlocks in ViT visual transformer
                self.clip_model.visual.transformer.resblocks[i].register_forward_hook(
                    self.make_hook(i)
                )

        if clip_model_name.startswith("RN"):
            layers = list(self.clip_model.visual.children())
            init_layers = torch.nn.Sequential(*layers)[:8]
            self.layer1 = layers[8]
            self.layer2 = layers[9]
            self.layer3 = layers[10]
            self.layer4 = layers[11]
            self.att_pool2d = layers[12]

    def make_hook(self, name):
        def hook(module, input, output):
            if len(output.shape) == 3:
                # LND -> NLD (B, 77, 768)
                self.feature_maps[name] = output.permute(1, 0, 2)
            else:
                self.feature_maps[name] = output

        return hook

    def _forward_vit(self, x: torch.Tensor) -> Tuple[torch.Tensor, List]:
        fc_feature = self.clip_model.encode_image(x).float()
        feature_maps = [self.feature_maps[k] for k in range(12)]

        # fc_feature len: 1 ,feature_maps len: 12
        return fc_feature, feature_maps

    def _forward_resnet(self, x: torch.Tensor) -> Tuple[torch.Tensor, List]:
        def stem(m, x):
            for conv, bn, relu in [(m.conv1, m.bn1, m.relu1), (m.conv2, m.bn2, m.relu2), (m.conv3, m.bn3, m.relu3)]:
                x = torch.relu(bn(conv(x)))
            x = m.avgpool(x)
            return x

        x = x.type(self.clip_model.visual.conv1.weight.dtype)
        x = stem(self.clip_model.visual, x)
        x1 = self.layer1(x)
        x2 = self.layer2(x1)
        x3 = self.layer3(x2)
        x4 = self.layer4(x3)
        y = self.att_pool2d(x4)

        # fc_features len: 1 ,feature_maps len: 5
        return y, [x, x1, x2, x3, x4]

    def forward(self, x) -> Tuple[torch.Tensor, List[torch.Tensor]]:
        if self.clip_model_name.startswith("ViT"):
            fc_feat, visual_feat_maps = self._forward_vit(x)
        if self.clip_model_name.startswith("RN"):
            fc_feat, visual_feat_maps = self._forward_resnet(x)

        return fc_feat, visual_feat_maps


class CLIPVisualLossWrapper(nn.Module):
    """
    Visual Feature Loss + FC loss
    """

    def __init__(
            self,
            visual_encoder: nn.Module,
            feats_loss_type: str = None,
            feats_loss_weights: List[float] = None,
            fc_loss_weight: float = None,
    ):
        super().__init__()
        self.visual_encoder = visual_encoder
        self.feats_loss_weights = feats_loss_weights
        self.fc_loss_weight = fc_loss_weight

        self.layer_criterion = layer_wise_distance(feats_loss_type)

    def forward(self, x: torch.Tensor, y: torch.Tensor):
        x_fc_feature, x_feat_maps = self.visual_encoder(x)
        y_fc_feature, y_feat_maps = self.visual_encoder(y)

        # visual feature loss
        if sum(self.feats_loss_weights) == 0:
            feats_loss_list = [torch.tensor(0, device=x.device)]
        else:
            feats_loss = self.layer_criterion(x_feat_maps, y_feat_maps, self.visual_encoder.clip_model_name)
            feats_loss_list = []
            for layer, w in enumerate(self.feats_loss_weights):
                if w:
                    feats_loss_list.append(feats_loss[layer] * w)

        # visual fc loss, default: cosine similarity
        if self.fc_loss_weight == 0:
            fc_loss = torch.tensor(0, device=x.device)
        else:
            fc_loss = (1 - torch.cosine_similarity(x_fc_feature, y_fc_feature, dim=1)).mean()
            fc_loss = fc_loss * self.fc_loss_weight

        return fc_loss, feats_loss_list


#################################################################################
#                                layer wise metric                              #
#################################################################################

def layer_wise_distance(metric_name: str):
    return {
        "l1": l1_layer_wise,
        "l2": l2_layer_wise,
        "cosine": cosine_layer_wise
    }.get(metric_name.lower())


def l2_layer_wise(x_features, y_features, clip_model_name):
    return [
        torch.square(x_conv - y_conv).mean()
        for x_conv, y_conv in zip(x_features, y_features)
    ]


def l1_layer_wise(x_features, y_features, clip_model_name):
    return [
        torch.abs(x_conv - y_conv).mean()
        for x_conv, y_conv in zip(x_features, y_features)
    ]


def cosine_layer_wise(x_features, y_features, clip_model_name):
    if clip_model_name.startswith("RN"):
        return [
            (1 - torch.cosine_similarity(x_conv, y_conv, dim=1)).mean()
            for x_conv, y_conv in zip(x_features, y_features)
        ]
    return [
        (1 - torch.cosine_similarity(x_conv, y_conv, dim=1)).mean()
        for x_conv, y_conv in zip(x_features, y_features)
    ]