File size: 4,694 Bytes
8e29a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch.nn as nn
import torch
from .head import VQAHead_cls,VARHead,VQAHead
from .backbone.blip import MyBLIP as BLIP

class VideoTextAlignmentModel(nn.Module):
    def __init__(

            self,

            backbone_size="divided",

            backbone_preserve_keys="fragments,resize",

            multi=False,

            layer=-1,

            backbone=dict(

                resize={"window_size": (4, 4, 4)}, fragments={"window_size": (4, 4, 4)}

            ),

            divide_head=False,

            head_type='VQAhead_cls',

            vqa_head=dict(in_channels=768),

            var=False,

            use_tn=False,

            model_path=None,

    ):
        self.backbone_preserve_keys = backbone_preserve_keys.split(",")
        self.multi = multi
        self.layer = layer
        super().__init__()

        for key, hypers in backbone.items():
            if key not in self.backbone_preserve_keys:
                continue
            if backbone_size == "divided":
                t_backbone_size = hypers["type"]
            else:
                t_backbone_size = backbone_size

            assert t_backbone_size == "blip"
            type = hypers["blip_type"]
            b = BLIP(type, model_path)

            setattr(self, key + "_backbone", b)
        if divide_head:
            for key in backbone:
                pre_pool = False  # if key == "technical" else True
                if key not in self.backbone_preserve_keys:
                    continue
                in_channel = 768
                b = VQAHead_cls(pre_pool=pre_pool, in_channels=in_channel, **vqa_head)
                setattr(self, key + "_head", b)
        else:
            if var:
                self.vqa_head = VARHead(**vqa_head)
            else:
                self.vqa_head = VQAHead(**vqa_head)

    def forward(

            self,

            vclips,

            prompts=None,

            inference=True,

            return_pooled_feats=False,

            return_raw_feats=False,

            reduce_scores=False,

            pooled=False,

            **kwargs

    ):
        # import pdb;pdb.set_trace()
        assert (return_pooled_feats & return_raw_feats) == False, "Please only choose one kind of features to return"
        if inference:
            self.eval()
            with torch.no_grad():
                scores = []
                feats = {}
                for key in self.backbone_preserve_keys:
                        feat = getattr(self, key.split("_")[0] + "_backbone")(
                            vclips[key], prompts
                        )
                        if hasattr(self, key.split("_")[0] + "_head"):
                            scores += [getattr(self, key.split("_")[0] + "_head")(feat)[0]]
                        else:
                            scores += [getattr(self, "vqa_head")(feat)]
                        if return_pooled_feats:
                            feats[key] = feat
                        if return_raw_feats:
                            feats[key] = feat
                if reduce_scores:
                    if len(scores) > 1:
                        scores = reduce(lambda x, y: x + y, scores)
                    else:
                        scores = scores[0]
                    if pooled:
                        scores = torch.mean(scores, (1, 2, 3, 4))
            self.train()
            if return_pooled_feats or return_raw_feats:
                return scores, feats
            return scores
        else:
            self.train()
            scores = []
            feats = {}

            for key in vclips:
                    feat = getattr(self, key.split("_")[0] + "_backbone")(
                            vclips[key], prompts
                    )
                    if hasattr(self, key.split("_")[0] + "_head"):
                        scores += [getattr(self, key.split("_")[0] + "_head")(feat)[0]]
                    else:
                        scores += [getattr(self, "vqa_head")(feat)]
                    if return_pooled_feats:
                        feats[key] = feat.mean((-3, -2, -1))
            if reduce_scores:
                if len(scores) > 1:
                    scores = reduce(lambda x, y: x + y, scores)
                else:
                    scores = scores[0]
                if pooled:
                    # print(scores.shape)
                    scores = torch.mean(scores, (1, 2, 3, 4))
                    # print(scores.shape)

            if return_pooled_feats:
                return scores, feats
            return scores