Skip to content

Commit 3a38f7d

Browse files
authored
[1/3][Preprocess] refactor preprocessing configs (hao-ai-lab#638)
1 parent f572319 commit 3a38f7d

3 files changed

Lines changed: 323 additions & 11 deletions

File tree

fastvideo/configs/configs.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import dataclasses
2+
from typing import Any, Optional
3+
4+
from fastvideo.configs.utils import update_config_from_args
5+
from fastvideo.utils import FlexibleArgumentParser, StoreBoolean
6+
7+
8+
@dataclasses.dataclass
9+
class PreprocessConfig:
10+
"""Configuration for preprocessing operations."""
11+
12+
# Model and dataset configuration
13+
model_path: str = ""
14+
dataset_path: str = ""
15+
dataset_output_dir: str = "./output"
16+
17+
# Dataloader configuration
18+
dataloader_num_workers: int = 1
19+
preprocess_video_batch_size: int = 2
20+
21+
# Saver configuration
22+
samples_per_file: int = 64
23+
flush_frequency: int = 256
24+
25+
# Video processing parameters
26+
max_height: int = 480
27+
max_width: int = 848
28+
num_frames: int = 163
29+
video_length_tolerance_range: float = 2.0
30+
train_fps: int = 30
31+
speed_factor: float = 1.0
32+
drop_short_ratio: float = 1.0
33+
do_temporal_sample: bool = False
34+
35+
# Model configuration
36+
training_cfg_rate: float = 0.0
37+
38+
@staticmethod
39+
def add_cli_args(parser: FlexibleArgumentParser,
40+
prefix: str = "preprocess") -> FlexibleArgumentParser:
41+
"""Add preprocessing configuration arguments to the parser."""
42+
prefix_with_dot = f"{prefix}." if (prefix.strip() != "") else ""
43+
44+
preprocess_args = parser.add_argument_group("Preprocessing Arguments")
45+
# Model & Dataset
46+
preprocess_args.add_argument(f"--{prefix_with_dot}model-path",
47+
type=str,
48+
default=PreprocessConfig.model_path,
49+
help="Path to the model for preprocessing")
50+
preprocess_args.add_argument(
51+
f"--{prefix_with_dot}dataset-path",
52+
type=str,
53+
default=PreprocessConfig.dataset_path,
54+
help="Path to the dataset directory for preprocessing")
55+
preprocess_args.add_argument(
56+
f"--{prefix_with_dot}dataset-output-dir",
57+
type=str,
58+
default=PreprocessConfig.dataset_output_dir,
59+
help="The output directory where the dataset will be written.")
60+
61+
# Dataloader
62+
preprocess_args.add_argument(
63+
f"--{prefix_with_dot}dataloader-num-workers",
64+
type=int,
65+
default=PreprocessConfig.dataloader_num_workers,
66+
help=
67+
"Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process."
68+
)
69+
preprocess_args.add_argument(
70+
f"--{prefix_with_dot}preprocess-video-batch-size",
71+
type=int,
72+
default=PreprocessConfig.preprocess_video_batch_size,
73+
help="Batch size (per device) for the training dataloader.")
74+
75+
# Saver
76+
preprocess_args.add_argument(f"--{prefix_with_dot}samples-per-file",
77+
type=int,
78+
default=PreprocessConfig.samples_per_file,
79+
help="Number of samples per output file")
80+
preprocess_args.add_argument(f"--{prefix_with_dot}flush-frequency",
81+
type=int,
82+
default=PreprocessConfig.flush_frequency,
83+
help="How often to save to parquet files")
84+
85+
# Video processing parameters
86+
preprocess_args.add_argument(f"--{prefix_with_dot}max-height",
87+
type=int,
88+
default=PreprocessConfig.max_height,
89+
help="Maximum height for video processing")
90+
preprocess_args.add_argument(f"--{prefix_with_dot}max-width",
91+
type=int,
92+
default=PreprocessConfig.max_width,
93+
help="Maximum width for video processing")
94+
preprocess_args.add_argument(f"--{prefix_with_dot}num-frames",
95+
type=int,
96+
default=PreprocessConfig.num_frames,
97+
help="Number of frames to process")
98+
preprocess_args.add_argument(
99+
f"--{prefix_with_dot}video-length-tolerance-range",
100+
type=float,
101+
default=PreprocessConfig.video_length_tolerance_range,
102+
help="Video length tolerance range")
103+
preprocess_args.add_argument(f"--{prefix_with_dot}train-fps",
104+
type=int,
105+
default=PreprocessConfig.train_fps,
106+
help="Training FPS")
107+
preprocess_args.add_argument(f"--{prefix_with_dot}speed-factor",
108+
type=float,
109+
default=PreprocessConfig.speed_factor,
110+
help="Speed factor for video processing")
111+
preprocess_args.add_argument(f"--{prefix_with_dot}drop-short-ratio",
112+
type=float,
113+
default=PreprocessConfig.drop_short_ratio,
114+
help="Ratio for dropping short videos")
115+
preprocess_args.add_argument(
116+
f"--{prefix_with_dot}do-temporal-sample",
117+
action=StoreBoolean,
118+
default=PreprocessConfig.do_temporal_sample,
119+
help="Whether to do temporal sampling")
120+
121+
# Model Training configuration
122+
preprocess_args.add_argument(f"--{prefix_with_dot}training-cfg-rate",
123+
type=float,
124+
default=PreprocessConfig.training_cfg_rate,
125+
help="Training CFG rate")
126+
127+
return parser
128+
129+
@classmethod
130+
def from_kwargs(cls, kwargs: dict[str,
131+
Any]) -> Optional["PreprocessConfig"]:
132+
"""Create PreprocessConfig from keyword arguments."""
133+
preprocess_config = cls()
134+
if not update_config_from_args(
135+
preprocess_config, kwargs, prefix="preprocess", pop_args=True):
136+
return None
137+
return preprocess_config
138+
139+
def check_preprocess_config(self) -> None:
140+
if self.dataset_path == "":
141+
raise ValueError("dataset_path must be set for preprocessing mode")
142+
if self.samples_per_file <= 0:
143+
raise ValueError("samples_per_file must be greater than 0")
144+
if self.flush_frequency <= 0:
145+
raise ValueError("flush_frequency must be greater than 0")

fastvideo/configs/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import argparse
12
from typing import Any
23

34

45
def update_config_from_args(config: Any,
56
args_dict: dict[str, Any],
67
prefix: str = "",
7-
pop_args: bool = False) -> None:
8+
pop_args: bool = False) -> bool:
89
"""
910
Update configuration object from arguments dictionary.
1011
@@ -43,3 +44,18 @@ def update_config_from_args(config: Any,
4344
for key in args_to_remove:
4445
if key not in args_not_to_remove:
4546
args_dict.pop(key)
47+
48+
return len(args_to_remove) > 0
49+
50+
51+
def clean_cli_args(args: argparse.Namespace) -> dict[str, Any]:
52+
"""
53+
Clean the arguments by removing the ones that not explicitly provided by the user.
54+
"""
55+
provided_args = {}
56+
for k, v in vars(args).items():
57+
if (v is not None and hasattr(args, '_provided')
58+
and k in args._provided):
59+
provided_args[k] = v
60+
61+
return provided_args

0 commit comments

Comments
 (0)