-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_ts.py
210 lines (155 loc) · 9.54 KB
/
video_ts.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import VivitForVideoClassification, BertModel
class Contrastive_model(nn.Module):
"""
The Contrastive_model class is a PyTorch module that implements a contrastive learning model for video, audio, and text inputs. The model consists of three embedding modules, one for each input modality, that project the inputs into a shared embedding space. The model then computes the contrastive loss between the embeddings of the different modalities using the CLIP loss function.
The forward method of the model takes in video, audio, and text inputs (the text input is optional) and returns the embeddings for each modality, as well as the overall contrastive loss. The embeddings are L2-normalized before being used to compute the contrastive loss.
The model also includes a learnable logit scale parameter that is used to scale the logits before computing the contrastive loss.
"""
def __init__(self, hidden_channels, out_channels):
super(Contrastive_model, self).__init__()
self.embedding_video = nn.Sequential(nn.Linear(400, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, out_channels))
self.embedding_audio = nn.Sequential(nn.Linear(768, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, out_channels))
self.embedding_text = nn.Sequential(nn.Linear(768, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, out_channels))
logit_scale_init_value=2.6592
self.logit_scale = nn.Parameter(torch.tensor(logit_scale_init_value))
def forward(self, x_video, x_audio, x_text = None):
x_video = self.embedding_video(x_video)
if x_audio is not None:
x_audio = self.embedding_audio(x_audio)
if x_text is not None:
x_text = self.embedding_text(x_text)
x_video = nn.functional.normalize(x_video)
if x_audio is not None:
x_audio= nn.functional.normalize(x_audio)
if x_text is not None:
x_text= nn.functional.normalize(x_text)
logit_scale = self.logit_scale.exp()
if x_audio is not None:
logits_audio_text = torch.matmul(x_audio, x_audio.t()) * logit_scale
if x_text is not None:
logits_video_text = torch.matmul(x_video, x_text.t()) * logit_scale
if x_audio is not None:
logits_audio_video = torch.matmul(x_video, x_audio.t()) * logit_scale
loss = 0
if x_audio is not None and x_text is not None:
loss = clip_loss(logits_audio_text)
if x_text is not None:
loss += clip_loss(logits_video_text)
if x_audio is not None:
loss += clip_loss(logits_audio_video)
return x_video, x_audio, x_text, loss
def contrastive_loss(logits: torch.Tensor) -> torch.Tensor:
"""
Computes the contrastive loss for a given set of logits.
The contrastive loss is calculated as the cross-entropy loss between the logits and a target tensor containing the sequence of indices from 0 to the length of the logits tensor.
Args:
logits (torch.Tensor): A 2D tensor containing the logits for the contrastive task.
Returns:
torch.Tensor: The contrastive loss.
"""
return nn.functional.cross_entropy(logits, torch.arange(len(logits), device=logits.device))
def clip_loss(similarity: torch.Tensor) -> torch.Tensor:
"""
Computes the contrastive loss for a given similarity matrix.
The contrastive loss is calculated as the average of the cross-entropy loss for the rows (caption loss) and the columns (image loss) of the similarity matrix.
Args:
similarity (torch.Tensor): A 2D tensor containing the similarity scores between captions and images.
Returns:
torch.Tensor: The contrastive loss.
"""
caption_loss = contrastive_loss(similarity)
image_loss = contrastive_loss(similarity.t())
return (caption_loss + image_loss) / 2.0
class Downstream_model_finetuned(nn.Module):
"""
Downstream model that uses a pre-trained contrastive model to extract features from video and audio inputs, and then applies a classifier to produce the final output.
The model first loads the pre-trained contrastive model from a specified path, and then applies a classifier on the concatenated video and audio features to produce the final output.
Args:
hidden_channels (int): The number of hidden channels in the contrastive model.
out_channels (int): The number of output channels in the classifier.
Returns:
torch.Tensor: The final output of the downstream model.
"""
def __init__(self, hidden_channels, out_channels, model_path = None):
super(Downstream_model_finetuned, self).__init__()
self.model = Contrastive_model(hidden_channels, 100)
if model_path is not None:
model_path = model_path
self.model.load_state_dict(torch.load(model_path))
self.classifier = nn.Sequential(nn.Linear(200, hidden_channels), nn.ReLU(), nn.Linear(hidden_channels, out_channels))
# self.classifier = nn.Sequential(nn.Linear(200, out_channels))
def forward(self, x_video, x_audio):
x_video, x_audio, _, _ = self.model(x_video, x_audio)
x = self.classifier(torch.cat((x_audio, x_video), dim=1))
return x
class Downstream_model(nn.Module):
"""
Downstream model that uses a pre-trained contrastive model to extract features from video and audio inputs, and then applies a classifier to produce the final output.
The model first loads the pre-trained contrastive model from a specified path, and then applies a classifier on the concatenated video and audio features to produce the final output.
Args:
hidden_channels (int): The number of hidden channels in the contrastive model.
out_channels (int): The number of output channels in the classifier.
Returns:
torch.Tensor: The final output of the downstream model.
"""
def __init__(self, hidden_channels, out_channels):
super(Downstream_model, self).__init__()
self.classifier = nn.Sequential(nn.Linear(1168, hidden_channels), nn.ReLU(), nn.Linear(hidden_channels, out_channels))
# self.classifier = nn.Sequential(nn.Linear(1168, out_channels))
def forward(self, x_video, x_audio):
x = self.classifier(torch.cat((x_video, x_audio), dim=1))
return x
class Downstream_model_finetuned_regressive(nn.Module):
"""
Downstream model that uses a pre-trained contrastive model to extract features from video and audio inputs, and then applies a classifier to produce the final output.
The model first loads the pre-trained contrastive model from a specified path, and then applies a classifier on the concatenated video and audio features to produce the final output.
Args:
hidden_channels (int): The number of hidden channels in the contrastive model.
out_channels (int): The number of output channels in the classifier.
Returns:
torch.Tensor: The final output of the downstream model.
"""
def __init__(self, hidden_channels, model_path = None):
super(Downstream_model_finetuned_regressive, self).__init__()
self.model = Contrastive_model(hidden_channels, 100)
if model_path is not None:
model_path = model_path
self.model.load_state_dict(torch.load(model_path))
self.classifier = nn.Sequential(nn.Linear(200, hidden_channels), nn.ReLU(), nn.Linear(hidden_channels, 1))
# self.classifier = nn.Sequential(nn.Linear(200, out_channels))
def forward(self, x_video, x_text):
x_video, _, x_text, _ = self.model(x_video, None, x_text)
x = self.classifier(torch.cat((x_text, x_video), dim=1))
return x
class Downstream_model_regressive(nn.Module):
"""
Downstream model that uses a pre-trained contrastive model to extract features from video and audio inputs, and then applies a classifier to produce the final output.
The model first loads the pre-trained contrastive model from a specified path, and then applies a classifier on the concatenated video and audio features to produce the final output.
Args:
hidden_channels (int): The number of hidden channels in the contrastive model.
out_channels (int): The number of output channels in the classifier.
Returns:
torch.Tensor: The final output of the downstream model.
"""
def __init__(self, hidden_channels):
super(Downstream_model_regressive, self).__init__()
self.classifier = nn.Sequential(nn.Linear(1168, hidden_channels), nn.ReLU(), nn.Linear(hidden_channels, 1))
# self.classifier = nn.Sequential(nn.Linear(1168, out_channels))
def forward(self, x_video, x_audio):
x = self.classifier(torch.cat((x_audio, x_video), dim=1))
return x