Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

[wip] fix scoring (#120)

Zheng Zangwei (Alex Zheng)5f9e6422789d
diff --git a/requirements/requirements-data.txt b/requirements/requirements-data.txt
index 008b90f..7a2d38d 100644
--- a/requirements/requirements-data.txt
+++ b/requirements/requirements-data.txt
@@ -20,6 +20,7 @@ lingua-language-detector==2.0.2
imageio>=2.34.1
# [aesthetic]
+setuptools==68.2.2
clip @ git+https://github.com/openai/CLIP.git
# [ocr]
diff --git a/tools/datasets/utils.py b/tools/datasets/utils.py
index fe723bf..ec7e1b9 100644
--- a/tools/datasets/utils.py
+++ b/tools/datasets/utils.py
@@ -1,6 +1,7 @@
import os
import cv2
+import numpy as np
from PIL import Image
IMG_EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp")
diff --git a/tools/scoring/aesthetic/inference.py b/tools/scoring/aesthetic/inference.py
index a291914..5d791c1 100644
--- a/tools/scoring/aesthetic/inference.py
+++ b/tools/scoring/aesthetic/inference.py
@@ -1,5 +1,8 @@
# adapted from https://github.com/christophschuhmann/improved-aesthetic-predictor/blob/main/simple_inference.py
+import cv2 # isort:skip
+
import argparse
+import gc
import os
from datetime import timedelta
@@ -11,7 +14,6 @@ import torch.distributed as dist
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
-from PIL import Image
from torch.utils.data import DataLoader, DistributedSampler
from torchvision.datasets.folder import pil_loader
from tqdm import tqdm
@@ -24,6 +26,7 @@ NUM_FRAMES_POINTS = {
3: (0.1, 0.5, 0.9),
}
+
def merge_scores(gathered_list: list, meta: pd.DataFrame, column):
# reorder
indices_list = list(map(lambda x: x[0], gathered_list))
@@ -41,32 +44,36 @@ def merge_scores(gathered_list: list, meta: pd.DataFrame, column):
# filter duplicates
unique_indices, unique_indices_idx = np.unique(flat_indices, return_index=True)
meta.loc[unique_indices, column] = flat_scores[unique_indices_idx]
-
- # jun 3 quickfix
- # lose indices in meta not in unique_indices
+
+ # drop indices in meta not in unique_indices
meta = meta.loc[unique_indices]
return meta
class VideoTextDataset(torch.utils.data.Dataset):
- def __init__(self, csv_path, transform=None, num_frames=3):
- self.csv_path = csv_path
- self.meta = pd.read_csv(csv_path)
+ def __init__(self, meta_path, transform=None, num_frames=3):
+ self.meta_path = meta_path
+ self.meta = pd.read_csv(meta_path)
self.transform = transform
self.points = NUM_FRAMES_POINTS[num_frames]
def __getitem__(self, index):
sample = self.meta.iloc[index]
path = sample["path"]
+
+ # extract frames
if not is_video(path):
images = [pil_loader(path)]
else:
- num_frames = None
- if "num_frames" in sample:
- num_frames = sample["num_frames"]
+ num_frames = sample["num_frames"] if "num_frames" in sample else None
images = extract_frames(sample["path"], points=self.points, backend="opencv", num_frames=num_frames)
+
+ # transform
images = [self.transform(img) for img in images]
+
+ # stack
images = torch.stack(images)
+
ret = dict(index=index, images=images)
return ret
@@ -97,7 +104,6 @@ class AestheticScorer(nn.Module):
def __init__(self, input_size, device):
super().__init__()
self.mlp = MLP(input_size)
- self.mlp.load_state_dict(torch.load("pretrained_models/aesthetic.pth"))
self.clip, self.preprocess = clip.load("ViT-L/14", device=device)
self.eval()
@@ -122,6 +128,7 @@ def main():
# build model
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AestheticScorer(768, device)
+ model.mlp.load_state_dict(torch.load("pretrained_models/aesthetic.pth", map_location=device))
preprocess = model.preprocess
# build dataset
@@ -138,7 +145,7 @@ def main():
drop_last=False,
),
)
-
+
# compute aesthetic scores
indices_list = []
scores_list = []
@@ -153,26 +160,28 @@ def main():
# compute score
with torch.no_grad():
scores = model(images)
+
scores = rearrange(scores, "(B N) 1 -> B N", B=B)
scores = scores.mean(dim=1)
scores_np = scores.to(torch.float32).cpu().numpy()
- indices_list.extend(indices)
- scores_list.extend(scores_np)
-
- # jun 3 quickfix
- meta_local = merge_scores([(indices_list, scores_list)], dataset.meta, column='aes')
- out_path_local = out_path.replace('.csv', f'_part_{dist.get_rank()}.csv')
+ indices_list.extend(indices.tolist())
+ scores_list.extend(scores_np.tolist())
+
+ # save local results
+ meta_local = merge_scores([(indices_list, scores_list)], dataset.meta, column="aes")
+ out_path_local = out_path.replace(".csv", f"_part_{dist.get_rank()}.csv")
meta_local.to_csv(out_path_local, index=False)
# wait for all ranks to finish data processing
- dist.barrier()
+ dist.barrier()
+ torch.cuda.empty_cache()
+ gc.collect()
gathered_list = [None] * dist.get_world_size()
- breakpoint()
dist.all_gather_object(gathered_list, (indices_list, scores_list))
if dist.get_rank() == 0:
- meta_new = merge_scores(gathered_list, dataset.meta, column='aes')
+ meta_new = merge_scores(gathered_list, dataset.meta, column="aes")
meta_new.to_csv(out_path, index=False)
print(f"New meta with aesthetic scores saved to '{out_path}'.")
@@ -182,11 +191,12 @@ def parse_args():
parser.add_argument("meta_path", type=str, help="Path to the input CSV file")
parser.add_argument("--bs", type=int, default=1024, help="Batch size")
parser.add_argument("--num_workers", type=int, default=16, help="Number of workers")
- parser.add_argument("--prefetch_factor", type=int, default=2, help="Prefetch factor")
+ parser.add_argument("--prefetch_factor", type=int, default=3, help="Prefetch factor")
parser.add_argument("--num_frames", type=int, default=3, help="Number of frames to extract")
args = parser.parse_args()
return args
+
if __name__ == "__main__":
main()
diff --git a/tools/scoring/optical_flow/inference.py b/tools/scoring/optical_flow/inference.py
index 3f7d85f..2ea0d7a 100644
--- a/tools/scoring/optical_flow/inference.py
+++ b/tools/scoring/optical_flow/inference.py
@@ -1,6 +1,7 @@
import cv2 # isort:skip
import argparse
+import gc
import os
from datetime import timedelta
@@ -38,8 +39,7 @@ def merge_scores(gathered_list: list, meta: pd.DataFrame, column):
unique_indices, unique_indices_idx = np.unique(flat_indices, return_index=True)
meta.loc[unique_indices, column] = flat_scores[unique_indices_idx]
- # jun 3 quickfix
- # lose indices in meta not in unique_indices
+ # drop indices in meta not in unique_indices
meta = meta.loc[unique_indices]
return meta
@@ -51,32 +51,30 @@ class VideoTextDataset(torch.utils.data.Dataset):
self.frame_inds = frame_inds
def __getitem__(self, index):
- row = self.meta.iloc[index]
- images = extract_frames(row["path"], frame_inds=self.frame_inds, backend="opencv")
+ sample = self.meta.iloc[index]
+ path = sample["path"]
+
+ # extract frames
+ images = extract_frames(path, frame_inds=self.frame_inds, backend="opencv")
# transform
- images = torch.stack([pil_to_tensor(x) for x in images]) # shape: [N, C, H, W]; dtype: torch.uint8
+ images = torch.stack([pil_to_tensor(x) for x in images])
+
+ # stack
+ # shape: [N, C, H, W]; dtype: torch.uint8
images = images.float()
H, W = images.shape[-2:]
if H > W:
images = rearrange(images, "N C H W -> N C W H")
images = F.interpolate(images, size=(320, 576), mode="bilinear", align_corners=True)
- return images, index
+ ret = dict(index=index, images=images)
+ return ret
def __len__(self):
return len(self.meta)
-def parse_args():
- parser = argparse.ArgumentParser()
- parser.add_argument("meta_path", type=str, help="Path to the input CSV file")
- parser.add_argument("--bs", type=int, default=4, help="Batch size") # don't use too large bs for unimatch
- parser.add_argument("--num_workers", type=int, default=16, help="Number of workers")
- args = parser.parse_args()
- return args
-
-
def main():
args = parse_args()
@@ -124,10 +122,11 @@ def main():
indices_list = []
scores_list = []
model.eval()
- for images, indices in tqdm(dataloader, disable=dist.get_rank() != 0):
- images = images.to(device)
- B = images.shape[0]
+ for batch in tqdm(dataloader, disable=dist.get_rank() != 0):
+ indices = batch["index"]
+ images = batch["images"].to(device, non_blocking=True)
+ B = images.shape[0]
batch_0 = rearrange(images[:, :-1], "B N C H W -> (B N) C H W").contiguous()
batch_1 = rearrange(images[:, 1:], "B N C H W -> (B N) C H W").contiguous()
@@ -148,10 +147,10 @@ def main():
flow_scores = flow_maps.abs().mean(dim=[1, 2, 3, 4])
flow_scores = flow_scores.tolist()
- indices_list.extend(indices)
+ indices_list.extend(indices.tolist())
scores_list.extend(flow_scores)
- # jun 3 quickfix
+ # save local results
meta_local = merge_scores([(indices_list, scores_list)], dataset.meta, column="flow")
out_path_local = out_path.replace(".csv", f"_part_{dist.get_rank()}.csv")
meta_local.to_csv(out_path_local, index=False)
@@ -159,6 +158,8 @@ def main():
# wait for all ranks to finish data processing
dist.barrier()
+ torch.cuda.empty_cache()
+ gc.collect()
gathered_list = [None] * dist.get_world_size()
dist.all_gather_object(gathered_list, (indices_list, scores_list))
if dist.get_rank() == 0:
@@ -167,5 +168,14 @@ def main():
print(f"New meta with optical flow scores saved to '{out_path}'.")
+def parse_args():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("meta_path", type=str, help="Path to the input CSV file")
+ parser.add_argument("--bs", type=int, default=4, help="Batch size") # don't use too large bs for unimatch
+ parser.add_argument("--num_workers", type=int, default=16, help="Number of workers")
+ args = parser.parse_args()
+ return args
+
+
if __name__ == "__main__":
main()