-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
277 lines (214 loc) · 10.1 KB
/
Copy pathmodel.py
File metadata and controls
277 lines (214 loc) · 10.1 KB
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
from sentence_transformers import SentenceTransformer
import random
import librosa
import numpy as np
import pandas as pd
import ffmpeg as ff
import cv2
import pickle
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchmetrics.regression import MeanAbsolutePercentageError
from torch.utils.data import Dataset, DataLoader
import os
import matplotlib.pyplot as plt
from facenet_pytorch import MTCNN, InceptionResnetV1
from torchvision import transforms
from PIL import Image
import json
from tqdm import tqdm
from catboost import CatBoostRegressor, Pool
from sklearn.metrics import mean_absolute_error
from time import time
import warnings
warnings.filterwarnings('ignore')
os.environ["TOKENIZERS_PARALLELISM"] = "true"
device = 'cpu'
class MultimodalTransformer:
# Класс для осуществления препроцессинга данных
def __init__(self, device, verbose=False, return_path=False):
self.text_model = SentenceTransformer("jinaai/jina-embeddings-v3", trust_remote_code=True).to(device)
self.mtcnn = MTCNN(margin=20, keep_all=True, device=device)
self.cv_model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
self.device = device
self.verbose = verbose
self.return_path = return_path
def __call__(self, sample):
path = sample['video_path']
text = sample['transcription']
# получение кадров из видео
frames = self.get_frames(path)
# получение мел-кепстральных коэффициентов из аудио
mfcc = self.transform_audio(path)
mfcc = torch.FloatTensor(mfcc)
if 'labels' in sample.keys():
if self.return_path:
return (path, text, frames, mfcc, sample['labels'])
return (text, frames, mfcc, sample['labels'])
else:
if self.return_path:
return (path, text, frames, mfcc, )
return (text, frames, mfcc, )
def transform_texts(self, texts):
# получение эмбеддингов транскрипций видео
if self.verbose:
print('Calculating text embeddings...')
embeddings = self.text_model.encode(texts, task='classification', prompt_name='classification')
return embeddings
def get_frames(self, path):
# получение кадров из видео
cap = cv2.VideoCapture(path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
random_indexes = sorted(random.sample(range(total_frames), 6))
video_frames = []
current_index = 0
for i in range(total_frames):
ret, frame = cap.read()
if not ret:
break
if i == random_indexes[current_index]:
video_frames.append(cv2.resize(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), (640, 360)))
current_index += 1
if current_index >= 6:
break
cap.release()
cv2.destroyAllWindows()
return video_frames
def detect_and_embed(self, batch_images):
# детекция и эмбеддинг лиц на кадрах из видео
if self.verbose:
print('Detecting faces...')
faces_list = self.mtcnn(batch_images)
del batch_images
all_faces = []
for faces in faces_list:
if faces is not None and len(faces) > 0:
faces_resized = [torch.nn.functional.interpolate(faces[0].unsqueeze(0), size=(160, 160), mode='bilinear', align_corners=False).squeeze(0)]
all_faces.extend(faces_resized)
else:
all_faces.extend([torch.zeros(3, 160, 160, dtype=torch.float32)])
if all_faces:
all_faces_tensor = torch.stack(all_faces).to(self.device)
del all_faces
if self.verbose:
print('Embedding faces...')
embeddings = self.cv_model(all_faces_tensor).cpu()
del all_faces_tensor
return embeddings
else:
return None
def aggregate_embeddings(self, embeddings):
# агрегирование эмбеддингов кадров
if embeddings is not None and len(embeddings) > 0:
return torch.mean(embeddings, axis=1)
else:
return None
def transform_video(self, video_frames):
# получение агрегированных эмбеддингов из кадров видео
if len(video_frames[0].shape) == 4:
tensor_data = torch.stack(video_frames, axis=1)
tensor_data_ = tensor_data.reshape(tensor_data.shape[0]*tensor_data.shape[1], *tensor_data.shape[2:])
embeddings = self.detect_and_embed(tensor_data_)
del tensor_data_
aggregated = self.aggregate_embeddings(embeddings.reshape(tensor_data.shape[0], tensor_data.shape[1], 512))
else:
tensor_data = torch.stack(video_frames, axis=0)
embeddings = self.detect_and_embed(tensor_data)
aggregated = self.aggregate_embeddings(embeddings.unsqueeze(0))
del embeddings
return aggregated
def transform_audio(self, path):
# получение мел-кепстральных коэффициентов из аудио
inputfile = ff.input(path) # загружаем видео
out = inputfile.output('-', format='f32le', acodec='pcm_f32le', ac=1, ar='44100', loglevel='quiet') # отделяем звук
raw = out.run(capture_stdout=True)
del inputfile, out
raw_data = np.frombuffer(raw[0],np.float32)
mfcc_data = librosa.feature.mfcc(y=raw_data, n_mfcc=16) # получаем MFCC из звуковой дорожки
mfcc_data_standardized = (mfcc_data - np.mean(mfcc_data)) / np.std(mfcc_data) # стандартизация
# truncating и padding
if mfcc_data_standardized.shape[1] > 1319:
processed = mfcc_data_standardized[:, :1319]
else:
n_pad_cols = 1319 - mfcc_data_standardized.shape[1]
padding = np.zeros((16, n_pad_cols))
processed = np.hstack((padding, mfcc_data_standardized))
del mfcc_data, mfcc_data_standardized, raw_data
return processed
class MultimodalInterviewModel(nn.Module):
def __init__(self, use_torch_clf=True):
super(MultimodalInterviewModel, self).__init__()
self.use_torch_clf = use_torch_clf
# audio
self.relu = nn.ReLU()
self.conv1 = nn.Conv1d(in_channels=16, out_channels=32, kernel_size=3)
self.batchnorm1 = nn.BatchNorm1d(num_features=32)
self.pool = nn.MaxPool1d(kernel_size=2)
self.conv2 = nn.Conv1d(in_channels=32, out_channels=4, kernel_size=3)
self.batchnorm2 = nn.BatchNorm1d(num_features=4)
self.fc_audio = nn.Linear(in_features=1312, out_features=512)
self.fusion_layer = nn.Linear(in_features=2048, out_features=2048)
# Final output layer
self.output_layer = nn.Linear(in_features=2048, out_features=6)
self.sigmoid = nn.Sigmoid()
def forward(self, audio_input, video_input, text_input):
# Get features from each modality
# AUDIO
aud = self.conv1(audio_input)
aud = self.batchnorm1(aud)
aud = self.relu(aud)
aud = self.pool(aud)
aud = self.conv2(aud)
aud = self.batchnorm2(aud)
aud = self.relu(aud)
aud = self.pool(aud)
aud = aud.flatten(start_dim=1)
aud = self.fc_audio(aud)
aud = self.relu(aud)
# Concatenate modality features
x = torch.cat((aud, video_input, text_input), dim=1)
if self.use_torch_clf:
# Fusion and prediction
x = self.fusion_layer(x)
x = self.relu(x)
x = self.output_layer(x)
out = self.sigmoid(x)
return out
else:
return x
class OneVideoProcessor:
def __init__(self, cb_model=None, emb_model=None, torch_path=None, transformer=None, device='cpu'):
self.transformer = MultimodalTransformer(device='cpu')
# катбуст не работает без гпу
self.cb_model = cb_model
self.emb_model = emb_model
torch_model = MultimodalInterviewModel()
torch_model.load_state_dict(torch.load(torch_path, map_location=torch.device('cpu')))
torch_model.eval()
self.torch_model = torch_model
self.ocean = ('O', 'C', 'E', 'A', 'N', 'I')
def transform(self, video_path, transcription):
text_embed = self.transformer.transform_texts(transcription)
text_embed = torch.FloatTensor(text_embed)
frames = self.transformer.get_frames(video_path)
frames = [torch.FloatTensor(frame) for frame in frames]
video_embed = self.transformer.transform_video(frames)
audio_embed = self.transformer.transform_audio(video_path)
audio_embed = torch.FloatTensor(audio_embed).unsqueeze(0)
return text_embed, video_embed, audio_embed
def predict_cb(self, video_path, transcription):
# без гпу не работает
text_embed, video_embed, audio_embed = self.transform(video_path, transcription)
audio_embed, video_embed, text_embed = audio_embed.to(device), video_embed.to(device), text_embed.to(device)
embed = self.emb_model(audio_embed, video_embed, text_embed)
pred = self.cb_model.predict(embed.cpu().detach().numpy())
pred_dict = dict(zip(self.ocean, pred))
return pred_dict
def predict_pt(self, video_path, transcription):
text_embed, video_embed, audio_embed = self.transform(video_path, transcription)
audio_embed, video_embed, text_embed = audio_embed.to(device), video_embed.to(device), text_embed.to(device)
pred = self.torch_model(audio_embed, video_embed, text_embed)
pred_dict = dict(zip(self.ocean, pred))
return pred_dict