-
Notifications
You must be signed in to change notification settings - Fork 0
/
musiq_features.py
executable file
·77 lines (59 loc) · 2.37 KB
/
musiq_features.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
#!/usr/bin/env python3
import argparse
import sys
import os
import gc
os.environ["TORCH_HOME"] = os.path.join(os.path.dirname(__file__), "pretrained_weights/tfmodels")
os.environ["HF_HOME"] = os.path.join(os.path.dirname(__file__), "pretrained_weights/hugginface")
import json
import pyiqa
import torch
import torchvision
DEVICE = 'cuda'
import numpy as np
import pandas as pd
import decord
from utils import video_frames
from utils import sample_non_uniform
from utils import prefix_dict
from utils import chunk_list
def extract_features(video_path, frame_sampling=True):
musiq_model = pyiqa.create_metric('musiq').cuda()
print(f"musiq features of {video_path}")
values = []
values_cc = []
for frame_batch in chunk_list(video_frames(video_path, bridge="torch"), 2):
musiq_scores = musiq_model(
torch.cat([x.permute(2,0, 1).unsqueeze(0) for x in frame_batch])
)
values.extend(musiq_scores.cpu().numpy().flatten())
musiq_scores_cc = musiq_model(
torch.cat([torchvision.transforms.CenterCrop(size=(2*224, 2*224))(x.permute(2,0, 1)).unsqueeze(0) for x in frame_batch])
)
values_cc.extend(musiq_scores_cc.cpu().numpy().flatten())
res = {
"musiq": values,
"musiq_cc": values_cc
}
df = pd.DataFrame(res)
mean = prefix_dict(df.mean().to_dict(), "mean_")
return mean
if __name__ == "__main__":
# argument parsing
parser = argparse.ArgumentParser(description='musiq feature estimation',
epilog="stg7 2024",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("video", type=str, nargs="+", help="video to extract scores")
parser.add_argument("--features_folder", type=str, default="features_musiq", help="only for calculate features, folder to store the features")
a = vars(parser.parse_args())
for video in a["video"]:
features = extract_features(video)
features["video"] = video
print(features)
featuresfile = os.path.join(
a["features_folder"], os.path.splitext(os.path.basename(video))[0] + ".json"
)
os.makedirs(a["features_folder"], exist_ok=True)
print(f"saving features in {featuresfile}")
with open(featuresfile, "w") as xfp:
json.dump(features, xfp, indent=4, sort_keys=True)