Download benchmarks/edit/code/FiVE-Bench/models/pyramid-edit/utilities/utils.py from Ouzhang/low-high-reference: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/datasets/Ouzhang/low-high-reference/resolve/main/benchmarks/edit/code/FiVE-Bench/models/pyramid-edit/utilities/utils.py
- Command line
-
hf download hf://datasets/Ouzhang/low-high-reference/benchmarks/edit/code/FiVE-Bench/models/pyramid-edit/utilities/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/Ouzhang/low-high-reference/resolve/main/benchmarks/edit/code/FiVE-Bench/models/pyramid-edit/utilities/utils.py
1.32 kB
| import gc | |
| import random | |
| import numpy as np | |
| import torch | |
| from typing import Union, List | |
| from torchvision.io import write_video | |
| video_codec = "libx264" | |
| video_options = { | |
| "crf": "17", # Constant Rate Factor (lower value = higher quality, 18 is a good balance) | |
| "preset": "slow", # Encoding preset (e.g., ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow) | |
| } | |
| def save_video(video, path): | |
| write_video( | |
| path, | |
| video, | |
| fps=10, | |
| video_codec=video_codec, | |
| options=video_options, | |
| ) | |
| def seed_everything(seed): | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed(seed) | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| def clean_memory(): | |
| torch.cuda.empty_cache() | |
| gc.collect() | |
| torch.cuda.empty_cache() | |
| gc.collect() | |
| def isinstance_str(x: object, cls_name: Union[str, List[str]]): | |
| """ | |
| Checks whether x has any class *named* cls_name in its ancestry. | |
| Doesn't require access to the class's implementation. | |
| Useful for patching! | |
| """ | |
| if type(cls_name) == str: | |
| for _cls in x.__class__.__mro__: | |
| if _cls.__name__ == cls_name: | |
| return True | |
| else: | |
| for _cls in x.__class__.__mro__: | |
| if _cls.__name__ in cls_name: | |
| return True | |
| return False |