diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b56bb68e0d77cfe39db25adaf27c8c710577556b --- /dev/null +++ b/.gitignore @@ -0,0 +1,231 @@ +**/__pycache__ +**/*.pyc +**/*.log +**/*.png +**/*.jpg +**/*.mp4 +HPSv2 +**/HPSv2 +wandb + +# VADER-VideoCrafter +VADER-VideoCrafter/.DS_Store +VADER-VideoCrafter/.vscode +VADER-VideoCrafter/__pycache__ +VADER-VideoCrafter/*.egg-info +VADER-VideoCrafter/checkpoints +VADER-VideoCrafter/results +VADER-VideoCrafter/wandb +VADER-VideoCrafter/project_dir +VADER-VideoCrafter/scripts/evaluation/__pycache__ +VADER-VideoCrafter/scripts/lvdm/__pycache__ + + +# Byte-compiled / optimized / DLL files +VADER-Open-Sora/__pycache__/ +VADER-Open-Sora/scripts/__pycache__ +VADER-Open-Sora/opensora/__pycache__ +VADER-Open-Sora/*.py[cod] +VADER-Open-Sora/*$py.class + +# C extensions +VADER-Open-Sora/*.so + +# Distribution / packaging +VADER-Open-Sora/.Python +VADER-Open-Sora/build/ +VADER-Open-Sora/develop-eggs/ +VADER-Open-Sora/dist/ +VADER-Open-Sora/downloads/ +VADER-Open-Sora/eggs/ +VADER-Open-Sora/.eggs/ +VADER-Open-Sora/lib/ +VADER-Open-Sora/lib64/ +VADER-Open-Sora/parts/ +VADER-Open-Sora/sdist/ +VADER-Open-Sora/var/ +VADER-Open-Sora/wheels/ +VADER-Open-Sora/share/python-wheels/ +VADER-Open-Sora/*.egg-info/ +VADER-Open-Sora/.installed.cfg +VADER-Open-Sora/*.egg +VADER-Open-Sora/MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +VADER-Open-Sora/*.manifest +VADER-Open-Sora/*.spec + +# Installer logs +VADER-Open-Sora/pip-log.txt +VADER-Open-Sora/pip-delete-this-directory.txt + +# Unit test / coverage reports +VADER-Open-Sora/htmlcov/ +VADER-Open-Sora/.tox/ +VADER-Open-Sora/.nox/ +VADER-Open-Sora/.coverage +VADER-Open-Sora/.coverage.* +VADER-Open-Sora/.cache +VADER-Open-Sora/nosetests.xml +VADER-Open-Sora/coverage.xml +VADER-Open-Sora/*.cover +VADER-Open-Sora/*.py,cover +VADER-Open-Sora/.hypothesis/ +VADER-Open-Sora/.pytest_cache/ +VADER-Open-Sora/cover/ + +# Translations +VADER-Open-Sora/*.mo +VADER-Open-Sora/*.pot + +# Django stuff: +VADER-Open-Sora/*.log +VADER-Open-Sora/local_settings.py +VADER-Open-Sora/db.sqlite3 +VADER-Open-Sora/db.sqlite3-journal + +# Flask stuff: +VADER-Open-Sora/instance/ +VADER-Open-Sora/.webassets-cache + +# Scrapy stuff: +VADER-Open-Sora/.scrapy + +# Sphinx documentation +VADER-Open-Sora/docs/_build/ + +# PyBuilder +VADER-Open-Sora/.pybuilder/ +VADER-Open-Sora/target/ + +# Jupyter Notebook +VADER-Open-Sora/.ipynb_checkpoints + +# IPython +VADER-Open-Sora/profile_default/ +VADER-Open-Sora/ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +VADER-Open-Sora/.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +VADER-Open-Sora/__pypackages__/ + +# Celery stuff +VADER-Open-Sora/celerybeat-schedule +VADER-Open-Sora/celerybeat.pid + +# SageMath parsed files +VADER-Open-Sora/*.sage.py + +# Environments +VADER-Open-Sora/.env +VADER-Open-Sora/.venv +VADER-Open-Sora/env/ +VADER-Open-Sora/venv/ +VADER-Open-Sora/ENV/ +VADER-Open-Sora/env.bak/ +VADER-Open-Sora/venv.bak/ + + +# Spyder project settings +VADER-Open-Sora/.spyderproject +VADER-Open-Sora/.spyproject + +# Rope project settings +VADER-Open-Sora/.ropeproject + +# mkdocs documentation +VADER-Open-Sora/site + +# mypy +VADER-Open-Sora/.mypy_cache/ +VADER-Open-Sora/.dmypy.json +VADER-Open-Sora/dmypy.json + +# Pyre type checker +VADER-Open-Sora/.pyre/ + +# pytype static type analyzer +VADER-Open-Sora/.pytype/ + +# Cython debug symbols +VADER-Open-Sora/cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +VADER-Open-Sora/.idea/ +VADER-Open-Sora/.vscode/ + +# macos +VADER-Open-Sora/*.DS_Store + +# misc files +VADER-Open-Sora/data/ +VADER-Open-Sora/dataset/ +VADER-Open-Sora/runs/ +VADER-Open-Sora/checkpoints/ +VADER-Open-Sora/outputs/ +VADER-Open-Sora/outputs +VADER-Open-Sora/samples/ +VADER-Open-Sora/samples +VADER-Open-Sora/logs/ +VADER-Open-Sora/pretrained_models/ +VADER-Open-Sora/pretrained_models +VADER-Open-Sora/evaluation_results/ +VADER-Open-Sora/cache/ +VADER-Open-Sora/*.swp + +# Secret files +VADER-Open-Sora/hostfile +VADER-Open-Sora/run.sh +VADER-Open-Sora/gradio_cached_examples/ +VADER-Open-Sora/wandb/ + +# vae weights +VADER-Open-Sora/eval/vae/flolpips/weights/ + +# npm +VADER-Open-Sora/node_modules/ +VADER-Open-Sora/package-lock.json +VADER-Open-Sora/package.json + +# PLLaVA +VADER-Open-Sora/tools/caption/pllava_dir/PLLaVA/ + +# vbench +VADER-Open-Sora/vbench +VADER-Open-Sora/!eval/vbench +VADER-Open-Sora/vbench2_beta_i2v + +# Video files +VADER-Open-Sora/project_dir \ No newline at end of file diff --git a/Core/actpred_scorer.py b/Core/actpred_scorer.py new file mode 100644 index 0000000000000000000000000000000000000000..acefab9f8de20eea74b51a7519ea137fd911a315 --- /dev/null +++ b/Core/actpred_scorer.py @@ -0,0 +1,88 @@ + +from transformers import VideoMAEFeatureExtractor, VideoMAEForVideoClassification +import torch +import numpy as np + +class ActPredScorer(torch.nn.Module): + + def __init__(self, model_name = "MCG-NJU/videomae-base-finetuned-kinetics", num_frames = 16, device = 'cuda', dtype=torch.float32): + super().__init__() + self.model = VideoMAEForVideoClassification.from_pretrained(model_name, num_frames = num_frames, torch_dtype=dtype) + self.feature_extractor = VideoMAEFeatureExtractor.from_pretrained(model_name) + self.device = device + self.model.to(device) + + def get_target_class_idx(self, target_action): + def mapping_func(x): + if 'piano' in x: + return 'playing piano' + if 'guitar' in x: + return 'playing guitar' + if 'doughnuts' in x: + return 'eating doughnuts' + if 'beer' in x: + return 'drinking beer' + if 'badminton' in x: + return 'playing badminton' + if 'cello' in x: + return 'playing cello' + if 'scooter' in x: + return 'riding scooter' + if 'ballet' in x: + return 'dancing ballet' + if 'pancake' in x: + return 'flipping pancake' + if 'violin' in x: + return 'playing violin' + if 'wood' in x: + return 'chopping wood' + if 'watermelon' in x: + return 'eating watermelon' + if 'jogging' in x: + return 'jogging' + else: + print(f"Please add your action mapping to ActPredScorer. Mapping not found for {x}") + raise NotImplementedError + + + try: + target_class_idx = self.model.config.label2id[target_action] + except: + target_class_idx = self.model.config.label2id[mapping_func(target_action)] + return target_class_idx + + def get_loss_and_score(self, norm_vid, target_action): + ''' video should be a torch array of dtype float, with values from 0-1, of dimension (num_frames, height, width, 3)''' + + target_class_idx = self.get_target_class_idx(target_action) + outputs = self.model(norm_vid, labels = torch.tensor([target_class_idx]).to(self.device)) + loss = outputs.loss + logits = outputs.logits + + norm_logits = torch.exp(logits)/ (torch.exp(logits).sum()) + norm_logits = norm_logits.squeeze() + + score = norm_logits[target_class_idx] + return loss, score, self.get_pred_class(logits) + + def get_pred_class(self, logits): + predicted_class_idx = logits.argmax(-1).item() + return self.model.config.id2label[predicted_class_idx] + +def gen_rand_labels_file(labels_list, out_file, num_labels = 50): + idxs = np.random.choice(len(labels_list), num_labels, replace = False) + rand_labels = [labels_list[i] for i in idxs] + rand_labels.sort() + with open(out_file, 'w') as f: + for line in rand_labels: + f.write(f"{line}\n") + +if __name__ == '__main__': + # import numpy as np + # scorer = ActPredScorer(num_frames = 7) + # video_torch = [torch.randn((3,256,256)).clamp(0,1) for _ in range(7)] + # encoding = scorer.feature_extractor(video_torch, do_rescale = False, return_tensors="pt") + # print(scorer.get_loss_and_score(video_torch)) + scorer = ActPredScorer(num_frames = 7) + labels = scorer.model.config.id2label + \ No newline at end of file diff --git a/Core/aesthetic_scorer.py b/Core/aesthetic_scorer.py new file mode 100644 index 0000000000000000000000000000000000000000..c6f9005394d1753211b104614376d6fc9c4b526f --- /dev/null +++ b/Core/aesthetic_scorer.py @@ -0,0 +1,46 @@ +# Based on https://github.com/christophschuhmann/improved-aesthetic-predictor/blob/fe88a163f4661b4ddabba0751ff645e2e620746e/simple_inference.py +# import ipdb +# st = ipdb.set_trace +from importlib_resources import files +import torch +import torch.nn as nn +import numpy as np +from transformers import CLIPModel, CLIPProcessor +from PIL import Image +ASSETS_PATH = files("assets") +# ASSETS_PATH = "assets" + +class MLPDiff(nn.Module): + def __init__(self): + super().__init__() + self.layers = nn.Sequential( + nn.Linear(768, 1024), + nn.Dropout(0.2), + nn.Linear(1024, 128), + nn.Dropout(0.2), + nn.Linear(128, 64), + nn.Dropout(0.1), + nn.Linear(64, 16), + nn.Linear(16, 1), + ) + + + def forward(self, embed): + return self.layers(embed) + + +class AestheticScorerDiff(torch.nn.Module): + def __init__(self, dtype): + super().__init__() + self.clip = CLIPModel.from_pretrained("openai/clip-vit-large-patch14") + self.mlp = MLPDiff() + state_dict = torch.load(ASSETS_PATH.joinpath("sac+logos+ava1-l14-linearMSE.pth")) + self.mlp.load_state_dict(state_dict) + self.dtype = dtype + self.eval() + + def __call__(self, images): + device = next(self.parameters()).device + embed = self.clip.get_image_features(pixel_values=images) + embed = embed / torch.linalg.vector_norm(embed, dim=-1, keepdim=True) + return self.mlp(embed).squeeze(1) diff --git a/Core/compression_scorer.py b/Core/compression_scorer.py new file mode 100644 index 0000000000000000000000000000000000000000..a9a1dbb630958571e50f7ff69f7599ef5d5d696f --- /dev/null +++ b/Core/compression_scorer.py @@ -0,0 +1,111 @@ +# Adapt from Cheng An Hsieh, et. al.: https://github.com/RewardMultiverse/reward-multiverse +from PIL import Image +import io +import numpy as np +import torch.nn as nn +import torch +import torchvision +import albumentations as A +from transformers import CLIPModel, CLIPProcessor +# import ipdb +# st = ipdb.set_trace + +def jpeg_compressibility(device): + def _fn(images): + ''' + args: + images: shape NCHW + ''' + org_type = images.dtype + if isinstance(images, torch.Tensor): + images = (images * 255).round().clamp(0, 255).to(torch.uint8).cpu().numpy() + images = images.transpose(0, 2, 3, 1) # NCHW -> NHWC + + transform_images_tensor = torch.Tensor(np.array(images)).to(device, dtype=org_type) + transform_images_tensor = (transform_images_tensor.permute(0,3,1,2) / 255).clamp(0,1) # NHWC -> NCHW + transform_images_pil = [Image.fromarray(image) for image in images] + buffers = [io.BytesIO() for _ in transform_images_pil] + + for image, buffer in zip(transform_images_pil, buffers): + image.save(buffer, format="JPEG", quality=95) + + sizes = [buffer.tell() / 1000 for buffer in buffers] + + return np.array(sizes), transform_images_tensor + + return _fn + + +class MLP(nn.Module): + def __init__(self): + super().__init__() + self.layers = nn.Sequential( + nn.Linear(768, 512), + nn.ReLU(), + nn.Dropout(0.2), + nn.Linear(512, 256), + nn.ReLU(), + nn.Dropout(0.2), + nn.Linear(256, 128), + nn.ReLU(), + nn.Dropout(0.2), + nn.Linear(128, 32), + nn.ReLU(), + nn.Dropout(0.1), + nn.Linear(32, 1), + ) + + def forward(self, embed): + return self.layers(embed) + +def jpegcompression_loss_fn(target=None, + grad_scale=0, + device=None, + accelerator=None, + torch_dtype=None, + reward_model_resume_from=None): + scorer = JpegCompressionScorer(dtype=torch_dtype, model_path=reward_model_resume_from).to(device, dtype=torch_dtype) + scorer.requires_grad_(False) + scorer.eval() + def loss_fn(im_pix_un): + if accelerator.mixed_precision == "fp16": + with accelerator.autocast(): + rewards = scorer(im_pix_un) + else: + rewards = scorer(im_pix_un) + + if target is None: + loss = rewards + else: + loss = abs(rewards - target) + return loss * grad_scale, rewards + return loss_fn + +class JpegCompressionScorer(nn.Module): + def __init__(self, dtype=None, model_path=None): + super().__init__() + self.clip = CLIPModel.from_pretrained("openai/clip-vit-large-patch14") + self.clip.requires_grad_(False) + self.score_generator = MLP() + + if model_path: + state_dict = torch.load(model_path) + self.score_generator.load_state_dict(state_dict) + if dtype: + self.dtype = dtype + self.target_size = (224,224) + self.normalize = torchvision.transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], + std=[0.26862954, 0.26130258, 0.27577711]) + + + def set_device(self, device, inference_type): + # self.clip.to(device, dtype = inference_type) + self.score_generator.to(device) # , dtype = inference_type + + def __call__(self, images): + device = next(self.parameters()).device + im_pix = torchvision.transforms.Resize(self.target_size)(images) + im_pix = self.normalize(im_pix).to(images.dtype) + embed = self.clip.get_image_features(pixel_values=im_pix) + embed = embed / torch.linalg.vector_norm(embed, dim=-1, keepdim=True) + return self.score_generator(embed).squeeze(1) \ No newline at end of file diff --git a/Core/prompts.py b/Core/prompts.py new file mode 100644 index 0000000000000000000000000000000000000000..bc2128b7cd2af5957992cbf491d589ff266e319d --- /dev/null +++ b/Core/prompts.py @@ -0,0 +1,159 @@ +from importlib_resources import files +import os +import functools +import random +import inflect + +IE = inflect.engine() +ASSETS_PATH = files("assets") +# ASSETS_PATH = "assets" + + +@functools.lru_cache(maxsize=None) +def _load_lines(path): + """ + Load lines from a file. First tries to load from `path` directly, and if that doesn't exist, searches the + `ddpo_pytorch/assets` directory for a file named `path`. + """ + if not os.path.exists(path): + newpath = ASSETS_PATH.joinpath(path) + if not os.path.exists(newpath): + raise FileNotFoundError(f"Could not find {path} or assets/{path}") + path = newpath + with open(path, "r") as f: + return [line.strip() for line in f.readlines()] + +def hps_v2_all(nouns_file=None, activities_file=None): + return from_file("hps_v2_all.txt") + +def hps_custom(nouns_file=None, activities_file=None): + return from_file("hps_custom.txt") + +def hps_debug(nouns_file=None, activities_file=None): + return from_file("hps_debug.txt") + +def hps_single(nouns_file=None, activities_file=None): + return from_file("hps_single.txt") + +def kinetics_4rand(nouns_file=None, activities_file=None): + return from_file("kinetics_4rand.txt") + +def kinetics_50rand(nouns_file=None, activities_file=None): + return from_file("kinetics_50rand.txt") + +def simple_animals(): + return from_file("simple_animals.txt") + +def eval_simple_animals(): + return from_file("eval_simple_animals.txt") + +def eval_hps_v2_all(nouns_file=None, activities_file=None): + return from_file("hps_v2_all_eval.txt") + +def chatgpt_custom(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom.txt") + +def chatgpt_custom_instruments(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_instruments.txt") + +def chatgpt_custom_human(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_human.txt") + +def chatgpt_custom_human_activity(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_human_activity.txt") + +def chatgpt_custom_animal(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal.txt") + +def chatgpt_custom_animal_sport(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_sport.txt") + +def chatgpt_custom_animal_sportV2(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_sportV2.txt") + +def chatgpt_custom_animal_clothes(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_clothes.txt") + +def chatgpt_custom_animal_clothesV2(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_clothesV2.txt") + +def chatgpt_custom_animal_clothesV3(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_clothesV3.txt") + +def chatgpt_custom_animal_technology(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_technology.txt") + +def chatgpt_custom_animal_housework(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_housework.txt") + +def chatgpt_custom_animal_action(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_animal_action.txt") + +def chatgpt_custom_outdoor(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_outdoor.txt") + +def chatgpt_custom_rainy(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_rainy.txt") + +def chatgpt_custom_snowy(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_snowy.txt") + +def chatgpt_custom_dog(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_dog.txt") + +def chatgpt_custom_banana(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_banana.txt") + +def chatgpt_custom_forest(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_forest.txt") + +def chatgpt_custom_forest_vivid(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_forest_vivid.txt") + +def chatgpt_custom_cruel_animal(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_cruel_animal.txt") + +def chatgpt_custom_cruel_animal2(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_cruel_animal2.txt") + +def chatgpt_custom_bottle_glass(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_bottle_glass.txt") + +def chatgpt_custom_book_cup(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_book_cup.txt") + +def chatgpt_custom_book_cup_character(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_book_cup_character.txt") + +def chatgpt_custom_cute(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_cute.txt") + +def chatgpt_custom_ice(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_ice.txt") + +def chatgpt_custom_compression(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_compression.txt") + +def chatgpt_custom_compression_animals(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_compression_animals.txt") + +def chatgpt_custom_actpred(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_actpred.txt") + +def chatgpt_custom_actpred2(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_actpred2.txt") + +def chatgpt_custom_instruments_unseen(nouns_file=None, activities_file=None): + return from_file("chatgpt_custom_instruments_unseen.txt") + +def from_file(path, low=None, high=None, **kwargs): + prompts = _load_lines(path)[low:high] + return random.choice(prompts), {} + +def from_str(_str, **kwargs): + return _str, {} + +def nouns_activities(nouns_file, activities_file, **kwargs): + nouns = _load_lines(nouns_file) + activities = _load_lines(activities_file) + return f"{IE.a(random.choice(nouns))} {random.choice(activities)}", {} \ No newline at end of file diff --git a/Core/weather_scorer.py b/Core/weather_scorer.py new file mode 100644 index 0000000000000000000000000000000000000000..29e34a89c054c9b63fca9b254dc73cad9bd48f24 --- /dev/null +++ b/Core/weather_scorer.py @@ -0,0 +1,161 @@ +# Copy from Cheng An Hsieh, et. al.: https://github.com/RewardMultiverse/reward-multiverse +import torch +import torch.nn as nn +import torchvision +from transformers import CLIPModel, CLIPProcessor + +class SimpleCNN(nn.Module): # parameter = 6333513 + def __init__(self, num_class = None): + super(SimpleCNN, self).__init__() + self.layer1 = nn.Sequential( + nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1), + nn.ReLU(), + nn.MaxPool2d(kernel_size=2, stride=2)) + self.layer2 = nn.Sequential( + nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1), + nn.ReLU(), + nn.MaxPool2d(kernel_size=2, stride=2)) + self.layer3 = nn.Sequential( + nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1), + nn.ReLU(), + nn.MaxPool2d(kernel_size=2, stride=2)) + self.layer4 = nn.Sequential( + nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), + nn.ReLU(), + nn.MaxPool2d(kernel_size=2, stride=2)) + self.fc1 = nn.Linear(128 * 32 * 32, 1000) + self.fc2 = nn.Linear(1000, num_class) + + def forward(self, x): + x = self.layer1(x) + # print("x1", x.shape) + x = self.layer2(x) + # print("x2", x.shape) + x = self.layer3(x) + # print("x3", x.shape) + x = self.layer4(x) + # print("x4", x.shape) + + x = x.reshape(x.size(0), -1) + # print("x reshape", x.shape) + x = torch.relu(self.fc1(x)) + x = self.fc2(x) + return x + + +class MLP(nn.Module): + def __init__(self): + super().__init__() + self.layers = nn.Sequential( # regression + nn.Linear(768, 1024), + nn.Dropout(0.2), + nn.Linear(1024, 128), + nn.Dropout(0.2), + nn.Linear(128, 64), + nn.Dropout(0.1), + nn.Linear(64, 16), + nn.Linear(16, 1), + nn.Sigmoid() + ) + + # self.layers = nn.Sequential( # classification + # nn.Linear(768, 1024), + # nn.Dropout(0.2), + # nn.Linear(1024, 128), + # nn.Dropout(0.2), + # nn.Linear(128, 64), + # nn.Dropout(0.1), + # nn.Linear(64, 16), + # nn.Linear(16, 2) + # ) + + def forward(self, embed): + return self.layers(embed) + +class MLP_Resnet(nn.Module): + def __init__(self, num_class): + super().__init__() + self.layers = nn.Sequential( + nn.Linear(1000, 128), + # nn.Dropout(0.2), + nn.Linear(128, 64), + # nn.Dropout(0.2), + nn.Linear(64, 16), + nn.Linear(16, num_class), + ) + + def forward(self, embed): + return self.layers(embed) + + +def weather_loss_fn(target=None, # TODO: use config.task to decide returned loss_fn + grad_scale=0, + device=None, + accelerator=None, + torch_dtype=None, + reward_model_resume_from=None, + num_of_labels=None): + scorer = WeatherScorer(dtype=torch_dtype, model_path=reward_model_resume_from, num_class=num_of_labels).to(device, dtype=torch_dtype) + scorer.requires_grad_(False) + scorer.eval() + + def loss_fn(im_pix_un): + if accelerator.mixed_precision == "fp16": + with accelerator.autocast(): + rewards = scorer(im_pix_un) + else: + rewards = scorer(im_pix_un) + + target_tensors = torch.full((rewards.shape[0],), target).to(rewards.device, dtype=rewards.dtype) # regression + criterion = torch.nn.MSELoss(reduction = "sum") # regression + # target_tensors = torch.full((rewards.shape[0],), target).to(rewards.device, dtype=torch.long) # classification + # criterion = nn.CrossEntropyLoss(reduction="sum") # classification + loss = criterion(rewards, target_tensors) + return loss * grad_scale, rewards #nn.Softmax(dim=-1)(rewards) # rewards (reg) + return loss_fn + + +class WeatherModel(nn.Module): + def __init__(self, num_class = None): + super().__init__() + self.embed_model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True) + self.score_model = MLP_Resnet(num_class) + def __call__(self, im): + return self.score_model(self.embed_model(im)) + + +class WeatherScorer(nn.Module): # Reward model + def __init__(self, dtype=None, model_path = None, num_class = None): + super().__init__() + self.clip = CLIPModel.from_pretrained("openai/clip-vit-large-patch14") + self.clip.requires_grad_(False) + self.clip.eval() + self.score_generator = MLP() + # self.score_generator = WeatherModel(num_class) # resnet + mlp + if model_path: + state_dict = torch.load(model_path) + self.score_generator.load_state_dict(state_dict) + self.score_generator.requires_grad_(False) + self.score_generator.eval() + # self.clip.requires_grad_(False) + # self.clip.eval() + else: + self.score_generator.requires_grad_(True) + if dtype: + self.dtype = dtype + self.target_size = (224,224) # resnet 224, cnn 512 (use 224 for both...?) + self.normalize = torchvision.transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], + std=[0.26862954, 0.26130258, 0.27577711]) + + def set_device(self, device, inference_type): + self.clip.to(device, dtype = inference_type) # uncomment for mlp + self.score_generator.to(device) # dtype = inference_dtype + + def __call__(self, images): + device = next(self.parameters()).device + im_pix = torchvision.transforms.Resize(self.target_size)(images) + im_pix = self.normalize(im_pix).to(images.dtype) + embed = self.clip.get_image_features(pixel_values=im_pix) + embed = embed / torch.linalg.vector_norm(embed, dim=-1, keepdim=True) + return self.score_generator(embed).squeeze(1) # CLIP + MLP + # return self.score_generator(im_pix).squeeze(1) # for simpleCNN \ No newline at end of file diff --git a/VADER-VideoCrafter/License b/VADER-VideoCrafter/License new file mode 100644 index 0000000000000000000000000000000000000000..e2741c4696c41ba1b3bf22748b45e9b5db09c445 --- /dev/null +++ b/VADER-VideoCrafter/License @@ -0,0 +1,470 @@ +This license applies to the source codes that are open sourced in connection with the VideoCrafter1. + +Copyright (C) 2023 THL A29 Limited, a Tencent company. + +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +10. This code is provided for research purposes only and is +not to be used for any commercial purposes. By using this code, +you agree that it will be used solely for academic research, scholarly work, +and non-commercial activities. Any use of this code for commercial purposes, +including but not limited to, selling, distributing, or incorporating it into +commercial products or services, is strictly prohibited. Violation of this +clause may result in legal actions and penalties. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Other dependencies and licenses (if such optional components are used): + + +Components under BSD 3-Clause License: +------------------------------------------------ +1. numpy +Copyright (c) 2005-2022, NumPy Developers. +All rights reserved. + +2. pytorch +Copyright (c) 2016- Facebook, Inc (Adam Paszke) +Copyright (c) 2014- Facebook, Inc (Soumith Chintala) +Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) +Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu) +Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) +Copyright (c) 2011-2013 NYU (Clement Farabet) +Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston) +Copyright (c) 2006 Idiap Research Institute (Samy Bengio) +Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz) + +3. torchvision +Copyright (c) Soumith Chintala 2016, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Component under Apache v2 License: +----------------------------------------------------- +1. timm +Copyright 2019 Ross Wightman + +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/VADER-VideoCrafter/configs/inference_i2v_512_v1.0.yaml b/VADER-VideoCrafter/configs/inference_i2v_512_v1.0.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7d490079dd655bd3e9fa7656532b2417085370e7 --- /dev/null +++ b/VADER-VideoCrafter/configs/inference_i2v_512_v1.0.yaml @@ -0,0 +1,83 @@ +model: + target: lvdm.models.ddpm3d.LatentVisualDiffusion + params: + linear_start: 0.00085 + linear_end: 0.012 + num_timesteps_cond: 1 + timesteps: 1000 + first_stage_key: video + cond_stage_key: caption + cond_stage_trainable: false + conditioning_key: crossattn + image_size: + - 40 + - 64 + channels: 4 + scale_by_std: false + scale_factor: 0.18215 + use_ema: false + uncond_type: empty_seq + use_scale: true + scale_b: 0.7 + finegrained: true + unet_config: + target: lvdm.modules.networks.openaimodel3d.UNetModel + params: + in_channels: 4 + out_channels: 4 + model_channels: 320 + attention_resolutions: + - 4 + - 2 + - 1 + num_res_blocks: 2 + channel_mult: + - 1 + - 2 + - 4 + - 4 + num_head_channels: 64 + transformer_depth: 1 + context_dim: 1024 + use_linear: true + use_checkpoint: true + temporal_conv: true + temporal_attention: true + temporal_selfatt_only: true + use_relative_position: false + use_causal_attention: false + use_image_attention: true + temporal_length: 16 + addition_attention: true + fps_cond: true + first_stage_config: + target: lvdm.models.autoencoder.AutoencoderKL + params: + embed_dim: 4 + monitor: val/rec_loss + ddconfig: + double_z: true + z_channels: 4 + resolution: 512 + in_channels: 3 + out_ch: 3 + ch: 128 + ch_mult: + - 1 + - 2 + - 4 + - 4 + num_res_blocks: 2 + attn_resolutions: [] + dropout: 0.0 + lossconfig: + target: torch.nn.Identity + cond_stage_config: + target: lvdm.modules.encoders.condition.FrozenOpenCLIPEmbedder + params: + freeze: true + layer: penultimate + cond_img_config: + target: lvdm.modules.encoders.condition.FrozenOpenCLIPImageEmbedderV2 + params: + freeze: true \ No newline at end of file diff --git a/VADER-VideoCrafter/configs/inference_t2v_1024_v1.0.yaml b/VADER-VideoCrafter/configs/inference_t2v_1024_v1.0.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4cb9af1562264f63e029ce536b16f03c30f0d279 --- /dev/null +++ b/VADER-VideoCrafter/configs/inference_t2v_1024_v1.0.yaml @@ -0,0 +1,77 @@ +model: + target: lvdm.models.ddpm3d.LatentDiffusion + params: + linear_start: 0.00085 + linear_end: 0.012 + num_timesteps_cond: 1 + timesteps: 1000 + first_stage_key: video + cond_stage_key: caption + cond_stage_trainable: false + conditioning_key: crossattn + image_size: + - 72 + - 128 + channels: 4 + scale_by_std: false + scale_factor: 0.18215 + use_ema: false + uncond_type: empty_seq + use_scale: true + fix_scale_bug: true + unet_config: + target: lvdm.modules.networks.openaimodel3d.UNetModel + params: + in_channels: 4 + out_channels: 4 + model_channels: 320 + attention_resolutions: + - 4 + - 2 + - 1 + num_res_blocks: 2 + channel_mult: + - 1 + - 2 + - 4 + - 4 + num_head_channels: 64 + transformer_depth: 1 + context_dim: 1024 + use_linear: true + use_checkpoint: true + temporal_conv: false + temporal_attention: true + temporal_selfatt_only: true + use_relative_position: true + use_causal_attention: false + temporal_length: 16 + addition_attention: true + fps_cond: true + first_stage_config: + target: lvdm.models.autoencoder.AutoencoderKL + params: + embed_dim: 4 + monitor: val/rec_loss + ddconfig: + double_z: true + z_channels: 4 + resolution: 512 + in_channels: 3 + out_ch: 3 + ch: 128 + ch_mult: + - 1 + - 2 + - 4 + - 4 + num_res_blocks: 2 + attn_resolutions: [] + dropout: 0.0 + lossconfig: + target: torch.nn.Identity + cond_stage_config: + target: lvdm.modules.encoders.condition.FrozenOpenCLIPEmbedder + params: + freeze: true + layer: penultimate diff --git a/VADER-VideoCrafter/configs/inference_t2v_512_v1.0.yaml b/VADER-VideoCrafter/configs/inference_t2v_512_v1.0.yaml new file mode 100644 index 0000000000000000000000000000000000000000..849623ebe95f6962cbb85cfa10189dd3f5b44fb4 --- /dev/null +++ b/VADER-VideoCrafter/configs/inference_t2v_512_v1.0.yaml @@ -0,0 +1,74 @@ +model: + target: lvdm.models.ddpm3d.LatentDiffusion + params: + linear_start: 0.00085 + linear_end: 0.012 + num_timesteps_cond: 1 + timesteps: 1000 + first_stage_key: video + cond_stage_key: caption + cond_stage_trainable: false + conditioning_key: crossattn + image_size: + - 40 + - 64 + channels: 4 + scale_by_std: false + scale_factor: 0.18215 + use_ema: false + uncond_type: empty_seq + unet_config: + target: lvdm.modules.networks.openaimodel3d.UNetModel + params: + in_channels: 4 + out_channels: 4 + model_channels: 320 + attention_resolutions: + - 4 + - 2 + - 1 + num_res_blocks: 2 + channel_mult: + - 1 + - 2 + - 4 + - 4 + num_head_channels: 64 + transformer_depth: 1 + context_dim: 1024 + use_linear: true + use_checkpoint: true + temporal_conv: false + temporal_attention: true + temporal_selfatt_only: true + use_relative_position: true + use_causal_attention: false + temporal_length: 16 + addition_attention: true + first_stage_config: + target: lvdm.models.autoencoder.AutoencoderKL + params: + embed_dim: 4 + monitor: val/rec_loss + ddconfig: + double_z: true + z_channels: 4 + resolution: 512 + in_channels: 3 + out_ch: 3 + ch: 128 + ch_mult: + - 1 + - 2 + - 4 + - 4 + num_res_blocks: 2 + attn_resolutions: [] + dropout: 0.0 + lossconfig: + target: torch.nn.Identity + cond_stage_config: + target: lvdm.modules.encoders.condition.FrozenOpenCLIPEmbedder + params: + freeze: true + layer: penultimate diff --git a/VADER-VideoCrafter/configs/inference_t2v_512_v2.0.yaml b/VADER-VideoCrafter/configs/inference_t2v_512_v2.0.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4a2e6c4e88ad32e439bb6b95b1a200d0ef104603 --- /dev/null +++ b/VADER-VideoCrafter/configs/inference_t2v_512_v2.0.yaml @@ -0,0 +1,77 @@ +model: + target: lvdm.models.ddpm3d.LatentDiffusion + params: + linear_start: 0.00085 + linear_end: 0.012 + num_timesteps_cond: 1 + timesteps: 1000 + first_stage_key: video + cond_stage_key: caption + cond_stage_trainable: false + conditioning_key: crossattn + image_size: + - 40 + - 64 + channels: 4 + scale_by_std: false + scale_factor: 0.18215 + use_ema: false + uncond_type: empty_seq + use_scale: true + scale_b: 0.7 + unet_config: + target: lvdm.modules.networks.openaimodel3d.UNetModel + params: + in_channels: 4 + out_channels: 4 + model_channels: 320 + attention_resolutions: + - 4 + - 2 + - 1 + num_res_blocks: 2 + channel_mult: + - 1 + - 2 + - 4 + - 4 + num_head_channels: 64 + transformer_depth: 1 + context_dim: 1024 + use_linear: true + use_checkpoint: true + temporal_conv: true + temporal_attention: true + temporal_selfatt_only: true + use_relative_position: false + use_causal_attention: false + temporal_length: 16 + addition_attention: true + fps_cond: true + first_stage_config: + target: lvdm.models.autoencoder.AutoencoderKL + params: + embed_dim: 4 + monitor: val/rec_loss + ddconfig: + double_z: true + z_channels: 4 + resolution: 512 + in_channels: 3 + out_ch: 3 + ch: 128 + ch_mult: + - 1 + - 2 + - 4 + - 4 + num_res_blocks: 2 + attn_resolutions: [] + dropout: 0.0 + lossconfig: + target: torch.nn.Identity + cond_stage_config: + target: lvdm.modules.encoders.condition.FrozenOpenCLIPEmbedder + params: + freeze: true + layer: penultimate diff --git a/VADER-VideoCrafter/lvdm/basics.py b/VADER-VideoCrafter/lvdm/basics.py new file mode 100644 index 0000000000000000000000000000000000000000..65c771d13a7f4a932ac370f08797a8b6ba9e85ff --- /dev/null +++ b/VADER-VideoCrafter/lvdm/basics.py @@ -0,0 +1,100 @@ +# adopted from +# https://github.com/openai/improved-diffusion/blob/main/improved_diffusion/gaussian_diffusion.py +# and +# https://github.com/lucidrains/denoising-diffusion-pytorch/blob/7706bdfc6f527f58d33f84b7b522e61e6e3164b3/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py +# and +# https://github.com/openai/guided-diffusion/blob/0ba878e517b276c45d1195eb29f6f5f72659a05b/guided_diffusion/nn.py +# +# thanks! + +import torch.nn as nn +from utils.utils import instantiate_from_config + + +def disabled_train(self, mode=True): + """Overwrite model.train with this function to make sure train/eval mode + does not change anymore.""" + return self + +def zero_module(module): + """ + Zero out the parameters of a module and return it. + """ + for p in module.parameters(): + p.detach().zero_() + return module + +def scale_module(module, scale): + """ + Scale the parameters of a module and return it. + """ + for p in module.parameters(): + p.detach().mul_(scale) + return module + + +def conv_nd(dims, *args, **kwargs): + """ + Create a 1D, 2D, or 3D convolution module. + """ + if dims == 1: + return nn.Conv1d(*args, **kwargs) + elif dims == 2: + return nn.Conv2d(*args, **kwargs) + elif dims == 3: + return nn.Conv3d(*args, **kwargs) + raise ValueError(f"unsupported dimensions: {dims}") + + +def linear(*args, **kwargs): + """ + Create a linear module. + """ + return nn.Linear(*args, **kwargs) + + +def avg_pool_nd(dims, *args, **kwargs): + """ + Create a 1D, 2D, or 3D average pooling module. + """ + if dims == 1: + return nn.AvgPool1d(*args, **kwargs) + elif dims == 2: + return nn.AvgPool2d(*args, **kwargs) + elif dims == 3: + return nn.AvgPool3d(*args, **kwargs) + raise ValueError(f"unsupported dimensions: {dims}") + + +def nonlinearity(type='silu'): + if type == 'silu': + return nn.SiLU() + elif type == 'leaky_relu': + return nn.LeakyReLU() + + +class GroupNormSpecific(nn.GroupNorm): + def forward(self, x): + return super().forward(x.float()).type(x.dtype) + + +def normalization(channels, num_groups=32): + """ + Make a standard normalization layer. + :param channels: number of input channels. + :return: an nn.Module for normalization. + """ + return GroupNormSpecific(num_groups, channels) + + +class HybridConditioner(nn.Module): + + def __init__(self, c_concat_config, c_crossattn_config): + super().__init__() + self.concat_conditioner = instantiate_from_config(c_concat_config) + self.crossattn_conditioner = instantiate_from_config(c_crossattn_config) + + def forward(self, c_concat, c_crossattn): + c_concat = self.concat_conditioner(c_concat) + c_crossattn = self.crossattn_conditioner(c_crossattn) + return {'c_concat': [c_concat], 'c_crossattn': [c_crossattn]} \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/common.py b/VADER-VideoCrafter/lvdm/common.py new file mode 100644 index 0000000000000000000000000000000000000000..1ba6c49587808d260a7404653823508076837095 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/common.py @@ -0,0 +1,96 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import math +from inspect import isfunction +import torch +from torch import nn +import torch.distributed as dist + + +def gather_data(data, return_np=True): + ''' gather data from multiple processes to one list ''' + data_list = [torch.zeros_like(data) for _ in range(dist.get_world_size())] + dist.all_gather(data_list, data) # gather not supported with NCCL + if return_np: + data_list = [data.cpu().numpy() for data in data_list] + return data_list + +def autocast(f): + def do_autocast(*args, **kwargs): + with torch.cuda.amp.autocast(enabled=True, + dtype=torch.get_autocast_gpu_dtype(), + cache_enabled=torch.is_autocast_cache_enabled()): + return f(*args, **kwargs) + return do_autocast + + +def extract_into_tensor(a, t, x_shape): + b, *_ = t.shape + out = a.gather(-1, t) + return out.reshape(b, *((1,) * (len(x_shape) - 1))) + + +def noise_like(shape, device, repeat=False): + repeat_noise = lambda: torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1,) * (len(shape) - 1))) + noise = lambda: torch.randn(shape, device=device) + return repeat_noise() if repeat else noise() + + +def default(val, d): + if exists(val): + return val + return d() if isfunction(d) else d + +def exists(val): + return val is not None + +def identity(*args, **kwargs): + return nn.Identity() + +def uniq(arr): + return{el: True for el in arr}.keys() + +def mean_flat(tensor): + """ + Take the mean over all non-batch dimensions. + """ + return tensor.mean(dim=list(range(1, len(tensor.shape)))) + +def ismap(x): + if not isinstance(x, torch.Tensor): + return False + return (len(x.shape) == 4) and (x.shape[1] > 3) + +def isimage(x): + if not isinstance(x,torch.Tensor): + return False + return (len(x.shape) == 4) and (x.shape[1] == 3 or x.shape[1] == 1) + +def max_neg_value(t): + return -torch.finfo(t.dtype).max + +def shape_to_str(x): + shape_str = "x".join([str(x) for x in x.shape]) + return shape_str + +def init_(tensor): + dim = tensor.shape[-1] + std = 1 / math.sqrt(dim) + tensor.uniform_(-std, std) + return tensor + +ckpt = torch.utils.checkpoint.checkpoint +def checkpoint(func, inputs, params, flag): + """ + Evaluate a function without caching intermediate activations, allowing for + reduced memory at the expense of extra compute in the backward pass. + :param func: the function to evaluate. + :param inputs: the argument sequence to pass to `func`. + :param params: a sequence of parameters `func` depends on but does not + explicitly take as arguments. + :param flag: if False, disable gradient checkpointing. + """ + if flag: + return ckpt(func, *inputs, use_reentrant=False) + else: + return func(*inputs) + diff --git a/VADER-VideoCrafter/lvdm/distributions.py b/VADER-VideoCrafter/lvdm/distributions.py new file mode 100644 index 0000000000000000000000000000000000000000..abe3e5477f75b142959f97dd42379f60838dd7f0 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/distributions.py @@ -0,0 +1,96 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import torch +import numpy as np + + +class AbstractDistribution: + def sample(self): + raise NotImplementedError() + + def mode(self): + raise NotImplementedError() + + +class DiracDistribution(AbstractDistribution): + def __init__(self, value): + self.value = value + + def sample(self): + return self.value + + def mode(self): + return self.value + + +class DiagonalGaussianDistribution(object): + def __init__(self, parameters, deterministic=False): + self.parameters = parameters + self.mean, self.logvar = torch.chunk(parameters, 2, dim=1) + self.logvar = torch.clamp(self.logvar, -30.0, 20.0) + self.deterministic = deterministic + self.std = torch.exp(0.5 * self.logvar) + self.var = torch.exp(self.logvar) + if self.deterministic: + self.var = self.std = torch.zeros_like(self.mean).to(device=self.parameters.device) + + def sample(self, noise=None): + if noise is None: + noise = torch.randn(self.mean.shape) + + x = self.mean + self.std * noise.to(device=self.parameters.device) + return x + + def kl(self, other=None): + if self.deterministic: + return torch.Tensor([0.]) + else: + if other is None: + return 0.5 * torch.sum(torch.pow(self.mean, 2) + + self.var - 1.0 - self.logvar, + dim=[1, 2, 3]) + else: + return 0.5 * torch.sum( + torch.pow(self.mean - other.mean, 2) / other.var + + self.var / other.var - 1.0 - self.logvar + other.logvar, + dim=[1, 2, 3]) + + def nll(self, sample, dims=[1,2,3]): + if self.deterministic: + return torch.Tensor([0.]) + logtwopi = np.log(2.0 * np.pi) + return 0.5 * torch.sum( + logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var, + dim=dims) + + def mode(self): + return self.mean + + +def normal_kl(mean1, logvar1, mean2, logvar2): + """ + source: https://github.com/openai/guided-diffusion/blob/27c20a8fab9cb472df5d6bdd6c8d11c8f430b924/guided_diffusion/losses.py#L12 + Compute the KL divergence between two gaussians. + Shapes are automatically broadcasted, so batches can be compared to + scalars, among other use cases. + """ + tensor = None + for obj in (mean1, logvar1, mean2, logvar2): + if isinstance(obj, torch.Tensor): + tensor = obj + break + assert tensor is not None, "at least one argument must be a Tensor" + + # Force variances to be Tensors. Broadcasting helps convert scalars to + # Tensors, but it does not work for torch.exp(). + logvar1, logvar2 = [ + x if isinstance(x, torch.Tensor) else torch.tensor(x).to(tensor) + for x in (logvar1, logvar2) + ] + + return 0.5 * ( + -1.0 + + logvar2 + - logvar1 + + torch.exp(logvar1 - logvar2) + + ((mean1 - mean2) ** 2) * torch.exp(-logvar2) + ) diff --git a/VADER-VideoCrafter/lvdm/ema.py b/VADER-VideoCrafter/lvdm/ema.py new file mode 100644 index 0000000000000000000000000000000000000000..0691aa757fb81eeaebc66e600facfb31f9e37d92 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/ema.py @@ -0,0 +1,77 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import torch +from torch import nn + + +class LitEma(nn.Module): + def __init__(self, model, decay=0.9999, use_num_upates=True): + super().__init__() + if decay < 0.0 or decay > 1.0: + raise ValueError('Decay must be between 0 and 1') + + self.m_name2s_name = {} + self.register_buffer('decay', torch.tensor(decay, dtype=torch.float32)) + self.register_buffer('num_updates', torch.tensor(0,dtype=torch.int) if use_num_upates + else torch.tensor(-1,dtype=torch.int)) + + for name, p in model.named_parameters(): + if p.requires_grad: + #remove as '.'-character is not allowed in buffers + s_name = name.replace('.','') + self.m_name2s_name.update({name:s_name}) + self.register_buffer(s_name,p.clone().detach().data) + + self.collected_params = [] + + def forward(self,model): + decay = self.decay + + if self.num_updates >= 0: + self.num_updates += 1 + decay = min(self.decay,(1 + self.num_updates) / (10 + self.num_updates)) + + one_minus_decay = 1.0 - decay + + with torch.no_grad(): + m_param = dict(model.named_parameters()) + shadow_params = dict(self.named_buffers()) + + for key in m_param: + if m_param[key].requires_grad: + sname = self.m_name2s_name[key] + shadow_params[sname] = shadow_params[sname].type_as(m_param[key]) + shadow_params[sname].sub_(one_minus_decay * (shadow_params[sname] - m_param[key])) + else: + assert not key in self.m_name2s_name + + def copy_to(self, model): + m_param = dict(model.named_parameters()) + shadow_params = dict(self.named_buffers()) + for key in m_param: + if m_param[key].requires_grad: + m_param[key].data.copy_(shadow_params[self.m_name2s_name[key]].data) + else: + assert not key in self.m_name2s_name + + def store(self, parameters): + """ + Save the current parameters for restoring later. + Args: + parameters: Iterable of `torch.nn.Parameter`; the parameters to be + temporarily stored. + """ + self.collected_params = [param.clone() for param in parameters] + + def restore(self, parameters): + """ + Restore the parameters stored with the `store` method. + Useful to validate the model with EMA parameters without affecting the + original optimization process. Store the parameters before the + `copy_to` method. After validation (or model saving), use this to + restore the former parameters. + Args: + parameters: Iterable of `torch.nn.Parameter`; the parameters to be + updated with the stored parameters. + """ + for c_param, param in zip(self.collected_params, parameters): + param.data.copy_(c_param.data) diff --git a/VADER-VideoCrafter/lvdm/models/autoencoder.py b/VADER-VideoCrafter/lvdm/models/autoencoder.py new file mode 100644 index 0000000000000000000000000000000000000000..c487e69b46f774dc1560e8c493f0cd9fe794609e --- /dev/null +++ b/VADER-VideoCrafter/lvdm/models/autoencoder.py @@ -0,0 +1,220 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import os +from contextlib import contextmanager +import torch +import numpy as np +from einops import rearrange +import torch.nn.functional as F +import pytorch_lightning as pl +from lvdm.modules.networks.ae_modules import Encoder, Decoder +from lvdm.distributions import DiagonalGaussianDistribution +from utils.utils import instantiate_from_config + + +class AutoencoderKL(pl.LightningModule): + def __init__(self, + ddconfig, + lossconfig, + embed_dim, + ckpt_path=None, + ignore_keys=[], + image_key="image", + colorize_nlabels=None, + monitor=None, + test=False, + logdir=None, + input_dim=4, + test_args=None, + ): + super().__init__() + self.image_key = image_key + self.encoder = Encoder(**ddconfig) + self.decoder = Decoder(**ddconfig) + self.loss = instantiate_from_config(lossconfig) + assert ddconfig["double_z"] + self.quant_conv = torch.nn.Conv2d(2*ddconfig["z_channels"], 2*embed_dim, 1) + self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1) + self.embed_dim = embed_dim + self.input_dim = input_dim + self.test = test + self.test_args = test_args + self.logdir = logdir + if colorize_nlabels is not None: + assert type(colorize_nlabels)==int + self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1)) + if monitor is not None: + self.monitor = monitor + if ckpt_path is not None: + self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys) + if self.test: + self.init_test() + + def init_test(self,): + self.test = True + save_dir = os.path.join(self.logdir, "test") + if 'ckpt' in self.test_args: + ckpt_name = os.path.basename(self.test_args.ckpt).split('.ckpt')[0] + f'_epoch{self._cur_epoch}' + self.root = os.path.join(save_dir, ckpt_name) + else: + self.root = save_dir + if 'test_subdir' in self.test_args: + self.root = os.path.join(save_dir, self.test_args.test_subdir) + + self.root_zs = os.path.join(self.root, "zs") + self.root_dec = os.path.join(self.root, "reconstructions") + self.root_inputs = os.path.join(self.root, "inputs") + os.makedirs(self.root, exist_ok=True) + + if self.test_args.save_z: + os.makedirs(self.root_zs, exist_ok=True) + if self.test_args.save_reconstruction: + os.makedirs(self.root_dec, exist_ok=True) + if self.test_args.save_input: + os.makedirs(self.root_inputs, exist_ok=True) + assert(self.test_args is not None) + self.test_maximum = getattr(self.test_args, 'test_maximum', None) + self.count = 0 + self.eval_metrics = {} + self.decodes = [] + self.save_decode_samples = 2048 + + def init_from_ckpt(self, path, ignore_keys=list()): + sd = torch.load(path, map_location="cpu") + try: + self._cur_epoch = sd['epoch'] + sd = sd["state_dict"] + except: + self._cur_epoch = 'null' + keys = list(sd.keys()) + for k in keys: + for ik in ignore_keys: + if k.startswith(ik): + print("Deleting key {} from state_dict.".format(k)) + del sd[k] + self.load_state_dict(sd, strict=False) + # self.load_state_dict(sd, strict=True) + print(f"Restored from {path}") + + def encode(self, x, **kwargs): + + h = self.encoder(x) + moments = self.quant_conv(h) + posterior = DiagonalGaussianDistribution(moments) + return posterior + + def decode(self, z, **kwargs): + z = self.post_quant_conv(z) + dec = self.decoder(z) + return dec + + def forward(self, input, sample_posterior=True): + posterior = self.encode(input) + if sample_posterior: + z = posterior.sample() + else: + z = posterior.mode() + dec = self.decode(z) + return dec, posterior + + def get_input(self, batch, k): + x = batch[k] + if x.dim() == 5 and self.input_dim == 4: + b,c,t,h,w = x.shape + self.b = b + self.t = t + x = rearrange(x, 'b c t h w -> (b t) c h w') + + return x + + def training_step(self, batch, batch_idx, optimizer_idx): + inputs = self.get_input(batch, self.image_key) + reconstructions, posterior = self(inputs) + + if optimizer_idx == 0: + # train encoder+decoder+logvar + aeloss, log_dict_ae = self.loss(inputs, reconstructions, posterior, optimizer_idx, self.global_step, + last_layer=self.get_last_layer(), split="train") + self.log("aeloss", aeloss, prog_bar=True, logger=True, on_step=True, on_epoch=True) + self.log_dict(log_dict_ae, prog_bar=False, logger=True, on_step=True, on_epoch=False) + return aeloss + + if optimizer_idx == 1: + # train the discriminator + discloss, log_dict_disc = self.loss(inputs, reconstructions, posterior, optimizer_idx, self.global_step, + last_layer=self.get_last_layer(), split="train") + + self.log("discloss", discloss, prog_bar=True, logger=True, on_step=True, on_epoch=True) + self.log_dict(log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=False) + return discloss + + def validation_step(self, batch, batch_idx): + inputs = self.get_input(batch, self.image_key) + reconstructions, posterior = self(inputs) + aeloss, log_dict_ae = self.loss(inputs, reconstructions, posterior, 0, self.global_step, + last_layer=self.get_last_layer(), split="val") + + discloss, log_dict_disc = self.loss(inputs, reconstructions, posterior, 1, self.global_step, + last_layer=self.get_last_layer(), split="val") + + self.log("val/rec_loss", log_dict_ae["val/rec_loss"]) + self.log_dict(log_dict_ae) + self.log_dict(log_dict_disc) + return self.log_dict + + def configure_optimizers(self): + lr = self.learning_rate + opt_ae = torch.optim.Adam(list(self.encoder.parameters())+ + list(self.decoder.parameters())+ + list(self.quant_conv.parameters())+ + list(self.post_quant_conv.parameters()), + lr=lr, betas=(0.5, 0.9)) + opt_disc = torch.optim.Adam(self.loss.discriminator.parameters(), + lr=lr, betas=(0.5, 0.9)) + return [opt_ae, opt_disc], [] + + def get_last_layer(self): + return self.decoder.conv_out.weight + + @torch.no_grad() + def log_images(self, batch, only_inputs=False, **kwargs): + log = dict() + x = self.get_input(batch, self.image_key) + x = x.to(self.device) + if not only_inputs: + xrec, posterior = self(x) + if x.shape[1] > 3: + # colorize with random projection + assert xrec.shape[1] > 3 + x = self.to_rgb(x) + xrec = self.to_rgb(xrec) + log["samples"] = self.decode(torch.randn_like(posterior.sample())) + log["reconstructions"] = xrec + log["inputs"] = x + return log + + def to_rgb(self, x): + assert self.image_key == "segmentation" + if not hasattr(self, "colorize"): + self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x)) + x = F.conv2d(x, weight=self.colorize) + x = 2.*(x-x.min())/(x.max()-x.min()) - 1. + return x + +class IdentityFirstStage(torch.nn.Module): + def __init__(self, *args, vq_interface=False, **kwargs): + self.vq_interface = vq_interface # TODO: Should be true by default but check to not break older stuff + super().__init__() + + def encode(self, x, *args, **kwargs): + return x + + def decode(self, x, *args, **kwargs): + return x + + def quantize(self, x, *args, **kwargs): + if self.vq_interface: + return x, None, [None, None, None] + return x + + def forward(self, x, *args, **kwargs): + return x diff --git a/VADER-VideoCrafter/lvdm/models/ddpm3d.py b/VADER-VideoCrafter/lvdm/models/ddpm3d.py new file mode 100644 index 0000000000000000000000000000000000000000..4e660774ca40f9887b6f421d829791b9c2792bf7 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/models/ddpm3d.py @@ -0,0 +1,765 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +""" +wild mixture of +https://github.com/openai/improved-diffusion/blob/e94489283bb876ac1477d5dd7709bbbd2d9902ce/improved_diffusion/gaussian_diffusion.py +https://github.com/lucidrains/denoising-diffusion-pytorch/blob/7706bdfc6f527f58d33f84b7b522e61e6e3164b3/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py +https://github.com/CompVis/taming-transformers +-- merci +""" + +from functools import partial +from contextlib import contextmanager +import numpy as np +from tqdm import tqdm +from einops import rearrange, repeat +import logging +mainlogger = logging.getLogger('mainlogger') +import torch +import torch.nn as nn +from torchvision.utils import make_grid +import pytorch_lightning as pl +from utils.utils import instantiate_from_config +from lvdm.ema import LitEma +from lvdm.distributions import DiagonalGaussianDistribution +from lvdm.models.utils_diffusion import make_beta_schedule +from lvdm.modules.encoders.ip_resampler import ImageProjModel, Resampler +from lvdm.basics import disabled_train +from lvdm.common import ( + extract_into_tensor, + noise_like, + exists, + default +) +# import ipdb +# st = ipdb.set_trace + +__conditioning_keys__ = {'concat': 'c_concat', + 'crossattn': 'c_crossattn', + 'adm': 'y'} + +class DDPM(pl.LightningModule): + # classic DDPM with Gaussian diffusion, in image space + def __init__(self, + unet_config, + timesteps=1000, + beta_schedule="linear", + loss_type="l2", + ckpt_path=None, + ignore_keys=[], + load_only_unet=False, + monitor=None, + use_ema=True, + first_stage_key="image", + image_size=256, + channels=3, + log_every_t=100, + clip_denoised=True, + linear_start=1e-4, + linear_end=2e-2, + cosine_s=8e-3, + given_betas=None, + original_elbo_weight=0., + v_posterior=0., # weight for choosing posterior variance as sigma = (1-v) * beta_tilde + v * beta + l_simple_weight=1., + conditioning_key=None, + parameterization="eps", # all assuming fixed variance schedules + scheduler_config=None, + use_positional_encodings=False, + learn_logvar=False, + logvar_init=0. + ): + super().__init__() + assert parameterization in ["eps", "x0"], 'currently only supporting "eps" and "x0"' + self.parameterization = parameterization + mainlogger.info(f"{self.__class__.__name__}: Running in {self.parameterization}-prediction mode") + self.cond_stage_model = None + self.clip_denoised = clip_denoised + self.log_every_t = log_every_t + self.first_stage_key = first_stage_key + self.channels = channels + self.temporal_length = unet_config.params.temporal_length + self.image_size = image_size + if isinstance(self.image_size, int): + self.image_size = [self.image_size, self.image_size] + self.use_positional_encodings = use_positional_encodings + self.model = DiffusionWrapper(unet_config, conditioning_key) + self.use_ema = use_ema + if self.use_ema: + self.model_ema = LitEma(self.model) + mainlogger.info(f"Keeping EMAs of {len(list(self.model_ema.buffers()))}.") + + self.use_scheduler = scheduler_config is not None + if self.use_scheduler: + self.scheduler_config = scheduler_config + + self.v_posterior = v_posterior + self.original_elbo_weight = original_elbo_weight + self.l_simple_weight = l_simple_weight + + if monitor is not None: + self.monitor = monitor + if ckpt_path is not None: + self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys, only_model=load_only_unet) + + self.register_schedule(given_betas=given_betas, beta_schedule=beta_schedule, timesteps=timesteps, + linear_start=linear_start, linear_end=linear_end, cosine_s=cosine_s) + + self.loss_type = loss_type + + self.learn_logvar = learn_logvar + self.logvar = torch.full(fill_value=logvar_init, size=(self.num_timesteps,)) + if self.learn_logvar: + self.logvar = nn.Parameter(self.logvar, requires_grad=True) + + + def register_schedule(self, given_betas=None, beta_schedule="linear", timesteps=1000, + linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3): + if exists(given_betas): + betas = given_betas + else: + betas = make_beta_schedule(beta_schedule, timesteps, linear_start=linear_start, linear_end=linear_end, + cosine_s=cosine_s) + alphas = 1. - betas + alphas_cumprod = np.cumprod(alphas, axis=0) + alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1]) + + timesteps, = betas.shape + self.num_timesteps = int(timesteps) + self.linear_start = linear_start + self.linear_end = linear_end + assert alphas_cumprod.shape[0] == self.num_timesteps, 'alphas have to be defined for each timestep' + + to_torch = partial(torch.tensor, dtype=torch.float32) + + self.register_buffer('betas', to_torch(betas)) + self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod)) + self.register_buffer('alphas_cumprod_prev', to_torch(alphas_cumprod_prev)) + + # calculations for diffusion q(x_t | x_{t-1}) and others + self.register_buffer('sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod))) + self.register_buffer('sqrt_one_minus_alphas_cumprod', to_torch(np.sqrt(1. - alphas_cumprod))) + self.register_buffer('log_one_minus_alphas_cumprod', to_torch(np.log(1. - alphas_cumprod))) + self.register_buffer('sqrt_recip_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod))) + self.register_buffer('sqrt_recipm1_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod - 1))) + + # calculations for posterior q(x_{t-1} | x_t, x_0) + posterior_variance = (1 - self.v_posterior) * betas * (1. - alphas_cumprod_prev) / ( + 1. - alphas_cumprod) + self.v_posterior * betas + # above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t) + self.register_buffer('posterior_variance', to_torch(posterior_variance)) + # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain + self.register_buffer('posterior_log_variance_clipped', to_torch(np.log(np.maximum(posterior_variance, 1e-20)))) + self.register_buffer('posterior_mean_coef1', to_torch( + betas * np.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod))) + self.register_buffer('posterior_mean_coef2', to_torch( + (1. - alphas_cumprod_prev) * np.sqrt(alphas) / (1. - alphas_cumprod))) + + if self.parameterization == "eps": + lvlb_weights = self.betas ** 2 / ( + 2 * self.posterior_variance * to_torch(alphas) * (1 - self.alphas_cumprod)) + elif self.parameterization == "x0": + lvlb_weights = 0.5 * np.sqrt(torch.Tensor(alphas_cumprod)) / (2. * 1 - torch.Tensor(alphas_cumprod)) + else: + raise NotImplementedError("mu not supported") + # TODO how to choose this term + lvlb_weights[0] = lvlb_weights[1] + self.register_buffer('lvlb_weights', lvlb_weights, persistent=False) + assert not torch.isnan(self.lvlb_weights).all() + + @contextmanager + def ema_scope(self, context=None): + if self.use_ema: + self.model_ema.store(self.model.parameters()) + self.model_ema.copy_to(self.model) + if context is not None: + mainlogger.info(f"{context}: Switched to EMA weights") + try: + yield None + finally: + if self.use_ema: + self.model_ema.restore(self.model.parameters()) + if context is not None: + mainlogger.info(f"{context}: Restored training weights") + + def init_from_ckpt(self, path, ignore_keys=list(), only_model=False): + sd = torch.load(path, map_location="cpu") + if "state_dict" in list(sd.keys()): + sd = sd["state_dict"] + keys = list(sd.keys()) + for k in keys: + for ik in ignore_keys: + if k.startswith(ik): + mainlogger.info("Deleting key {} from state_dict.".format(k)) + del sd[k] + missing, unexpected = self.load_state_dict(sd, strict=False) if not only_model else self.model.load_state_dict( + sd, strict=False) + mainlogger.info(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys") + if len(missing) > 0: + mainlogger.info(f"Missing Keys: {missing}") + if len(unexpected) > 0: + mainlogger.info(f"Unexpected Keys: {unexpected}") + + def q_mean_variance(self, x_start, t): + """ + Get the distribution q(x_t | x_0). + :param x_start: the [N x C x ...] tensor of noiseless inputs. + :param t: the number of diffusion steps (minus 1). Here, 0 means one step. + :return: A tuple (mean, variance, log_variance), all of x_start's shape. + """ + mean = (extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start) + variance = extract_into_tensor(1.0 - self.alphas_cumprod, t, x_start.shape) + log_variance = extract_into_tensor(self.log_one_minus_alphas_cumprod, t, x_start.shape) + return mean, variance, log_variance + + def predict_start_from_noise(self, x_t, t, noise): + return ( + extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - + extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) * noise + ) + + def q_posterior(self, x_start, x_t, t): + posterior_mean = ( + extract_into_tensor(self.posterior_mean_coef1, t, x_t.shape) * x_start + + extract_into_tensor(self.posterior_mean_coef2, t, x_t.shape) * x_t + ) + posterior_variance = extract_into_tensor(self.posterior_variance, t, x_t.shape) + posterior_log_variance_clipped = extract_into_tensor(self.posterior_log_variance_clipped, t, x_t.shape) + return posterior_mean, posterior_variance, posterior_log_variance_clipped + + def p_mean_variance(self, x, t, clip_denoised: bool): + model_out = self.model(x, t) + if self.parameterization == "eps": + x_recon = self.predict_start_from_noise(x, t=t, noise=model_out) + elif self.parameterization == "x0": + x_recon = model_out + if clip_denoised: + x_recon.clamp_(-1., 1.) + + model_mean, posterior_variance, posterior_log_variance = self.q_posterior(x_start=x_recon, x_t=x, t=t) + return model_mean, posterior_variance, posterior_log_variance + + @torch.no_grad() + def p_sample(self, x, t, clip_denoised=True, repeat_noise=False): + b, *_, device = *x.shape, x.device + model_mean, _, model_log_variance = self.p_mean_variance(x=x, t=t, clip_denoised=clip_denoised) + noise = noise_like(x.shape, device, repeat_noise) + # no noise when t == 0 + nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1))) + return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise + + @torch.no_grad() + def p_sample_loop(self, shape, return_intermediates=False): + device = self.betas.device + b = shape[0] + img = torch.randn(shape, device=device) + intermediates = [img] + for i in tqdm(reversed(range(0, self.num_timesteps)), desc='Sampling t', total=self.num_timesteps): + img = self.p_sample(img, torch.full((b,), i, device=device, dtype=torch.long), + clip_denoised=self.clip_denoised) + if i % self.log_every_t == 0 or i == self.num_timesteps - 1: + intermediates.append(img) + if return_intermediates: + return img, intermediates + return img + + @torch.no_grad() + def sample(self, batch_size=16, return_intermediates=False): + image_size = self.image_size + channels = self.channels + return self.p_sample_loop((batch_size, channels, image_size, image_size), + return_intermediates=return_intermediates) + + def q_sample(self, x_start, t, noise=None): + noise = default(noise, lambda: torch.randn_like(x_start)) + return (extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start * + extract_into_tensor(self.scale_arr, t, x_start.shape) + + extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise) + + def get_input(self, batch, k): + x = batch[k] + x = x.to(memory_format=torch.contiguous_format).float() + return x + + def _get_rows_from_list(self, samples): + n_imgs_per_row = len(samples) + denoise_grid = rearrange(samples, 'n b c h w -> b n c h w') + denoise_grid = rearrange(denoise_grid, 'b n c h w -> (b n) c h w') + denoise_grid = make_grid(denoise_grid, nrow=n_imgs_per_row) + return denoise_grid + + @torch.no_grad() + def log_images(self, batch, N=8, n_row=2, sample=True, return_keys=None, **kwargs): + log = dict() + x = self.get_input(batch, self.first_stage_key) + N = min(x.shape[0], N) + n_row = min(x.shape[0], n_row) + x = x.to(self.device)[:N] + log["inputs"] = x + + # get diffusion row + diffusion_row = list() + x_start = x[:n_row] + + for t in range(self.num_timesteps): + if t % self.log_every_t == 0 or t == self.num_timesteps - 1: + t = repeat(torch.tensor([t]), '1 -> b', b=n_row) + t = t.to(self.device).long() + noise = torch.randn_like(x_start) + x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise) + diffusion_row.append(x_noisy) + + log["diffusion_row"] = self._get_rows_from_list(diffusion_row) + + if sample: + # get denoise row + with self.ema_scope("Plotting"): + samples, denoise_row = self.sample(batch_size=N, return_intermediates=True) + + log["samples"] = samples + log["denoise_row"] = self._get_rows_from_list(denoise_row) + + if return_keys: + if np.intersect1d(list(log.keys()), return_keys).shape[0] == 0: + return log + else: + return {key: log[key] for key in return_keys} + return log + + +class LatentDiffusion(DDPM): + """main class""" + def __init__(self, + first_stage_config, + cond_stage_config, + num_timesteps_cond=None, + cond_stage_key="caption", + cond_stage_trainable=False, + cond_stage_forward=None, + conditioning_key=None, + uncond_prob=0.2, + uncond_type="empty_seq", + scale_factor=1.0, + scale_by_std=False, + encoder_type="2d", + only_model=False, + use_scale=False, + scale_a=1, + scale_b=0.3, + mid_step=400, + fix_scale_bug=False, + *args, **kwargs): + self.num_timesteps_cond = default(num_timesteps_cond, 1) + self.scale_by_std = scale_by_std + assert self.num_timesteps_cond <= kwargs['timesteps'] + # for backwards compatibility after implementation of DiffusionWrapper + ckpt_path = kwargs.pop("ckpt_path", None) + ignore_keys = kwargs.pop("ignore_keys", []) + conditioning_key = default(conditioning_key, 'crossattn') + super().__init__(conditioning_key=conditioning_key, *args, **kwargs) + + self.cond_stage_trainable = cond_stage_trainable + self.cond_stage_key = cond_stage_key + + # scale factor + self.use_scale=use_scale + if self.use_scale: + self.scale_a=scale_a + self.scale_b=scale_b + if fix_scale_bug: + scale_step=self.num_timesteps-mid_step + else: #bug + scale_step = self.num_timesteps + + scale_arr1 = np.linspace(scale_a, scale_b, mid_step) + scale_arr2 = np.full(scale_step, scale_b) + scale_arr = np.concatenate((scale_arr1, scale_arr2)) + scale_arr_prev = np.append(scale_a, scale_arr[:-1]) + to_torch = partial(torch.tensor, dtype=torch.float32) + self.register_buffer('scale_arr', to_torch(scale_arr)) + + try: + self.num_downs = len(first_stage_config.params.ddconfig.ch_mult) - 1 + except: + self.num_downs = 0 + if not scale_by_std: + self.scale_factor = scale_factor + else: + self.register_buffer('scale_factor', torch.tensor(scale_factor)) + self.instantiate_first_stage(first_stage_config) + self.instantiate_cond_stage(cond_stage_config) + self.first_stage_config = first_stage_config + self.cond_stage_config = cond_stage_config + self.clip_denoised = False + + self.cond_stage_forward = cond_stage_forward + self.encoder_type = encoder_type + assert(encoder_type in ["2d", "3d"]) + self.uncond_prob = uncond_prob + self.classifier_free_guidance = True if uncond_prob > 0 else False + assert(uncond_type in ["zero_embed", "empty_seq"]) + self.uncond_type = uncond_type + + + self.restarted_from_ckpt = False + if ckpt_path is not None: + self.init_from_ckpt(ckpt_path, ignore_keys, only_model=only_model) + self.restarted_from_ckpt = True + + + def make_cond_schedule(self, ): + self.cond_ids = torch.full(size=(self.num_timesteps,), fill_value=self.num_timesteps - 1, dtype=torch.long) + ids = torch.round(torch.linspace(0, self.num_timesteps - 1, self.num_timesteps_cond)).long() + self.cond_ids[:self.num_timesteps_cond] = ids + + def q_sample(self, x_start, t, noise=None): + noise = default(noise, lambda: torch.randn_like(x_start)) + if self.use_scale: + return (extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start * + extract_into_tensor(self.scale_arr, t, x_start.shape) + + extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise) + else: + return (extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start + + extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise) + + + def _freeze_model(self): + for name, para in self.model.diffusion_model.named_parameters(): + para.requires_grad = False + + def instantiate_first_stage(self, config): + model = instantiate_from_config(config) + self.first_stage_model = model.eval() + self.first_stage_model.train = disabled_train + for param in self.first_stage_model.parameters(): + param.requires_grad = False + + def instantiate_cond_stage(self, config): + if not self.cond_stage_trainable: + model = instantiate_from_config(config) + self.cond_stage_model = model.eval() + self.cond_stage_model.train = disabled_train + for param in self.cond_stage_model.parameters(): + param.requires_grad = False + else: + model = instantiate_from_config(config) + self.cond_stage_model = model + + def get_learned_conditioning(self, c): + if self.cond_stage_forward is None: + if hasattr(self.cond_stage_model, 'encode') and callable(self.cond_stage_model.encode): + c = self.cond_stage_model.encode(c) + if isinstance(c, DiagonalGaussianDistribution): + c = c.mode() + else: + c = self.cond_stage_model(c) + else: + assert hasattr(self.cond_stage_model, self.cond_stage_forward) + c = getattr(self.cond_stage_model, self.cond_stage_forward)(c) + return c + + def get_first_stage_encoding(self, encoder_posterior, noise=None): + if isinstance(encoder_posterior, DiagonalGaussianDistribution): + z = encoder_posterior.sample(noise=noise) + elif isinstance(encoder_posterior, torch.Tensor): + z = encoder_posterior + else: + raise NotImplementedError(f"encoder_posterior of type '{type(encoder_posterior)}' not yet implemented") + return self.scale_factor * z + + @torch.no_grad() + def encode_first_stage(self, x): + if self.encoder_type == "2d" and x.dim() == 5: + b, _, t, _, _ = x.shape + x = rearrange(x, 'b c t h w -> (b t) c h w') + reshape_back = True + else: + reshape_back = False + + encoder_posterior = self.first_stage_model.encode(x) + results = self.get_first_stage_encoding(encoder_posterior).detach() + + if reshape_back: + results = rearrange(results, '(b t) c h w -> b c t h w', b=b,t=t) + + return results + + @torch.no_grad() + def encode_first_stage_2DAE(self, x): + + b, _, t, _, _ = x.shape + results = torch.cat([self.get_first_stage_encoding(self.first_stage_model.encode(x[:,:,i])).detach().unsqueeze(2) for i in range(t)], dim=2) + + return results + + def decode_core(self, z, **kwargs): + if self.encoder_type == "2d" and z.dim() == 5: + b, _, t, _, _ = z.shape + z = rearrange(z, 'b c t h w -> (b t) c h w') + reshape_back = True + else: + reshape_back = False + + z = 1. / self.scale_factor * z + + results = self.first_stage_model.decode(z, **kwargs) + + if reshape_back: + results = rearrange(results, '(b t) c h w -> b c t h w', b=b,t=t) + return results + + @torch.no_grad() + def decode_first_stage(self, z, **kwargs): + return self.decode_core(z, **kwargs) + + def apply_model(self, x_noisy, t, cond, **kwargs): + if isinstance(cond, dict): + # hybrid case, cond is exptected to be a dict + pass + else: + if not isinstance(cond, list): + cond = [cond] + key = 'c_concat' if self.model.conditioning_key == 'concat' else 'c_crossattn' + cond = {key: cond} + + x_recon = self.model(x_noisy, t, **cond, **kwargs) + + if isinstance(x_recon, tuple): + return x_recon[0] + else: + return x_recon + + def _get_denoise_row_from_list(self, samples, desc=''): + denoise_row = [] + for zd in tqdm(samples, desc=desc): + denoise_row.append(self.decode_first_stage(zd.to(self.device))) + n_log_timesteps = len(denoise_row) + + denoise_row = torch.stack(denoise_row) # n_log_timesteps, b, C, H, W + + if denoise_row.dim() == 5: + # img, num_imgs= n_log_timesteps * bs, grid_size=[bs,n_log_timesteps] + denoise_grid = rearrange(denoise_row, 'n b c h w -> b n c h w') + denoise_grid = rearrange(denoise_grid, 'b n c h w -> (b n) c h w') + denoise_grid = make_grid(denoise_grid, nrow=n_log_timesteps) + elif denoise_row.dim() == 6: + # video, grid_size=[n_log_timesteps*bs, t] + video_length = denoise_row.shape[3] + denoise_grid = rearrange(denoise_row, 'n b c t h w -> b n c t h w') + denoise_grid = rearrange(denoise_grid, 'b n c t h w -> (b n) c t h w') + denoise_grid = rearrange(denoise_grid, 'n c t h w -> (n t) c h w') + denoise_grid = make_grid(denoise_grid, nrow=video_length) + else: + raise ValueError + + return denoise_grid + + + # @torch.no_grad() + def decode_first_stage_2DAE(self, z, **kwargs): + + b, _, t, _, _ = z.shape + z = 1. / self.scale_factor * z + results = torch.cat([self.first_stage_model.decode(z[:,:,i], **kwargs).unsqueeze(2) for i in range(t)], dim=2) + + return results + + + def p_mean_variance(self, x, c, t, clip_denoised: bool, return_x0=False, score_corrector=None, corrector_kwargs=None, **kwargs): + t_in = t + model_out = self.apply_model(x, t_in, c, **kwargs) + + if score_corrector is not None: + assert self.parameterization == "eps" + model_out = score_corrector.modify_score(self, model_out, x, t, c, **corrector_kwargs) + + if self.parameterization == "eps": + x_recon = self.predict_start_from_noise(x, t=t, noise=model_out) + elif self.parameterization == "x0": + x_recon = model_out + else: + raise NotImplementedError() + + if clip_denoised: + x_recon.clamp_(-1., 1.) + + model_mean, posterior_variance, posterior_log_variance = self.q_posterior(x_start=x_recon, x_t=x, t=t) + + if return_x0: + return model_mean, posterior_variance, posterior_log_variance, x_recon + else: + return model_mean, posterior_variance, posterior_log_variance + + @torch.no_grad() + def p_sample(self, x, c, t, clip_denoised=False, repeat_noise=False, return_x0=False, \ + temperature=1., noise_dropout=0., score_corrector=None, corrector_kwargs=None, **kwargs): + b, *_, device = *x.shape, x.device + outputs = self.p_mean_variance(x=x, c=c, t=t, clip_denoised=clip_denoised, return_x0=return_x0, \ + score_corrector=score_corrector, corrector_kwargs=corrector_kwargs, **kwargs) + if return_x0: + model_mean, _, model_log_variance, x0 = outputs + else: + model_mean, _, model_log_variance = outputs + + noise = noise_like(x.shape, device, repeat_noise) * temperature + if noise_dropout > 0.: + noise = torch.nn.functional.dropout(noise, p=noise_dropout) + # no noise when t == 0 + nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1))) + + if return_x0: + return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise, x0 + else: + return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise + + @torch.no_grad() + def p_sample_loop(self, cond, shape, return_intermediates=False, x_T=None, verbose=True, callback=None, \ + timesteps=None, mask=None, x0=None, img_callback=None, start_T=None, log_every_t=None, **kwargs): + + if not log_every_t: + log_every_t = self.log_every_t + device = self.betas.device + b = shape[0] + # sample an initial noise + if x_T is None: + img = torch.randn(shape, device=device) + else: + img = x_T + + intermediates = [img] + if timesteps is None: + timesteps = self.num_timesteps + if start_T is not None: + timesteps = min(timesteps, start_T) + + iterator = tqdm(reversed(range(0, timesteps)), desc='Sampling t', total=timesteps) if verbose else reversed(range(0, timesteps)) + + if mask is not None: + assert x0 is not None + assert x0.shape[2:3] == mask.shape[2:3] # spatial size has to match + + for i in iterator: + ts = torch.full((b,), i, device=device, dtype=torch.long) + if self.shorten_cond_schedule: + assert self.model.conditioning_key != 'hybrid' + tc = self.cond_ids[ts].to(cond.device) + cond = self.q_sample(x_start=cond, t=tc, noise=torch.randn_like(cond)) + + img = self.p_sample(img, cond, ts, clip_denoised=self.clip_denoised, **kwargs) + if mask is not None: + img_orig = self.q_sample(x0, ts) + img = img_orig * mask + (1. - mask) * img + + if i % log_every_t == 0 or i == timesteps - 1: + intermediates.append(img) + if callback: callback(i) + if img_callback: img_callback(img, i) + + if return_intermediates: + return img, intermediates + return img + + +class LatentVisualDiffusion(LatentDiffusion): + def __init__(self, cond_img_config, finegrained=False, random_cond=False, *args, **kwargs): + super().__init__(*args, **kwargs) + self.random_cond = random_cond + self.instantiate_img_embedder(cond_img_config, freeze=True) + num_tokens = 16 if finegrained else 4 + self.image_proj_model = self.init_projector(use_finegrained=finegrained, num_tokens=num_tokens, input_dim=1024,\ + cross_attention_dim=1024, dim=1280) + + def instantiate_img_embedder(self, config, freeze=True): + embedder = instantiate_from_config(config) + if freeze: + self.embedder = embedder.eval() + self.embedder.train = disabled_train + for param in self.embedder.parameters(): + param.requires_grad = False + + def init_projector(self, use_finegrained, num_tokens, input_dim, cross_attention_dim, dim): + if not use_finegrained: + image_proj_model = ImageProjModel(clip_extra_context_tokens=num_tokens, cross_attention_dim=cross_attention_dim, + clip_embeddings_dim=input_dim + ) + else: + image_proj_model = Resampler(dim=input_dim, depth=4, dim_head=64, heads=12, num_queries=num_tokens, + embedding_dim=dim, output_dim=cross_attention_dim, ff_mult=4 + ) + return image_proj_model + + ## Never delete this func: it is used in log_images() and inference stage + def get_image_embeds(self, batch_imgs): + ## img: b c h w + img_token = self.embedder(batch_imgs) + img_emb = self.image_proj_model(img_token) + return img_emb + + +class DiffusionWrapper(pl.LightningModule): + def __init__(self, diff_model_config, conditioning_key): + super().__init__() + self.diffusion_model = instantiate_from_config(diff_model_config) + self.conditioning_key = conditioning_key + + def forward(self, x, t, c_concat: list = None, c_crossattn: list = None, + c_adm=None, s=None, mask=None, **kwargs): + # temporal_context = fps is foNone + if self.conditioning_key is None: + out = self.diffusion_model(x, t) + elif self.conditioning_key == 'concat': + xc = torch.cat([x] + c_concat, dim=1) + out = self.diffusion_model(xc, t, **kwargs) + elif self.conditioning_key == 'crossattn': + cc = torch.cat(c_crossattn, 1) + out = self.diffusion_model(x, t, context=cc, **kwargs) + elif self.conditioning_key == 'hybrid': + ## it is just right [b,c,t,h,w]: concatenate in channel dim + xc = torch.cat([x] + c_concat, dim=1) + cc = torch.cat(c_crossattn, 1) + out = self.diffusion_model(xc, t, context=cc) + elif self.conditioning_key == 'resblockcond': + cc = c_crossattn[0] + out = self.diffusion_model(x, t, context=cc) + elif self.conditioning_key == 'adm': + cc = c_crossattn[0] + out = self.diffusion_model(x, t, y=cc) + elif self.conditioning_key == 'hybrid-adm': + assert c_adm is not None + xc = torch.cat([x] + c_concat, dim=1) + cc = torch.cat(c_crossattn, 1) + out = self.diffusion_model(xc, t, context=cc, y=c_adm) + elif self.conditioning_key == 'hybrid-time': + assert s is not None + xc = torch.cat([x] + c_concat, dim=1) + cc = torch.cat(c_crossattn, 1) + out = self.diffusion_model(xc, t, context=cc, s=s) + elif self.conditioning_key == 'concat-time-mask': + # assert s is not None + # mainlogger.info('x & mask:',x.shape,c_concat[0].shape) + xc = torch.cat([x] + c_concat, dim=1) + out = self.diffusion_model(xc, t, context=None, s=s, mask=mask) + elif self.conditioning_key == 'concat-adm-mask': + # assert s is not None + # mainlogger.info('x & mask:',x.shape,c_concat[0].shape) + if c_concat is not None: + xc = torch.cat([x] + c_concat, dim=1) + else: + xc = x + out = self.diffusion_model(xc, t, context=None, y=s, mask=mask) + elif self.conditioning_key == 'hybrid-adm-mask': + cc = torch.cat(c_crossattn, 1) + if c_concat is not None: + xc = torch.cat([x] + c_concat, dim=1) + else: + xc = x + out = self.diffusion_model(xc, t, context=cc, y=s, mask=mask) + elif self.conditioning_key == 'hybrid-time-adm': # adm means y, e.g., class index + # assert s is not None + assert c_adm is not None + xc = torch.cat([x] + c_concat, dim=1) + cc = torch.cat(c_crossattn, 1) + out = self.diffusion_model(xc, t, context=cc, s=s, y=c_adm) + else: + raise NotImplementedError() + + return out \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/models/samplers/ddim.py b/VADER-VideoCrafter/lvdm/models/samplers/ddim.py new file mode 100644 index 0000000000000000000000000000000000000000..bb5daf326c2a3ef3acf462c26971f32ecca19de1 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/models/samplers/ddim.py @@ -0,0 +1,368 @@ +# Adapted from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import numpy as np +from tqdm import tqdm +import torch +from lvdm.models.utils_diffusion import make_ddim_sampling_parameters, make_ddim_timesteps +from lvdm.common import noise_like +import random +# import ipdb +# st = ipdb.set_trace + + +class DDIMSampler(object): + def __init__(self, model, schedule="linear", **kwargs): + super().__init__() + self.model = model + self.ddpm_num_timesteps = model.num_timesteps + self.schedule = schedule + self.counter = 0 + self.backprop_mode = 'last' # default + self.training_mode = False # default + + def register_buffer(self, name, attr): + if type(attr) == torch.Tensor: + if attr.device != torch.device("cuda"): + attr = attr.to(torch.device("cuda")) + setattr(self, name, attr) + + def make_schedule(self, ddim_num_steps, ddim_discretize="uniform", ddim_eta=0., verbose=True): + self.ddim_timesteps = make_ddim_timesteps(ddim_discr_method=ddim_discretize, num_ddim_timesteps=ddim_num_steps, + num_ddpm_timesteps=self.ddpm_num_timesteps,verbose=verbose) + alphas_cumprod = self.model.alphas_cumprod + assert alphas_cumprod.shape[0] == self.ddpm_num_timesteps, 'alphas have to be defined for each timestep' + to_torch = lambda x: x.clone().detach().to(torch.float32).to(self.model.device) + + self.register_buffer('betas', to_torch(self.model.betas)) + self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod)) + self.register_buffer('alphas_cumprod_prev', to_torch(self.model.alphas_cumprod_prev)) + self.use_scale = self.model.use_scale + # print('DDIM scale', self.use_scale) + + if self.use_scale: + self.register_buffer('scale_arr', to_torch(self.model.scale_arr)) + ddim_scale_arr = self.scale_arr.cpu()[self.ddim_timesteps] + self.register_buffer('ddim_scale_arr', ddim_scale_arr) + ddim_scale_arr = np.asarray([self.scale_arr.cpu()[0]] + self.scale_arr.cpu()[self.ddim_timesteps[:-1]].tolist()) + self.register_buffer('ddim_scale_arr_prev', ddim_scale_arr) + + # calculations for diffusion q(x_t | x_{t-1}) and others + self.register_buffer('sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod.cpu()))) + self.register_buffer('sqrt_one_minus_alphas_cumprod', to_torch(np.sqrt(1. - alphas_cumprod.cpu()))) + self.register_buffer('log_one_minus_alphas_cumprod', to_torch(np.log(1. - alphas_cumprod.cpu()))) + self.register_buffer('sqrt_recip_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod.cpu()))) + self.register_buffer('sqrt_recipm1_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod.cpu() - 1))) + + # ddim sampling parameters + ddim_sigmas, ddim_alphas, ddim_alphas_prev = make_ddim_sampling_parameters(alphacums=alphas_cumprod.cpu(), + ddim_timesteps=self.ddim_timesteps, + eta=ddim_eta,verbose=verbose) + self.register_buffer('ddim_sigmas', ddim_sigmas) + self.register_buffer('ddim_alphas', ddim_alphas) + self.register_buffer('ddim_alphas_prev', ddim_alphas_prev) + self.register_buffer('ddim_sqrt_one_minus_alphas', np.sqrt(1. - ddim_alphas)) + sigmas_for_original_sampling_steps = ddim_eta * torch.sqrt( + (1 - self.alphas_cumprod_prev) / (1 - self.alphas_cumprod) * ( + 1 - self.alphas_cumprod / self.alphas_cumprod_prev)) + self.register_buffer('ddim_sigmas_for_original_num_steps', sigmas_for_original_sampling_steps) + + # @torch.no_grad() + def sample(self, + S, + batch_size, + shape, + conditioning=None, + callback=None, + normals_sequence=None, + img_callback=None, + quantize_x0=False, + eta=0., + mask=None, + x0=None, + temperature=1., + noise_dropout=0., + score_corrector=None, + corrector_kwargs=None, + verbose=True, + schedule_verbose=False, + x_T=None, + log_every_t=100, + unconditional_guidance_scale=1., + unconditional_conditioning=None, + # this has to come in the same format as the conditioning, # e.g. as encoded tokens, ... + **kwargs + ): + + # check condition bs + if conditioning is not None: + if isinstance(conditioning, dict): + try: + cbs = conditioning[list(conditioning.keys())[0]].shape[0] + except: + cbs = conditioning[list(conditioning.keys())[0]][0].shape[0] + + if cbs != batch_size: + print(f"Warning: Got {cbs} conditionings but batch-size is {batch_size}") + else: + if conditioning.shape[0] != batch_size: + print(f"Warning: Got {conditioning.shape[0]} conditionings but batch-size is {batch_size}") + + self.make_schedule(ddim_num_steps=S, ddim_eta=eta, verbose=schedule_verbose) + self.ddim_num_steps = S # for ddim_sampling + + # make shape + if len(shape) == 3: + C, H, W = shape + size = (batch_size, C, H, W) + elif len(shape) == 4: + C, T, H, W = shape + size = (batch_size, C, T, H, W) + # print(f'Data shape for DDIM sampling is {size}, eta {eta}') + + samples, intermediates = self.ddim_sampling(conditioning, size, # samples: batch, c, t, h, w + callback=callback, + img_callback=img_callback, + quantize_denoised=quantize_x0, + mask=mask, x0=x0, + ddim_use_original_steps=False, + noise_dropout=noise_dropout, + temperature=temperature, + score_corrector=score_corrector, + corrector_kwargs=corrector_kwargs, + x_T=x_T, + log_every_t=log_every_t, + unconditional_guidance_scale=unconditional_guidance_scale, + unconditional_conditioning=unconditional_conditioning, + verbose=verbose, + **kwargs) + return samples, intermediates + + # @torch.no_grad() + def ddim_sampling(self, cond, shape, + x_T=None, ddim_use_original_steps=False, + callback=None, timesteps=None, quantize_denoised=False, + mask=None, x0=None, img_callback=None, log_every_t=100, + temperature=1., noise_dropout=0., score_corrector=None, corrector_kwargs=None, + unconditional_guidance_scale=1., unconditional_conditioning=None, verbose=True, + cond_tau=1., target_size=None, start_timesteps=None, + **kwargs): + device = self.model.betas.device + # print('ddim device', device) + b = shape[0] + if x_T is None: + img = torch.randn(shape, device=device) + else: + img = x_T + + if timesteps is None: + timesteps = self.ddpm_num_timesteps if ddim_use_original_steps else self.ddim_timesteps + elif timesteps is not None and not ddim_use_original_steps: + subset_end = int(min(timesteps / self.ddim_timesteps.shape[0], 1) * self.ddim_timesteps.shape[0]) - 1 + timesteps = self.ddim_timesteps[:subset_end] + + intermediates = {'x_inter': [img], 'pred_x0': [img]} + time_range = reversed(range(0,timesteps)) if ddim_use_original_steps else np.flip(timesteps) + total_steps = timesteps if ddim_use_original_steps else timesteps.shape[0] + if verbose: + iterator = tqdm(time_range, desc='DDIM Sampler', total=total_steps) + else: + iterator = time_range + + init_x0 = False + clean_cond = kwargs.pop("clean_cond", False) + + if self.training_mode == True: + if self.backprop_mode == 'last': + backprop_cutoff_idx = self.ddim_num_steps - 1 + elif self.backprop_mode == 'rand': + backprop_cutoff_idx = random.randint(0, self.ddim_num_steps - 1) + elif self.backprop_mode == 'specific': + backprop_cutoff_idx = 15 + + for i, step in enumerate(iterator): + index = total_steps - i - 1 + ts = torch.full((b,), step, device=device, dtype=torch.long) + + if self.training_mode == True: + if i >= backprop_cutoff_idx: + for name, param in self.model.named_parameters(): + if "lora" in name: + param.requires_grad = True + else: + for name, param in self.model.named_parameters(): + param.requires_grad = False + + + if start_timesteps is not None: + assert x0 is not None + if step > start_timesteps*time_range[0]: + continue + elif not init_x0: + img = self.model.q_sample(x0, ts) + init_x0 = True + + # use mask to blend noised original latent (img_orig) & new sampled latent (img) + if mask is not None: + assert x0 is not None + if clean_cond: + img_orig = x0 + else: + img_orig = self.model.q_sample(x0, ts) # TODO: deterministic forward pass? + img = img_orig * mask + (1. - mask) * img # keep original & modify use img + + index_clip = int((1 - cond_tau) * total_steps) + if index <= index_clip and target_size is not None: + target_size_ = [target_size[0], target_size[1]//8, target_size[2]//8] + img = torch.nn.functional.interpolate( + img, + size=target_size_, + mode="nearest", + ) + + forward_context = torch.autograd.graph.save_on_cpu + with forward_context(): + outs = self.p_sample_ddim(img, cond, ts, index=index, use_original_steps=ddim_use_original_steps, + quantize_denoised=quantize_denoised, temperature=temperature, + noise_dropout=noise_dropout, score_corrector=score_corrector, + corrector_kwargs=corrector_kwargs, + unconditional_guidance_scale=unconditional_guidance_scale, + unconditional_conditioning=unconditional_conditioning, + x0=x0, + **kwargs) + + img, pred_x0 = outs + + if callback: callback(i) + if img_callback: img_callback(pred_x0, i) + + if index % log_every_t == 0 or index == total_steps - 1: + intermediates['x_inter'].append(img) + intermediates['pred_x0'].append(pred_x0) + + return img, intermediates + + # @torch.no_grad() + def p_sample_ddim(self, x, c, t, index, repeat_noise=False, use_original_steps=False, quantize_denoised=False, + temperature=1., noise_dropout=0., score_corrector=None, corrector_kwargs=None, + unconditional_guidance_scale=1., unconditional_conditioning=None, + uc_type=None, conditional_guidance_scale_temporal=None, **kwargs): + b, *_, device = *x.shape, x.device + + if x.dim() == 5: + is_video = True + else: + is_video = False + if unconditional_conditioning is None or unconditional_guidance_scale == 1.: + e_t = self.model.apply_model(x, t, c, **kwargs) # unet denoiser + else: + # with unconditional condition + if isinstance(c, torch.Tensor): + e_t = self.model.apply_model(x, t, c, **kwargs) # unet denoiser + e_t_uncond = self.model.apply_model(x, t, unconditional_conditioning, **kwargs) + elif isinstance(c, dict): + e_t = self.model.apply_model(x, t, c, **kwargs) # unet denoiser + e_t_uncond = self.model.apply_model(x, t, unconditional_conditioning, **kwargs) # unet denoiser + else: + raise NotImplementedError + + if uc_type is None: + e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond) + else: + if uc_type == 'cfg_original': + e_t = e_t + unconditional_guidance_scale * (e_t - e_t_uncond) + elif uc_type == 'cfg_ours': + e_t = e_t + unconditional_guidance_scale * (e_t_uncond - e_t) + else: + raise NotImplementedError + + # temporal guidance + if conditional_guidance_scale_temporal is not None: + e_t_temporal = self.model.apply_model(x, t, c, **kwargs) + e_t_image = self.model.apply_model(x, t, c, no_temporal_attn=True, **kwargs) + e_t = e_t + conditional_guidance_scale_temporal * (e_t_temporal - e_t_image) + + if score_corrector is not None: + assert self.model.parameterization == "eps" + e_t = score_corrector.modify_score(self.model, e_t, x, t, c, **corrector_kwargs) + + alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas + alphas_prev = self.model.alphas_cumprod_prev if use_original_steps else self.ddim_alphas_prev + sqrt_one_minus_alphas = self.model.sqrt_one_minus_alphas_cumprod if use_original_steps else self.ddim_sqrt_one_minus_alphas + sigmas = self.model.ddim_sigmas_for_original_num_steps if use_original_steps else self.ddim_sigmas + # select parameters corresponding to the currently considered timestep + + if is_video: + size = (b, 1, 1, 1, 1) + else: + size = (b, 1, 1, 1) + a_t = torch.full(size, alphas[index], device=device) + a_prev = torch.full(size, alphas_prev[index], device=device) + sigma_t = torch.full(size, sigmas[index], device=device) + sqrt_one_minus_at = torch.full(size, sqrt_one_minus_alphas[index],device=device) + + # current prediction for x_0 + pred_x0 = (x - sqrt_one_minus_at * e_t) / a_t.sqrt() + if quantize_denoised: + pred_x0, _, *_ = self.model.first_stage_model.quantize(pred_x0) + # direction pointing to x_t + dir_xt = (1. - a_prev - sigma_t**2).sqrt() * e_t + + noise = sigma_t * noise_like(x.shape, device, repeat_noise) * temperature + if noise_dropout > 0.: + noise = torch.nn.functional.dropout(noise, p=noise_dropout) + alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas + if self.use_scale: + scale_arr = self.model.scale_arr if use_original_steps else self.ddim_scale_arr + scale_t = torch.full(size, scale_arr[index], device=device) + scale_arr_prev = self.model.scale_arr_prev if use_original_steps else self.ddim_scale_arr_prev + scale_t_prev = torch.full(size, scale_arr_prev[index], device=device) + pred_x0 /= scale_t + x_prev = a_prev.sqrt() * scale_t_prev * pred_x0 + dir_xt + noise + else: + x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise + + return x_prev, pred_x0 + + + @torch.no_grad() + def stochastic_encode(self, x0, t, use_original_steps=False, noise=None): + # fast, but does not allow for exact reconstruction + # t serves as an index to gather the correct alphas + if use_original_steps: + sqrt_alphas_cumprod = self.sqrt_alphas_cumprod + sqrt_one_minus_alphas_cumprod = self.sqrt_one_minus_alphas_cumprod + else: + sqrt_alphas_cumprod = torch.sqrt(self.ddim_alphas) + sqrt_one_minus_alphas_cumprod = self.ddim_sqrt_one_minus_alphas + + if noise is None: + noise = torch.randn_like(x0) + + def extract_into_tensor(a, t, x_shape): + b, *_ = t.shape + out = a.gather(-1, t) + return out.reshape(b, *((1,) * (len(x_shape) - 1))) + + return (extract_into_tensor(sqrt_alphas_cumprod, t, x0.shape) * x0 + + extract_into_tensor(sqrt_one_minus_alphas_cumprod, t, x0.shape) * noise) + + @torch.no_grad() + def decode(self, x_latent, cond, t_start, unconditional_guidance_scale=1.0, unconditional_conditioning=None, + use_original_steps=False): + + timesteps = np.arange(self.ddpm_num_timesteps) if use_original_steps else self.ddim_timesteps + timesteps = timesteps[:t_start] + + time_range = np.flip(timesteps) + total_steps = timesteps.shape[0] + print(f"Running DDIM Sampling with {total_steps} timesteps") + + iterator = tqdm(time_range, desc='Decoding image', total=total_steps) + x_dec = x_latent + for i, step in enumerate(iterator): + index = total_steps - i - 1 + ts = torch.full((x_latent.shape[0],), step, device=x_latent.device, dtype=torch.long) + x_dec, _ = self.p_sample_ddim(x_dec, cond, ts, index=index, use_original_steps=use_original_steps, + unconditional_guidance_scale=unconditional_guidance_scale, + unconditional_conditioning=unconditional_conditioning) + return x_dec + diff --git a/VADER-VideoCrafter/lvdm/models/utils_diffusion.py b/VADER-VideoCrafter/lvdm/models/utils_diffusion.py new file mode 100644 index 0000000000000000000000000000000000000000..59403ee224726a34b89b550c7db9b2be2f1aeb58 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/models/utils_diffusion.py @@ -0,0 +1,105 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import math +import numpy as np +from einops import repeat +import torch +import torch.nn.functional as F + + +def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False): + """ + Create sinusoidal timestep embeddings. + :param timesteps: a 1-D Tensor of N indices, one per batch element. + These may be fractional. + :param dim: the dimension of the output. + :param max_period: controls the minimum frequency of the embeddings. + :return: an [N x dim] Tensor of positional embeddings. + """ + if not repeat_only: + half = dim // 2 + freqs = torch.exp( + -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half + ).to(device=timesteps.device) + args = timesteps[:, None].float() * freqs[None] + embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) + if dim % 2: + embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1) + else: + embedding = repeat(timesteps, 'b -> b d', d=dim) + return embedding + + +def make_beta_schedule(schedule, n_timestep, linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3): + if schedule == "linear": + betas = ( + torch.linspace(linear_start ** 0.5, linear_end ** 0.5, n_timestep, dtype=torch.float64) ** 2 + ) + + elif schedule == "cosine": + timesteps = ( + torch.arange(n_timestep + 1, dtype=torch.float64) / n_timestep + cosine_s + ) + alphas = timesteps / (1 + cosine_s) * np.pi / 2 + alphas = torch.cos(alphas).pow(2) + alphas = alphas / alphas[0] + betas = 1 - alphas[1:] / alphas[:-1] + betas = np.clip(betas, a_min=0, a_max=0.999) + + elif schedule == "sqrt_linear": + betas = torch.linspace(linear_start, linear_end, n_timestep, dtype=torch.float64) + elif schedule == "sqrt": + betas = torch.linspace(linear_start, linear_end, n_timestep, dtype=torch.float64) ** 0.5 + else: + raise ValueError(f"schedule '{schedule}' unknown.") + return betas.numpy() + + +def make_ddim_timesteps(ddim_discr_method, num_ddim_timesteps, num_ddpm_timesteps, verbose=True): + if ddim_discr_method == 'uniform': + c = num_ddpm_timesteps // num_ddim_timesteps + ddim_timesteps = np.asarray(list(range(0, num_ddpm_timesteps, c))) + elif ddim_discr_method == 'quad': + ddim_timesteps = ((np.linspace(0, np.sqrt(num_ddpm_timesteps * .8), num_ddim_timesteps)) ** 2).astype(int) + else: + raise NotImplementedError(f'There is no ddim discretization method called "{ddim_discr_method}"') + + # assert ddim_timesteps.shape[0] == num_ddim_timesteps + # add one to get the final alpha values right (the ones from first scale to data during sampling) + steps_out = ddim_timesteps + 1 + if verbose: + print(f'Selected timesteps for ddim sampler: {steps_out}') + return steps_out + + +def make_ddim_sampling_parameters(alphacums, ddim_timesteps, eta, verbose=True): + # select alphas for computing the variance schedule + # print(f'ddim_timesteps={ddim_timesteps}, len_alphacums={len(alphacums)}') + alphas = alphacums[ddim_timesteps] + alphas_prev = np.asarray([alphacums[0]] + alphacums[ddim_timesteps[:-1]].tolist()) + + # according the the formula provided in https://arxiv.org/abs/2010.02502 + sigmas = eta * np.sqrt((1 - alphas_prev) / (1 - alphas) * (1 - alphas / alphas_prev)) + if verbose: + print(f'Selected alphas for ddim sampler: a_t: {alphas}; a_(t-1): {alphas_prev}') + print(f'For the chosen value of eta, which is {eta}, ' + f'this results in the following sigma_t schedule for ddim sampler {sigmas}') + return sigmas, alphas, alphas_prev + + +def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999): + """ + Create a beta schedule that discretizes the given alpha_t_bar function, + which defines the cumulative product of (1-beta) over time from t = [0,1]. + :param num_diffusion_timesteps: the number of betas to produce. + :param alpha_bar: a lambda that takes an argument t from 0 to 1 and + produces the cumulative product of (1-beta) up to that + part of the diffusion process. + :param max_beta: the maximum beta to use; use values lower than 1 to + prevent singularities. + """ + betas = [] + for i in range(num_diffusion_timesteps): + t1 = i / num_diffusion_timesteps + t2 = (i + 1) / num_diffusion_timesteps + betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta)) + return np.array(betas) \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/modules/attention.py b/VADER-VideoCrafter/lvdm/modules/attention.py new file mode 100644 index 0000000000000000000000000000000000000000..c0d2f962c6f89632353348cb6230aff62c65f85a --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/attention.py @@ -0,0 +1,476 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +from functools import partial +import torch +from torch import nn, einsum +import torch.nn.functional as F +from einops import rearrange, repeat +try: + import xformers + import xformers.ops + XFORMERS_IS_AVAILBLE = True +except: + XFORMERS_IS_AVAILBLE = False +from lvdm.common import ( + checkpoint, + exists, + default, +) +from lvdm.basics import ( + zero_module, +) + +class RelativePosition(nn.Module): + """ https://github.com/evelinehong/Transformer_Relative_Position_PyTorch/blob/master/relative_position.py """ + + def __init__(self, num_units, max_relative_position): + super().__init__() + self.num_units = num_units + self.max_relative_position = max_relative_position + self.embeddings_table = nn.Parameter(torch.Tensor(max_relative_position * 2 + 1, num_units)) + nn.init.xavier_uniform_(self.embeddings_table) + + def forward(self, length_q, length_k): + device = self.embeddings_table.device + range_vec_q = torch.arange(length_q, device=device) + range_vec_k = torch.arange(length_k, device=device) + distance_mat = range_vec_k[None, :] - range_vec_q[:, None] + distance_mat_clipped = torch.clamp(distance_mat, -self.max_relative_position, self.max_relative_position) + final_mat = distance_mat_clipped + self.max_relative_position + final_mat = final_mat.long() + embeddings = self.embeddings_table[final_mat] + return embeddings + + +class CrossAttention(nn.Module): + + def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0., + relative_position=False, temporal_length=None, img_cross_attention=False): + super().__init__() + inner_dim = dim_head * heads + context_dim = default(context_dim, query_dim) + + self.scale = dim_head**-0.5 + self.heads = heads + self.dim_head = dim_head + self.to_q = nn.Linear(query_dim, inner_dim, bias=False) + self.to_k = nn.Linear(context_dim, inner_dim, bias=False) + self.to_v = nn.Linear(context_dim, inner_dim, bias=False) + self.to_out = nn.Sequential(nn.Linear(inner_dim, query_dim), nn.Dropout(dropout)) + + self.image_cross_attention_scale = 1.0 + self.text_context_len = 77 + self.img_cross_attention = img_cross_attention + if self.img_cross_attention: + self.to_k_ip = nn.Linear(context_dim, inner_dim, bias=False) + self.to_v_ip = nn.Linear(context_dim, inner_dim, bias=False) + + self.relative_position = relative_position + if self.relative_position: + assert(temporal_length is not None) + self.relative_position_k = RelativePosition(num_units=dim_head, max_relative_position=temporal_length) + self.relative_position_v = RelativePosition(num_units=dim_head, max_relative_position=temporal_length) + else: + ## only used for spatial attention, while NOT for temporal attention + if XFORMERS_IS_AVAILBLE and temporal_length is None: + self.forward = self.efficient_forward + + def forward(self, x, context=None, mask=None): + h = self.heads + + q = self.to_q(x) + context = default(context, x) + ## considering image token additionally + if context is not None and self.img_cross_attention: + context, context_img = context[:,:self.text_context_len,:], context[:,self.text_context_len:,:] + k = self.to_k(context) + v = self.to_v(context) + k_ip = self.to_k_ip(context_img) + v_ip = self.to_v_ip(context_img) + else: + k = self.to_k(context) + v = self.to_v(context) + + q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v)) + sim = torch.einsum('b i d, b j d -> b i j', q, k) * self.scale + if self.relative_position: + len_q, len_k, len_v = q.shape[1], k.shape[1], v.shape[1] + k2 = self.relative_position_k(len_q, len_k) + sim2 = einsum('b t d, t s d -> b t s', q, k2) * self.scale # TODO check + sim += sim2 + del k + + if exists(mask): + ## feasible for causal attention mask only + max_neg_value = -torch.finfo(sim.dtype).max + mask = repeat(mask, 'b i j -> (b h) i j', h=h) + sim.masked_fill_(~(mask>0.5), max_neg_value) + + # attention, what we cannot get enough of + sim = sim.softmax(dim=-1) + out = torch.einsum('b i j, b j d -> b i d', sim, v) + if self.relative_position: + v2 = self.relative_position_v(len_q, len_v) + out2 = einsum('b t s, t s d -> b t d', sim, v2) # TODO check + out += out2 + out = rearrange(out, '(b h) n d -> b n (h d)', h=h) + + ## considering image token additionally + if context is not None and self.img_cross_attention: + k_ip, v_ip = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (k_ip, v_ip)) + sim_ip = torch.einsum('b i d, b j d -> b i j', q, k_ip) * self.scale + del k_ip + sim_ip = sim_ip.softmax(dim=-1) + out_ip = torch.einsum('b i j, b j d -> b i d', sim_ip, v_ip) + out_ip = rearrange(out_ip, '(b h) n d -> b n (h d)', h=h) + out = out + self.image_cross_attention_scale * out_ip + del q + + return self.to_out(out) + + def efficient_forward(self, x, context=None, mask=None): + q = self.to_q(x) + context = default(context, x) + + ## considering image token additionally + if context is not None and self.img_cross_attention: + context, context_img = context[:,:self.text_context_len,:], context[:,self.text_context_len:,:] + k = self.to_k(context) + v = self.to_v(context) + k_ip = self.to_k_ip(context_img) + v_ip = self.to_v_ip(context_img) + else: + k = self.to_k(context) + v = self.to_v(context) + + b, _, _ = q.shape + q, k, v = map( + lambda t: t.unsqueeze(3) + .reshape(b, t.shape[1], self.heads, self.dim_head) + .permute(0, 2, 1, 3) + .reshape(b * self.heads, t.shape[1], self.dim_head) + .contiguous(), + (q, k, v), + ) + # actually compute the attention, what we cannot get enough of + out = xformers.ops.memory_efficient_attention(q, k, v, attn_bias=None, op=None) + + ## considering image token additionally + if context is not None and self.img_cross_attention: + k_ip, v_ip = map( + lambda t: t.unsqueeze(3) + .reshape(b, t.shape[1], self.heads, self.dim_head) + .permute(0, 2, 1, 3) + .reshape(b * self.heads, t.shape[1], self.dim_head) + .contiguous(), + (k_ip, v_ip), + ) + out_ip = xformers.ops.memory_efficient_attention(q, k_ip, v_ip, attn_bias=None, op=None) + out_ip = ( + out_ip.unsqueeze(0) + .reshape(b, self.heads, out.shape[1], self.dim_head) + .permute(0, 2, 1, 3) + .reshape(b, out.shape[1], self.heads * self.dim_head) + ) + + if exists(mask): + raise NotImplementedError + out = ( + out.unsqueeze(0) + .reshape(b, self.heads, out.shape[1], self.dim_head) + .permute(0, 2, 1, 3) + .reshape(b, out.shape[1], self.heads * self.dim_head) + ) + if context is not None and self.img_cross_attention: + out = out + self.image_cross_attention_scale * out_ip + return self.to_out(out) + + +class BasicTransformerBlock(nn.Module): + + def __init__(self, dim, n_heads, d_head, dropout=0., context_dim=None, gated_ff=True, checkpoint=True, + disable_self_attn=False, attention_cls=None, img_cross_attention=False): + super().__init__() + attn_cls = CrossAttention if attention_cls is None else attention_cls + self.disable_self_attn = disable_self_attn + self.attn1 = attn_cls(query_dim=dim, heads=n_heads, dim_head=d_head, dropout=dropout, + context_dim=context_dim if self.disable_self_attn else None) + self.ff = FeedForward(dim, dropout=dropout, glu=gated_ff) + self.attn2 = attn_cls(query_dim=dim, context_dim=context_dim, heads=n_heads, dim_head=d_head, dropout=dropout, + img_cross_attention=img_cross_attention) + self.norm1 = nn.LayerNorm(dim) + self.norm2 = nn.LayerNorm(dim) + self.norm3 = nn.LayerNorm(dim) + self.checkpoint = checkpoint + + def forward(self, x, context=None, mask=None): + ## implementation tricks: because checkpointing doesn't support non-tensor (e.g. None or scalar) arguments + input_tuple = (x,) ## should not be (x), otherwise *input_tuple will decouple x into multiple arguments + if context is not None: + input_tuple = (x, context) + if mask is not None: + forward_mask = partial(self._forward, mask=mask) + return checkpoint(forward_mask, (x,), self.parameters(), self.checkpoint) + if context is not None and mask is not None: + input_tuple = (x, context, mask) + return checkpoint(self._forward, input_tuple, self.parameters(), self.checkpoint) + + def _forward(self, x, context=None, mask=None): + x = self.attn1(self.norm1(x), context=context if self.disable_self_attn else None, mask=mask) + x + x = self.attn2(self.norm2(x), context=context, mask=mask) + x + x = self.ff(self.norm3(x)) + x + return x + + +class SpatialTransformer(nn.Module): + """ + Transformer block for image-like data in spatial axis. + First, project the input (aka embedding) + and reshape to b, t, d. + Then apply standard transformer action. + Finally, reshape to image + NEW: use_linear for more efficiency instead of the 1x1 convs + """ + + def __init__(self, in_channels, n_heads, d_head, depth=1, dropout=0., context_dim=None, + use_checkpoint=True, disable_self_attn=False, use_linear=False, img_cross_attention=False): + super().__init__() + self.in_channels = in_channels + inner_dim = n_heads * d_head + self.norm = torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) + if not use_linear: + self.proj_in = nn.Conv2d(in_channels, inner_dim, kernel_size=1, stride=1, padding=0) + else: + self.proj_in = nn.Linear(in_channels, inner_dim) + + self.transformer_blocks = nn.ModuleList([ + BasicTransformerBlock( + inner_dim, + n_heads, + d_head, + dropout=dropout, + context_dim=context_dim, + img_cross_attention=img_cross_attention, + disable_self_attn=disable_self_attn, + checkpoint=use_checkpoint) for d in range(depth) + ]) + if not use_linear: + self.proj_out = zero_module(nn.Conv2d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)) + else: + self.proj_out = zero_module(nn.Linear(inner_dim, in_channels)) + self.use_linear = use_linear + + + def forward(self, x, context=None): + b, c, h, w = x.shape + x_in = x + x = self.norm(x) + if not self.use_linear: + x = self.proj_in(x) + x = rearrange(x, 'b c h w -> b (h w) c').contiguous() + if self.use_linear: + x = self.proj_in(x) + for i, block in enumerate(self.transformer_blocks): + x = block(x, context=context) + if self.use_linear: + x = self.proj_out(x) + x = rearrange(x, 'b (h w) c -> b c h w', h=h, w=w).contiguous() + if not self.use_linear: + x = self.proj_out(x) + return x + x_in + + +class TemporalTransformer(nn.Module): + """ + Transformer block for image-like data in temporal axis. + First, reshape to b, t, d. + Then apply standard transformer action. + Finally, reshape to image + """ + def __init__(self, in_channels, n_heads, d_head, depth=1, dropout=0., context_dim=None, + use_checkpoint=True, use_linear=False, only_self_att=True, causal_attention=False, + relative_position=False, temporal_length=None): + super().__init__() + self.only_self_att = only_self_att + self.relative_position = relative_position + self.causal_attention = causal_attention + self.in_channels = in_channels + inner_dim = n_heads * d_head + self.norm = torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) + self.proj_in = nn.Conv1d(in_channels, inner_dim, kernel_size=1, stride=1, padding=0) + if not use_linear: + self.proj_in = nn.Conv1d(in_channels, inner_dim, kernel_size=1, stride=1, padding=0) + else: + self.proj_in = nn.Linear(in_channels, inner_dim) + + if relative_position: + assert(temporal_length is not None) + attention_cls = partial(CrossAttention, relative_position=True, temporal_length=temporal_length) + else: + attention_cls = None + if self.causal_attention: + assert(temporal_length is not None) + self.mask = torch.tril(torch.ones([1, temporal_length, temporal_length])) + + if self.only_self_att: + context_dim = None + self.transformer_blocks = nn.ModuleList([ + BasicTransformerBlock( + inner_dim, + n_heads, + d_head, + dropout=dropout, + context_dim=context_dim, + attention_cls=attention_cls, + checkpoint=use_checkpoint) for d in range(depth) + ]) + if not use_linear: + self.proj_out = zero_module(nn.Conv1d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)) + else: + self.proj_out = zero_module(nn.Linear(inner_dim, in_channels)) + self.use_linear = use_linear + + def forward(self, x, context=None): + b, c, t, h, w = x.shape + x_in = x + x = self.norm(x) + x = rearrange(x, 'b c t h w -> (b h w) c t').contiguous() + if not self.use_linear: + x = self.proj_in(x) + x = rearrange(x, 'bhw c t -> bhw t c').contiguous() + if self.use_linear: + x = self.proj_in(x) + + if self.causal_attention: + mask = self.mask.to(x.device) + mask = repeat(mask, 'l i j -> (l bhw) i j', bhw=b*h*w) + else: + mask = None + + if self.only_self_att: + ## note: if no context is given, cross-attention defaults to self-attention + for i, block in enumerate(self.transformer_blocks): + x = block(x, mask=mask) + x = rearrange(x, '(b hw) t c -> b hw t c', b=b).contiguous() + else: + x = rearrange(x, '(b hw) t c -> b hw t c', b=b).contiguous() + context = rearrange(context, '(b t) l con -> b t l con', t=t).contiguous() + for i, block in enumerate(self.transformer_blocks): + # calculate each batch one by one (since number in shape could not greater then 65,535 for some package) + for j in range(b): + context_j = repeat( + context[j], + 't l con -> (t r) l con', r=(h * w) // t, t=t).contiguous() + ## note: causal mask will not applied in cross-attention case + x[j] = block(x[j], context=context_j) + + if self.use_linear: + x = self.proj_out(x) + x = rearrange(x, 'b (h w) t c -> b c t h w', h=h, w=w).contiguous() + if not self.use_linear: + x = rearrange(x, 'b hw t c -> (b hw) c t').contiguous() + x = self.proj_out(x) + x = rearrange(x, '(b h w) c t -> b c t h w', b=b, h=h, w=w).contiguous() + + return x + x_in + + +class GEGLU(nn.Module): + def __init__(self, dim_in, dim_out): + super().__init__() + self.proj = nn.Linear(dim_in, dim_out * 2) + + def forward(self, x): + x, gate = self.proj(x).chunk(2, dim=-1) + return x * F.gelu(gate) + + +class FeedForward(nn.Module): + def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.): + super().__init__() + inner_dim = int(dim * mult) + dim_out = default(dim_out, dim) + project_in = nn.Sequential( + nn.Linear(dim, inner_dim), + nn.GELU() + ) if not glu else GEGLU(dim, inner_dim) + + self.net = nn.Sequential( + project_in, + nn.Dropout(dropout), + nn.Linear(inner_dim, dim_out) + ) + + def forward(self, x): + return self.net(x) + + +class LinearAttention(nn.Module): + def __init__(self, dim, heads=4, dim_head=32): + super().__init__() + self.heads = heads + hidden_dim = dim_head * heads + self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) + self.to_out = nn.Conv2d(hidden_dim, dim, 1) + + def forward(self, x): + b, c, h, w = x.shape + qkv = self.to_qkv(x) + q, k, v = rearrange(qkv, 'b (qkv heads c) h w -> qkv b heads c (h w)', heads = self.heads, qkv=3) + k = k.softmax(dim=-1) + context = torch.einsum('bhdn,bhen->bhde', k, v) + out = torch.einsum('bhde,bhdn->bhen', context, q) + out = rearrange(out, 'b heads c (h w) -> b (heads c) h w', heads=self.heads, h=h, w=w) + return self.to_out(out) + + +class SpatialSelfAttention(nn.Module): + def __init__(self, in_channels): + super().__init__() + self.in_channels = in_channels + + self.norm = torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) + self.q = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.k = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.v = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.proj_out = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + + def forward(self, x): + h_ = x + h_ = self.norm(h_) + q = self.q(h_) + k = self.k(h_) + v = self.v(h_) + + # compute attention + b,c,h,w = q.shape + q = rearrange(q, 'b c h w -> b (h w) c') + k = rearrange(k, 'b c h w -> b c (h w)') + w_ = torch.einsum('bij,bjk->bik', q, k) + + w_ = w_ * (int(c)**(-0.5)) + w_ = torch.nn.functional.softmax(w_, dim=2) + + # attend to values + v = rearrange(v, 'b c h w -> b c (h w)') + w_ = rearrange(w_, 'b i j -> b j i') + h_ = torch.einsum('bij,bjk->bik', v, w_) + h_ = rearrange(h_, 'b c (h w) -> b c h w', h=h) + h_ = self.proj_out(h_) + + return x+h_ diff --git a/VADER-VideoCrafter/lvdm/modules/encoders/condition.py b/VADER-VideoCrafter/lvdm/modules/encoders/condition.py new file mode 100644 index 0000000000000000000000000000000000000000..ccc1a07dea9473778e64540639f61b512bbb6f27 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/encoders/condition.py @@ -0,0 +1,393 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import torch +import torch.nn as nn +from torch.utils.checkpoint import checkpoint +import kornia +import open_clip +from transformers import T5Tokenizer, T5EncoderModel, CLIPTokenizer, CLIPTextModel +from lvdm.common import autocast +from utils.utils import count_params + +class AbstractEncoder(nn.Module): + def __init__(self): + super().__init__() + + def encode(self, *args, **kwargs): + raise NotImplementedError + + +class IdentityEncoder(AbstractEncoder): + + def encode(self, x): + return x + + +class ClassEmbedder(nn.Module): + def __init__(self, embed_dim, n_classes=1000, key='class', ucg_rate=0.1): + super().__init__() + self.key = key + self.embedding = nn.Embedding(n_classes, embed_dim) + self.n_classes = n_classes + self.ucg_rate = ucg_rate + + def forward(self, batch, key=None, disable_dropout=False): + if key is None: + key = self.key + # this is for use in crossattn + c = batch[key][:, None] + if self.ucg_rate > 0. and not disable_dropout: + mask = 1. - torch.bernoulli(torch.ones_like(c) * self.ucg_rate) + c = mask * c + (1 - mask) * torch.ones_like(c) * (self.n_classes - 1) + c = c.long() + c = self.embedding(c) + return c + + def get_unconditional_conditioning(self, bs, device="cuda"): + uc_class = self.n_classes - 1 # 1000 classes --> 0 ... 999, one extra class for ucg (class 1000) + uc = torch.ones((bs,), device=device) * uc_class + uc = {self.key: uc} + return uc + + +def disabled_train(self, mode=True): + """Overwrite model.train with this function to make sure train/eval mode + does not change anymore.""" + return self + + +class FrozenT5Embedder(AbstractEncoder): + """Uses the T5 transformer encoder for text""" + + def __init__(self, version="google/t5-v1_1-large", device="cuda", max_length=77, + freeze=True): # others are google/t5-v1_1-xl and google/t5-v1_1-xxl + super().__init__() + self.tokenizer = T5Tokenizer.from_pretrained(version) + self.transformer = T5EncoderModel.from_pretrained(version) + self.device = device + self.max_length = max_length # TODO: typical value? + if freeze: + self.freeze() + + def freeze(self): + self.transformer = self.transformer.eval() + # self.train = disabled_train + for param in self.parameters(): + param.requires_grad = False + + def forward(self, text): + batch_encoding = self.tokenizer(text, truncation=True, max_length=self.max_length, return_length=True, + return_overflowing_tokens=False, padding="max_length", return_tensors="pt") + tokens = batch_encoding["input_ids"].to(self.device) + outputs = self.transformer(input_ids=tokens) + + z = outputs.last_hidden_state + return z + + def encode(self, text): + return self(text) + + +class FrozenCLIPEmbedder(AbstractEncoder): + """Uses the CLIP transformer encoder for text (from huggingface)""" + LAYERS = [ + "last", + "pooled", + "hidden" + ] + + def __init__(self, version="openai/clip-vit-large-patch14", device="cuda", max_length=77, + freeze=True, layer="last", layer_idx=None): # clip-vit-base-patch32 + super().__init__() + assert layer in self.LAYERS + self.tokenizer = CLIPTokenizer.from_pretrained(version) + self.transformer = CLIPTextModel.from_pretrained(version) + self.device = device + self.max_length = max_length + if freeze: + self.freeze() + self.layer = layer + self.layer_idx = layer_idx + if layer == "hidden": + assert layer_idx is not None + assert 0 <= abs(layer_idx) <= 12 + + def freeze(self): + self.transformer = self.transformer.eval() + # self.train = disabled_train + for param in self.parameters(): + param.requires_grad = False + + def forward(self, text): + batch_encoding = self.tokenizer(text, truncation=True, max_length=self.max_length, return_length=True, + return_overflowing_tokens=False, padding="max_length", return_tensors="pt") + tokens = batch_encoding["input_ids"].to(self.device) + outputs = self.transformer(input_ids=tokens, output_hidden_states=self.layer == "hidden") + if self.layer == "last": + z = outputs.last_hidden_state + elif self.layer == "pooled": + z = outputs.pooler_output[:, None, :] + else: + z = outputs.hidden_states[self.layer_idx] + return z + + def encode(self, text): + return self(text) + + +class ClipImageEmbedder(nn.Module): + def __init__( + self, + model, + jit=False, + device='cuda' if torch.cuda.is_available() else 'cpu', + antialias=True, + ucg_rate=0. + ): + super().__init__() + from clip import load as load_clip + self.model, _ = load_clip(name=model, device=device, jit=jit) + + self.antialias = antialias + + self.register_buffer('mean', torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False) + self.register_buffer('std', torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False) + self.ucg_rate = ucg_rate + + def preprocess(self, x): + # normalize to [0,1] + x = kornia.geometry.resize(x, (224, 224), + interpolation='bicubic', align_corners=True, + antialias=self.antialias) + x = (x + 1.) / 2. + # re-normalize according to clip + x = kornia.enhance.normalize(x, self.mean, self.std) + return x + + def forward(self, x, no_dropout=False): + # x is assumed to be in range [-1,1] + out = self.model.encode_image(self.preprocess(x)) + out = out.to(x.dtype) + if self.ucg_rate > 0. and not no_dropout: + out = torch.bernoulli((1. - self.ucg_rate) * torch.ones(out.shape[0], device=out.device))[:, None] * out + return out + + +class FrozenOpenCLIPEmbedder(AbstractEncoder): + """ + Uses the OpenCLIP transformer encoder for text + """ + LAYERS = [ + # "pooled", + "last", + "penultimate" + ] + + def __init__(self, arch="ViT-H-14", version="laion2b_s32b_b79k", device="cuda", max_length=77, + freeze=True, layer="last"): + super().__init__() + assert layer in self.LAYERS + model, _, _ = open_clip.create_model_and_transforms(arch, device=torch.device('cpu')) + del model.visual + self.model = model + + self.device = device + self.max_length = max_length + if freeze: + self.freeze() + self.layer = layer + if self.layer == "last": + self.layer_idx = 0 + elif self.layer == "penultimate": + self.layer_idx = 1 + else: + raise NotImplementedError() + + def freeze(self): + self.model = self.model.eval() + for param in self.parameters(): + param.requires_grad = False + + def forward(self, text): + self.device = self.model.positional_embedding.device + tokens = open_clip.tokenize(text) + z = self.encode_with_transformer(tokens.to(self.device)) + return z + + def encode_with_transformer(self, text): + x = self.model.token_embedding(text) # [batch_size, n_ctx, d_model] + x = x + self.model.positional_embedding + x = x.permute(1, 0, 2) # NLD -> LND + x = self.text_transformer_forward(x, attn_mask=self.model.attn_mask) + x = x.permute(1, 0, 2) # LND -> NLD + x = self.model.ln_final(x) + return x + + def text_transformer_forward(self, x: torch.Tensor, attn_mask=None): + for i, r in enumerate(self.model.transformer.resblocks): + if i == len(self.model.transformer.resblocks) - self.layer_idx: + break + if self.model.transformer.grad_checkpointing and not torch.jit.is_scripting(): + x = checkpoint(r, x, attn_mask) + else: + x = r(x, attn_mask=attn_mask) + return x + + def encode(self, text): + return self(text) + + +class FrozenOpenCLIPImageEmbedder(AbstractEncoder): + """ + Uses the OpenCLIP vision transformer encoder for images + """ + + def __init__(self, arch="ViT-H-14", version="laion2b_s32b_b79k", device="cuda", max_length=77, + freeze=True, layer="pooled", antialias=True, ucg_rate=0.): + super().__init__() + model, _, _ = open_clip.create_model_and_transforms(arch, device=torch.device('cpu'), + pretrained=version, ) + del model.transformer + self.model = model + + self.device = device + self.max_length = max_length + if freeze: + self.freeze() + self.layer = layer + if self.layer == "penultimate": + raise NotImplementedError() + self.layer_idx = 1 + + self.antialias = antialias + + self.register_buffer('mean', torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False) + self.register_buffer('std', torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False) + self.ucg_rate = ucg_rate + + def preprocess(self, x): + # normalize to [0,1] + x = kornia.geometry.resize(x, (224, 224), + interpolation='bicubic', align_corners=True, + antialias=self.antialias) + x = (x + 1.) / 2. + # renormalize according to clip + x = kornia.enhance.normalize(x, self.mean, self.std) + return x + + def freeze(self): + self.model = self.model.eval() + for param in self.parameters(): + param.requires_grad = False + + @autocast + def forward(self, image, no_dropout=False): + z = self.encode_with_vision_transformer(image) + if self.ucg_rate > 0. and not no_dropout: + z = torch.bernoulli((1. - self.ucg_rate) * torch.ones(z.shape[0], device=z.device))[:, None] * z + return z + + def encode_with_vision_transformer(self, img): + img = self.preprocess(img) + x = self.model.visual(img) + return x + + def encode(self, text): + return self(text) + + + +class FrozenOpenCLIPImageEmbedderV2(AbstractEncoder): + """ + Uses the OpenCLIP vision transformer encoder for images + """ + + def __init__(self, arch="ViT-H-14", version="laion2b_s32b_b79k", device="cuda", + freeze=True, layer="pooled", antialias=True): + super().__init__() + model, _, _ = open_clip.create_model_and_transforms(arch, device=torch.device('cpu'), + pretrained=version, ) + del model.transformer + self.model = model + self.device = device + + if freeze: + self.freeze() + self.layer = layer + if self.layer == "penultimate": + raise NotImplementedError() + self.layer_idx = 1 + + self.antialias = antialias + self.register_buffer('mean', torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False) + self.register_buffer('std', torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False) + + + def preprocess(self, x): + # normalize to [0,1] + x = kornia.geometry.resize(x, (224, 224), + interpolation='bicubic', align_corners=True, + antialias=self.antialias) + x = (x + 1.) / 2. + # renormalize according to clip + x = kornia.enhance.normalize(x, self.mean, self.std) + return x + + def freeze(self): + self.model = self.model.eval() + for param in self.model.parameters(): + param.requires_grad = False + + def forward(self, image, no_dropout=False): + ## image: b c h w + z = self.encode_with_vision_transformer(image) + return z + + def encode_with_vision_transformer(self, x): + x = self.preprocess(x) + + # to patches - whether to use dual patchnorm - https://arxiv.org/abs/2302.01327v1 + if self.model.visual.input_patchnorm: + # einops - rearrange(x, 'b c (h p1) (w p2) -> b (h w) (c p1 p2)') + x = x.reshape(x.shape[0], x.shape[1], self.model.visual.grid_size[0], self.model.visual.patch_size[0], self.model.visual.grid_size[1], self.model.visual.patch_size[1]) + x = x.permute(0, 2, 4, 1, 3, 5) + x = x.reshape(x.shape[0], self.model.visual.grid_size[0] * self.model.visual.grid_size[1], -1) + x = self.model.visual.patchnorm_pre_ln(x) + x = self.model.visual.conv1(x) + else: + x = self.model.visual.conv1(x) # shape = [*, width, grid, grid] + x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] + x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] + + # class embeddings and positional embeddings + x = torch.cat( + [self.model.visual.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), + x], dim=1) # shape = [*, grid ** 2 + 1, width] + x = x + self.model.visual.positional_embedding.to(x.dtype) + + # a patch_dropout of 0. would mean it is disabled and this function would do nothing but return what was passed in + x = self.model.visual.patch_dropout(x) + x = self.model.visual.ln_pre(x) + + x = x.permute(1, 0, 2) # NLD -> LND + x = self.model.visual.transformer(x) + x = x.permute(1, 0, 2) # LND -> NLD + + return x + + +class FrozenCLIPT5Encoder(AbstractEncoder): + def __init__(self, clip_version="openai/clip-vit-large-patch14", t5_version="google/t5-v1_1-xl", device="cuda", + clip_max_length=77, t5_max_length=77): + super().__init__() + self.clip_encoder = FrozenCLIPEmbedder(clip_version, device, max_length=clip_max_length) + self.t5_encoder = FrozenT5Embedder(t5_version, device, max_length=t5_max_length) + print(f"{self.clip_encoder.__class__.__name__} has {count_params(self.clip_encoder) * 1.e-6:.2f} M parameters, " + f"{self.t5_encoder.__class__.__name__} comes with {count_params(self.t5_encoder) * 1.e-6:.2f} M params.") + + def encode(self, text): + return self(text) + + def forward(self, text): + clip_z = self.clip_encoder.encode(text) + t5_z = self.t5_encoder.encode(text) + return [clip_z, t5_z] \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/modules/encoders/ip_resampler.py b/VADER-VideoCrafter/lvdm/modules/encoders/ip_resampler.py new file mode 100644 index 0000000000000000000000000000000000000000..d274f5aa77af36f5a12522a6e0254893ef6879d6 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/encoders/ip_resampler.py @@ -0,0 +1,137 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +# modified from https://github.com/mlfoundations/open_flamingo/blob/main/open_flamingo/src/helpers.py +import math +import torch +import torch.nn as nn + + +class ImageProjModel(nn.Module): + """Projection Model""" + def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024, clip_extra_context_tokens=4): + super().__init__() + self.cross_attention_dim = cross_attention_dim + self.clip_extra_context_tokens = clip_extra_context_tokens + self.proj = nn.Linear(clip_embeddings_dim, self.clip_extra_context_tokens * cross_attention_dim) + self.norm = nn.LayerNorm(cross_attention_dim) + + def forward(self, image_embeds): + #embeds = image_embeds + embeds = image_embeds.type(list(self.proj.parameters())[0].dtype) + clip_extra_context_tokens = self.proj(embeds).reshape(-1, self.clip_extra_context_tokens, self.cross_attention_dim) + clip_extra_context_tokens = self.norm(clip_extra_context_tokens) + return clip_extra_context_tokens + +# FFN +def FeedForward(dim, mult=4): + inner_dim = int(dim * mult) + return nn.Sequential( + nn.LayerNorm(dim), + nn.Linear(dim, inner_dim, bias=False), + nn.GELU(), + nn.Linear(inner_dim, dim, bias=False), + ) + + +def reshape_tensor(x, heads): + bs, length, width = x.shape + #(bs, length, width) --> (bs, length, n_heads, dim_per_head) + x = x.view(bs, length, heads, -1) + # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head) + x = x.transpose(1, 2) + # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head) + x = x.reshape(bs, heads, length, -1) + return x + + +class PerceiverAttention(nn.Module): + def __init__(self, *, dim, dim_head=64, heads=8): + super().__init__() + self.scale = dim_head**-0.5 + self.dim_head = dim_head + self.heads = heads + inner_dim = dim_head * heads + + self.norm1 = nn.LayerNorm(dim) + self.norm2 = nn.LayerNorm(dim) + + self.to_q = nn.Linear(dim, inner_dim, bias=False) + self.to_kv = nn.Linear(dim, inner_dim * 2, bias=False) + self.to_out = nn.Linear(inner_dim, dim, bias=False) + + + def forward(self, x, latents): + """ + Args: + x (torch.Tensor): image features + shape (b, n1, D) + latent (torch.Tensor): latent features + shape (b, n2, D) + """ + x = self.norm1(x) + latents = self.norm2(latents) + + b, l, _ = latents.shape + + q = self.to_q(latents) + kv_input = torch.cat((x, latents), dim=-2) + k, v = self.to_kv(kv_input).chunk(2, dim=-1) + + q = reshape_tensor(q, self.heads) + k = reshape_tensor(k, self.heads) + v = reshape_tensor(v, self.heads) + + # attention + scale = 1 / math.sqrt(math.sqrt(self.dim_head)) + weight = (q * scale) @ (k * scale).transpose(-2, -1) # More stable with f16 than dividing afterwards + weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype) + out = weight @ v + + out = out.permute(0, 2, 1, 3).reshape(b, l, -1) + + return self.to_out(out) + + +class Resampler(nn.Module): + def __init__( + self, + dim=1024, + depth=8, + dim_head=64, + heads=16, + num_queries=8, + embedding_dim=768, + output_dim=1024, + ff_mult=4, + ): + super().__init__() + + self.latents = nn.Parameter(torch.randn(1, num_queries, dim) / dim**0.5) + + self.proj_in = nn.Linear(embedding_dim, dim) + + self.proj_out = nn.Linear(dim, output_dim) + self.norm_out = nn.LayerNorm(output_dim) + + self.layers = nn.ModuleList([]) + for _ in range(depth): + self.layers.append( + nn.ModuleList( + [ + PerceiverAttention(dim=dim, dim_head=dim_head, heads=heads), + FeedForward(dim=dim, mult=ff_mult), + ] + ) + ) + + def forward(self, x): + + latents = self.latents.repeat(x.size(0), 1, 1) + + x = self.proj_in(x) + + for attn, ff in self.layers: + latents = attn(x, latents) + latents + latents = ff(latents) + latents + + latents = self.proj_out(latents) + return self.norm_out(latents) \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/modules/networks/ae_modules.py b/VADER-VideoCrafter/lvdm/modules/networks/ae_modules.py new file mode 100644 index 0000000000000000000000000000000000000000..210762e56ec00bfd12c720f10bb2d96a3f402d71 --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/networks/ae_modules.py @@ -0,0 +1,846 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +# pytorch_diffusion + derived encoder decoder +import math +import torch +import numpy as np +import torch.nn as nn +from einops import rearrange +from utils.utils import instantiate_from_config +from lvdm.modules.attention import LinearAttention + +def nonlinearity(x): + # swish + return x*torch.sigmoid(x) + + +def Normalize(in_channels, num_groups=32): + return torch.nn.GroupNorm(num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True) + + + +class LinAttnBlock(LinearAttention): + """to match AttnBlock usage""" + def __init__(self, in_channels): + super().__init__(dim=in_channels, heads=1, dim_head=in_channels) + + +class AttnBlock(nn.Module): + def __init__(self, in_channels): + super().__init__() + self.in_channels = in_channels + + self.norm = Normalize(in_channels) + self.q = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.k = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.v = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + self.proj_out = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=1, + stride=1, + padding=0) + + def forward(self, x): + h_ = x + h_ = self.norm(h_) + q = self.q(h_) + k = self.k(h_) + v = self.v(h_) + + # compute attention + b,c,h,w = q.shape + q = q.reshape(b,c,h*w) # bcl + q = q.permute(0,2,1) # bcl -> blc l=hw + k = k.reshape(b,c,h*w) # bcl + + w_ = torch.bmm(q,k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] + w_ = w_ * (int(c)**(-0.5)) + w_ = torch.nn.functional.softmax(w_, dim=2) + + # attend to values + v = v.reshape(b,c,h*w) + w_ = w_.permute(0,2,1) # b,hw,hw (first hw of k, second of q) + h_ = torch.bmm(v,w_) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] + h_ = h_.reshape(b,c,h,w) + + h_ = self.proj_out(h_) + + return x+h_ + +def make_attn(in_channels, attn_type="vanilla"): + assert attn_type in ["vanilla", "linear", "none"], f'attn_type {attn_type} unknown' + #print(f"making attention of type '{attn_type}' with {in_channels} in_channels") + if attn_type == "vanilla": + return AttnBlock(in_channels) + elif attn_type == "none": + return nn.Identity(in_channels) + else: + return LinAttnBlock(in_channels) + +class Downsample(nn.Module): + def __init__(self, in_channels, with_conv): + super().__init__() + self.with_conv = with_conv + self.in_channels = in_channels + if self.with_conv: + # no asymmetric padding in torch conv, must do it ourselves + self.conv = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=3, + stride=2, + padding=0) + def forward(self, x): + if self.with_conv: + pad = (0,1,0,1) + x = torch.nn.functional.pad(x, pad, mode="constant", value=0) + x = self.conv(x) + else: + x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) + return x + +class Upsample(nn.Module): + def __init__(self, in_channels, with_conv): + super().__init__() + self.with_conv = with_conv + self.in_channels = in_channels + if self.with_conv: + self.conv = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, x): + x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest") + if self.with_conv: + x = self.conv(x) + return x + +def get_timestep_embedding(timesteps, embedding_dim): + """ + This matches the implementation in Denoising Diffusion Probabilistic Models: + From Fairseq. + Build sinusoidal embeddings. + This matches the implementation in tensor2tensor, but differs slightly + from the description in Section 3.5 of "Attention Is All You Need". + """ + assert len(timesteps.shape) == 1 + + half_dim = embedding_dim // 2 + emb = math.log(10000) / (half_dim - 1) + emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb) + emb = emb.to(device=timesteps.device) + emb = timesteps.float()[:, None] * emb[None, :] + emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1) + if embedding_dim % 2 == 1: # zero pad + emb = torch.nn.functional.pad(emb, (0,1,0,0)) + return emb + + + +class ResnetBlock(nn.Module): + def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, + dropout, temb_channels=512): + super().__init__() + self.in_channels = in_channels + out_channels = in_channels if out_channels is None else out_channels + self.out_channels = out_channels + self.use_conv_shortcut = conv_shortcut + + self.norm1 = Normalize(in_channels) + self.conv1 = torch.nn.Conv2d(in_channels, + out_channels, + kernel_size=3, + stride=1, + padding=1) + if temb_channels > 0: + self.temb_proj = torch.nn.Linear(temb_channels, + out_channels) + self.norm2 = Normalize(out_channels) + self.dropout = torch.nn.Dropout(dropout) + self.conv2 = torch.nn.Conv2d(out_channels, + out_channels, + kernel_size=3, + stride=1, + padding=1) + if self.in_channels != self.out_channels: + if self.use_conv_shortcut: + self.conv_shortcut = torch.nn.Conv2d(in_channels, + out_channels, + kernel_size=3, + stride=1, + padding=1) + else: + self.nin_shortcut = torch.nn.Conv2d(in_channels, + out_channels, + kernel_size=1, + stride=1, + padding=0) + + def forward(self, x, temb): + h = x + h = self.norm1(h) + h = nonlinearity(h) + h = self.conv1(h) + + if temb is not None: + h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None] + + h = self.norm2(h) + h = nonlinearity(h) + h = self.dropout(h) + h = self.conv2(h) + + if self.in_channels != self.out_channels: + if self.use_conv_shortcut: + x = self.conv_shortcut(x) + else: + x = self.nin_shortcut(x) + + return x+h + +class Model(nn.Module): + def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, + attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, + resolution, use_timestep=True, use_linear_attn=False, attn_type="vanilla"): + super().__init__() + if use_linear_attn: attn_type = "linear" + self.ch = ch + self.temb_ch = self.ch*4 + self.num_resolutions = len(ch_mult) + self.num_res_blocks = num_res_blocks + self.resolution = resolution + self.in_channels = in_channels + + self.use_timestep = use_timestep + if self.use_timestep: + # timestep embedding + self.temb = nn.Module() + self.temb.dense = nn.ModuleList([ + torch.nn.Linear(self.ch, + self.temb_ch), + torch.nn.Linear(self.temb_ch, + self.temb_ch), + ]) + + # downsampling + self.conv_in = torch.nn.Conv2d(in_channels, + self.ch, + kernel_size=3, + stride=1, + padding=1) + + curr_res = resolution + in_ch_mult = (1,)+tuple(ch_mult) + self.down = nn.ModuleList() + for i_level in range(self.num_resolutions): + block = nn.ModuleList() + attn = nn.ModuleList() + block_in = ch*in_ch_mult[i_level] + block_out = ch*ch_mult[i_level] + for i_block in range(self.num_res_blocks): + block.append(ResnetBlock(in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout)) + block_in = block_out + if curr_res in attn_resolutions: + attn.append(make_attn(block_in, attn_type=attn_type)) + down = nn.Module() + down.block = block + down.attn = attn + if i_level != self.num_resolutions-1: + down.downsample = Downsample(block_in, resamp_with_conv) + curr_res = curr_res // 2 + self.down.append(down) + + # middle + self.mid = nn.Module() + self.mid.block_1 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) + self.mid.block_2 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + + # upsampling + self.up = nn.ModuleList() + for i_level in reversed(range(self.num_resolutions)): + block = nn.ModuleList() + attn = nn.ModuleList() + block_out = ch*ch_mult[i_level] + skip_in = ch*ch_mult[i_level] + for i_block in range(self.num_res_blocks+1): + if i_block == self.num_res_blocks: + skip_in = ch*in_ch_mult[i_level] + block.append(ResnetBlock(in_channels=block_in+skip_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout)) + block_in = block_out + if curr_res in attn_resolutions: + attn.append(make_attn(block_in, attn_type=attn_type)) + up = nn.Module() + up.block = block + up.attn = attn + if i_level != 0: + up.upsample = Upsample(block_in, resamp_with_conv) + curr_res = curr_res * 2 + self.up.insert(0, up) # prepend to get consistent order + + # end + self.norm_out = Normalize(block_in) + self.conv_out = torch.nn.Conv2d(block_in, + out_ch, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, x, t=None, context=None): + #assert x.shape[2] == x.shape[3] == self.resolution + if context is not None: + # assume aligned context, cat along channel axis + x = torch.cat((x, context), dim=1) + if self.use_timestep: + # timestep embedding + assert t is not None + temb = get_timestep_embedding(t, self.ch) + temb = self.temb.dense[0](temb) + temb = nonlinearity(temb) + temb = self.temb.dense[1](temb) + else: + temb = None + + # downsampling + hs = [self.conv_in(x)] + for i_level in range(self.num_resolutions): + for i_block in range(self.num_res_blocks): + h = self.down[i_level].block[i_block](hs[-1], temb) + if len(self.down[i_level].attn) > 0: + h = self.down[i_level].attn[i_block](h) + hs.append(h) + if i_level != self.num_resolutions-1: + hs.append(self.down[i_level].downsample(hs[-1])) + + # middle + h = hs[-1] + h = self.mid.block_1(h, temb) + h = self.mid.attn_1(h) + h = self.mid.block_2(h, temb) + + # upsampling + for i_level in reversed(range(self.num_resolutions)): + for i_block in range(self.num_res_blocks+1): + h = self.up[i_level].block[i_block]( + torch.cat([h, hs.pop()], dim=1), temb) + if len(self.up[i_level].attn) > 0: + h = self.up[i_level].attn[i_block](h) + if i_level != 0: + h = self.up[i_level].upsample(h) + + # end + h = self.norm_out(h) + h = nonlinearity(h) + h = self.conv_out(h) + return h + + def get_last_layer(self): + return self.conv_out.weight + + +class Encoder(nn.Module): + def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, + attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, + resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla", + **ignore_kwargs): + super().__init__() + if use_linear_attn: attn_type = "linear" + self.ch = ch + self.temb_ch = 0 + self.num_resolutions = len(ch_mult) + self.num_res_blocks = num_res_blocks + self.resolution = resolution + self.in_channels = in_channels + + # downsampling + self.conv_in = torch.nn.Conv2d(in_channels, + self.ch, + kernel_size=3, + stride=1, + padding=1) + + curr_res = resolution + in_ch_mult = (1,)+tuple(ch_mult) + self.in_ch_mult = in_ch_mult + self.down = nn.ModuleList() + for i_level in range(self.num_resolutions): + block = nn.ModuleList() + attn = nn.ModuleList() + block_in = ch*in_ch_mult[i_level] + block_out = ch*ch_mult[i_level] + for i_block in range(self.num_res_blocks): + block.append(ResnetBlock(in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout)) + block_in = block_out + if curr_res in attn_resolutions: + attn.append(make_attn(block_in, attn_type=attn_type)) + down = nn.Module() + down.block = block + down.attn = attn + if i_level != self.num_resolutions-1: + down.downsample = Downsample(block_in, resamp_with_conv) + curr_res = curr_res // 2 + self.down.append(down) + + # middle + self.mid = nn.Module() + self.mid.block_1 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) + self.mid.block_2 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + + # end + self.norm_out = Normalize(block_in) + self.conv_out = torch.nn.Conv2d(block_in, + 2*z_channels if double_z else z_channels, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, x): + # timestep embedding + temb = None + + # print(f'encoder-input={x.shape}') + # downsampling + hs = [self.conv_in(x)] + # print(f'encoder-conv in feat={hs[0].shape}') + for i_level in range(self.num_resolutions): + for i_block in range(self.num_res_blocks): + h = self.down[i_level].block[i_block](hs[-1], temb) + # print(f'encoder-down feat={h.shape}') + if len(self.down[i_level].attn) > 0: + h = self.down[i_level].attn[i_block](h) + hs.append(h) + if i_level != self.num_resolutions-1: + # print(f'encoder-downsample (input)={hs[-1].shape}') + hs.append(self.down[i_level].downsample(hs[-1])) + # print(f'encoder-downsample (output)={hs[-1].shape}') + + # middle + h = hs[-1] + h = self.mid.block_1(h, temb) + # print(f'encoder-mid1 feat={h.shape}') + h = self.mid.attn_1(h) + h = self.mid.block_2(h, temb) + # print(f'encoder-mid2 feat={h.shape}') + + # end + h = self.norm_out(h) + h = nonlinearity(h) + h = self.conv_out(h) + # print(f'end feat={h.shape}') + return h + + +class Decoder(nn.Module): + def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, + attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, + resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, + attn_type="vanilla", **ignorekwargs): + super().__init__() + if use_linear_attn: attn_type = "linear" + self.ch = ch + self.temb_ch = 0 + self.num_resolutions = len(ch_mult) + self.num_res_blocks = num_res_blocks + self.resolution = resolution + self.in_channels = in_channels + self.give_pre_end = give_pre_end + self.tanh_out = tanh_out + + # compute in_ch_mult, block_in and curr_res at lowest res + in_ch_mult = (1,)+tuple(ch_mult) + block_in = ch*ch_mult[self.num_resolutions-1] + curr_res = resolution // 2**(self.num_resolutions-1) + self.z_shape = (1,z_channels,curr_res,curr_res) + print("AE working on z of shape {} = {} dimensions.".format( + self.z_shape, np.prod(self.z_shape))) + + # z to block_in + self.conv_in = torch.nn.Conv2d(z_channels, + block_in, + kernel_size=3, + stride=1, + padding=1) + + # middle + self.mid = nn.Module() + self.mid.block_1 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) + self.mid.block_2 = ResnetBlock(in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout) + + # upsampling + self.up = nn.ModuleList() + for i_level in reversed(range(self.num_resolutions)): + block = nn.ModuleList() + attn = nn.ModuleList() + block_out = ch*ch_mult[i_level] + for i_block in range(self.num_res_blocks+1): + block.append(ResnetBlock(in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout)) + block_in = block_out + if curr_res in attn_resolutions: + attn.append(make_attn(block_in, attn_type=attn_type)) + up = nn.Module() + up.block = block + up.attn = attn + if i_level != 0: + up.upsample = Upsample(block_in, resamp_with_conv) + curr_res = curr_res * 2 + self.up.insert(0, up) # prepend to get consistent order + + # end + self.norm_out = Normalize(block_in) + self.conv_out = torch.nn.Conv2d(block_in, + out_ch, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, z): + #assert z.shape[1:] == self.z_shape[1:] + self.last_z_shape = z.shape + + # print(f'decoder-input={z.shape}') + # timestep embedding + temb = None + + # z to block_in + h = self.conv_in(z) + # print(f'decoder-conv in feat={h.shape}') + + # middle + h = self.mid.block_1(h, temb) + h = self.mid.attn_1(h) + h = self.mid.block_2(h, temb) + # print(f'decoder-mid feat={h.shape}') + + # upsampling + for i_level in reversed(range(self.num_resolutions)): + for i_block in range(self.num_res_blocks+1): + h = self.up[i_level].block[i_block](h, temb) + if len(self.up[i_level].attn) > 0: + h = self.up[i_level].attn[i_block](h) + # print(f'decoder-up feat={h.shape}') + if i_level != 0: + h = self.up[i_level].upsample(h) + # print(f'decoder-upsample feat={h.shape}') + + # end + if self.give_pre_end: + return h + + h = self.norm_out(h) + h = nonlinearity(h) + h = self.conv_out(h) + # print(f'decoder-conv_out feat={h.shape}') + if self.tanh_out: + h = torch.tanh(h) + return h + + +class SimpleDecoder(nn.Module): + def __init__(self, in_channels, out_channels, *args, **kwargs): + super().__init__() + self.model = nn.ModuleList([nn.Conv2d(in_channels, in_channels, 1), + ResnetBlock(in_channels=in_channels, + out_channels=2 * in_channels, + temb_channels=0, dropout=0.0), + ResnetBlock(in_channels=2 * in_channels, + out_channels=4 * in_channels, + temb_channels=0, dropout=0.0), + ResnetBlock(in_channels=4 * in_channels, + out_channels=2 * in_channels, + temb_channels=0, dropout=0.0), + nn.Conv2d(2*in_channels, in_channels, 1), + Upsample(in_channels, with_conv=True)]) + # end + self.norm_out = Normalize(in_channels) + self.conv_out = torch.nn.Conv2d(in_channels, + out_channels, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, x): + for i, layer in enumerate(self.model): + if i in [1,2,3]: + x = layer(x, None) + else: + x = layer(x) + + h = self.norm_out(x) + h = nonlinearity(h) + x = self.conv_out(h) + return x + + +class UpsampleDecoder(nn.Module): + def __init__(self, in_channels, out_channels, ch, num_res_blocks, resolution, + ch_mult=(2,2), dropout=0.0): + super().__init__() + # upsampling + self.temb_ch = 0 + self.num_resolutions = len(ch_mult) + self.num_res_blocks = num_res_blocks + block_in = in_channels + curr_res = resolution // 2 ** (self.num_resolutions - 1) + self.res_blocks = nn.ModuleList() + self.upsample_blocks = nn.ModuleList() + for i_level in range(self.num_resolutions): + res_block = [] + block_out = ch * ch_mult[i_level] + for i_block in range(self.num_res_blocks + 1): + res_block.append(ResnetBlock(in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout)) + block_in = block_out + self.res_blocks.append(nn.ModuleList(res_block)) + if i_level != self.num_resolutions - 1: + self.upsample_blocks.append(Upsample(block_in, True)) + curr_res = curr_res * 2 + + # end + self.norm_out = Normalize(block_in) + self.conv_out = torch.nn.Conv2d(block_in, + out_channels, + kernel_size=3, + stride=1, + padding=1) + + def forward(self, x): + # upsampling + h = x + for k, i_level in enumerate(range(self.num_resolutions)): + for i_block in range(self.num_res_blocks + 1): + h = self.res_blocks[i_level][i_block](h, None) + if i_level != self.num_resolutions - 1: + h = self.upsample_blocks[k](h) + h = self.norm_out(h) + h = nonlinearity(h) + h = self.conv_out(h) + return h + + +class LatentRescaler(nn.Module): + def __init__(self, factor, in_channels, mid_channels, out_channels, depth=2): + super().__init__() + # residual block, interpolate, residual block + self.factor = factor + self.conv_in = nn.Conv2d(in_channels, + mid_channels, + kernel_size=3, + stride=1, + padding=1) + self.res_block1 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, + out_channels=mid_channels, + temb_channels=0, + dropout=0.0) for _ in range(depth)]) + self.attn = AttnBlock(mid_channels) + self.res_block2 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, + out_channels=mid_channels, + temb_channels=0, + dropout=0.0) for _ in range(depth)]) + + self.conv_out = nn.Conv2d(mid_channels, + out_channels, + kernel_size=1, + ) + + def forward(self, x): + x = self.conv_in(x) + for block in self.res_block1: + x = block(x, None) + x = torch.nn.functional.interpolate(x, size=(int(round(x.shape[2]*self.factor)), int(round(x.shape[3]*self.factor)))) + x = self.attn(x) + for block in self.res_block2: + x = block(x, None) + x = self.conv_out(x) + return x + + +class MergedRescaleEncoder(nn.Module): + def __init__(self, in_channels, ch, resolution, out_ch, num_res_blocks, + attn_resolutions, dropout=0.0, resamp_with_conv=True, + ch_mult=(1,2,4,8), rescale_factor=1.0, rescale_module_depth=1): + super().__init__() + intermediate_chn = ch * ch_mult[-1] + self.encoder = Encoder(in_channels=in_channels, num_res_blocks=num_res_blocks, ch=ch, ch_mult=ch_mult, + z_channels=intermediate_chn, double_z=False, resolution=resolution, + attn_resolutions=attn_resolutions, dropout=dropout, resamp_with_conv=resamp_with_conv, + out_ch=None) + self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=intermediate_chn, + mid_channels=intermediate_chn, out_channels=out_ch, depth=rescale_module_depth) + + def forward(self, x): + x = self.encoder(x) + x = self.rescaler(x) + return x + + +class MergedRescaleDecoder(nn.Module): + def __init__(self, z_channels, out_ch, resolution, num_res_blocks, attn_resolutions, ch, ch_mult=(1,2,4,8), + dropout=0.0, resamp_with_conv=True, rescale_factor=1.0, rescale_module_depth=1): + super().__init__() + tmp_chn = z_channels*ch_mult[-1] + self.decoder = Decoder(out_ch=out_ch, z_channels=tmp_chn, attn_resolutions=attn_resolutions, dropout=dropout, + resamp_with_conv=resamp_with_conv, in_channels=None, num_res_blocks=num_res_blocks, + ch_mult=ch_mult, resolution=resolution, ch=ch) + self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=z_channels, mid_channels=tmp_chn, + out_channels=tmp_chn, depth=rescale_module_depth) + + def forward(self, x): + x = self.rescaler(x) + x = self.decoder(x) + return x + + +class Upsampler(nn.Module): + def __init__(self, in_size, out_size, in_channels, out_channels, ch_mult=2): + super().__init__() + assert out_size >= in_size + num_blocks = int(np.log2(out_size//in_size))+1 + factor_up = 1.+ (out_size % in_size) + print(f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}") + self.rescaler = LatentRescaler(factor=factor_up, in_channels=in_channels, mid_channels=2*in_channels, + out_channels=in_channels) + self.decoder = Decoder(out_ch=out_channels, resolution=out_size, z_channels=in_channels, num_res_blocks=2, + attn_resolutions=[], in_channels=None, ch=in_channels, + ch_mult=[ch_mult for _ in range(num_blocks)]) + + def forward(self, x): + x = self.rescaler(x) + x = self.decoder(x) + return x + + +class Resize(nn.Module): + def __init__(self, in_channels=None, learned=False, mode="bilinear"): + super().__init__() + self.with_conv = learned + self.mode = mode + if self.with_conv: + print(f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode") + raise NotImplementedError() + assert in_channels is not None + # no asymmetric padding in torch conv, must do it ourselves + self.conv = torch.nn.Conv2d(in_channels, + in_channels, + kernel_size=4, + stride=2, + padding=1) + + def forward(self, x, scale_factor=1.0): + if scale_factor==1.0: + return x + else: + x = torch.nn.functional.interpolate(x, mode=self.mode, align_corners=False, scale_factor=scale_factor) + return x + +class FirstStagePostProcessor(nn.Module): + + def __init__(self, ch_mult:list, in_channels, + pretrained_model:nn.Module=None, + reshape=False, + n_channels=None, + dropout=0., + pretrained_config=None): + super().__init__() + if pretrained_config is None: + assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' + self.pretrained_model = pretrained_model + else: + assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' + self.instantiate_pretrained(pretrained_config) + + self.do_reshape = reshape + + if n_channels is None: + n_channels = self.pretrained_model.encoder.ch + + self.proj_norm = Normalize(in_channels,num_groups=in_channels//2) + self.proj = nn.Conv2d(in_channels,n_channels,kernel_size=3, + stride=1,padding=1) + + blocks = [] + downs = [] + ch_in = n_channels + for m in ch_mult: + blocks.append(ResnetBlock(in_channels=ch_in,out_channels=m*n_channels,dropout=dropout)) + ch_in = m * n_channels + downs.append(Downsample(ch_in, with_conv=False)) + + self.model = nn.ModuleList(blocks) + self.downsampler = nn.ModuleList(downs) + + + def instantiate_pretrained(self, config): + model = instantiate_from_config(config) + self.pretrained_model = model.eval() + # self.pretrained_model.train = False + for param in self.pretrained_model.parameters(): + param.requires_grad = False + + + @torch.no_grad() + def encode_with_pretrained(self,x): + c = self.pretrained_model.encode(x) + if isinstance(c, DiagonalGaussianDistribution): + c = c.mode() + return c + + def forward(self,x): + z_fs = self.encode_with_pretrained(x) + z = self.proj_norm(z_fs) + z = self.proj(z) + z = nonlinearity(z) + + for submodel, downmodel in zip(self.model,self.downsampler): + z = submodel(z,temb=None) + z = downmodel(z) + + if self.do_reshape: + z = rearrange(z,'b c h w -> b (h w) c') + return z + diff --git a/VADER-VideoCrafter/lvdm/modules/networks/openaimodel3d.py b/VADER-VideoCrafter/lvdm/modules/networks/openaimodel3d.py new file mode 100644 index 0000000000000000000000000000000000000000..b6882d01be932f949b7ae790917d48f3a152305b --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/networks/openaimodel3d.py @@ -0,0 +1,578 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +from functools import partial +from abc import abstractmethod +import torch +import torch.nn as nn +from einops import rearrange +import torch.nn.functional as F +from lvdm.models.utils_diffusion import timestep_embedding +from lvdm.common import checkpoint +from lvdm.basics import ( + zero_module, + conv_nd, + linear, + avg_pool_nd, + normalization +) +from lvdm.modules.attention import SpatialTransformer, TemporalTransformer + + +class TimestepBlock(nn.Module): + """ + Any module where forward() takes timestep embeddings as a second argument. + """ + @abstractmethod + def forward(self, x, emb): + """ + Apply the module to `x` given `emb` timestep embeddings. + """ + + +class TimestepEmbedSequential(nn.Sequential, TimestepBlock): + """ + A sequential module that passes timestep embeddings to the children that + support it as an extra input. + """ + + def forward(self, x, emb, context=None, batch_size=None): + for layer in self: + if isinstance(layer, TimestepBlock): + x = layer(x, emb, batch_size) + elif isinstance(layer, SpatialTransformer): + x = layer(x, context) + elif isinstance(layer, TemporalTransformer): + x = rearrange(x, '(b f) c h w -> b c f h w', b=batch_size) + x = layer(x, context) + x = rearrange(x, 'b c f h w -> (b f) c h w') + else: + x = layer(x,) + return x + + +class Downsample(nn.Module): + """ + A downsampling layer with an optional convolution. + :param channels: channels in the inputs and outputs. + :param use_conv: a bool determining if a convolution is applied. + :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then + downsampling occurs in the inner-two dimensions. + """ + + def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1): + super().__init__() + self.channels = channels + self.out_channels = out_channels or channels + self.use_conv = use_conv + self.dims = dims + stride = 2 if dims != 3 else (1, 2, 2) + if use_conv: + self.op = conv_nd( + dims, self.channels, self.out_channels, 3, stride=stride, padding=padding + ) + else: + assert self.channels == self.out_channels + self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride) + + def forward(self, x): + assert x.shape[1] == self.channels + return self.op(x) + + +class Upsample(nn.Module): + """ + An upsampling layer with an optional convolution. + :param channels: channels in the inputs and outputs. + :param use_conv: a bool determining if a convolution is applied. + :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then + upsampling occurs in the inner-two dimensions. + """ + + def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1): + super().__init__() + self.channels = channels + self.out_channels = out_channels or channels + self.use_conv = use_conv + self.dims = dims + if use_conv: + self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=padding) + + def forward(self, x): + assert x.shape[1] == self.channels + if self.dims == 3: + x = F.interpolate(x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode='nearest') + else: + x = F.interpolate(x, scale_factor=2, mode='nearest') + if self.use_conv: + x = self.conv(x) + return x + + +class ResBlock(TimestepBlock): + """ + A residual block that can optionally change the number of channels. + :param channels: the number of input channels. + :param emb_channels: the number of timestep embedding channels. + :param dropout: the rate of dropout. + :param out_channels: if specified, the number of out channels. + :param use_conv: if True and out_channels is specified, use a spatial + convolution instead of a smaller 1x1 convolution to change the + channels in the skip connection. + :param dims: determines if the signal is 1D, 2D, or 3D. + :param up: if True, use this block for upsampling. + :param down: if True, use this block for downsampling. + """ + + def __init__( + self, + channels, + emb_channels, + dropout, + out_channels=None, + use_scale_shift_norm=False, + dims=2, + use_checkpoint=False, + use_conv=False, + up=False, + down=False, + use_temporal_conv=False, + tempspatial_aware=False + ): + super().__init__() + self.channels = channels + self.emb_channels = emb_channels + self.dropout = dropout + self.out_channels = out_channels or channels + self.use_conv = use_conv + self.use_checkpoint = use_checkpoint + self.use_scale_shift_norm = use_scale_shift_norm + self.use_temporal_conv = use_temporal_conv + + self.in_layers = nn.Sequential( + normalization(channels), + nn.SiLU(), + conv_nd(dims, channels, self.out_channels, 3, padding=1), + ) + + self.updown = up or down + + if up: + self.h_upd = Upsample(channels, False, dims) + self.x_upd = Upsample(channels, False, dims) + elif down: + self.h_upd = Downsample(channels, False, dims) + self.x_upd = Downsample(channels, False, dims) + else: + self.h_upd = self.x_upd = nn.Identity() + + self.emb_layers = nn.Sequential( + nn.SiLU(), + nn.Linear( + emb_channels, + 2 * self.out_channels if use_scale_shift_norm else self.out_channels, + ), + ) + self.out_layers = nn.Sequential( + normalization(self.out_channels), + nn.SiLU(), + nn.Dropout(p=dropout), + zero_module(nn.Conv2d(self.out_channels, self.out_channels, 3, padding=1)), + ) + + if self.out_channels == channels: + self.skip_connection = nn.Identity() + elif use_conv: + self.skip_connection = conv_nd(dims, channels, self.out_channels, 3, padding=1) + else: + self.skip_connection = conv_nd(dims, channels, self.out_channels, 1) + + if self.use_temporal_conv: + self.temopral_conv = TemporalConvBlock( + self.out_channels, + self.out_channels, + dropout=0.1, + spatial_aware=tempspatial_aware + ) + + def forward(self, x, emb, batch_size=None): + """ + Apply the block to a Tensor, conditioned on a timestep embedding. + :param x: an [N x C x ...] Tensor of features. + :param emb: an [N x emb_channels] Tensor of timestep embeddings. + :return: an [N x C x ...] Tensor of outputs. + """ + input_tuple = (x, emb,) + if batch_size: + forward_batchsize = partial(self._forward, batch_size=batch_size) + return checkpoint(forward_batchsize, input_tuple, self.parameters(), self.use_checkpoint) + return checkpoint(self._forward, input_tuple, self.parameters(), self.use_checkpoint) + + def _forward(self, x, emb, batch_size=None,): + if self.updown: + in_rest, in_conv = self.in_layers[:-1], self.in_layers[-1] + h = in_rest(x) + h = self.h_upd(h) + x = self.x_upd(x) + h = in_conv(h) + else: + h = self.in_layers(x) + emb_out = self.emb_layers(emb).type(h.dtype) + while len(emb_out.shape) < len(h.shape): + emb_out = emb_out[..., None] + if self.use_scale_shift_norm: + out_norm, out_rest = self.out_layers[0], self.out_layers[1:] + scale, shift = torch.chunk(emb_out, 2, dim=1) + h = out_norm(h) * (1 + scale) + shift + h = out_rest(h) + else: + h = h + emb_out + h = self.out_layers(h) + h = self.skip_connection(x) + h + + if self.use_temporal_conv and batch_size: + h = rearrange(h, '(b t) c h w -> b c t h w', b=batch_size) + h = self.temopral_conv(h) + h = rearrange(h, 'b c t h w -> (b t) c h w') + return h + + +class TemporalConvBlock(nn.Module): + """ + Adapted from modelscope: https://github.com/modelscope/modelscope/blob/master/modelscope/models/multi_modal/video_synthesis/unet_sd.py + """ + + def __init__(self, in_channels, out_channels=None, dropout=0.0, spatial_aware=False): + super(TemporalConvBlock, self).__init__() + if out_channels is None: + out_channels = in_channels + self.in_channels = in_channels + self.out_channels = out_channels + kernel_shape = (3, 1, 1) if not spatial_aware else (3, 3, 3) + padding_shape = (1, 0, 0) if not spatial_aware else (1, 1, 1) + + # conv layers + self.conv1 = nn.Sequential( + nn.GroupNorm(32, in_channels), nn.SiLU(), + nn.Conv3d(in_channels, out_channels, kernel_shape, padding=padding_shape)) + self.conv2 = nn.Sequential( + nn.GroupNorm(32, out_channels), nn.SiLU(), nn.Dropout(dropout), + nn.Conv3d(out_channels, in_channels, kernel_shape, padding=padding_shape)) + self.conv3 = nn.Sequential( + nn.GroupNorm(32, out_channels), nn.SiLU(), nn.Dropout(dropout), + nn.Conv3d(out_channels, in_channels, (3, 1, 1), padding=(1, 0, 0))) + self.conv4 = nn.Sequential( + nn.GroupNorm(32, out_channels), nn.SiLU(), nn.Dropout(dropout), + nn.Conv3d(out_channels, in_channels, (3, 1, 1), padding=(1, 0, 0))) + + # zero out the last layer params,so the conv block is identity + nn.init.zeros_(self.conv4[-1].weight) + nn.init.zeros_(self.conv4[-1].bias) + + def forward(self, x): + identity = x + x = self.conv1(x) + x = self.conv2(x) + x = self.conv3(x) + x = self.conv4(x) + + return x + identity + + +class UNetModel(nn.Module): + """ + The full UNet model with attention and timestep embedding. + :param in_channels: in_channels in the input Tensor. + :param model_channels: base channel count for the model. + :param out_channels: channels in the output Tensor. + :param num_res_blocks: number of residual blocks per downsample. + :param attention_resolutions: a collection of downsample rates at which + attention will take place. May be a set, list, or tuple. + For example, if this contains 4, then at 4x downsampling, attention + will be used. + :param dropout: the dropout probability. + :param channel_mult: channel multiplier for each level of the UNet. + :param conv_resample: if True, use learned convolutions for upsampling and + downsampling. + :param dims: determines if the signal is 1D, 2D, or 3D. + :param num_classes: if specified (as an int), then this model will be + class-conditional with `num_classes` classes. + :param use_checkpoint: use gradient checkpointing to reduce memory usage. + :param num_heads: the number of attention heads in each attention layer. + :param num_heads_channels: if specified, ignore num_heads and instead use + a fixed channel width per attention head. + :param num_heads_upsample: works with num_heads to set a different number + of heads for upsampling. Deprecated. + :param use_scale_shift_norm: use a FiLM-like conditioning mechanism. + :param resblock_updown: use residual blocks for up/downsampling. + """ + + def __init__(self, + in_channels, + model_channels, + out_channels, + num_res_blocks, + attention_resolutions, + dropout=0.0, + channel_mult=(1, 2, 4, 8), + conv_resample=True, + dims=2, + context_dim=None, + use_scale_shift_norm=False, + resblock_updown=False, + num_heads=-1, + num_head_channels=-1, + transformer_depth=1, + use_linear=False, + use_checkpoint=False, + temporal_conv=False, + tempspatial_aware=False, + temporal_attention=True, + temporal_selfatt_only=True, + use_relative_position=True, + use_causal_attention=False, + temporal_length=None, + use_fp16=False, + addition_attention=False, + use_image_attention=False, + temporal_transformer_depth=1, + fps_cond=False, + ): + super(UNetModel, self).__init__() + if num_heads == -1: + assert num_head_channels != -1, 'Either num_heads or num_head_channels has to be set' + if num_head_channels == -1: + assert num_heads != -1, 'Either num_heads or num_head_channels has to be set' + + self.in_channels = in_channels + self.model_channels = model_channels + self.out_channels = out_channels + self.num_res_blocks = num_res_blocks + self.attention_resolutions = attention_resolutions + self.dropout = dropout + self.channel_mult = channel_mult + self.conv_resample = conv_resample + self.temporal_attention = temporal_attention + time_embed_dim = model_channels * 4 + self.use_checkpoint = use_checkpoint + self.dtype = torch.float16 if use_fp16 else torch.float32 + self.addition_attention=addition_attention + self.use_image_attention = use_image_attention + self.fps_cond=fps_cond + + + + self.time_embed = nn.Sequential( + linear(model_channels, time_embed_dim), + nn.SiLU(), + linear(time_embed_dim, time_embed_dim), + ) + if self.fps_cond: + self.fps_embedding = nn.Sequential( + linear(model_channels, time_embed_dim), + nn.SiLU(), + linear(time_embed_dim, time_embed_dim), + ) + + self.input_blocks = nn.ModuleList( + [ + TimestepEmbedSequential(conv_nd(dims, in_channels, model_channels, 3, padding=1)) + ] + ) + if self.addition_attention: + self.init_attn=TimestepEmbedSequential( + TemporalTransformer( + model_channels, + n_heads=8, + d_head=num_head_channels, + depth=transformer_depth, + context_dim=context_dim, + use_checkpoint=use_checkpoint, only_self_att=temporal_selfatt_only, + causal_attention=use_causal_attention, relative_position=use_relative_position, + temporal_length=temporal_length)) + + input_block_chans = [model_channels] + ch = model_channels + ds = 1 + for level, mult in enumerate(channel_mult): + for _ in range(num_res_blocks): + layers = [ + ResBlock(ch, time_embed_dim, dropout, + out_channels=mult * model_channels, dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, tempspatial_aware=tempspatial_aware, + use_temporal_conv=temporal_conv + ) + ] + ch = mult * model_channels + if ds in attention_resolutions: + if num_head_channels == -1: + dim_head = ch // num_heads + else: + num_heads = ch // num_head_channels + dim_head = num_head_channels + layers.append( + SpatialTransformer(ch, num_heads, dim_head, + depth=transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, disable_self_attn=False, + img_cross_attention=self.use_image_attention + ) + ) + if self.temporal_attention: + layers.append( + TemporalTransformer(ch, num_heads, dim_head, + depth=temporal_transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, only_self_att=temporal_selfatt_only, + causal_attention=use_causal_attention, relative_position=use_relative_position, + temporal_length=temporal_length + ) + ) + self.input_blocks.append(TimestepEmbedSequential(*layers)) + input_block_chans.append(ch) + if level != len(channel_mult) - 1: + out_ch = ch + self.input_blocks.append( + TimestepEmbedSequential( + ResBlock(ch, time_embed_dim, dropout, + out_channels=out_ch, dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, + down=True + ) + if resblock_updown + else Downsample(ch, conv_resample, dims=dims, out_channels=out_ch) + ) + ) + ch = out_ch + input_block_chans.append(ch) + ds *= 2 + + if num_head_channels == -1: + dim_head = ch // num_heads + else: + num_heads = ch // num_head_channels + dim_head = num_head_channels + layers = [ + ResBlock(ch, time_embed_dim, dropout, + dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, tempspatial_aware=tempspatial_aware, + use_temporal_conv=temporal_conv + ), + SpatialTransformer(ch, num_heads, dim_head, + depth=transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, disable_self_attn=False, + img_cross_attention=self.use_image_attention + ) + ] + if self.temporal_attention: + layers.append( + TemporalTransformer(ch, num_heads, dim_head, + depth=temporal_transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, only_self_att=temporal_selfatt_only, + causal_attention=use_causal_attention, relative_position=use_relative_position, + temporal_length=temporal_length + ) + ) + layers.append( + ResBlock(ch, time_embed_dim, dropout, + dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, tempspatial_aware=tempspatial_aware, + use_temporal_conv=temporal_conv + ) + ) + self.middle_block = TimestepEmbedSequential(*layers) + + self.output_blocks = nn.ModuleList([]) + for level, mult in list(enumerate(channel_mult))[::-1]: + for i in range(num_res_blocks + 1): + ich = input_block_chans.pop() + layers = [ + ResBlock(ch + ich, time_embed_dim, dropout, + out_channels=mult * model_channels, dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, tempspatial_aware=tempspatial_aware, + use_temporal_conv=temporal_conv + ) + ] + ch = model_channels * mult + if ds in attention_resolutions: + if num_head_channels == -1: + dim_head = ch // num_heads + else: + num_heads = ch // num_head_channels + dim_head = num_head_channels + layers.append( + SpatialTransformer(ch, num_heads, dim_head, + depth=transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, disable_self_attn=False, + img_cross_attention=self.use_image_attention + ) + ) + if self.temporal_attention: + layers.append( + TemporalTransformer(ch, num_heads, dim_head, + depth=temporal_transformer_depth, context_dim=context_dim, use_linear=use_linear, + use_checkpoint=use_checkpoint, only_self_att=temporal_selfatt_only, + causal_attention=use_causal_attention, relative_position=use_relative_position, + temporal_length=temporal_length + ) + ) + if level and i == num_res_blocks: + out_ch = ch + layers.append( + ResBlock(ch, time_embed_dim, dropout, + out_channels=out_ch, dims=dims, use_checkpoint=use_checkpoint, + use_scale_shift_norm=use_scale_shift_norm, + up=True + ) + if resblock_updown + else Upsample(ch, conv_resample, dims=dims, out_channels=out_ch) + ) + ds //= 2 + self.output_blocks.append(TimestepEmbedSequential(*layers)) + + self.out = nn.Sequential( + normalization(ch), + nn.SiLU(), + zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)), + ) + + def forward(self, x, timesteps, context=None, features_adapter=None, fps=16, **kwargs): + t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False) + emb = self.time_embed(t_emb) + + if self.fps_cond: + if type(fps) == int: + fps = torch.full_like(timesteps, fps) + fps_emb = timestep_embedding(fps,self.model_channels, repeat_only=False) + emb += self.fps_embedding(fps_emb) + + b,_,t,_,_ = x.shape + ## repeat t times for context [(b t) 77 768] & time embedding + context = context.repeat_interleave(repeats=t, dim=0) + emb = emb.repeat_interleave(repeats=t, dim=0) + + ## always in shape (b t) c h w, except for temporal layer + x = rearrange(x, 'b c t h w -> (b t) c h w') + + h = x.type(self.dtype) + adapter_idx = 0 + hs = [] + for id, module in enumerate(self.input_blocks): + h = module(h, emb, context=context, batch_size=b) + if id ==0 and self.addition_attention: + h = self.init_attn(h, emb, context=context, batch_size=b) + ## plug-in adapter features + if ((id+1)%3 == 0) and features_adapter is not None: + h = h + features_adapter[adapter_idx] + adapter_idx += 1 + hs.append(h) + if features_adapter is not None: + assert len(features_adapter)==adapter_idx, 'Wrong features_adapter' + + h = self.middle_block(h, emb, context=context, batch_size=b) + for module in self.output_blocks: + h = torch.cat([h, hs.pop()], dim=1) + h = module(h, emb, context=context, batch_size=b) + h = h.type(x.dtype) + y = self.out(h) + + # reshape back to (b c t h w) + y = rearrange(y, '(b t) c h w -> b c t h w', b=b) + return y + \ No newline at end of file diff --git a/VADER-VideoCrafter/lvdm/modules/x_transformer.py b/VADER-VideoCrafter/lvdm/modules/x_transformer.py new file mode 100644 index 0000000000000000000000000000000000000000..f252ab4032a78407ed487495807940c4ba802ffa --- /dev/null +++ b/VADER-VideoCrafter/lvdm/modules/x_transformer.py @@ -0,0 +1,640 @@ +"""shout-out to https://github.com/lucidrains/x-transformers/tree/main/x_transformers""" +from functools import partial +from inspect import isfunction +from collections import namedtuple +from einops import rearrange, repeat +import torch +from torch import nn, einsum +import torch.nn.functional as F + +# constants +DEFAULT_DIM_HEAD = 64 + +Intermediates = namedtuple('Intermediates', [ + 'pre_softmax_attn', + 'post_softmax_attn' +]) + +LayerIntermediates = namedtuple('Intermediates', [ + 'hiddens', + 'attn_intermediates' +]) + + +class AbsolutePositionalEmbedding(nn.Module): + def __init__(self, dim, max_seq_len): + super().__init__() + self.emb = nn.Embedding(max_seq_len, dim) + self.init_() + + def init_(self): + nn.init.normal_(self.emb.weight, std=0.02) + + def forward(self, x): + n = torch.arange(x.shape[1], device=x.device) + return self.emb(n)[None, :, :] + + +class FixedPositionalEmbedding(nn.Module): + def __init__(self, dim): + super().__init__() + inv_freq = 1. / (10000 ** (torch.arange(0, dim, 2).float() / dim)) + self.register_buffer('inv_freq', inv_freq) + + def forward(self, x, seq_dim=1, offset=0): + t = torch.arange(x.shape[seq_dim], device=x.device).type_as(self.inv_freq) + offset + sinusoid_inp = torch.einsum('i , j -> i j', t, self.inv_freq) + emb = torch.cat((sinusoid_inp.sin(), sinusoid_inp.cos()), dim=-1) + return emb[None, :, :] + + +# helpers + +def exists(val): + return val is not None + + +def default(val, d): + if exists(val): + return val + return d() if isfunction(d) else d + + +def always(val): + def inner(*args, **kwargs): + return val + return inner + + +def not_equals(val): + def inner(x): + return x != val + return inner + + +def equals(val): + def inner(x): + return x == val + return inner + + +def max_neg_value(tensor): + return -torch.finfo(tensor.dtype).max + + +# keyword argument helpers + +def pick_and_pop(keys, d): + values = list(map(lambda key: d.pop(key), keys)) + return dict(zip(keys, values)) + + +def group_dict_by_key(cond, d): + return_val = [dict(), dict()] + for key in d.keys(): + match = bool(cond(key)) + ind = int(not match) + return_val[ind][key] = d[key] + return (*return_val,) + + +def string_begins_with(prefix, str): + return str.startswith(prefix) + + +def group_by_key_prefix(prefix, d): + return group_dict_by_key(partial(string_begins_with, prefix), d) + + +def groupby_prefix_and_trim(prefix, d): + kwargs_with_prefix, kwargs = group_dict_by_key(partial(string_begins_with, prefix), d) + kwargs_without_prefix = dict(map(lambda x: (x[0][len(prefix):], x[1]), tuple(kwargs_with_prefix.items()))) + return kwargs_without_prefix, kwargs + + +# classes +class Scale(nn.Module): + def __init__(self, value, fn): + super().__init__() + self.value = value + self.fn = fn + + def forward(self, x, **kwargs): + x, *rest = self.fn(x, **kwargs) + return (x * self.value, *rest) + + +class Rezero(nn.Module): + def __init__(self, fn): + super().__init__() + self.fn = fn + self.g = nn.Parameter(torch.zeros(1)) + + def forward(self, x, **kwargs): + x, *rest = self.fn(x, **kwargs) + return (x * self.g, *rest) + + +class ScaleNorm(nn.Module): + def __init__(self, dim, eps=1e-5): + super().__init__() + self.scale = dim ** -0.5 + self.eps = eps + self.g = nn.Parameter(torch.ones(1)) + + def forward(self, x): + norm = torch.norm(x, dim=-1, keepdim=True) * self.scale + return x / norm.clamp(min=self.eps) * self.g + + +class RMSNorm(nn.Module): + def __init__(self, dim, eps=1e-8): + super().__init__() + self.scale = dim ** -0.5 + self.eps = eps + self.g = nn.Parameter(torch.ones(dim)) + + def forward(self, x): + norm = torch.norm(x, dim=-1, keepdim=True) * self.scale + return x / norm.clamp(min=self.eps) * self.g + + +class Residual(nn.Module): + def forward(self, x, residual): + return x + residual + + +class GRUGating(nn.Module): + def __init__(self, dim): + super().__init__() + self.gru = nn.GRUCell(dim, dim) + + def forward(self, x, residual): + gated_output = self.gru( + rearrange(x, 'b n d -> (b n) d'), + rearrange(residual, 'b n d -> (b n) d') + ) + + return gated_output.reshape_as(x) + + +# feedforward + +class GEGLU(nn.Module): + def __init__(self, dim_in, dim_out): + super().__init__() + self.proj = nn.Linear(dim_in, dim_out * 2) + + def forward(self, x): + x, gate = self.proj(x).chunk(2, dim=-1) + return x * F.gelu(gate) + + +class FeedForward(nn.Module): + def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.): + super().__init__() + inner_dim = int(dim * mult) + dim_out = default(dim_out, dim) + project_in = nn.Sequential( + nn.Linear(dim, inner_dim), + nn.GELU() + ) if not glu else GEGLU(dim, inner_dim) + + self.net = nn.Sequential( + project_in, + nn.Dropout(dropout), + nn.Linear(inner_dim, dim_out) + ) + + def forward(self, x): + return self.net(x) + + +# attention. +class Attention(nn.Module): + def __init__( + self, + dim, + dim_head=DEFAULT_DIM_HEAD, + heads=8, + causal=False, + mask=None, + talking_heads=False, + sparse_topk=None, + use_entmax15=False, + num_mem_kv=0, + dropout=0., + on_attn=False + ): + super().__init__() + if use_entmax15: + raise NotImplementedError("Check out entmax activation instead of softmax activation!") + self.scale = dim_head ** -0.5 + self.heads = heads + self.causal = causal + self.mask = mask + + inner_dim = dim_head * heads + + self.to_q = nn.Linear(dim, inner_dim, bias=False) + self.to_k = nn.Linear(dim, inner_dim, bias=False) + self.to_v = nn.Linear(dim, inner_dim, bias=False) + self.dropout = nn.Dropout(dropout) + + # talking heads + self.talking_heads = talking_heads + if talking_heads: + self.pre_softmax_proj = nn.Parameter(torch.randn(heads, heads)) + self.post_softmax_proj = nn.Parameter(torch.randn(heads, heads)) + + # explicit topk sparse attention + self.sparse_topk = sparse_topk + + # entmax + #self.attn_fn = entmax15 if use_entmax15 else F.softmax + self.attn_fn = F.softmax + + # add memory key / values + self.num_mem_kv = num_mem_kv + if num_mem_kv > 0: + self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) + self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) + + # attention on attention + self.attn_on_attn = on_attn + self.to_out = nn.Sequential(nn.Linear(inner_dim, dim * 2), nn.GLU()) if on_attn else nn.Linear(inner_dim, dim) + + def forward( + self, + x, + context=None, + mask=None, + context_mask=None, + rel_pos=None, + sinusoidal_emb=None, + prev_attn=None, + mem=None + ): + b, n, _, h, talking_heads, device = *x.shape, self.heads, self.talking_heads, x.device + kv_input = default(context, x) + + q_input = x + k_input = kv_input + v_input = kv_input + + if exists(mem): + k_input = torch.cat((mem, k_input), dim=-2) + v_input = torch.cat((mem, v_input), dim=-2) + + if exists(sinusoidal_emb): + # in shortformer, the query would start at a position offset depending on the past cached memory + offset = k_input.shape[-2] - q_input.shape[-2] + q_input = q_input + sinusoidal_emb(q_input, offset=offset) + k_input = k_input + sinusoidal_emb(k_input) + + q = self.to_q(q_input) + k = self.to_k(k_input) + v = self.to_v(v_input) + + q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=h), (q, k, v)) + + input_mask = None + if any(map(exists, (mask, context_mask))): + q_mask = default(mask, lambda: torch.ones((b, n), device=device).bool()) + k_mask = q_mask if not exists(context) else context_mask + k_mask = default(k_mask, lambda: torch.ones((b, k.shape[-2]), device=device).bool()) + q_mask = rearrange(q_mask, 'b i -> b () i ()') + k_mask = rearrange(k_mask, 'b j -> b () () j') + input_mask = q_mask * k_mask + + if self.num_mem_kv > 0: + mem_k, mem_v = map(lambda t: repeat(t, 'h n d -> b h n d', b=b), (self.mem_k, self.mem_v)) + k = torch.cat((mem_k, k), dim=-2) + v = torch.cat((mem_v, v), dim=-2) + if exists(input_mask): + input_mask = F.pad(input_mask, (self.num_mem_kv, 0), value=True) + + dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale + mask_value = max_neg_value(dots) + + if exists(prev_attn): + dots = dots + prev_attn + + pre_softmax_attn = dots + + if talking_heads: + dots = einsum('b h i j, h k -> b k i j', dots, self.pre_softmax_proj).contiguous() + + if exists(rel_pos): + dots = rel_pos(dots) + + if exists(input_mask): + dots.masked_fill_(~input_mask, mask_value) + del input_mask + + if self.causal: + i, j = dots.shape[-2:] + r = torch.arange(i, device=device) + mask = rearrange(r, 'i -> () () i ()') < rearrange(r, 'j -> () () () j') + mask = F.pad(mask, (j - i, 0), value=False) + dots.masked_fill_(mask, mask_value) + del mask + + if exists(self.sparse_topk) and self.sparse_topk < dots.shape[-1]: + top, _ = dots.topk(self.sparse_topk, dim=-1) + vk = top[..., -1].unsqueeze(-1).expand_as(dots) + mask = dots < vk + dots.masked_fill_(mask, mask_value) + del mask + + attn = self.attn_fn(dots, dim=-1) + post_softmax_attn = attn + + attn = self.dropout(attn) + + if talking_heads: + attn = einsum('b h i j, h k -> b k i j', attn, self.post_softmax_proj).contiguous() + + out = einsum('b h i j, b h j d -> b h i d', attn, v) + out = rearrange(out, 'b h n d -> b n (h d)') + + intermediates = Intermediates( + pre_softmax_attn=pre_softmax_attn, + post_softmax_attn=post_softmax_attn + ) + + return self.to_out(out), intermediates + + +class AttentionLayers(nn.Module): + def __init__( + self, + dim, + depth, + heads=8, + causal=False, + cross_attend=False, + only_cross=False, + use_scalenorm=False, + use_rmsnorm=False, + use_rezero=False, + rel_pos_num_buckets=32, + rel_pos_max_distance=128, + position_infused_attn=False, + custom_layers=None, + sandwich_coef=None, + par_ratio=None, + residual_attn=False, + cross_residual_attn=False, + macaron=False, + pre_norm=True, + gate_residual=False, + **kwargs + ): + super().__init__() + ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) + attn_kwargs, _ = groupby_prefix_and_trim('attn_', kwargs) + + dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) + + self.dim = dim + self.depth = depth + self.layers = nn.ModuleList([]) + + self.has_pos_emb = position_infused_attn + self.pia_pos_emb = FixedPositionalEmbedding(dim) if position_infused_attn else None + self.rotary_pos_emb = always(None) + + assert rel_pos_num_buckets <= rel_pos_max_distance, 'number of relative position buckets must be less than the relative position max distance' + self.rel_pos = None + + self.pre_norm = pre_norm + + self.residual_attn = residual_attn + self.cross_residual_attn = cross_residual_attn + + norm_class = ScaleNorm if use_scalenorm else nn.LayerNorm + norm_class = RMSNorm if use_rmsnorm else norm_class + norm_fn = partial(norm_class, dim) + + norm_fn = nn.Identity if use_rezero else norm_fn + branch_fn = Rezero if use_rezero else None + + if cross_attend and not only_cross: + default_block = ('a', 'c', 'f') + elif cross_attend and only_cross: + default_block = ('c', 'f') + else: + default_block = ('a', 'f') + + if macaron: + default_block = ('f',) + default_block + + if exists(custom_layers): + layer_types = custom_layers + elif exists(par_ratio): + par_depth = depth * len(default_block) + assert 1 < par_ratio <= par_depth, 'par ratio out of range' + default_block = tuple(filter(not_equals('f'), default_block)) + par_attn = par_depth // par_ratio + depth_cut = par_depth * 2 // 3 # 2 / 3 attention layer cutoff suggested by PAR paper + par_width = (depth_cut + depth_cut // par_attn) // par_attn + assert len(default_block) <= par_width, 'default block is too large for par_ratio' + par_block = default_block + ('f',) * (par_width - len(default_block)) + par_head = par_block * par_attn + layer_types = par_head + ('f',) * (par_depth - len(par_head)) + elif exists(sandwich_coef): + assert sandwich_coef > 0 and sandwich_coef <= depth, 'sandwich coefficient should be less than the depth' + layer_types = ('a',) * sandwich_coef + default_block * (depth - sandwich_coef) + ('f',) * sandwich_coef + else: + layer_types = default_block * depth + + self.layer_types = layer_types + self.num_attn_layers = len(list(filter(equals('a'), layer_types))) + + for layer_type in self.layer_types: + if layer_type == 'a': + layer = Attention(dim, heads=heads, causal=causal, **attn_kwargs) + elif layer_type == 'c': + layer = Attention(dim, heads=heads, **attn_kwargs) + elif layer_type == 'f': + layer = FeedForward(dim, **ff_kwargs) + layer = layer if not macaron else Scale(0.5, layer) + else: + raise Exception(f'invalid layer type {layer_type}') + + if isinstance(layer, Attention) and exists(branch_fn): + layer = branch_fn(layer) + + if gate_residual: + residual_fn = GRUGating(dim) + else: + residual_fn = Residual() + + self.layers.append(nn.ModuleList([ + norm_fn(), + layer, + residual_fn + ])) + + def forward( + self, + x, + context=None, + mask=None, + context_mask=None, + mems=None, + return_hiddens=False + ): + hiddens = [] + intermediates = [] + prev_attn = None + prev_cross_attn = None + + mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers + + for ind, (layer_type, (norm, block, residual_fn)) in enumerate(zip(self.layer_types, self.layers)): + is_last = ind == (len(self.layers) - 1) + + if layer_type == 'a': + hiddens.append(x) + layer_mem = mems.pop(0) + + residual = x + + if self.pre_norm: + x = norm(x) + + if layer_type == 'a': + out, inter = block(x, mask=mask, sinusoidal_emb=self.pia_pos_emb, rel_pos=self.rel_pos, + prev_attn=prev_attn, mem=layer_mem) + elif layer_type == 'c': + out, inter = block(x, context=context, mask=mask, context_mask=context_mask, prev_attn=prev_cross_attn) + elif layer_type == 'f': + out = block(x) + + x = residual_fn(out, residual) + + if layer_type in ('a', 'c'): + intermediates.append(inter) + + if layer_type == 'a' and self.residual_attn: + prev_attn = inter.pre_softmax_attn + elif layer_type == 'c' and self.cross_residual_attn: + prev_cross_attn = inter.pre_softmax_attn + + if not self.pre_norm and not is_last: + x = norm(x) + + if return_hiddens: + intermediates = LayerIntermediates( + hiddens=hiddens, + attn_intermediates=intermediates + ) + + return x, intermediates + + return x + + +class Encoder(AttentionLayers): + def __init__(self, **kwargs): + assert 'causal' not in kwargs, 'cannot set causality on encoder' + super().__init__(causal=False, **kwargs) + + + +class TransformerWrapper(nn.Module): + def __init__( + self, + *, + num_tokens, + max_seq_len, + attn_layers, + emb_dim=None, + max_mem_len=0., + emb_dropout=0., + num_memory_tokens=None, + tie_embedding=False, + use_pos_emb=True + ): + super().__init__() + assert isinstance(attn_layers, AttentionLayers), 'attention layers must be one of Encoder or Decoder' + + dim = attn_layers.dim + emb_dim = default(emb_dim, dim) + + self.max_seq_len = max_seq_len + self.max_mem_len = max_mem_len + self.num_tokens = num_tokens + + self.token_emb = nn.Embedding(num_tokens, emb_dim) + self.pos_emb = AbsolutePositionalEmbedding(emb_dim, max_seq_len) if ( + use_pos_emb and not attn_layers.has_pos_emb) else always(0) + self.emb_dropout = nn.Dropout(emb_dropout) + + self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() + self.attn_layers = attn_layers + self.norm = nn.LayerNorm(dim) + + self.init_() + + self.to_logits = nn.Linear(dim, num_tokens) if not tie_embedding else lambda t: t @ self.token_emb.weight.t() + + # memory tokens (like [cls]) from Memory Transformers paper + num_memory_tokens = default(num_memory_tokens, 0) + self.num_memory_tokens = num_memory_tokens + if num_memory_tokens > 0: + self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim)) + + # let funnel encoder know number of memory tokens, if specified + if hasattr(attn_layers, 'num_memory_tokens'): + attn_layers.num_memory_tokens = num_memory_tokens + + def init_(self): + nn.init.normal_(self.token_emb.weight, std=0.02) + + def forward( + self, + x, + return_embeddings=False, + mask=None, + return_mems=False, + return_attn=False, + mems=None, + **kwargs + ): + b, n, device, num_mem = *x.shape, x.device, self.num_memory_tokens + x = self.token_emb(x) + x += self.pos_emb(x) + x = self.emb_dropout(x) + + x = self.project_emb(x) + + if num_mem > 0: + mem = repeat(self.memory_tokens, 'n d -> b n d', b=b) + x = torch.cat((mem, x), dim=1) + + # auto-handle masking after appending memory tokens + if exists(mask): + mask = F.pad(mask, (num_mem, 0), value=True) + + x, intermediates = self.attn_layers(x, mask=mask, mems=mems, return_hiddens=True, **kwargs) + x = self.norm(x) + + mem, x = x[:, :num_mem], x[:, num_mem:] + + out = self.to_logits(x) if not return_embeddings else x + + if return_mems: + hiddens = intermediates.hiddens + new_mems = list(map(lambda pair: torch.cat(pair, dim=-2), zip(mems, hiddens))) if exists(mems) else hiddens + new_mems = list(map(lambda t: t[..., -self.max_mem_len:, :].detach(), new_mems)) + return out, new_mems + + if return_attn: + attn_maps = list(map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)) + return out, attn_maps + + return out + diff --git a/VADER-VideoCrafter/readme.md b/VADER-VideoCrafter/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..33ea67e1979971c67cbfdbd7e3c54fa6b889f2c3 --- /dev/null +++ b/VADER-VideoCrafter/readme.md @@ -0,0 +1,138 @@ +
+ + +# 🌟**VADER-VideoCrafter** +
+ + + +We **highly recommend** proceeding with the VADER-VideoCrafter model first, which performs better than the other two. + +## ⚙️ Installation +Assuming you are in the `VADER/` directory, you are able to create a Conda environments for VADER-VideoCrafter using the following commands: +```bash +cd VADER-VideoCrafter +conda create -n vader_videocrafter python=3.10 +conda activate vader_videocrafter +conda install pytorch==2.3.0 torchvision==0.18.0 torchaudio==2.3.0 pytorch-cuda=12.1 -c pytorch -c nvidia +conda install xformers -c xformers +pip install -r requirements.txt +git clone https://github.com/tgxs002/HPSv2.git +cd HPSv2/ +pip install -e . +cd .. +``` + + +- We are using the pretrained Text-to-Video [VideoCrafter2](https://huggingface.co/VideoCrafter/VideoCrafter2/blob/main/model.ckpt) model via Hugging Face. If you unfortunately find the model is not automatically downloaded when you running inference or training script, you can manually download it and put the `model.ckpt` in `VADER/VADER-VideoCrafter/checkpoints/base_512_v2/model.ckpt`. + + +## 📺 Inference +Please run `accelerate config` as the first step to configure accelerator settings. If you are not familiar with the accelerator configuration, you can refer to VADER-VideoCrafter [documentation](../documentation/VADER-VideoCrafter.md). + +Assuming you are in the `VADER/` directory, you are able to do inference using the following commands: +```bash +cd VADER-VideoCrafter +sh scripts/run_text2video_inference.sh +``` +- We have tested on PyTorch 2.3.0 and CUDA 12.1. The inferece script works on a single GPU with 16GBs VRAM, when we set `val_batch_size=1` and use `fp16` mixed precision. It should also work with recent PyTorch and CUDA versions. +- `VADER/VADER-VideoCrafter/scripts/main/train_t2v_lora.py` is a script for inference of the VideoCrafter2 using VADER via LoRA. + - Most of the arguments are the same as the training process. The main difference is that `--inference_only` should be set to `True`. + - `--lora_ckpt_path` is required to set to the path of the pretrained LoRA model. Otherwise, the original VideoCrafter model will be used for inference. + +## 🔧 Training +Please run `accelerate config` as the first step to configure accelerator settings. If you are not familiar with the accelerator configuration, you can refer to VADER-VideoCrafter [documentation](../documentation/VADER-VideoCrafter.md). + +Assuming you are in the `VADER/` directory, you are able to train the model using the following commands: + +```bash +cd VADER-VideoCrafter +sh scripts/run_text2video_train.sh +``` +- Our experiments are conducted on PyTorch 2.3.0 and CUDA 12.1 while using 4 A6000s (48GB RAM). It should also work with recent PyTorch and CUDA versions. The training script have been tested on a single GPU with 16GBs VRAM, when we set `train_batch_size=1 val_batch_size=1` and use `fp16` mixed precision. +- `VADER/VADER-VideoCrafter/scripts/main/train_t2v_lora.py` is also a script for fine-tuning the VideoCrafter2 using VADER via LoRA. + - You can read the VADER-VideoCrafter [documentation](../documentation/VADER-VideoCrafter.md) to understand the usage of arguments. + +## 💡 Tutorial +This section is to provide a tutorial on how to implement the VADER method on VideoCrafter by yourself. We will provide a step-by-step guide to help you understand the implementation details. Thus, you can easily adapt the VADER method to later versions of VideCrafter. This tutorial is based on the VideoCrafter2. + +### Step 1: Install the dependencies +First, you need to install the dependencies according to the [VideoCrafter](https://github.com/AILab-CVC/VideoCrafter) repository. You can also follow the instructions in the repository to install the dependencies. +```bash +conda create -n vader_videocrafter python=3.8.5 +conda activate vader_videocrafter +pip install -r requirements.txt +``` + +You have to download pretrained Text-to-Video [VideoCrafter2](https://huggingface.co/VideoCrafter/VideoCrafter2/blob/main/model.ckpt) model via Hugging Face, and put the `model.ckpt` in the downloaded VideoCrafter directionary as `VideoCrafter/checkpoints/base_512_v2/model.ckpt`. + +There are a list of extra dependencies that you need to install for VADER. You can install them by running the following command. +```bash +# Install the HPS +git clone https://github.com/tgxs002/HPSv2.git +cd HPSv2/ +pip install -e . +cd .. + +# Install the dependencies +pip install albumentations \ +peft \ +bitsandbytes \ +accelerate \ +inflect \ +wandb \ +ipdb \ +pytorch_lightning +``` + +### Step 2: Transfer VADER scripts +You can copy our `VADER/VADER-VideoCrafter/scripts/main/train_t2v_lora.py` to the `VideoCrafter/scripts/evaluation/` directory of VideoCrafter. It is better to copy our `run_text2video_train.sh` and `run_text2video_inference.sh` to the directionary `VideoCrafter/scripts/` as well. Then, you need to copy All the files in `VADER/Core/` and `VADER/assets/` to the parent directory of VideoCrafter, which means `Core/`, `assets` and `VideoCrafter/` should be in the same directory. Now, you may have a directory structure like: +```bash +. +├── Core +│ ├── ... +├── VideoCrafter +│ ├── scripts +│ │ ├── evaluation +│ │ │ ├── train_t2v_lora.py +│ │ ├── run_text2video_train.sh +│ │ ├── run_text2video_inference.sh +│ ├── checkpoints +│ │ ├── base_512_v2 +│ │ │ ├── model.ckpt +├── assets +│ ├── ... +``` + +### Step 3: Modify the VideoCrafter code +You need to modify the VideoCrafter code to adapt the VADER method. You can follow the instructions below to modify the code. + +- Modify the `batch_ddim_sampling()` function in `VideoCrafter/scripts/evaluation/funcs.py` as our implementation in `VADER/VADER-VideoCrafter/scripts/main/funcs.py`. +- Modify the `DDIMSampler.__init__()`, `DDIMSampler.sample()` and `DDIMSampler.ddim_sampling` functions in `VideoCrafter\lvdm\models\samplers\ddim.py` as our implementation in `VADER/VADER-VideoCrafter\lvdm\models\samplers\ddim.py`. +- Comment out the `@torch.no_grad()` before `DDIMSampler.sample()`, `DDIMSampler.ddim_sampling`, and `DDIMSampler.p_sample_ddim()` in `VideoCrafter\lvdm\models\samplers\ddim.py`. Also, comment out the `@torch.no_grad()` before `LatentDiffusion.decode_first_stage_2DAE()` in `VideoCrafter\lvdm\models\ddpm3d.py`. +- Because we have commented out the `@torch.no_grad()`, you can add `with torch.no_grad():` at some places in `VideoCrater/scripts/evaluation/inference.py` to avoid the gradient calculation. + +### Step 4: Ready to Train +Now you have all the files in the right place and modified the VideoCrafter source code. You can run the training script by running the following command. +```bash +cd VideoCrafter + +# training +sh scripts/run_text2video_train.sh + +# or inference +sh scripts/run_text2video_inference.sh +``` + + +## Acknowledgement + +Our codebase is directly built on top of [VideoCrafter](https://github.com/AILab-CVC/VideoCrafter), [Open-Sora](https://github.com/hpcaitech/Open-Sora), and [Animate Anything](https://github.com/alibaba/animate-anything/). We would like to thank the authors for open-sourcing their code. + +## Citation + +If you find this work useful in your research, please cite: + +```bibtex + +``` diff --git a/VADER-VideoCrafter/requirements.txt b/VADER-VideoCrafter/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..0f89a58109182c6b921a60671336fd9aa1f0750e --- /dev/null +++ b/VADER-VideoCrafter/requirements.txt @@ -0,0 +1,28 @@ +decord==0.6.0 +einops==0.3.0 +imageio==2.9.0 +numpy==1.24.2 +omegaconf==2.1.1 +opencv_python +pandas==2.0.0 +Pillow==9.5.0 +pytorch_lightning==2.3.1 +PyYAML==6.0 +setuptools==65.6.3 +tqdm==4.65.0 +transformers==4.25.1 +moviepy==1.0.3 +av==12.2.0 +gradio +timm==1.0.7 +scikit-learn==1.5.0 +open_clip_torch==2.22.0 +kornia==0.7.3 +albumentations==1.3.1 +peft==0.11.1 +bitsandbytes==0.42.0 +accelerate==0.31.0 +inflect==7.3.0 +wandb==0.17.3 +ipdb==0.13.13 +huggingface-hub==0.23.4 \ No newline at end of file diff --git a/VADER-VideoCrafter/scripts/main/ddp_wrapper.py b/VADER-VideoCrafter/scripts/main/ddp_wrapper.py new file mode 100644 index 0000000000000000000000000000000000000000..67cf97522b1f56e7a20d476373d99ca02b6e5dca --- /dev/null +++ b/VADER-VideoCrafter/scripts/main/ddp_wrapper.py @@ -0,0 +1,47 @@ +# Copied from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import datetime +import argparse, importlib +from pytorch_lightning import seed_everything + +import torch +import torch.distributed as dist + +def setup_dist(local_rank): + if dist.is_initialized(): + return + torch.cuda.set_device(local_rank) + torch.distributed.init_process_group('nccl', init_method='env://') + + +def get_dist_info(): + if dist.is_available(): + initialized = dist.is_initialized() + else: + initialized = False + if initialized: + rank = dist.get_rank() + world_size = dist.get_world_size() + else: + rank = 0 + world_size = 1 + return rank, world_size + + +if __name__ == '__main__': + now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + parser = argparse.ArgumentParser() + parser.add_argument("--module", type=str, help="module name", default="inference") + parser.add_argument("--local_rank", type=int, nargs="?", help="for ddp", default=0) + args, unknown = parser.parse_known_args() + inference_api = importlib.import_module(args.module, package=None) + + inference_parser = inference_api.get_parser() + inference_args, unknown = inference_parser.parse_known_args() + + seed_everything(inference_args.seed) + setup_dist(args.local_rank) + torch.backends.cudnn.benchmark = True + rank, gpu_num = get_dist_info() + + print("@CoLVDM Inference [rank%d]: %s"%(rank, now)) + inference_api.run_inference(inference_args, gpu_num, rank) \ No newline at end of file diff --git a/VADER-VideoCrafter/scripts/main/funcs.py b/VADER-VideoCrafter/scripts/main/funcs.py new file mode 100644 index 0000000000000000000000000000000000000000..e04714020150f9c64df679bc83a070db2491609f --- /dev/null +++ b/VADER-VideoCrafter/scripts/main/funcs.py @@ -0,0 +1,231 @@ +# Adapted from VideoCrafter: https://github.com/AILab-CVC/VideoCrafter +import os, sys, glob +import numpy as np +from collections import OrderedDict +from decord import VideoReader, cpu +import cv2 +import random + +import torch +import torchvision +sys.path.insert(1, os.path.join(sys.path[0], '..', '..')) +from lvdm.models.samplers.ddim import DDIMSampler +# import ipdb +# st = ipdb.set_trace + +def batch_ddim_sampling(model, cond, noise_shape, n_samples=1, ddim_steps=50, ddim_eta=1.0,\ + cfg_scale=1.0, temporal_cfg_scale=None, backprop_mode=None, decode_frame='-1', **kwargs): + ddim_sampler = DDIMSampler(model) + if backprop_mode is not None: # it is for training now, backprop_mode != None also means vader training mode + ddim_sampler.backprop_mode = backprop_mode + ddim_sampler.training_mode = True + uncond_type = model.uncond_type + batch_size = noise_shape[0] + + ## construct unconditional guidance + if cfg_scale != 1.0: + if uncond_type == "empty_seq": + prompts = batch_size * [""] + + uc_emb = model.get_learned_conditioning(prompts) + elif uncond_type == "zero_embed": + c_emb = cond["c_crossattn"][0] if isinstance(cond, dict) else cond + uc_emb = torch.zeros_like(c_emb) + + ## process image embedding token + if hasattr(model, 'embedder'): + uc_img = torch.zeros(noise_shape[0],3,224,224).to(model.device) + ## img: b c h w >> b l c + uc_img = model.get_image_embeds(uc_img) + uc_emb = torch.cat([uc_emb, uc_img], dim=1) + + if isinstance(cond, dict): + uc = {key:cond[key] for key in cond.keys()} + uc.update({'c_crossattn': [uc_emb]}) + else: + uc = uc_emb + else: + uc = None + + x_T = None + batch_variants = [] + + for _ in range(n_samples): + if ddim_sampler is not None: + kwargs.update({"clean_cond": True}) + samples, _ = ddim_sampler.sample(S=ddim_steps, # samples: batch, c, t, h, w + conditioning=cond, + batch_size=noise_shape[0], + shape=noise_shape[1:], + verbose=False, + unconditional_guidance_scale=cfg_scale, + unconditional_conditioning=uc, + eta=ddim_eta, + temporal_length=noise_shape[2], + conditional_guidance_scale_temporal=temporal_cfg_scale, + x_T=x_T, + **kwargs + ) + + ## reconstruct from latent to pixel space + if backprop_mode is not None: # it is for training now. Use one frame randomly to save memory + try: + decode_frame=int(decode_frame) + #it's a int + except: + pass + if type(decode_frame) == int: + frame_index = random.randint(0,samples.shape[2]-1) if decode_frame == -1 else decode_frame # samples: batch, c, t, h, w + batch_images = model.decode_first_stage_2DAE(samples[:,:,frame_index:frame_index+1,:,:]) + elif decode_frame in ['alt', 'all']: + idxs = range(0, samples.shape[2], 2) if decode_frame == 'alt' else range(samples.shape[2]) + batch_images = model.decode_first_stage_2DAE(samples[:,:,idxs,:,:]) + + + else: # inference mode + batch_images = model.decode_first_stage_2DAE(samples) + batch_variants.append(batch_images) + + ## batch, , c, t, h, w + batch_variants = torch.stack(batch_variants, dim=1) + return batch_variants + + +def get_filelist(data_dir, ext='*'): + file_list = glob.glob(os.path.join(data_dir, '*.%s'%ext)) + file_list.sort() + return file_list + +def get_dirlist(path): + list = [] + if (os.path.exists(path)): + files = os.listdir(path) + for file in files: + m = os.path.join(path,file) + if (os.path.isdir(m)): + list.append(m) + list.sort() + return list + + +def load_model_checkpoint(model, ckpt): + def load_checkpoint(model, ckpt, full_strict): + state_dict = torch.load(ckpt, map_location="cpu") + try: + ## deepspeed + new_pl_sd = OrderedDict() + for key in state_dict['module'].keys(): + new_pl_sd[key[16:]]=state_dict['module'][key] + model.load_state_dict(new_pl_sd, strict=full_strict) + except: + if "state_dict" in list(state_dict.keys()): + state_dict = state_dict["state_dict"] + model.load_state_dict(state_dict, strict=full_strict) + return model + load_checkpoint(model, ckpt, full_strict=True) + print('>>> model checkpoint loaded.') + return model + + +def load_prompts(prompt_file): + f = open(prompt_file, 'r') + prompt_list = [] + for idx, line in enumerate(f.readlines()): + l = line.strip() + if len(l) != 0: + prompt_list.append(l) + f.close() + return prompt_list + + +def load_video_batch(filepath_list, frame_stride, video_size=(256,256), video_frames=16): + ''' + Notice about some special cases: + 1. video_frames=-1 means to take all the frames (with fs=1) + 2. when the total video frames is less than required, padding strategy will be used (repreated last frame) + ''' + fps_list = [] + batch_tensor = [] + assert frame_stride > 0, "valid frame stride should be a positive interge!" + for filepath in filepath_list: + padding_num = 0 + vidreader = VideoReader(filepath, ctx=cpu(0), width=video_size[1], height=video_size[0]) + fps = vidreader.get_avg_fps() + total_frames = len(vidreader) + max_valid_frames = (total_frames-1) // frame_stride + 1 + if video_frames < 0: + ## all frames are collected: fs=1 is a must + required_frames = total_frames + frame_stride = 1 + else: + required_frames = video_frames + query_frames = min(required_frames, max_valid_frames) + frame_indices = [frame_stride*i for i in range(query_frames)] + + ## [t,h,w,c] -> [c,t,h,w] + frames = vidreader.get_batch(frame_indices) + frame_tensor = torch.tensor(frames.asnumpy()).permute(3, 0, 1, 2).float() + frame_tensor = (frame_tensor / 255. - 0.5) * 2 + if max_valid_frames < required_frames: + padding_num = required_frames - max_valid_frames + frame_tensor = torch.cat([frame_tensor, *([frame_tensor[:,-1:,:,:]]*padding_num)], dim=1) + print(f'{os.path.split(filepath)[1]} is not long enough: {padding_num} frames padded.') + batch_tensor.append(frame_tensor) + sample_fps = int(fps/frame_stride) + fps_list.append(sample_fps) + + return torch.stack(batch_tensor, dim=0) + +from PIL import Image +def load_image_batch(filepath_list, image_size=(256,256)): + batch_tensor = [] + for filepath in filepath_list: + _, filename = os.path.split(filepath) + _, ext = os.path.splitext(filename) + if ext == '.mp4': + vidreader = VideoReader(filepath, ctx=cpu(0), width=image_size[1], height=image_size[0]) + frame = vidreader.get_batch([0]) + img_tensor = torch.tensor(frame.asnumpy()).squeeze(0).permute(2, 0, 1).float() + elif ext == '.png' or ext == '.jpg': + img = Image.open(filepath).convert("RGB") + rgb_img = np.array(img, np.float32) + #bgr_img = cv2.imread(filepath, cv2.IMREAD_COLOR) + #bgr_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) + rgb_img = cv2.resize(rgb_img, (image_size[1],image_size[0]), interpolation=cv2.INTER_LINEAR) + img_tensor = torch.from_numpy(rgb_img).permute(2, 0, 1).float() + else: + print(f'ERROR: <{ext}> image loading only support format: [mp4], [png], [jpg]') + raise NotImplementedError + img_tensor = (img_tensor / 255. - 0.5) * 2 + batch_tensor.append(img_tensor) + return torch.stack(batch_tensor, dim=0) + + +def save_videos(batch_tensors, savedir, filenames, fps=10): + # b,samples,c,t,h,w + n_samples = batch_tensors.shape[1] + for idx, vid_tensor in enumerate(batch_tensors): + video = vid_tensor.detach().cpu() + video = torch.clamp(video.float(), -1., 1.) + video = video.permute(2, 0, 1, 3, 4) # t,n,c,h,w + frame_grids = [torchvision.utils.make_grid(framesheet, nrow=int(n_samples)) for framesheet in video] #[3, 1*h, n*w] + grid = torch.stack(frame_grids, dim=0) # stack in temporal dim [t, 3, n*h, w] + grid = (grid + 1.0) / 2.0 + grid = (grid * 255).to(torch.uint8).permute(0, 2, 3, 1) + savepath = os.path.join(savedir, f"{filenames[idx]}.mp4") + torchvision.io.write_video(savepath, grid, fps=fps, video_codec='h264', options={'crf': '10'}) + +def get_videos(batch_tensors, fps=10): + # b,samples,c,t,h,w + n_samples = batch_tensors.shape[1] + vid_tensor = batch_tensors[0] + video = vid_tensor.detach().cpu() + video = torch.clamp(video.float(), -1., 1.) + video = video.permute(2, 0, 1, 3, 4) # t,n,c,h,w + frame_grids = [torchvision.utils.make_grid(framesheet, nrow=int(n_samples)) for framesheet in video] #[3, 1*h, n*w] + grid = torch.stack(frame_grids, dim=0) # stack in temporal dim [t, 3, n*h, w] + grid = (grid + 1.0) / 2.0 + grid = (grid * 255).to(torch.uint8).permute(0, 2, 3, 1) + + return grid + diff --git a/VADER-VideoCrafter/scripts/main/train_t2v_lora.py b/VADER-VideoCrafter/scripts/main/train_t2v_lora.py new file mode 100644 index 0000000000000000000000000000000000000000..30d4dfc7f4008dee7bb82dfaf123c5f4b3629c88 --- /dev/null +++ b/VADER-VideoCrafter/scripts/main/train_t2v_lora.py @@ -0,0 +1,817 @@ +import argparse, os, sys, glob, yaml, math, random +sys.path.append('../') # setting path to get Core and assets + +import datetime, time +import numpy as np +from omegaconf import OmegaConf +from collections import OrderedDict +from tqdm import trange, tqdm +from einops import repeat +from einops import rearrange, repeat +from functools import partial +import torch +from pytorch_lightning import seed_everything + +from funcs import load_model_checkpoint, load_prompts, load_image_batch, get_filelist, save_videos, get_videos +from funcs import batch_ddim_sampling +from utils.utils import instantiate_from_config + +import peft +import torchvision +from transformers.utils import ContextManagers +from transformers import AutoProcessor, AutoModel, AutoImageProcessor, AutoModelForObjectDetection, AutoModelForZeroShotObjectDetection +from Core.aesthetic_scorer import AestheticScorerDiff +from Core.actpred_scorer import ActPredScorer +from Core.weather_scorer import WeatherScorer +from Core.compression_scorer import JpegCompressionScorer, jpeg_compressibility +import Core.prompts as prompts_file +from hpsv2.src.open_clip import create_model_and_transforms, get_tokenizer +import hpsv2 +import bitsandbytes as bnb +from accelerate import Accelerator +from accelerate.logging import get_logger +from accelerate.utils import gather_object +import torch.distributed as dist +import logging +import gc +from PIL import Image +import io +import albumentations as A +from huggingface_hub import snapshot_download +import cv2 +# import ipdb +# st = ipdb.set_trace + + +logger = get_logger(__name__, log_level="INFO") # get logger for current module + +def create_logging(logging, logger, accelerator): + logging.basicConfig( + format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", + datefmt="%m/%d/%Y %H:%M:%S", + level=logging.INFO, + ) + logger.info(accelerator.state, main_process_only=False) + +def create_output_folders(output_dir, run_name): + out_dir = os.path.join(output_dir, run_name) + os.makedirs(out_dir, exist_ok=True) + os.makedirs(f"{out_dir}/samples", exist_ok=True) + return out_dir + +def str2bool(v): + if isinstance(v, bool): + return v + if v.lower() in ('yes', 'true', 't', 'y', '1'): + return True + elif v.lower() in ('no', 'false', 'f', 'n', '0'): + return False + else: + raise argparse.ArgumentTypeError('Boolean value expected.') + +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument("--seed", type=int, default=20230211, help="seed for seed_everything") + parser.add_argument("--mode", default="base", type=str, help="which kind of inference mode: {'base', 'i2v'}") + parser.add_argument("--ckpt_path", type=str, default='VADER-VideoCrafter/checkpoints/base_512_v2/model.ckpt', help="checkpoint path") + parser.add_argument("--config", type=str, default='VADER-VideoCrafter/configs/inference_t2v_512_v2.0.yaml', help="config (yaml) path") + parser.add_argument("--savefps", type=str, default=10, help="video fps to generate") + parser.add_argument("--n_samples", type=int, default=1, help="num of samples per prompt",) + parser.add_argument("--ddim_steps", type=int, default=50, help="steps of ddim if positive, otherwise use DDPM",) + parser.add_argument("--ddim_eta", type=float, default=1.0, help="eta for ddim sampling (0.0 yields deterministic sampling)",) + parser.add_argument("--height", type=int, default=512, help="image height, in pixel space") + parser.add_argument("--width", type=int, default=512, help="image width, in pixel space") + parser.add_argument("--frames", type=int, default=-1, help="frames num to inference") + parser.add_argument("--fps", type=int, default=24) + parser.add_argument("--unconditional_guidance_scale", type=float, default=1.0, help="prompt classifier-free guidance") + parser.add_argument("--unconditional_guidance_scale_temporal", type=float, default=None, help="temporal consistency guidance") + ## for conditional i2v only + parser.add_argument("--cond_input", type=str, default=None, help="data dir of conditional input") + ## for training + parser.add_argument("--lr", type=float, default=2e-4, help="learning rate") + parser.add_argument("--val_batch_size", type=int, default=1, help="batch size for validation") + parser.add_argument("--num_val_runs", type=int, default=1, help="total number of validation samples = num_val_runs * num_gpus * num_val_batch") + parser.add_argument("--train_batch_size", type=int, default=1, help="batch size for training") + parser.add_argument("--reward_fn", type=str, default="aesthetic", help="reward function: 'aesthetic', 'hps', 'aesthetic_hps', 'pick_score', 'rainy', 'snowy', 'objectDetection', 'actpred', 'compression'") + parser.add_argument("--compression_model_path", type=str, default='assets/compression_reward.pt', help="compression model path") # The compression model is used only when reward_fn is 'compression' + # The "book." is for grounding-dino model . Remember to add "." at the end of the object name for grounding-dino model. + # But for yolos model, do not add "." at the end of the object name. Instead, you should set the object name to "book" for example. + parser.add_argument("--target_object", type=str, default="book", help="target object for object detection reward function") + parser.add_argument("--detector_model", type=str, default="yolos-base", help="object detection model", + choices=["yolos-base", "yolos-tiny", "grounding-dino-base", "grounding-dino-tiny"]) + parser.add_argument("--hps_version", type=str, default="v2.1", help="hps version: 'v2.0', 'v2.1'") + parser.add_argument("--prompt_fn", type=str, default="hps_custom", help="prompt function") + parser.add_argument("--nouns_file", type=str, default="simple_animals.txt", help="nouns file") + parser.add_argument("--activities_file", type=str, default="activities.txt", help="activities file") + parser.add_argument("--num_train_epochs", type=int, default=200, help="number of training epochs") + parser.add_argument("--max_train_steps", type=int, default=10000, help="max training steps") + parser.add_argument("--backprop_mode", type=str, default="last", help="backpropagation mode: 'last', 'rand', 'specific'") # backprop_mode != None also means training mode for batch_ddim_sampling + parser.add_argument("--gradient_accumulation_steps", type=int, default=1, help="gradient accumulation steps") + parser.add_argument("--mixed_precision", type=str, default='fp16', help="mixed precision training: 'no', 'fp8', 'fp16', 'bf16'") + parser.add_argument("--project_dir", type=str, default="VADER-VideoCrafter/project_dir", help="project directory") + parser.add_argument("--validation_steps", type=int, default=1, help="The frequency of validation, e.g., 1 means validate every 1*accelerator.num_processes steps") + parser.add_argument("--checkpointing_steps", type=int, default=1, help="The frequency of checkpointing") + parser.add_argument("--wandb_entity", type=str, default="", help="wandb entity") + parser.add_argument("--debug", type=str2bool, default=False, help="debug mode") + parser.add_argument("--max_grad_norm", type=float, default=1.0, help="max gradient norm") + parser.add_argument("--use_AdamW8bit", type=str2bool, default=False, help="use AdamW8bit optimizer") + parser.add_argument("--is_sample_preview", type=str2bool, default=True, help="sample preview during training") + parser.add_argument("--decode_frame", type=str, default="-1", help="decode frame: '-1', 'fml', 'all', 'alt'") # it could also be any number str like '3', '10'. alt: alternate frames, fml: first, middle, last frames, all: all frames. '-1': random frame + parser.add_argument("--inference_only", type=str2bool, default=True, help="only do inference") + parser.add_argument("--lora_ckpt_path", type=str, default=None, help="LoRA checkpoint path") + parser.add_argument("--lora_rank", type=int, default=16, help="LoRA rank") + + return parser + + +def aesthetic_loss_fn(aesthetic_target=None, + grad_scale=0, + device=None, + torch_dtype=None): + ''' + Args: + aesthetic_target: float, the target value of the aesthetic score. it is 10 in this experiment + grad_scale: float, the scale of the gradient. it is 0.1 in this experiment + device: torch.device, the device to run the model. + torch_dtype: torch.dtype, the data type of the model. + + Returns: + loss_fn: function, the loss function of the aesthetic reward function. + ''' + target_size = (224, 224) + normalize = torchvision.transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], + std=[0.26862954, 0.26130258, 0.27577711]) + + scorer = AestheticScorerDiff(dtype=torch_dtype).to(device, dtype=torch_dtype) + scorer.requires_grad_(False) + + def loss_fn(im_pix_un): + im_pix = ((im_pix_un / 2) + 0.5).clamp(0, 1) + im_pix = torchvision.transforms.Resize(target_size)(im_pix) + im_pix = normalize(im_pix).to(im_pix_un.dtype) + rewards = scorer(im_pix) + if aesthetic_target is None: # default maximization + loss = -1 * rewards + else: + # using L1 to keep on same scale + loss = abs(rewards - aesthetic_target) + return loss.mean() * grad_scale, rewards.mean() + return loss_fn + + +def hps_loss_fn(inference_dtype=None, device=None, hps_version="v2.0"): + ''' + Args: + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + hps_version: str, the version of the HPS model. It is "v2.0" or "v2.1" in this experiment. + + Returns: + loss_fn: function, the loss function of the HPS reward function. + ''' + model_name = "ViT-H-14" + + model, preprocess_train, preprocess_val = create_model_and_transforms( + model_name, + 'laion2B-s32B-b79K', + precision=inference_dtype, + device=device, + jit=False, + force_quick_gelu=False, + force_custom_text=False, + force_patch_dropout=False, + force_image_size=None, + pretrained_image=False, + image_mean=None, + image_std=None, + light_augmentation=True, + aug_cfg={}, + output_dict=True, + with_score_predictor=False, + with_region_predictor=False + ) + + tokenizer = get_tokenizer(model_name) + + if hps_version == "v2.0": # if there is a error, please download the model manually and set the path + checkpoint_path = f"{os.path.expanduser('~')}/.cache/huggingface/hub/models--xswu--HPSv2/snapshots/697403c78157020a1ae59d23f111aa58ced35b0a/HPS_v2_compressed.pt" + else: # hps_version == "v2.1" + checkpoint_path = f"{os.path.expanduser('~')}/.cache/huggingface/hub/models--xswu--HPSv2/snapshots/697403c78157020a1ae59d23f111aa58ced35b0a/HPS_v2.1_compressed.pt" + # force download of model via score + hpsv2.score([], "", hps_version=hps_version) + + checkpoint = torch.load(checkpoint_path, map_location=device) + model.load_state_dict(checkpoint['state_dict']) + tokenizer = get_tokenizer(model_name) + model = model.to(device, dtype=inference_dtype) + model.eval() + + target_size = (224, 224) + normalize = torchvision.transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], + std=[0.26862954, 0.26130258, 0.27577711]) + + def loss_fn(im_pix, prompts): + im_pix = ((im_pix / 2) + 0.5).clamp(0, 1) + x_var = torchvision.transforms.Resize(target_size)(im_pix) + x_var = normalize(x_var).to(im_pix.dtype) + caption = tokenizer(prompts) + caption = caption.to(device) + outputs = model(x_var, caption) + image_features, text_features = outputs["image_features"], outputs["text_features"] + logits = image_features @ text_features.T + scores = torch.diagonal(logits) + loss = 1.0 - scores + return loss.mean(), scores.mean() + + return loss_fn + +def aesthetic_hps_loss_fn(aesthetic_target=None, + grad_scale=0, + inference_dtype=None, + device=None, + hps_version="v2.0"): + ''' + Args: + aesthetic_target: float, the target value of the aesthetic score. it is 10 in this experiment + grad_scale: float, the scale of the gradient. it is 0.1 in this experiment + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + hps_version: str, the version of the HPS model. It is "v2.0" or "v2.1" in this experiment. + + Returns: + loss_fn: function, the loss function of a combination of aesthetic and HPS reward function. + ''' + # HPS + model_name = "ViT-H-14" + + model, preprocess_train, preprocess_val = create_model_and_transforms( + model_name, + 'laion2B-s32B-b79K', + precision=inference_dtype, + device=device, + jit=False, + force_quick_gelu=False, + force_custom_text=False, + force_patch_dropout=False, + force_image_size=None, + pretrained_image=False, + image_mean=None, + image_std=None, + light_augmentation=True, + aug_cfg={}, + output_dict=True, + with_score_predictor=False, + with_region_predictor=False + ) + + # tokenizer = get_tokenizer(model_name) + + if hps_version == "v2.0": # if there is a error, please download the model manually and set the path + checkpoint_path = f"{os.path.expanduser('~')}/.cache/huggingface/hub/models--xswu--HPSv2/snapshots/697403c78157020a1ae59d23f111aa58ced35b0a/HPS_v2_compressed.pt" + else: # hps_version == "v2.1" + checkpoint_path = f"{os.path.expanduser('~')}/.cache/huggingface/hub/models--xswu--HPSv2/snapshots/697403c78157020a1ae59d23f111aa58ced35b0a/HPS_v2.1_compressed.pt" + # force download of model via score + hpsv2.score([], "", hps_version=hps_version) + + checkpoint = torch.load(checkpoint_path, map_location=device) + model.load_state_dict(checkpoint['state_dict']) + tokenizer = get_tokenizer(model_name) + model = model.to(device, dtype=inference_dtype) + model.eval() + + target_size = (224, 224) + normalize = torchvision.transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073], + std=[0.26862954, 0.26130258, 0.27577711]) + # Aesthetic + scorer = AestheticScorerDiff(dtype=inference_dtype).to(device, dtype=inference_dtype) + scorer.requires_grad_(False) + + def loss_fn(im_pix_un, prompts): + # Aesthetic + im_pix = ((im_pix_un / 2) + 0.5).clamp(0, 1) + im_pix = torchvision.transforms.Resize(target_size)(im_pix) + im_pix = normalize(im_pix).to(im_pix_un.dtype) + + aesthetic_rewards = scorer(im_pix) + if aesthetic_target is None: # default maximization + aesthetic_loss = -1 * aesthetic_rewards + else: + # using L1 to keep on same scale + aesthetic_loss = abs(aesthetic_rewards - aesthetic_target) + aesthetic_loss = aesthetic_loss.mean() * grad_scale + aesthetic_rewards = aesthetic_rewards.mean() + + # HPS + caption = tokenizer(prompts) + caption = caption.to(device) + outputs = model(im_pix, caption) + image_features, text_features = outputs["image_features"], outputs["text_features"] + logits = image_features @ text_features.T + scores = torch.diagonal(logits) + hps_loss = abs(1.0 - scores) + hps_loss = hps_loss.mean() + hps_rewards = scores.mean() + + loss = (1.5 * aesthetic_loss + hps_loss) /2 # 1.5 is a hyperparameter. Set it to 1.5 because experimentally hps_loss is 1.5 times larger than aesthetic_loss + rewards = (aesthetic_rewards + 15 * hps_rewards) / 2 # 15 is a hyperparameter. Set it to 15 because experimentally aesthetic_rewards is 15 times larger than hps_reward + return loss, rewards + + return loss_fn + +def pick_score_loss_fn(inference_dtype=None, device=None): + ''' + Args: + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + + Returns: + loss_fn: function, the loss function of the PickScore reward function. + ''' + processor_name_or_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K" + model_pretrained_name_or_path = "yuvalkirstain/PickScore_v1" + processor = AutoProcessor.from_pretrained(processor_name_or_path, torch_dtype=inference_dtype) + model = AutoModel.from_pretrained(model_pretrained_name_or_path, torch_dtype=inference_dtype).eval().to(device) + model.requires_grad_(False) + + def loss_fn(im_pix_un, prompts): # im_pix_un: b,c,h,w + im_pix = ((im_pix_un / 2) + 0.5).clamp(0, 1) + + # reproduce the pick_score preprocessing + im_pix = im_pix * 255 # b,c,h,w + + if im_pix.shape[2] < im_pix.shape[3]: + height = 224 + width = im_pix.shape[3] * height // im_pix.shape[2] # keep the aspect ratio, so the width is w * 224/h + else: + width = 224 + height = im_pix.shape[2] * width // im_pix.shape[3] # keep the aspect ratio, so the height is h * 224/w + + # interpolation and antialiasing should be the same as below + im_pix = torchvision.transforms.Resize((height, width), + interpolation=torchvision.transforms.InterpolationMode.BICUBIC, + antialias=True)(im_pix) + im_pix = im_pix.permute(0, 2, 3, 1) # b,c,h,w -> (b,h,w,c) + # crop the center 224x224 + startx = width//2 - (224//2) + starty = height//2 - (224//2) + im_pix = im_pix[:, starty:starty+224, startx:startx+224, :] + # do rescale and normalize as CLIP + im_pix = im_pix * 0.00392156862745098 # rescale factor + mean = torch.tensor([0.48145466, 0.4578275, 0.40821073]).to(device) + std = torch.tensor([0.26862954, 0.26130258, 0.27577711]).to(device) + im_pix = (im_pix - mean) / std + im_pix = im_pix.permute(0, 3, 1, 2) # BHWC -> BCHW + + text_inputs = processor( + text=prompts, + padding=True, + truncation=True, + max_length=77, + return_tensors="pt", + ).to(device) + + + # embed + image_embs = model.get_image_features(pixel_values=im_pix) + image_embs = image_embs / torch.norm(image_embs, dim=-1, keepdim=True) + + text_embs = model.get_text_features(**text_inputs) + text_embs = text_embs / torch.norm(text_embs, dim=-1, keepdim=True) + + # score + scores = model.logit_scale.exp() * (text_embs @ image_embs.T)[0] + loss = abs(1.0 - scores / 100.0) + return loss.mean(), scores.mean() + + return loss_fn + +def weather_loss_fn(inference_dtype=None, device=None, weather="rainy", target=None, grad_scale=0): + ''' + Args: + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + weather: str, the weather condition. It is "rainy" or "snowy" in this experiment. + target: float, the target value of the weather score. It is 1.0 in this experiment. + grad_scale: float, the scale of the gradient. It is 1 in this experiment. + + Returns: + loss_fn: function, the loss function of the weather reward function. + ''' + if weather == "rainy": + reward_model_path = "../assets/rainy_reward.pt" + elif weather == "snowy": + reward_model_path = "../assets/snowy_reward.pt" + else: + raise NotImplementedError + scorer = WeatherScorer(dtype=inference_dtype, model_path=reward_model_path).to(device, dtype=inference_dtype) + scorer.requires_grad_(False) + scorer.eval() + def loss_fn(im_pix_un): + im_pix = ((im_pix_un + 1) / 2).clamp(0, 1) # from [-1, 1] to [0, 1] + rewards = scorer(im_pix) + + if target is None: + loss = rewards + else: + loss = abs(rewards - target) + + return loss.mean() * grad_scale, rewards.mean() + return loss_fn + +def objectDetection_loss_fn(inference_dtype=None, device=None, targetObject='dog.', model_name='grounding-dino-base'): + ''' + This reward function is used to remove the target object from the generated video. + We use yolo-s-tiny model to detect the target object in the generated video. + + Args: + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + targetObject: str, the object to detect. It is "dog" in this experiment. + + Returns: + loss_fn: function, the loss function of the object detection reward function. + ''' + if model_name == "yolos-base": + image_processor = AutoImageProcessor.from_pretrained("hustvl/yolos-base", torch_dtype=inference_dtype) + model = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-base", torch_dtype=inference_dtype).to(device) + # check if "." in the targetObject name for yolos model + if "." in targetObject: + raise ValueError("The targetObject name should not contain '.' for yolos-base model.") + elif model_name == "yolos-tiny": + image_processor = AutoImageProcessor.from_pretrained("hustvl/yolos-tiny", torch_dtype=inference_dtype) + model = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny", torch_dtype=inference_dtype).to(device) + # check if "." in the targetObject name for yolos model + if "." in targetObject: + raise ValueError("The targetObject name should not contain '.' for yolos-tiny model.") + elif model_name == "grounding-dino-base": + image_processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-base", torch_dtype=inference_dtype) + model = AutoModelForZeroShotObjectDetection.from_pretrained("IDEA-Research/grounding-dino-base",torch_dtype=inference_dtype).to(device) + # check if "." in the targetObject name for grounding-dino model + if "." not in targetObject: + raise ValueError("The targetObject name should contain '.' for grounding-dino-base model.") + elif model_name == "grounding-dino-tiny": + image_processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-tiny", torch_dtype=inference_dtype) + model = AutoModelForZeroShotObjectDetection.from_pretrained("IDEA-Research/grounding-dino-tiny", torch_dtype=inference_dtype).to(device) + # check if "." in the targetObject name for grounding-dino model + if "." not in targetObject: + raise ValueError("The targetObject name should contain '.' for grounding-dino-tiny model.") + else: + raise NotImplementedError + + model.requires_grad_(False) + model.eval() + + def loss_fn(im_pix_un): # im_pix_un: b,c,h,w + images = ((im_pix_un / 2) + 0.5).clamp(0.0, 1.0) + + # reproduce the yolo preprocessing + height = 512 + width = 512 * images.shape[3] // images.shape[2] # keep the aspect ratio, so the width is 512 * w/h + images = torchvision.transforms.Resize((height, width), antialias=False)(images) + images = images.permute(0, 2, 3, 1) # b,c,h,w -> (b,h,w,c) + + image_mean = torch.tensor([0.485, 0.456, 0.406]).to(device) + image_std = torch.tensor([0.229, 0.224, 0.225]).to(device) + + images = (images - image_mean) / image_std + normalized_image = images.permute(0,3,1,2) # NHWC -> NCHW + + # Process images + if model_name == "yolos-base" or model_name == "yolos-tiny": + outputs = model(pixel_values=normalized_image) + else: # grounding-dino model + inputs = image_processor(text=targetObject, return_tensors="pt").to(device) + outputs = model(pixel_values=normalized_image, input_ids=inputs.input_ids) + + # Get target sizes for each image + target_sizes = torch.tensor([normalized_image[0].shape[1:]]*normalized_image.shape[0]).to(device) + + # Post-process results for each image + if model_name == "yolos-base" or model_name == "yolos-tiny": + results = image_processor.post_process_object_detection(outputs, threshold=0.2, target_sizes=target_sizes) + else: # grounding-dino model + results = image_processor.post_process_grounded_object_detection( + outputs, + inputs.input_ids, + box_threshold=0.4, + text_threshold=0.3, + target_sizes=target_sizes + ) + + sum_avg_scores = 0 + for i, result in enumerate(results): + if model_name == "yolos-base" or model_name == "yolos-tiny": + id = model.config.label2id[targetObject] + # get index of targetObject's label + index = torch.where(result["labels"] == id) + if len(index[0]) == 0: # index: ([],[]) so index[0] is the first list + sum_avg_scores = torch.sum(outputs.logits - outputs.logits) # set sum_avg_scores to 0 + continue + scores = result["scores"][index] + else: # grounding-dino model + if result["scores"].shape[0] == 0: + sum_avg_scores = torch.sum(outputs.last_hidden_state - outputs.last_hidden_state) # set sum_avg_scores to 0 + continue + scores = result["scores"] + sum_avg_scores = sum_avg_scores + (torch.sum(scores) / scores.shape[0]) + + loss = sum_avg_scores / len(results) + reward = 1 - loss + + return loss, reward + return loss_fn + +def compression_loss_fn(inference_dtype=None, device=None, target=None, grad_scale=0, model_path=None): + ''' + Args: + inference_dtype: torch.dtype, the data type of the model. + device: torch.device, the device to run the model. + model_path: str, the path of the compression model. + + Returns: + loss_fn: function, the loss function of the compression reward function. + ''' + scorer = JpegCompressionScorer(dtype=inference_dtype, model_path=model_path).to(device, dtype=inference_dtype) + scorer.requires_grad_(False) + scorer.eval() + def loss_fn(im_pix_un): + im_pix = ((im_pix_un + 1) / 2).clamp(0, 1) + rewards = scorer(im_pix) + + if target is None: + loss = rewards + else: + loss = abs(rewards - target) + return loss.mean() * grad_scale, rewards.mean() + + return loss_fn + +def actpred_loss_fn(inference_dtype=None, device=None, num_frames = 14, target_size=224): + scorer = ActPredScorer(device=device, num_frames = num_frames, dtype=inference_dtype) + scorer.requires_grad_(False) + + def preprocess_img(img): + img = ((img/2) + 0.5).clamp(0,1) + img = torchvision.transforms.Resize((target_size, target_size), antialias = True)(img) + img = torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(img) + return img + def loss_fn(vid, target_action_label): + vid = torch.cat([preprocess_img(img).unsqueeze(0) for img in vid])[None] + return scorer.get_loss_and_score(vid, target_action_label) + + return loss_fn + + +def should_sample(global_step, validation_steps, is_sample_preview): + return (global_step % validation_steps == 0 or global_step ==1) \ + and is_sample_preview + + +def run_training(args, peft_model, **kwargs): + ## ---------------------step 1: accelerator setup--------------------------- + accelerator = Accelerator( # Initialize Accelerator + gradient_accumulation_steps=args.gradient_accumulation_steps, + mixed_precision=args.mixed_precision, + project_dir=args.project_dir + + ) + output_dir = args.project_dir + + # Make one log on every process with the configuration for debugging. + create_logging(logging, logger, accelerator) + + # ## ------------------------step 2: model config----------------------------- + # # download the checkpoint for VideoCrafter2 model + # ckpt_dir = args.ckpt_path.split('/') # args.ckpt='checkpoints/base_512_v2/model.ckpt' -> 'checkpoints/base_512_v2' + # ckpt_dir = '/'.join(ckpt_dir[:-1]) + # snapshot_download(repo_id='VideoCrafter/VideoCrafter2', local_dir =ckpt_dir) + + # # load the model + # config = OmegaConf.load(args.config) + # model_config = config.pop("model", OmegaConf.create()) + # model = instantiate_from_config(model_config) + + # assert os.path.exists(args.ckpt_path), f"Error: checkpoint [{args.ckpt_path}] Not Found!" + # model = load_model_checkpoint(model, args.ckpt_path) + + + # # convert first_stage_model and cond_stage_model to torch.float16 if mixed_precision is True + # if args.mixed_precision != 'no': + # model.first_stage_model = model.first_stage_model.half() + # model.cond_stage_model = model.cond_stage_model.half() + + # # step 2.1: add LoRA using peft + # config = peft.LoraConfig( + # r=args.lora_rank, + # target_modules=["to_k", "to_v", "to_q"], # only diffusion_model has these modules + # lora_dropout=0.01, + # ) + + # peft_model = peft.get_peft_model(model, config) + + # peft_model.print_trainable_parameters() + + # # load the pretrained LoRA model + # if args.lora_ckpt_path is not None: + # if args.lora_ckpt_path == "huggingface-hps-aesthetic": # download the pretrained LoRA model from huggingface + # snapshot_download(repo_id='zheyangqin/VADER', local_dir ='VADER-VideoCrafter/checkpoints/pretrained_lora') + # args.lora_ckpt_path = 'VADER-VideoCrafter/checkpoints/pretrained_lora/vader_videocrafter_hps_aesthetic.pt' + # elif args.lora_ckpt_path == "huggingface-pickscore": # download the pretrained LoRA model from huggingface + # snapshot_download(repo_id='zheyangqin/VADER', local_dir ='VADER-VideoCrafter/checkpoints/pretrained_lora') + # args.lora_ckpt_path = 'VADER-VideoCrafter/checkpoints/pretrained_lora/vader_videocrafter_pickscore.pt' + # # load the pretrained LoRA model + # peft.set_peft_model_state_dict(peft_model, torch.load(args.lora_ckpt_path)) + + # Inference Step: only do inference and save the videos. Skip this step if it is training + # ================================================================== + if args.inference_only: + peft_model = accelerator.prepare(peft_model) + # sample shape + assert (args.height % 16 == 0) and (args.width % 16 == 0), "Error: image size [h,w] should be multiples of 16!" + # latent noise shape + h, w = args.height // 8, args.width // 8 + if isinstance(peft_model, torch.nn.parallel.DistributedDataParallel): + frames = peft_model.module.temporal_length if args.frames < 0 else args.frames + channels = peft_model.module.channels + else: + frames = peft_model.temporal_length if args.frames < 0 else args.frames + channels = peft_model.channels + + ## Inference step 2: run Inference over samples + logger.info("***** Running inference *****") + + first_epoch = 0 + global_step = 0 + + + ## Inference Step 3: generate new validation videos + with torch.no_grad(): + + # set random seed for each process + random.seed(args.seed) + torch.manual_seed(args.seed) + + prompts_all = [args.prompt_str] + val_prompt = list(prompts_all) + + assert len(val_prompt) == 1, "Error: only one prompt is allowed for inference in gradio!" + + # store output of generations in dict + results=dict(filenames=[],dir_name=[], prompt=[]) + + # Inference Step 3.1: forward pass + batch_size = len(val_prompt) + noise_shape = [batch_size, channels, frames, h, w] + + fps = torch.tensor([args.fps]*batch_size).to(accelerator.device).long() + + prompts = val_prompt + if isinstance(prompts, str): + prompts = [prompts] + + + with accelerator.autocast(): # mixed precision + if isinstance(peft_model, torch.nn.parallel.DistributedDataParallel): + text_emb = peft_model.module.get_learned_conditioning(prompts).to(accelerator.device) + else: + text_emb = peft_model.get_learned_conditioning(prompts).to(accelerator.device) + + if args.mode == 'base': + cond = {"c_crossattn": [text_emb], "fps": fps} + else: # TODO: implement i2v mode training in the future + raise NotImplementedError + + # Inference Step 3.2: inference, batch_samples shape: batch, , c, t, h, w + # no backprop_mode=args.backprop_mode because it is inference process + if isinstance(peft_model, torch.nn.parallel.DistributedDataParallel): + batch_samples = batch_ddim_sampling(peft_model.module, cond, noise_shape, args.n_samples, \ + args.ddim_steps, args.ddim_eta, args.unconditional_guidance_scale, None, decode_frame=args.decode_frame, **kwargs) + else: + batch_samples = batch_ddim_sampling(peft_model, cond, noise_shape, args.n_samples, \ + args.ddim_steps, args.ddim_eta, args.unconditional_guidance_scale, None, decode_frame=args.decode_frame, **kwargs) + + # batch_samples: b,samples,c,t,h,w + dir_name = os.path.join(output_dir, "samples") + # filenames should be related to the gpu index + # get timestamps for filenames to avoid overwriting + # current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S") + filenames = [f"temporal"] # only one sample + # if dir_name is not exists, create it + os.makedirs(dir_name, exist_ok=True) + + save_videos(batch_samples, dir_name, filenames, fps=args.savefps) + + results["filenames"].extend(filenames) + results["dir_name"].extend([dir_name]*len(filenames)) + results["prompt"].extend(prompts) + results=[ results ] # transform to list, otherwise gather_object() will not collect correctly + + # Inference Step 3.3: collect inference results and save the videos to wandb + # collect inference results from all the GPUs + results_gathered=gather_object(results) + + if accelerator.is_main_process: + filenames = [] + dir_name = [] + prompts = [] + for i in range(len(results_gathered)): + filenames.extend(results_gathered[i]["filenames"]) + dir_name.extend(results_gathered[i]["dir_name"]) + prompts.extend(results_gathered[i]["prompt"]) + + logger.info("Validation sample saved!") + + # # batch size is 1, so only one video is generated + + # video = get_videos(batch_samples) + + # # read the video from the saved path + video_path = os.path.join(dir_name[0], filenames[0]+".mp4") + + + + # release memory + del batch_samples + torch.cuda.empty_cache() + gc.collect() + + return video_path + + # end of inference only, training script continues + # ================================================================== + + +def setup_model(lora_ckpt_path="huggingface-pickscore", lora_rank=16): + parser = get_parser() + args = parser.parse_args() + + ## ------------------------step 2: model config----------------------------- + # download the checkpoint for VideoCrafter2 model + ckpt_dir = args.ckpt_path.split('/') # args.ckpt='checkpoints/base_512_v2/model.ckpt' -> 'checkpoints/base_512_v2' + ckpt_dir = '/'.join(ckpt_dir[:-1]) + snapshot_download(repo_id='VideoCrafter/VideoCrafter2', local_dir =ckpt_dir) + + # load the model + config = OmegaConf.load(args.config) + model_config = config.pop("model", OmegaConf.create()) + model = instantiate_from_config(model_config) + + assert os.path.exists(args.ckpt_path), f"Error: checkpoint [{args.ckpt_path}] Not Found!" + model = load_model_checkpoint(model, args.ckpt_path) + + # convert first_stage_model and cond_stage_model to torch.float16 if mixed_precision is True + if args.mixed_precision != 'no': + model.first_stage_model = model.first_stage_model.half() + model.cond_stage_model = model.cond_stage_model.half() + + # step 2.1: add LoRA using peft + config = peft.LoraConfig( + r=args.lora_rank, + target_modules=["to_k", "to_v", "to_q"], # only diffusion_model has these modules + lora_dropout=0.01, + ) + + peft_model = peft.get_peft_model(model, config) + + peft_model.print_trainable_parameters() + + # load the pretrained LoRA model + if lora_ckpt_path != "Base Model": + if lora_ckpt_path == "huggingface-hps-aesthetic": # download the pretrained LoRA model from huggingface + snapshot_download(repo_id='zheyangqin/VADER', local_dir ='VADER-VideoCrafter/checkpoints/pretrained_lora') + lora_ckpt_path = 'VADER-VideoCrafter/checkpoints/pretrained_lora/vader_videocrafter_hps_aesthetic.pt' + elif lora_ckpt_path == "huggingface-pickscore": # download the pretrained LoRA model from huggingface + snapshot_download(repo_id='zheyangqin/VADER', local_dir ='VADER-VideoCrafter/checkpoints/pretrained_lora') + lora_ckpt_path = 'VADER-VideoCrafter/checkpoints/pretrained_lora/vader_videocrafter_pickscore.pt' + # load the pretrained LoRA model + peft.set_peft_model_state_dict(peft_model, torch.load(lora_ckpt_path)) + + print("Model setup complete!") + return peft_model + + +def main_fn(prompt, seed=200, height=320, width=512, unconditional_guidance_scale=12, ddim_steps=25, ddim_eta=1.0, + frames=24, savefps=10, model=None): + + parser = get_parser() + args = parser.parse_args() + + + + # overwrite the default arguments + args.prompt_str = prompt + args.seed = seed + args.height = height + args.width = width + args.unconditional_guidance_scale = unconditional_guidance_scale + args.ddim_steps = ddim_steps + args.ddim_eta = ddim_eta + args.frames = frames + args.savefps = savefps + + seed_everything(args.seed) + + video_path = run_training(args, model) + + return video_path + diff --git a/VADER-VideoCrafter/scripts/run_text2video_inference.sh b/VADER-VideoCrafter/scripts/run_text2video_inference.sh new file mode 100644 index 0000000000000000000000000000000000000000..02f771622ccf7fac0964092d5078602775787862 --- /dev/null +++ b/VADER-VideoCrafter/scripts/run_text2video_inference.sh @@ -0,0 +1,30 @@ +ckpt='checkpoints/base_512_v2/model.ckpt' +config='configs/inference_t2v_512_v2.0.yaml' +PORT=$((20000 + RANDOM % 10000)) + +accelerate launch --multi_gpu --main_process_port $PORT scripts/main/train_t2v_lora.py \ +--seed 200 \ +--mode 'base' \ +--ckpt_path $ckpt \ +--config $config \ +--height 320 --width 512 \ +--unconditional_guidance_scale 12.0 \ +--ddim_steps 25 \ +--ddim_eta 1.0 \ +--frames 24 \ +--prompt_fn 'chatgpt_custom_cute' \ +--val_batch_size 1 \ +--num_val_runs 1 \ +--lora_rank 16 \ +--inference_only True \ +--project_dir ./project_dir/inference \ +--lora_ckpt_path huggingface-pickscore \ +--is_sample_preview True + + + +ckpt='checkpoints/base_512_v2/model.ckpt' +config='configs/inference_t2v_512_v2.0.yaml' +PORT=$((20000 + RANDOM % 10000)) + +accelerate launch --multi_gpu --main_process_port 15009 scripts/main/train_t2v_lora.py --seed 200 --mode 'base' --ckpt_path 'checkpoints/base_512_v2/model.ckpt' --config 'configs/inference_t2v_512_v2.0.yaml' --height 320 --width 512 --unconditional_guidance_scale 12.0 --ddim_steps 25 --ddim_eta 1.0 --frames 24 --prompt_fn 'chatgpt_custom_cute' --val_batch_size 1 --num_val_runs 1 --lora_rank 16 --inference_only True --project_dir ./project_dir/inference --lora_ckpt_path huggingface-pickscore --is_sample_preview True diff --git a/VADER-VideoCrafter/scripts/run_text2video_train.sh b/VADER-VideoCrafter/scripts/run_text2video_train.sh new file mode 100644 index 0000000000000000000000000000000000000000..7c982c94b11e3fce97a36849cadc3ab512a3b072 --- /dev/null +++ b/VADER-VideoCrafter/scripts/run_text2video_train.sh @@ -0,0 +1,28 @@ +ckpt='checkpoints/base_512_v2/model.ckpt' +config='configs/inference_t2v_512_v2.0.yaml' +PORT=$((20000 + RANDOM % 10000)) + +accelerate launch --multi_gpu --main_process_port $PORT scripts/main/train_t2v_lora.py \ +--seed 300 \ +--mode 'base' \ +--ckpt_path $ckpt \ +--config $config \ +--height 320 --width 512 \ +--unconditional_guidance_scale 12.0 \ +--ddim_steps 25 \ +--ddim_eta 1.0 \ +--frames 12 \ +--prompt_fn 'chatgpt_custom_instruments' \ +--gradient_accumulation_steps 8 \ +--num_train_epochs 200 \ +--train_batch_size 1 \ +--val_batch_size 1 \ +--num_val_runs 1 \ +--reward_fn 'aesthetic_hps' \ +--decode_frame '-1' \ +--hps_version 'v2.1' \ +--lr 0.0002 \ +--validation_steps 10 \ +--lora_rank 16 \ +--is_sample_preview True + diff --git a/VADER-VideoCrafter/utils/utils.py b/VADER-VideoCrafter/utils/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c73b93e006c4250161b427e4d1fff512ca046f7c --- /dev/null +++ b/VADER-VideoCrafter/utils/utils.py @@ -0,0 +1,77 @@ +import importlib +import numpy as np +import cv2 +import torch +import torch.distributed as dist + + +def count_params(model, verbose=False): + total_params = sum(p.numel() for p in model.parameters()) + if verbose: + print(f"{model.__class__.__name__} has {total_params*1.e-6:.2f} M params.") + return total_params + + +def check_istarget(name, para_list): + """ + name: full name of source para + para_list: partial name of target para + """ + istarget=False + for para in para_list: + if para in name: + return True + return istarget + + +def instantiate_from_config(config): + if not "target" in config: + if config == '__is_first_stage__': + return None + elif config == "__is_unconditional__": + return None + raise KeyError("Expected key `target` to instantiate.") + return get_obj_from_str(config["target"])(**config.get("params", dict())) + + +def get_obj_from_str(string, reload=False): + module, cls = string.rsplit(".", 1) + if reload: + module_imp = importlib.import_module(module) + importlib.reload(module_imp) + return getattr(importlib.import_module(module, package=None), cls) + + +def load_npz_from_dir(data_dir): + data = [np.load(os.path.join(data_dir, data_name))['arr_0'] for data_name in os.listdir(data_dir)] + data = np.concatenate(data, axis=0) + return data + + +def load_npz_from_paths(data_paths): + data = [np.load(data_path)['arr_0'] for data_path in data_paths] + data = np.concatenate(data, axis=0) + return data + + +def resize_numpy_image(image, max_resolution=512 * 512, resize_short_edge=None): + h, w = image.shape[:2] + if resize_short_edge is not None: + k = resize_short_edge / min(h, w) + else: + k = max_resolution / (h * w) + k = k**0.5 + h = int(np.round(h * k / 64)) * 64 + w = int(np.round(w * k / 64)) * 64 + image = cv2.resize(image, (w, h), interpolation=cv2.INTER_LANCZOS4) + return image + + +def setup_dist(args): + if dist.is_initialized(): + return + torch.cuda.set_device(args.local_rank) + torch.distributed.init_process_group( + 'nccl', + init_method='env://' + ) \ No newline at end of file diff --git a/app.py b/app.py index cbffdf1ba490e3ae1fb244c10909cccfa7652993..abc0cddec12c0e709857cd3c55e42b4106fcb1b3 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,210 @@ import gradio as gr +import os -def greet(name): - return "Hello " + name + "!!" +import sys +sys.path.append('./VADER-VideoCrafter/scripts/main') +sys.path.append('./VADER-VideoCrafter/scripts') +sys.path.append('./VADER-VideoCrafter') -demo = gr.Interface(fn=greet, inputs="text", outputs="text") -demo.launch() \ No newline at end of file +from train_t2v_lora import main_fn, setup_model + +model = None # Placeholder for model + +def gradio_main_fn(prompt, seed, height, width, unconditional_guidance_scale, ddim_steps, ddim_eta, + frames, savefps): + global model + if model is None: + return "Model is not loaded. Please load the model first." + video_path = main_fn(prompt=prompt, + seed=int(seed), + height=int(height), + width=int(width), + unconditional_guidance_scale=float(unconditional_guidance_scale), + ddim_steps=int(ddim_steps), + ddim_eta=float(ddim_eta), + frames=int(frames), + savefps=int(savefps), + model=model) + + return video_path + +def reset_fn(): + return ("A mermaid with flowing hair and a shimmering tail discovers a hidden underwater kingdom adorned with coral palaces, glowing pearls, and schools of colorful fish, encountering both wonders and dangers along the way.", + 200, 320, 512, 12.0, 25, 1.0, 24, 16, 10, "huggingface-pickscore") + +def update_lora_rank(lora_model): + if lora_model == "huggingface-pickscore": + return gr.update(value=16) + elif lora_model == "huggingface-hps-aesthetic": + return gr.update(value=8) + else: # "Base Model" + return gr.update(value=0) + +def update_dropdown(lora_rank): + if lora_rank == 16: + return gr.update(value="huggingface-pickscore") + elif lora_rank == 8: + return gr.update(value="huggingface-hps-aesthetic") + else: # 0 + return gr.update(value="Base Model") + + +def setup_model_progress(lora_model, lora_rank): + global model + + # Disable buttons and show loading indicator + yield (gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), "Loading model...") + + model = setup_model(lora_model, lora_rank) # Ensure you pass the necessary parameters to the setup_model function + + # Enable buttons after loading and update indicator + yield (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), "Model loaded successfully") + +css = """ +.centered { + display: flex; + justify-content: center; +} +""" + + +with gr.Blocks(css=css) as demo: + with gr.Row(): + with gr.Column(): + gr.HTML( + """ +

+ Video Diffusion Alignment via Reward Gradient +

+ """ + ) + gr.HTML( + """ + + +
+ Mihir Prabhudesai*         + Russell Mendonca*         + Zheyang Qin*         + Katerina Fragkiadaki         + Deepak Pathak + + +
+
+ Carnegie Mellon University +
+ + """ + ) + gr.HTML( + """ + + + + + + + + """ + ) + + with gr.Row(elem_classes="centered"): + with gr.Column(scale=0.6): + output_video = gr.Video() + + with gr.Row(): + lora_model = gr.Dropdown( + label="VADER Model", + choices=["huggingface-pickscore", "huggingface-hps-aesthetic", "Base Model"], + value="huggingface-pickscore" + ) + lora_rank = gr.Slider(minimum=0, maximum=16, label="LoRA Rank", step = 8, value=16) + load_btn = gr.Button("Load Model") + # Add a label to show the loading indicator + loading_indicator = gr.Label(value="", label="Loading Indicator") + + prompt = gr.Textbox(placeholder="Enter prompt text here", lines=4, label="Text Prompt", + value="A mermaid with flowing hair and a shimmering tail discovers a hidden underwater kingdom adorned with coral palaces, glowing pearls, and schools of colorful fish, encountering both wonders and dangers along the way.") + + seed = gr.Slider(minimum=0, maximum=65536, label="Seed", step = 1, value=200) + + run_btn = gr.Button("Run Inference") + + + with gr.Row(): + height = gr.Slider(minimum=0, maximum=1024, label="Height", step = 16, value=320) + width = gr.Slider(minimum=0, maximum=1024, label="Width", step = 16, value=512) + + with gr.Row(): + frames = gr.Slider(minimum=0, maximum=50, label="Frames", step = 1, value=24) + savefps = gr.Slider(minimum=0, maximum=60, label="Save FPS", step = 1, value=10) + + + with gr.Row(): + DDIM_Steps = gr.Slider(minimum=0, maximum=100, label="DDIM Steps", step = 1, value=25) + unconditional_guidance_scale = gr.Slider(minimum=0, maximum=50, label="Guidance Scale", step = 0.1, value=12.0) + DDIM_Eta = gr.Slider(minimum=0, maximum=1, label="DDIM Eta", step = 0.01, value=1.0) + + # reset button + reset_btn = gr.Button("Reset") + + reset_btn.click(fn=reset_fn, outputs=[prompt, seed, height, width, unconditional_guidance_scale, DDIM_Steps, DDIM_Eta, frames, lora_rank, savefps, lora_model]) + + + + load_btn.click(fn=setup_model_progress, inputs=[lora_model, lora_rank], outputs=[load_btn, run_btn, reset_btn, loading_indicator]) + run_btn.click(fn=gradio_main_fn, + inputs=[prompt, seed, height, width, unconditional_guidance_scale, DDIM_Steps, DDIM_Eta, frames, savefps], + outputs=output_video + ) + + lora_model.change(fn=update_lora_rank, inputs=lora_model, outputs=lora_rank) + lora_rank.change(fn=update_dropdown, inputs=lora_rank, outputs=lora_model) + +demo.launch() + +# main_fn(prompt="A mermaid with flowing hair and a shimmering tail discovers a hidden underwater kingdom adorned with coral palaces, glowing pearls, and schools of colorful fish, encountering both wonders and dangers along the way.",) \ No newline at end of file diff --git a/assets/activities.txt b/assets/activities.txt new file mode 100644 index 0000000000000000000000000000000000000000..abea0458a5836b50ec85da2f732ff3a7d63b8c3a --- /dev/null +++ b/assets/activities.txt @@ -0,0 +1,3 @@ +washing the dishes +riding a bike +playing chess \ No newline at end of file diff --git a/assets/chatgpt_custom.txt b/assets/chatgpt_custom.txt new file mode 100644 index 0000000000000000000000000000000000000000..1552981a94e6cdbdd11e8e2c3951c92c28e7c820 --- /dev/null +++ b/assets/chatgpt_custom.txt @@ -0,0 +1,50 @@ +A dog catching a red frisbee in mid-air with the sun setting behind, casting a golden glow. +A dog fetching a stick thrown into a crystal-clear lake surrounded by trees. +A dog running alongside a bicycle on a park trail lined with golden autumn leaves. +A dog leaping over a fence to catch a ball in a yard illuminated by string lights. +A dog performing agility course obstacles with precision in a park decorated with lanterns. +A dog running through a tunnel on an agility course with soft, colorful lighting. +A dog playing tug-of-war with a human in the backyard under a canopy of twinkling lights. +A dog retrieving a ball from the water in a pond surrounded by blooming lilies. +A dog weaving through poles on an agility course with soft lighting creating a magical atmosphere. +A dog running to catch a thrown toy in the air with vibrant flowers in the background. +A dog diving into a pool to retrieve a toy with sunlight sparkling on the water. +A dog catching a ball on the run during a game of fetch in a picturesque park. +A dog jumping through a hoop in an agility course with colorful flags. +A dog playing with a toy in a garden filled with blooming flowers. +A dog retrieving a frisbee from the air in a park under a bright blue sky. +A dog running at full speed across a field with a rainbow overhead. +A dog chasing a thrown stick in a field with golden sunlight. +A dog performing tricks in a park filled with children and laughter. +A dog chasing its tail in a backyard with colorful flowers. +A dog cuddling with a human under a tree with soft, ambient lighting. +A dog and cat playing together in a garden with soft morning light filtering through the trees. +A dog and cat chasing each other around the living room with fairy lights twinkling. +A dog and cat lounging together under a tree with soft, ambient lighting. +A dog and cat playing with a toy in a cozy, warmly lit room. +A dog and cat cuddling with a human on a couch with a fireplace glowing. +A cat jumping onto a high shelf to reach a dangling toy mouse, with soft light filtering through the window. +A cat chasing a laser pointer around the living room with fairy lights twinkling in the background. +A cat climbing up a curtain to reach a window ledge overlooking a moonlit garden. +A cat playing with a toy on a string hanging from a door, with soft ambient lighting creating a cozy atmosphere. +A cat pouncing on a moving object on the floor with gentle candlelight flickering nearby. +A cat batting at a moving object on a string, with a fireplace crackling in the background. +A cat chasing after a butterfly in the garden with soft morning light filtering through the trees. +A cat stalking a bird from behind a bush in the yard, with evening light casting long shadows. +A cat swiping at a moving toy on the floor with a cozy, warmly lit room. +A cat jumping from one surface to another on a bookshelf with soft lighting. +A cat chasing a toy car on the floor with sunlight streaming through the window. +A cat leaping to catch a feather toy hanging from a door with sunlight streaming in. +A cat playing with a ball in the living room with soft ambient lighting. +A cat batting at a dangling feather toy in a cozy, warmly lit room. +A cat chasing a string in a room filled with soft, ambient light. +A cat playing hide and seek with a toy in the living room with a warm glow. +A cat lounging in a sunbeam with a toy nearby. +A cat curling up in a cozy bed with soft ambient light around. +A cat playing with a small ball in a room with a warm, inviting atmosphere. +A cat and dog playing together in a garden with soft morning light filtering through the trees. +A cat and dog chasing each other around the living room with fairy lights twinkling. +A cat and dog lounging together under a tree with soft, ambient lighting. +A cat and dog playing with a toy in a cozy, warmly lit room. +A cat and dog cuddling with a human on a couch with a fireplace glowing. +A cat jumping onto a high shelf to reach a dangling toy mouse, with soft light filtering through the window. \ No newline at end of file diff --git a/assets/chatgpt_custom_actpred.txt b/assets/chatgpt_custom_actpred.txt new file mode 100644 index 0000000000000000000000000000000000000000..d1c2ae795a67914da2f1ee6cfd1168e1dbab73c7 --- /dev/null +++ b/assets/chatgpt_custom_actpred.txt @@ -0,0 +1,41 @@ +A young girl playing the piano in a cozy living room. +A boy playing guitar on a sunny beach. +A woman chopping wood in a forest. +A girl jogging along a scenic trail. +A boy playing the piano in a school auditorium. +A young woman playing guitar by a campfire. +A woman jogging in a park at sunrise. +A young girl chopping wood in a rustic backyard. +A boy jogging along a beach at sunset. +A man playing the piano in a modern living room. +A woman playing guitar at a local music festival. +A boy chopping wood in a snowy forest. +A young man jogging through a busy city street. +A woman playing the piano in an elegant ballroom. +A man playing guitar in a cozy café. +A boy jogging on a mountain trail. +A young girl playing the piano in a music classroom. +A man chopping wood in a rural countryside. +A woman jogging along a riverbank. +A boy playing guitar on a rooftop terrace. +A young man chopping wood in a quiet forest. +A woman playing the piano in a beautifully decorated hall. +A man jogging in a suburban neighborhood. +A girl playing guitar at a summer camp. +A young woman jogging on a scenic beach. +A man playing the piano at a fancy restaurant. +A woman chopping wood in her backyard. +A young girl jogging in a peaceful park. +A boy playing guitar in a busy city square. +A woman playing guitar in a cozy living room. +A girl jogging through a bustling market. +A boy playing the piano at a local talent show. +A young man chopping wood at a cabin retreat. +A young girl jogging in a colorful garden. +A man playing guitar at a rooftop party. +A woman playing the piano in a vintage-style parlor. +A boy chopping wood on a farm. +A young man jogging in an urban park. +A woman playing guitar in a chic coffee shop. +A man jogging along a cliffside trail. +A young girl playing the piano in a sunlit room. \ No newline at end of file diff --git a/assets/chatgpt_custom_actpred2.txt b/assets/chatgpt_custom_actpred2.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0be8e8e4f92e39ac71669b82a3a6bf972fd1555 --- /dev/null +++ b/assets/chatgpt_custom_actpred2.txt @@ -0,0 +1,50 @@ +A young girl playing the piano in a cozy living room. +A boy playing guitar on a sunny beach. +A man playing the violin in a bustling city square. +A woman chopping wood in a forest. +A girl jogging along a scenic trail. +A boy playing the piano in a school auditorium. +A young woman playing guitar by a campfire. +A man playing the violin in a grand concert hall. +A woman jogging in a park at sunrise. +A young girl chopping wood in a rustic backyard. +A boy jogging along a beach at sunset. +A man playing the piano in a modern living room. +A woman playing guitar at a local music festival. +A young girl playing the violin in a tranquil garden. +A boy chopping wood in a snowy forest. +A young man jogging through a busy city street. +A woman playing the piano in an elegant ballroom. +A man playing guitar in a cozy café. +A girl playing the violin in a historic church. +A boy jogging on a mountain trail. +A young girl playing the piano in a music classroom. +A man chopping wood in a rural countryside. +A woman jogging along a riverbank. +A boy playing guitar on a rooftop terrace. +A young girl playing the violin in a flower-filled meadow. +A young man chopping wood in a quiet forest. +A woman playing the piano in a beautifully decorated hall. +A man jogging in a suburban neighborhood. +A girl playing guitar at a summer camp. +A boy playing the violin in a quaint library. +A young woman jogging on a scenic beach. +A man playing the piano at a fancy restaurant. +A woman chopping wood in her backyard. +A young girl jogging in a peaceful park. +A boy playing guitar in a busy city square. +A man playing the violin in a modern art gallery. +A woman playing guitar in a cozy living room. +A girl jogging through a bustling market. +A boy playing the piano at a local talent show. +A young man chopping wood at a cabin retreat. +A woman playing the violin on a balcony overlooking a city. +A young girl jogging in a colorful garden. +A man playing guitar at a rooftop party. +A woman playing the piano in a vintage-style parlor. +A boy chopping wood on a farm. +A girl playing the violin in a serene forest. +A young man jogging in an urban park. +A woman playing guitar in a chic coffee shop. +A man jogging along a cliffside trail. +A young girl playing the piano in a sunlit room. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal.txt b/assets/chatgpt_custom_animal.txt new file mode 100644 index 0000000000000000000000000000000000000000..ebb742b6c12e12c03089493337d938a25ed6be68 --- /dev/null +++ b/assets/chatgpt_custom_animal.txt @@ -0,0 +1,24 @@ +A happy dog running in a sunny park, with children playing in the background and birds flying above. +A cozy cat sleeping on a couch, surrounded by soft pillows and a warm blanket, with a gentle breeze coming through an open window. +A graceful bird flying across a clear blue sky, with fluffy white clouds and distant mountains in the background. +An excited dog fetching a bright red frisbee and bringing it back to its smiling owner in a green park. +A curious cat climbing a tall tree to chase a quick squirrel in a thick forest. +A big elephant spraying water with its trunk, making a rainbow in the bright sunlight at a calm waterhole. +A friendly dog playing with a curious cat in a colorful garden full of blooming flowers and buzzing bees. +A busy bird feeding its hungry chicks in a cozy nest on a tall tree, with the morning sun shining. +A strong lion and a graceful lioness resting together in the shade of a big tree on a wide grassland. +A playful penguin sliding down a snowy hill with its friends, the snow sparkling under a bright winter sun. +A clever monkey riding a small bicycle through a lively village, drawing the amazed looks of local children. +A tall giraffe bending down to drink water from a clear river, surrounded by green trees and colorful birds. +A majestic whale swimming in the deep blue ocean during a beautiful sunset, with the sky painted in orange and pink colors. +A peaceful deer eating grass in a thick forest, with sunlight filtering through the trees creating a magical feeling. +A determined turtle crawling on a sandy beach, with waves gently crashing in the background under a bright, clear sky. +A joyful dog playing in the snow, leaving paw prints and trying to catch snowflakes on its nose. +A cautious cat hiding from the rain under a tree, its fur slightly wet and raindrops making ripples in puddles. +A lively horse running through a field of blooming flowers in spring, the landscape full of colors and sweet smells. +A sad puppy looking sorry after being scolded, its ears down and eyes wet with tears in a quiet room. +An excited kitten playing with a ball of yarn, moving quickly and nimbly, in a cozy living room full of toys. +A curious elephant exploring its surroundings, its trunk gently touching things and eyes wide with wonder in a dense jungle. +A group of dolphins jumping out of the water, the sun setting behind them and casting a golden light on the waves. +A flock of birds flying during autumn, with colorful leaves swirling around them in the cool air. +A content panda eating bamboo in a peaceful forest, with a gentle waterfall flowing in the background. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_action.txt b/assets/chatgpt_custom_animal_action.txt new file mode 100644 index 0000000000000000000000000000000000000000..a52fea40204bb4a4c17c8bb3ba562be8ed5467fb --- /dev/null +++ b/assets/chatgpt_custom_animal_action.txt @@ -0,0 +1,40 @@ +Cat riding a skateboard. +Dog riding a skateboard. +Rabbit riding a skateboard. +Raccoon riding a skateboard. +Monkey riding a skateboard. +Hedgehog riding a skateboard. +Bat riding a skateboard. +Mouse riding a skateboard. +Cat working on a laptop. +Dog working on a laptop. +Rabbit working on a laptop. +Raccoon working on a laptop. +Monkey working on a laptop. +Hedgehog working on a laptop. +Bat working on a laptop. +Mouse working on a laptop. +Cat reading a book. +Dog reading a book. +Rabbit reading a book. +Raccoon reading a book. +Monkey reading a book. +Hedgehog reading a book. +Bat reading a book. +Mouse reading a book. +Cat eating an apple. +Dog eating an apple. +Rabbit eating an apple. +Raccoon eating an apple. +Monkey eating an apple. +Hedgehog eating an apple. +Bat eating an apple. +Mouse eating an apple. +Cat eating watermelon. +Dog eating watermelon. +Rabbit eating watermelon. +Raccoon eating watermelon. +Monkey eating watermelon. +Hedgehog eating watermelon. +Bat eating watermelon. +Mouse eating watermelon. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_clothes.txt b/assets/chatgpt_custom_animal_clothes.txt new file mode 100644 index 0000000000000000000000000000000000000000..429755e6b8871d944445d180d2cc1d2fc77665ee --- /dev/null +++ b/assets/chatgpt_custom_animal_clothes.txt @@ -0,0 +1,50 @@ +A dog in a red shirt, running in a park. +A cat in a blue shirt, sitting on a windowsill. +A rabbit in a green shirt, pretending to cook. +A bear in a yellow shirt, splashing in puddles. +A penguin in a red scarf, waddling on ice. +A squirrel in a blue scarf, collecting acorns. +A monkey in a green scarf, juggling balls. +A lion in a yellow scarf, sitting on a rock. +A dolphin in a red hat, jumping out of the water. +A turtle in a blue hat, relaxing on a beach. +A dog in a firefighter hat, standing by a toy fire truck. +A cat in a ballet hat, practicing dance moves. +A rabbit in a magician's hat, pulling a carrot from a hat. +A bear in a cowboy hat, pretending to chop wood. +A penguin in a Santa hat, sliding on ice. +A squirrel in a knight's hat, holding a tiny sword. +A monkey in a detective hat, holding a magnifying glass. +A lion in a wizard's hat, looking at the stars. +A dolphin in a red shirt, hitting a ball with its nose. +A turtle in a blue shirt, holding a small wand. +A dog in a green shirt, skateboarding in a park. +A cat in a yellow shirt, sleeping on a bed. +A rabbit in a red shirt, picking flowers. +A bear in a blue shirt, fishing by a river. +A penguin in a green shirt, walking to an office. +A squirrel in a yellow shirt, collecting nuts. +A monkey in a red shirt, playing on a jungle gym. +A lion in a blue shirt, playing catch. +A dolphin in a green shirt, swimming near a boat. +A turtle in a yellow shirt, hiking on a trail. +A dog in a red hat, walking down an aisle. +A cat in a blue hat, sitting at a table. +A rabbit in a green hat, holding a diploma. +A bear in a yellow hat, trick-or-treating. +A penguin in a red scarf, decorating a Christmas tree. +A squirrel in a blue scarf, celebrating a birthday. +A monkey in a green scarf, holding confetti. +A lion in a yellow scarf, sitting by a cake. +A dolphin in a red hat, jumping in a pool. +A turtle in a blue hat, at a garden party. +A dog in a red jacket, playing in the snow. +A cat in a blue jacket, lounging under an umbrella. +A rabbit in a green jacket, hopping in a garden. +A bear in a yellow jacket, sitting by a campfire. +A penguin in a red jacket, skating on ice. +A squirrel in a blue jacket, enjoying a sunny day. +A monkey in a green jacket, swinging from trees. +A lion in a yellow jacket, building a sandcastle. +A dolphin in a red jacket, jumping through hoops. +A turtle in a blue jacket, celebrating a holiday. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_clothesV2.txt b/assets/chatgpt_custom_animal_clothesV2.txt new file mode 100644 index 0000000000000000000000000000000000000000..2325f8318c5a5bc8217efae33e26e79a0bad7660 --- /dev/null +++ b/assets/chatgpt_custom_animal_clothesV2.txt @@ -0,0 +1,50 @@ +The dog is wearing a red shirt and eating from a bowl. +The cat is wearing a blue shirt and playing with yarn. +The rabbit is wearing a green shirt and reading a book. +The bear is wearing a yellow shirt and painting a picture. +The raccoon is wearing a red dress and sliding down a tree. +The squirrel is wearing a blue dress and holding a nut. +The monkey is wearing a green dress and climbing a tree. +The lion is wearing a yellow dress and lying on a rock. +The fox is wearing a red hat and balancing a ball. +The panda is wearing a blue hat and looking at flowers. +The dog is wearing a firefighter hat and holding a hose. +The cat is wearing a ballet hat and standing on one foot. +The rabbit is wearing a magician's hat and holding a wand. +The bear is wearing a cowboy hat and tipping its hat. +The raccoon is wearing a Santa hat and carrying a gift. +The squirrel is wearing a knight's hat and holding a toy sword. +The monkey is wearing a detective hat and looking through a magnifying glass. +The lion is wearing a wizard's hat and holding a book. +The fox is wearing a red shirt and splashing water. +The panda is wearing a blue shirt and blowing bubbles. +The dog is wearing a green shirt and digging a hole. +The cat is wearing a yellow shirt and napping on a cushion. +The rabbit is wearing a red shirt and watering plants. +The bear is wearing a blue shirt and eating honey. +The raccoon is wearing a green shirt and building a small fort. +The squirrel is wearing a yellow shirt and sitting on a branch. +The monkey is wearing a red shirt and peeling a banana. +The lion is wearing a blue shirt and looking at the sky. +The fox is wearing a green shirt and sitting in a garden. +The panda is wearing a yellow shirt and walking in a forest +The dog is wearing a red hat and carrying a flower. +The cat is wearing a blue hat and sitting at a table. +The rabbit is wearing a green hat and holding a diploma. +The bear is wearing a yellow hat and holding a pumpkin. +The raccoon is wearing a red dress and hanging ornaments. +The squirrel is wearing a blue dress and eating cake. +The monkey is wearing a green dress and blowing a whistle. +The lion is wearing a yellow dress and standing next to a cake. +The fox is wearing a red hat and playing with leaves. +The panda is wearing a blue hat and holding a balloon. +The dog is wearing a red coat and sniffing the snow. +The cat is wearing a blue coat and sunbathing. +The rabbit is wearing a green coat and picking flowers. +The bear is wearing a yellow coat and roasting marshmallows. +The raccoon is wearing a red coat and holding a snowball. +The squirrel is wearing a blue coat and looking at leaves. +The monkey is wearing a green coat and eating fruit. +The lion is wearing a yellow coat and digging in the sand. +The fox is wearing a red coat and jumping in a pile of leaves. +The panda is wearing a blue coat and sitting by a fire. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_clothesV3.txt b/assets/chatgpt_custom_animal_clothesV3.txt new file mode 100644 index 0000000000000000000000000000000000000000..f50634304ab37d82df93ce2d3741c5ee5ac411ad --- /dev/null +++ b/assets/chatgpt_custom_animal_clothesV3.txt @@ -0,0 +1,96 @@ +Cat in a red jacket +Cat in a blue jacket +Cat in a yellow jacket +Cat in a black jacket +Cat in a red shirt +Cat in a blue shirt +Cat in a yellow shirt +Cat in a black shirt +Cat in a red dress +Cat in a blue dress +Cat in a yellow dress +Cat in a black dress +Dog in a red jacket +Dog in a blue jacket +Dog in a yellow jacket +Dog in a black jacket +Dog in a red shirt +Dog in a blue shirt +Dog in a yellow shirt +Dog in a black shirt +Dog in a red dress +Dog in a blue dress +Dog in a yellow dress +Dog in a black dress +Rabbit in a red jacket +Rabbit in a blue jacket +Rabbit in a yellow jacket +Rabbit in a black jacket +Rabbit in a red shirt +Rabbit in a blue shirt +Rabbit in a yellow shirt +Rabbit in a black shirt +Rabbit in a red dress +Rabbit in a blue dress +Rabbit in a yellow dress +Rabbit in a black dress +Raccoon in a red jacket +Raccoon in a blue jacket +Raccoon in a yellow jacket +Raccoon in a black jacket +Raccoon in a red shirt +Raccoon in a blue shirt +Raccoon in a yellow shirt +Raccoon in a black shirt +Raccoon in a red dress +Raccoon in a blue dress +Raccoon in a yellow dress +Raccoon in a black dress +Monkey in a red jacket +Monkey in a blue jacket +Monkey in a yellow jacket +Monkey in a black jacket +Monkey in a red shirt +Monkey in a blue shirt +Monkey in a yellow shirt +Monkey in a black shirt +Monkey in a red dress +Monkey in a blue dress +Monkey in a yellow dress +Monkey in a black dress +Hedgehog in a red jacket +Hedgehog in a blue jacket +Hedgehog in a yellow jacket +Hedgehog in a black jacket +Hedgehog in a red shirt +Hedgehog in a blue shirt +Hedgehog in a yellow shirt +Hedgehog in a black shirt +Hedgehog in a red dress +Hedgehog in a blue dress +Hedgehog in a yellow dress +Hedgehog in a black dress +Mouse in a red jacket +Mouse in a blue jacket +Mouse in a yellow jacket +Mouse in a black jacket +Mouse in a red shirt +Mouse in a blue shirt +Mouse in a yellow shirt +Mouse in a black shirt +Mouse in a red dress +Mouse in a blue dress +Mouse in a yellow dress +Mouse in a black dress +Panda in a red jacket +Panda in a blue jacket +Panda in a yellow jacket +Panda in a black jacket +Panda in a red shirt +Panda in a blue shirt +Panda in a yellow shirt +Panda in a black shirt +Panda in a red dress +Panda in a blue dress +Panda in a yellow dress +Panda in a black dress \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_housework.txt b/assets/chatgpt_custom_animal_housework.txt new file mode 100644 index 0000000000000000000000000000000000000000..622c1f6f89d9c3806fa4c9de5e6ea21e9de77de7 --- /dev/null +++ b/assets/chatgpt_custom_animal_housework.txt @@ -0,0 +1,150 @@ +The monkey is watering the plants. +The raccoon is vacuuming the carpet. +The cat is sweeping the floor. +The rabbit is wiping the windows. +The hedgehog is washing the car. +The dog is wiping the dining table. +The bat is dusting the shelves. +The monkey is watering the garden. +The raccoon is vacuuming the stairs. +The cat is sweeping the porch. +The rabbit is wiping the windowsills. +The hedgehog is washing the car windows. +The dog is wiping the kitchen table. +The bat is dusting the bookshelf. +The monkey is watering the indoor plants. +The raccoon is vacuuming the bedroom. +The cat is sweeping the garage. +The rabbit is wiping the patio doors. +The hedgehog is washing the car hood. +The dog is wiping the coffee table. +The bat is dusting the mantle. +The monkey is watering the hanging plants. +The raccoon is vacuuming the living room rug. +The cat is sweeping the basement. +The rabbit is wiping the bedroom windows. +The hedgehog is washing the car tires. +The dog is wiping the side table. +The bat is dusting the picture frames. +The monkey is watering the flower pots. +The raccoon is vacuuming the hallway. +The cat is sweeping the deck. +The rabbit is wiping the kitchen windows. +The hedgehog is washing the car bumper. +The dog is wiping the dining chairs. +The bat is dusting the TV stand. +The monkey is watering the balcony plants. +The raccoon is vacuuming the study. +The cat is sweeping the driveway. +The rabbit is wiping the glass doors. +The hedgehog is washing the car rear window. +The dog is wiping the dining table before dinner. +The bat is dusting the living room shelves. +The monkey is watering the backyard garden. +The raccoon is vacuuming the hallway carpet. +The cat is sweeping the patio. +The rabbit is wiping the sliding doors. +The hedgehog is washing the car front grille. +The dog is wiping the kitchen table after a meal. +The bat is dusting the study shelves. +The monkey is watering the windowsill plants. +The raccoon is vacuuming the nursery. +The cat is sweeping the front porch. +The rabbit is wiping the exterior windows. +The hedgehog is washing the car side mirrors. +The dog is wiping the dining table after dinner. +The bat is dusting the bedroom nightstands. +The monkey is watering the kitchen herb garden. +The raccoon is vacuuming the playroom. +The cat is sweeping the mudroom. +The rabbit is wiping the windows with a squeegee. +The hedgehog is washing the car headlights. +The dog is wiping the dining table after breakfast. +The bat is dusting the entertainment center. +The monkey is watering the sunroom plants. +The raccoon is vacuuming the family room. +The cat is sweeping the garden shed. +The rabbit is wiping the windows in the family room. +The hedgehog is washing the car roof. +The dog is wiping the living room table. +The bat is dusting the home office shelves. +The monkey is watering the front yard flowers. +The raccoon is vacuuming the home theater. +The cat is sweeping the steps. +The rabbit is wiping the kitchen windows with a sponge. +The hedgehog is washing the car front bumper. +The dog is wiping the dining table after lunch. +The bat is dusting the mantelpiece. +The monkey is watering the windowsill herbs. +The raccoon is vacuuming the dining room. +The cat is sweeping the entryway. +The rabbit is wiping the interior windows. +The hedgehog is washing the car fenders. +The dog is wiping the dining table before a meal. +The bat is dusting the entertainment center. +The monkey is watering the patio plants. +The raccoon is vacuuming the basement. +The cat is sweeping the sidewalk. +The rabbit is wiping the blinds. +The hedgehog is washing the car tires. +The dog is wiping the kitchen table before setting it. +The bat is dusting the bookshelf. +The monkey is watering the office plants. +The raccoon is vacuuming the guest room. +The cat is sweeping the garden shed. +The rabbit is wiping the playroom windows. +The hedgehog is washing the car roof. +The dog is wiping the dining table after breakfast. +The bat is dusting the picture frames. +The monkey is watering the porch plants. +The raccoon is vacuuming the family room carpet. +The cat is sweeping the garage. +The rabbit is wiping the sunroom windows. +The hedgehog is washing the car hood. +The dog is wiping the kitchen table after lunch. +The bat is dusting the nightstands. +The monkey is watering the balcony plants. +The raccoon is vacuuming the home theater carpet. +The cat is sweeping the front steps. +The rabbit is wiping the living room windows. +The hedgehog is washing the car side mirrors. +The dog is wiping the dining table before dinner. +The bat is dusting the study shelves. +The monkey is watering the backyard flowers. +The raccoon is vacuuming the bedroom carpet. +The cat is sweeping the patio. +The rabbit is wiping the glass doors. +The hedgehog is washing the car front grille. +The dog is wiping the dining table after a meal. +The bat is dusting the bookshelf. +The monkey is watering the hallway plants. +The raccoon is vacuuming the living room carpet. +The cat is sweeping the garage floor. +The rabbit is wiping the patio doors. +The hedgehog is washing the car bumper. +The dog is wiping the dining table before setting it. +The bat is dusting the family room shelves. +The monkey is watering the kitchen herbs. +The raccoon is vacuuming the playroom carpet. +The cat is sweeping the front porch. +The rabbit is wiping the windowsills. +The hedgehog is washing the car hood. +The dog is wiping the dining chairs. +The bat is dusting the TV stand. +The monkey is watering the garden. +The raccoon is vacuuming the stairs. +The cat is sweeping the basement. +The rabbit is wiping the kitchen windows. +The hedgehog is washing the car rear window. +The dog is wiping the dining table before dinner. +The bat is dusting the nightstands. +The monkey is watering the flower pots. +The raccoon is vacuuming the hallway. +The cat is sweeping the deck. +The rabbit is wiping the kitchen windows. +The hedgehog is washing the car tires. +The dog is wiping the dining chairs. +The bat is dusting the TV stand. +The monkey is watering the indoor plants. +The raccoon is vacuuming the bedroom carpet. +The cat is sweeping the floor. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_sport.txt b/assets/chatgpt_custom_animal_sport.txt new file mode 100644 index 0000000000000000000000000000000000000000..30dc538f5edf5e67350d1d8e33b18abf25faaf08 --- /dev/null +++ b/assets/chatgpt_custom_animal_sport.txt @@ -0,0 +1,25 @@ +A dog playing soccer in a green field, wearing a red jersey and kicking a ball towards a goal. +A cat playing tennis on a sunny court, wearing a white visor and swinging a racket to hit the ball. +A kangaroo boxing in a ring, wearing blue boxing gloves and shorts, jumping and throwing punches. +A bear lifting weights in a gym, wearing a headband and wristbands, straining as it lifts a heavy barbell. +A penguin ice skating in an indoor rink, wearing a bright scarf and gracefully gliding on the ice. +A rabbit running in a track race, wearing running shoes and a number bib, sprinting towards the finish line. +A horse playing polo on a green field, wearing a helmet and riding gear, skillfully hitting the ball with a mallet. +A squirrel surfing on a big wave, wearing a wetsuit and balancing on a surfboard with skill. +A monkey climbing a rock wall, wearing climbing shoes and a harness, reaching for the next hold with determination. +An elephant playing basketball in a playground, wearing a jersey and shooting the ball into the hoop with its trunk. +A group of dogs playing football, all wearing team jerseys and helmets, running and tackling each other on the field. +A pair of cats playing beach volleyball, wearing sunglasses and swim trunks, jumping to spike the ball over the net. +A herd of elephants playing rugby, wearing matching uniforms and charging towards the goal line. +A flock of birds playing badminton in a park, wearing tiny sweatbands and fluttering to hit the shuttlecock. +A team of rabbits playing baseball, wearing caps and gloves, one rabbit pitching the ball while another gets ready to bat. +A fox skateboarding in a skate park, wearing a helmet and knee pads, performing tricks on the ramps. +A cheetah racing in a motocross event, wearing a racing suit and helmet, speeding over dirt hills and jumps. +A raccoon doing parkour in an urban environment, wearing a bandana and wristbands, skillfully jumping between buildings. +A dolphin windsurfing on the ocean, wearing a bright life vest and maneuvering the board with precision. +A tiger bungee jumping off a bridge, wearing a safety harness and roaring as it plunges towards the river below. +A polar bear swimming competitively in a pool, wearing goggles and a swim cap, racing towards the finish line. +A seal playing water polo, wearing a swim cap and maneuvering the ball with its flippers in a heated match. +A duck rowing a boat in a lake, wearing a life jacket and paddling vigorously with its team. +A turtle snorkeling in a coral reef, wearing a snorkel mask and fins, exploring the colorful underwater world. +A frog kayaking down a river, wearing a helmet and life vest, skillfully navigating through the rapids. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_sportV2.txt b/assets/chatgpt_custom_animal_sportV2.txt new file mode 100644 index 0000000000000000000000000000000000000000..2738ce9e849240247edd4ede67627fb92c2e3326 --- /dev/null +++ b/assets/chatgpt_custom_animal_sportV2.txt @@ -0,0 +1,50 @@ +A dog playing soccer in a green field, wearing a red jersey and kicking a ball towards a goal. +A cat playing tennis on a sunny court, wearing a white visor and swinging a racket to hit the ball. +A kangaroo boxing in a ring, wearing blue boxing gloves and shorts, jumping and throwing punches. +A bear lifting weights in a gym, wearing a headband and wristbands, straining as it lifts a heavy barbell. +A penguin ice skating in an indoor rink, wearing a bright scarf and gracefully gliding on the ice. +A rabbit running in a track race, wearing running shoes and a number bib, sprinting towards the finish line. +A sea otter surfing on a big wave, wearing a wetsuit and balancing on a surfboard with ease. +A monkey climbing a rock wall, wearing climbing shoes and a harness, reaching for the next hold with determination. +An elephant playing basketball in a playground, wearing a jersey and shooting the ball into the hoop with its trunk. +A kangaroo playing rugby, wearing a jersey and running with the ball towards the goal line. +A group of dogs playing football, all wearing team jerseys and helmets, running and tackling each other on the field. +A pair of cats playing beach volleyball, wearing sunglasses and swim trunks, jumping to spike the ball over the net. +A herd of elephants playing rugby, wearing matching uniforms and charging towards the goal line. +A team of rabbits playing baseball, wearing caps and gloves, one rabbit pitching the ball while another gets ready to bat. +A team of squirrels playing soccer, wearing colorful jerseys and skillfully passing the ball to each other. +A fox skateboarding in a skate park, wearing a helmet and knee pads, performing tricks on the ramps. +A cheetah racing in a motocross event, wearing a racing suit and helmet, speeding over dirt hills and jumps. +A raccoon doing parkour in an urban environment, wearing a bandana and wristbands, skillfully jumping between buildings. +A dolphin windsurfing on the ocean, wearing a bright life vest and maneuvering the board with precision. +A squirrel bungee jumping off a bridge, wearing a safety harness and looking excited as it bounces. +A polar bear swimming competitively in a pool, wearing goggles and a swim cap, racing towards the finish line. +A seal playing water polo, wearing a swim cap and maneuvering the ball with its flippers in a heated match. +A duck rowing a boat in a calm lake, wearing a life jacket and paddling vigorously with its webbed feet. +A turtle snorkeling in a coral reef, wearing a snorkel mask and fins, exploring the colorful underwater world. +A frog kayaking down a river, wearing a helmet and life vest, skillfully navigating through the rapids. +A dog sledding in a snowy landscape, wearing a harness and pulling a sled through the snow. +A rabbit skiing down a snowy hill, wearing a winter hat and goggles, expertly navigating the slopes. +A penguin sliding down an icy slope, wearing a scarf and having fun in the winter wonderland. +A polar bear playing ice hockey, wearing a jersey and skates, skillfully handling the puck with its stick. +A cat snowboarding on a mountain, wearing a colorful jacket and performing tricks on the snow. +A dog playing frisbee in a park, wearing a bandana and leaping high to catch the flying disc. +A cat performing gymnastics, wearing a leotard and balancing gracefully on a beam. +A rabbit participating in an agility course, wearing a harness and quickly navigating through the obstacles. +A bear doing archery in a field, wearing a quiver and aiming carefully at the target. +A kangaroo playing basketball, wearing a jersey and dribbling the ball before making a jump shot. +A group of dogs playing basketball, wearing team jerseys and passing the ball to each other. +A pair of cats playing doubles tennis, wearing matching visors and coordinating their shots. +A team of rabbits playing soccer, wearing colorful jerseys and skillfully dribbling the ball. +A pair of kangaroos playing doubles badminton, wearing wristbands and jumping to hit the shuttlecock. +A team of monkeys playing volleyball, wearing headbands and high-fiving each other after a point. +A cheetah running in a marathon, wearing running shoes and a number bib, speeding towards the finish line. +A raccoon skateboarding down a city street, wearing a helmet and knee pads, performing tricks on the pavement. +A dolphin doing synchronized swimming, wearing a swim cap and gracefully moving in sync with others. +A squirrel doing a high-wire act in a circus, wearing a tiny costume and balancing skillfully on the rope. +A monkey doing bungee jumping, wearing a safety harness and looking thrilled as it bounces up and down. +A sea lion surfing on the ocean waves, wearing a wetsuit and riding the surfboard with expertise. +A duck swimming in a lake race, wearing a small cap and paddling swiftly with its webbed feet. +A turtle diving in a pool, wearing goggles and fins, gracefully exploring the underwater environment. +A frog rowing a boat in a calm river, wearing a life jacket and paddling smoothly with its strong legs. +A seal doing synchronized swimming, wearing a swim cap and performing elegant moves in the water. \ No newline at end of file diff --git a/assets/chatgpt_custom_animal_technology.txt b/assets/chatgpt_custom_animal_technology.txt new file mode 100644 index 0000000000000000000000000000000000000000..ec743a90a816c7725d3f110cdcf584f1b3ef5947 --- /dev/null +++ b/assets/chatgpt_custom_animal_technology.txt @@ -0,0 +1,50 @@ +The dog is using a computer, typing on the keyboard. +The cat is talking on a phone. +The rabbit is playing a video game with a controller. +The bear is watching TV. +The raccoon is taking a selfie with a phone. +The squirrel is wearing headphones and listening to music. +The monkey is using a tablet. +The lion is using a laptop. +The fox is changing TV channels with a remote control. +The panda is checking the time on a smartwatch. +The dog is browsing on a tablet. +The cat is taking a photo with a camera. +The rabbit is video calling a friend on a laptop. +The bear is listening to music on a speaker. +The raccoon is charging a phone. +The squirrel is reading an e-book on a tablet. +The monkey is typing on a laptop. +The lion is taking a video with a phone. +The fox is setting an alarm on a smartwatch. +The panda is playing a game on a tablet. +The dog is watching videos on a phone. +The cat is using a fitness tracker. +The rabbit is browsing the internet on a computer. +The bear is listening to a podcast on a phone. +The raccoon is texting on a phone. +The squirrel is watching a movie on a tablet. +The monkey is scrolling through social media on a phone. +The lion is video calling a friend on a laptop. +The fox is using a calculator on a phone. +The panda is playing a video game with a controller. +The dog is checking email on a computer. +The cat is listening to music with earbuds. +The rabbit is using a webcam for a video chat. +The bear is typing a document on a laptop. +The raccoon is taking a photo with a tablet. +The squirrel is using a microwave to heat food. +The monkey is reading a digital book on an e-reader. +The lion is taking a picture with a smartphone. +The fox is watching a show on a tablet. +The panda is checking the weather on a smartphone. +The dog is using a laptop to write a story. +The cat is using a smart light to change colors. +The rabbit is listening to an audiobook on a phone. +The bear is making a video call on a tablet. +The raccoon is setting a reminder on a smartwatch. +The squirrel is using a digital thermometer to check the temperature. +The monkey is editing a photo on a laptop. +The lion is playing music on a Bluetooth speaker. +The fox is setting a timer on a smartphone. +The panda is watching a tutorial on a computer. \ No newline at end of file diff --git a/assets/chatgpt_custom_banana.txt b/assets/chatgpt_custom_banana.txt new file mode 100644 index 0000000000000000000000000000000000000000..059a474863dcbf7e4cc2fc4926f8e836e3151109 --- /dev/null +++ b/assets/chatgpt_custom_banana.txt @@ -0,0 +1,50 @@ +A banana and a vase of flowers on a marble kitchen counter. +A banana and a cup of tea on a wooden desk. +A banana and a water bottle on a park bench. +A banana and a small pot of herbs on a windowsill. +A banana and a wicker basket on a picnic table. +A banana and a soft blanket on a bookshelf. +A banana and a lamp on a nightstand. +A banana and a latte on a café table. +A banana and sunglasses on a car dashboard. +A banana and fine china on a dining table. +A banana and a notebook on a classroom desk. +A banana and a water bottle on a gym bench. +A banana and seashells on a beach towel. +A banana and a potted plant on a windowsill. +A banana and candles on a fireplace mantel. +A banana and flowers in a bicycle basket. +A banana and old books on a library table. +A banana and a remote control on a couch armrest. +A banana and vegetables on a kitchen island. +A banana and a small vase of daisies on a restaurant table. +A banana and a book on a park table. +A banana and sheet music on a grand piano. +A banana and roses on a garden bench. +A banana and a water bottle on a yoga mat. +A banana and a cup of pens on a teacher’s desk. +A banana and a glass of water on a bedside table. +A banana and paintbrushes on a workbench. +A banana and a playbill on a theater seat. +A banana and a small bag of snacks on a park fountain edge. +A banana and a fishing rod on a boat deck. +A banana and other fruits on a street vendor’s cart. +A banana and a glass of wine on a rooftop terrace table. +A banana and a bag of popcorn on a stadium seat. +A banana and a coffee mug on a computer desk. +A banana and a book on a garden swing. +A banana and a magazine on a city bus seat. +A banana and a travel brochure on a hotel lobby table. +A banana and a stack of notebooks on a classroom bookshelf. +A banana and a cappuccino on a café counter. +A banana and a water bottle on a treadmill console. +A banana and a bowl of nuts on a restaurant bar counter. +A banana and a thermos of tea on a park path bench. +A banana and a magazine on a hospital waiting room chair. +A banana and a suitcase on a train station bench. +A banana and a basket of snacks on a beach picnic blanket. +A banana and a program on a concert hall seat. +A banana and a small vase of flowers on a kitchen window ledge. +A banana and a sketchbook on an artist’s worktable. +A banana and a water bottle on a hiking trail bench. +A banana and a cup of tea on a coffee table. \ No newline at end of file diff --git a/assets/chatgpt_custom_book_cup.txt b/assets/chatgpt_custom_book_cup.txt new file mode 100644 index 0000000000000000000000000000000000000000..9293cf3f8598b53bc862c502992fc6aedc99c5ba --- /dev/null +++ b/assets/chatgpt_custom_book_cup.txt @@ -0,0 +1,25 @@ +A book and a cup of coffee on a cozy armchair. +A book and a cup of tea on a windowsill with raindrops. +A book and a cup of hot chocolate on a wooden desk. +A book and a cup of herbal tea on a bedside table. +A book and a cup of coffee on a park bench under a tree. +A book and a cup of tea on a picnic blanket in a meadow. +A book and a cup of coffee on a café table by the window. +A book and a cup of tea on a porch swing. +A book and a cup of coffee on a kitchen counter with a fruit bowl. +A book and a cup of tea on a library table surrounded by shelves of books. +A book and a cup of hot chocolate on a coffee table by the fireplace. +A book and a cup of tea on a garden bench among blooming flowers. +A book and a cup of coffee on a balcony with a city view. +A book and a cup of tea on a nightstand with a lamp. +A book and a cup of herbal tea on a yoga mat in a serene room. +A book and a cup of coffee on a desk in a home office. +A book and a cup of tea on a park bench by a pond. +A book and a cup of coffee on a café patio with string lights. +A book and a cup of tea on a picnic table in the forest. +A book and a cup of hot chocolate on a windowsill with a snowy view. +A book and a cup of tea on a hammock in the garden. +A book and a cup of coffee on a rustic wooden table in a cabin. +A book and a cup of tea on a bench in a botanical garden. +A book and a cup of coffee on a study table with a globe. +A book and a cup of tea on a blanket in a sunflower field. \ No newline at end of file diff --git a/assets/chatgpt_custom_book_cup_character.txt b/assets/chatgpt_custom_book_cup_character.txt new file mode 100644 index 0000000000000000000000000000000000000000..40912840d331e60f18fe2e7298a7bf4d7a5504b6 --- /dev/null +++ b/assets/chatgpt_custom_book_cup_character.txt @@ -0,0 +1,25 @@ +A woman reading a book and sipping a cup of coffee on a cozy armchair. +A man reading a book and drinking a cup of tea on a windowsill with raindrops. +A girl with a book and a cup of hot chocolate on a wooden desk. +A man reading a book and enjoying a cup of herbal tea on a bedside table. +A girl reading a book and holding a cup of coffee on a park bench under a tree. +A woman lying on a picnic blanket in a meadow with a book and a cup of tea. +A man reading a book and sipping a cup of coffee on a café table by the window. +A girl swinging on a porch swing with a book and a cup of tea. +A woman reading a book and enjoying a cup of coffee on a kitchen counter with a fruit bowl. +A man surrounded by books, reading with a cup of tea on a library table. +A child reading a book and drinking a cup of hot chocolate on a coffee table by the fireplace. +A woman sitting on a garden bench, reading a book and sipping a cup of tea among blooming flowers. +A man reading a book and enjoying a cup of coffee on a balcony with a city view. +A woman reading a book and drinking a cup of tea on a nightstand with a lamp. +A woman reading a book and sipping a cup of herbal tea on a yoga mat in a serene room. +A man reading a book and drinking a cup of coffee on a desk in a home office. +A man reading a book and enjoying a cup of tea on a park bench by a pond. +A couple reading books and sipping cups of coffee on a café patio with string lights. +A woman reading a book and drinking a cup of tea on a picnic table in the forest. +A child reading a book and drinking a cup of hot chocolate on a windowsill with a snowy view. +A woman lounging in a hammock with a book and a cup of tea in the garden. +A man reading a book and sipping a cup of coffee on a rustic wooden table in a cabin. +A woman reading a book and drinking a cup of tea on a bench in a botanical garden. +A child reading a book and enjoying a cup of coffee on a study table with a globe. +A girl lying on a blanket in a sunflower field with a book and a cup of tea. \ No newline at end of file diff --git a/assets/chatgpt_custom_bottle_glass.txt b/assets/chatgpt_custom_bottle_glass.txt new file mode 100644 index 0000000000000000000000000000000000000000..a786829bcecabe1cc3497db728d9eacb0cd368d3 --- /dev/null +++ b/assets/chatgpt_custom_bottle_glass.txt @@ -0,0 +1,50 @@ +A wine glass and a bottle on a dining table set for a romantic dinner. +A wine glass and a bottle on a picnic blanket in a sunny meadow. +A wine glass and a bottle on a coffee table in a cozy living room. +A wine glass and a bottle on a kitchen island during a dinner party. +A wine glass and a bottle on a balcony table overlooking the city. +A wine glass and a bottle on a restaurant table with a candle. +A wine glass and a bottle on a garden table under a pergola. +A wine glass and a bottle on a beach towel by the ocean. +A wine glass and a bottle on a wooden picnic table in the forest. +A wine glass and a bottle on a patio table at sunset. +A wine glass and a bottle on a small table in a wine cellar. +A wine glass and a bottle on a bar counter in a chic lounge. +A wine glass and a bottle on a blanket under a starlit sky. +A wine glass and a bottle on a stone wall in a vineyard. +A wine glass and a bottle on a windowsill with a city view. +A wine glass and a bottle on a garden bench surrounded by flowers. +A wine glass and a bottle on a dining table with a cheese platter. +A wine glass and a bottle on a coffee table beside a roaring fire. +A wine glass and a bottle on a boat deck with the sea in the background. +A wine glass and a bottle on a terrace with a mountain view. +A wine glass and a bottle on a bar cart in a stylish living room. +A wine glass and a bottle on a round table in a cozy nook. +A wine glass and a bottle on a table set for a garden party. +A wine glass and a bottle on a balcony with a scenic view. +A wine glass and a bottle on a dining table with a holiday centerpiece. +A wine glass and a bottle on a side table at a pool party. +A wine glass and a bottle on a picnic table at a lakeside. +A wine glass and a bottle on a kitchen counter with snacks. +A wine glass and a bottle on a wooden table in a rustic cabin. +A wine glass and a bottle on a patio table with string lights above. +A wine glass and a bottle on a stone table in a courtyard. +A wine glass and a bottle on a table in a cozy café. +A wine glass and a bottle on a blanket in a flower field. +A wine glass and a bottle on a rooftop terrace at night. +A wine glass and a bottle on a table in a gazebo. +A wine glass and a bottle on a dining table with an elegant tablecloth. +A wine glass and a bottle on a picnic table with a view of the hills. +A wine glass and a bottle on a lounge chair by the pool. +A wine glass and a bottle on a wooden bench in a botanical garden. +A wine glass and a bottle on a table at an outdoor concert. +A wine glass and a bottle on a windowsill with a sunset view. +A wine glass and a bottle on a dining table with fresh flowers. +A wine glass and a bottle on a table in a country house. +A wine glass and a bottle on a patio table with a view of the vineyard. +A wine glass and a bottle on a café table with a street view. +A wine glass and a bottle on a picnic blanket by a river. +A wine glass and a bottle on a table in a seaside restaurant. +A wine glass and a bottle on a blanket in a park. +A wine glass and a bottle on a dining table in a cozy kitchen. +A wine glass and a bottle on a rooftop garden with fairy lights. \ No newline at end of file diff --git a/assets/chatgpt_custom_compression.txt b/assets/chatgpt_custom_compression.txt new file mode 100644 index 0000000000000000000000000000000000000000..06e213f055e71e96064e31b5fa8e2eea34d87dc3 --- /dev/null +++ b/assets/chatgpt_custom_compression.txt @@ -0,0 +1,32 @@ +A group of birds is singing melodiously from the branches of a tall pine tree. +A raccoon is exploring a hollow log, the forest floor covered in a soft carpet of leaves with a light fog settling in. +A pair of owls is watching over their nest, with the ground covered in a light dusting of snow. +Beagle walking on a rainy street, splashing through puddles. +Persian cat lounging by a window, watching outside. +Border collie carrying a watering can in a sunlit garden. +A flock of colorful birds singing in a tree. +A majestic eagle gliding over snow-capped mountains with a clear blue sky as the backdrop. +A peacock spreading its magnificent, colorful feathers in a park. +Seagulls flying and calling over a busy harbor with boats and ships docked. +A colorful parrot perched on a tropical tree branch, talking and mimicking sounds with lush green foliage in the background. +A vibrant parrot meticulously building its nest in a tall tree. +A fluffy Persian cat peacefully napping in a sunbeam streaming through a window in a cozy room. +A curious Siamese cat scaling a tall bookshelf, surrounded by books and trinkets, in a warm, softly lit study. +A frisky kitten pouncing and playing in a pile of autumn leaves. +A cute rabbit munching on a bright orange carrot in a cozy garden. +A white rabbit hopping through a snowy landscape, leaving little paw prints in the fresh snow under a clear, crisp winter sky. +A flamingo standing gracefully in a shallow lagoon, its pink feathers vibrant against the blue water. +A hummingbird hovering near a bright red flower, its wings a blur as it drinks nectar. +A flock of cranes taking flight from a serene wetland. +A kingfisher diving into a clear river to catch a fish, the water splashing around it. +A pair of swans gliding smoothly across a tranquil lake with autumn foliage reflected in the water. +A woodpecker diligently tapping on the trunk of an old oak tree in a dense forest. +A group of penguins huddled together on an icy Antarctic shore, with the ocean in the background. +A sparrow building its nest in the eaves of a rustic farmhouse, using twigs and straw. +A majestic hawk perched on a cliff edge, scanning the valley below for prey. +A robin singing cheerfully from a snow-dusted fence post in a winter garden. +A pelican soaring above the waves, with a coastal town visible in the distance. +A cardinal sitting on a snowy branch, its bright red feathers standing out against the white landscape. +A group of ducklings following their mother across a calm pond, lily pads floating nearby. +An eagle's nest high in a rocky mountain crag, with eaglets peeking over the edge. +A pair of lovebirds snuggling close together on a leafy branch. \ No newline at end of file diff --git a/assets/chatgpt_custom_compression_animals.txt b/assets/chatgpt_custom_compression_animals.txt new file mode 100644 index 0000000000000000000000000000000000000000..6248f05127c43a69f201ee6cce44c58e66816f2f --- /dev/null +++ b/assets/chatgpt_custom_compression_animals.txt @@ -0,0 +1,90 @@ +A lion roaring in the savannah. +A lioness hunting in the grasslands. +A pride of lions resting under a tree. +A lion cub playing in the wild. +A lion drinking water from a river. +A lion walking through the tall grass. +A lion basking in the sun. +A lion chasing a gazelle. +A lion grooming its cub. +A lion standing on a rock overlooking the savannah. +An elephant bathing in a river. +A herd of elephants crossing the savannah. +A baby elephant playing with its trunk. +An elephant feeding on tree leaves. +An elephant spraying water with its trunk. +An elephant walking through the forest. +An elephant family interacting. +An elephant trumpeting loudly. +An elephant pulling down a tree branch. +An elephant resting in the shade. +A panda eating bamboo. +A panda climbing a tree. +A panda playing in the snow. +A panda mother with her cub. +A panda rolling down a hill. +A panda resting in a tree. +A panda exploring its enclosure. +A panda chewing on leaves. +A panda playing with a ball. +A panda taking a nap. +A kangaroo hopping across the plains. +A mother kangaroo with a joey in her pouch. +A kangaroo boxing with another kangaroo. +A kangaroo grazing on grass. +A kangaroo resting under a tree. +A kangaroo jumping over a fence. +A kangaroo drinking water from a pond. +A kangaroo scratching its ear. +A kangaroo exploring the bushland. +A kangaroo looking out from a hilltop. +A giraffe eating leaves from a tall tree. +A giraffe walking across the savannah. +A baby giraffe standing next to its mother. +A giraffe drinking water from a pond. +A giraffe running in the wild. +A giraffe interacting with other animals. +A giraffe bending down to drink. +A giraffe using its long neck to reach leaves. +A giraffe resting in the shade. +A giraffe exploring its habitat. +A tiger stalking through the jungle. +A tiger swimming in a river. +A tiger roaring loudly. +A tiger mother with her cubs. +A tiger hunting in the grass. +A tiger climbing a tree. +A tiger playing with its cubs. +A tiger resting on a rock. +A tiger patrolling its territory. +A tiger drinking from a stream. +A parrot mimicking human speech. +A parrot eating fruit in the rainforest. +A parrot flying in the jungle. +A parrot grooming its feathers. +A parrot perched on a branch. +A parrot interacting with other birds. +A parrot playing with a toy. +A parrot spreading its colorful wings. +A parrot drinking water from a bowl. +A parrot dancing to music. +A robin singing on a tree branch. +A robin feeding on the ground. +A robin building a nest. +A robin feeding its chicks. +A robin hopping on a lawn. +A robin interacting with other birds. +A robin taking a bath in a birdbath. +A robin perched on a fence. +A robin flying through a garden. +A robin resting in the shade. +A sparrow hopping on the ground. +A sparrow building a nest. +A sparrow singing on a tree branch. +A sparrow feeding its chicks. +A sparrow searching for food in the grass. +A sparrow taking a dust bath. +A sparrow flying through a garden. +A sparrow perching on a fence. +A sparrow drinking from a birdbath. +A sparrow interacting with other small birds. \ No newline at end of file diff --git a/assets/chatgpt_custom_cruel_animal.txt b/assets/chatgpt_custom_cruel_animal.txt new file mode 100644 index 0000000000000000000000000000000000000000..7a5dc1c492e51660161e19f7e4183329a526d2a5 --- /dev/null +++ b/assets/chatgpt_custom_cruel_animal.txt @@ -0,0 +1,20 @@ +A lion is majestically standing on a rocky outcrop, surveying its territory with the golden light of dawn casting a regal glow on its mane. +A lioness is gently nuzzling her cubs in the tall grass, the setting sun creating a warm, serene atmosphere with a rainbow in the distance. +A lion is roaring powerfully at the edge of the savanna, the sound echoing through the vast landscape under a dramatic sunset sky. +A pride of lions is resting under the shade of an acacia tree, the midday sun filtering through the leaves and creating a peaceful scene. +A lion is drinking from a clear watering hole, the water sparkling under the bright sun with a faint rainbow appearing in the mist. +A python is coiled around a tree branch, its scales glistening in the dappled sunlight of the dense jungle. +A python is slithering through the underbrush, its movements silent and fluid as the morning fog begins to lift. +A python is basking on a sun-warmed rock, its eyes scanning the surroundings with a rainbow appearing in the distant mist. +A python is stealthily approaching a small stream, its body moving gracefully through the tall grass as the sun sets in a blaze of color. +A python is wrapped around a large tree trunk, the forest alive with the sounds of twilight and a light fog settling in. +A dragon is soaring through the sky, its wings casting a shadow over the landscape as the sun sets in a fiery display. +A dragon is perched atop a mountain peak, its scales shimmering in the moonlight as it gazes down at the world below. +A dragon is breathing fire into the night sky, the flames illuminating its powerful form with the stars twinkling above. +A dragon is curled up in its lair, the treasure around it glinting in the soft light of dawn with a rainbow visible through the cave entrance. +A dragon is flying low over a calm lake, the water reflecting its majestic form with a light fog rising from the surface. +A tyrannosaurus is roaring in a lush prehistoric forest, the sound reverberating through the trees as the sun rises. +A tyrannosaurus is hunting near a riverbank, its powerful legs moving silently through the tall ferns with a light fog in the background. +A tyrannosaurus is standing on a hill, its silhouette framed against the setting sun with a rainbow appearing in the distant sky. +A tyrannosaurus is drinking from a clear stream, the water sparkling in the midday sun with prehistoric plants surrounding the scene. +A tyrannosaurus is stalking through a dense jungle, the moonlight casting eerie shadows on its massive form as the ground is lightly dusted with mist. \ No newline at end of file diff --git a/assets/chatgpt_custom_cruel_animal2.txt b/assets/chatgpt_custom_cruel_animal2.txt new file mode 100644 index 0000000000000000000000000000000000000000..d09c103bc15c15944bd1b5b87771db1b1d4c72da --- /dev/null +++ b/assets/chatgpt_custom_cruel_animal2.txt @@ -0,0 +1,30 @@ +A unicorn is charging through a burning forest, its mane and tail flowing as the flames create a dramatic and fiery backdrop. +A unicorn is standing on a cliff's edge, rearing up against the backdrop of a blazing sunset, its horn gleaming in the intense light. +A unicorn is galloping across a scorched plain, the ground still smoldering from a recent fire, its hooves kicking up sparks as it runs. +A unicorn is battling a fierce beast in the midst of a volcanic eruption, the lava glowing ominously around them. +A unicorn is leaping over a wall of fire, its silhouette stark against the roaring flames as it escapes danger. +A Pegasus is soaring through the sky above a volcano, its wings cutting through the smoke and ash with the molten lava glowing below. +A Pegasus is flying low over a desert, the hot wind ruffling its feathers as it races against a backdrop of towering sand dunes. +A Pegasus is hovering above a battlefield, its eyes scanning the chaos below as the setting sun casts a fiery glow. +A Pegasus is emerging from a dense cloud of smoke, its wings beating powerfully as it flies towards the burning horizon. +A Pegasus is gliding through the night sky, lit only by the intense glow of a distant wildfire. +A Stegosaurus is defending itself from a predator in a hot, arid landscape, its tail swinging powerfully under the blazing sun. +A Stegosaurus is grazing near a bubbling hot spring, the steam rising around it in the intense midday heat. +A Stegosaurus is battling another dinosaur in a dusty canyon, the heat shimmering in the air as they clash. +A Stegosaurus is trudging through a volcanic plain, the ground cracking and smoldering beneath its heavy steps. +A Stegosaurus is standing its ground in a dry riverbed, the intense heat creating waves in the air as it faces off against a threat. +A Tyrannosaurus is roaring triumphantly on a rocky outcrop, the sun setting behind it in a blaze of red and orange. +A Tyrannosaurus is stalking its prey through a scorched forest, the air thick with smoke and the ground littered with burning embers. +A Tyrannosaurus is locked in a fierce battle with another dinosaur, their roars echoing through a hot, dry valley. +A Tyrannosaurus is standing tall on a volcanic ridge, the molten lava flowing below as ash rains down from the sky. +A Tyrannosaurus is chasing a herd of dinosaurs across a dusty plain, the heat and dust creating a dramatic, intense scene. +A dragon is unleashing a torrent of fire on a desolate landscape, the ground cracking and burning under the intense heat. +A dragon is flying over a city in flames, its wings casting a shadow against the inferno below. +A dragon is perched on a mountain peak, breathing fire into the sky as the molten lava flows down the slopes. +A dragon is battling a rival dragon in mid-air, their fiery breaths illuminating the night as they clash. +A dragon is emerging from a cave in the heart of a volcano, its scales glowing with the heat of the molten rock around it. +A Mosasaur is breaking the surface of a turbulent, lava-heated ocean, its massive jaws snapping at the air as steam rises around it. +A Mosasaur is battling a giant squid in the depths of a volcanic underwater trench, the glow of the molten rock illuminating their struggle. +A Mosasaur is swimming through the hot, sulfurous waters near an underwater volcano, its powerful tail propelling it swiftly through the currents. +A Mosasaur is breaching the surface near a tropical island, the setting sun casting a fiery glow over the scene as it searches for prey. +A Mosasaur is navigating through a sea of floating volcanic rock, the intense heat creating a dramatic and dangerous environment as it hunts. diff --git a/assets/chatgpt_custom_cute.txt b/assets/chatgpt_custom_cute.txt new file mode 100644 index 0000000000000000000000000000000000000000..8a1ebc4e77adc4e20e84a92d2004e74b8f9434e6 --- /dev/null +++ b/assets/chatgpt_custom_cute.txt @@ -0,0 +1,100 @@ +A young prince battling a fierce, fire-breathing dragon amid thunderous roars, stormy skies, and blazing flames to save a princess from a tall, enchanted tower wrapped in vines and glowing runes. +A talking frog with a regal voice and a golden crown helps a lost princess navigate a mystical forest filled with sparkling flowers and twinkling fairy lights to find her way back to her grand castle. +A fairy godmother, shimmering with glittering wings and a glowing wand, transforms a humble pumpkin into an opulent, golden carriage pulled by white horses with jeweled harnesses for a magical night at the ball. +A mischievous elf with a cheeky grin and a twinkle in his eye leads children through a mysterious forest of towering, twisted trees, glowing mushrooms, and whispering shadows, revealing hidden secrets and treasures. +A brave knight clad in gleaming armor and a flowing cape faces off against a wicked witch with a crooked staff and a cackling laugh in an enchanted forest swirling with dark magic and eerie mists. +A mermaid with flowing hair and a shimmering tail discovers a hidden underwater kingdom adorned with coral palaces, glowing pearls, and schools of colorful fish, encountering both wonders and dangers along the way. +A young girl befriends a gentle giant in a land of enormous plants and creatures, where they embark on adventures through fields of giant flowers, towering mushrooms, and sparkling waterfalls. +A magical duel between a good wizard with a flowing beard and a staff of light and an evil sorcerer cloaked in shadows and wielding dark magic, set against a backdrop of a stormy sky and crackling energy. +A princess and her animal friends, including talking birds and dancing deer, prepare for a royal ball in a castle filled with glittering chandeliers, flowing gowns, and enchanting music. +A group of fairies with iridescent wings and glowing wands work together to save their enchanted forest from a dark curse, flying through a landscape of ancient trees, sparkling streams, and hidden groves. +A young hero finds a magic lamp in a dusty, ancient cave, releasing a wish-granting genie with a booming voice and a swirling, ethereal form, leading to adventures filled with treasure and peril. +A talking cat with a sharp wit and a stylish hat leads a humble miller’s son through a series of clever schemes, bringing them both fortune and fame in a kingdom of grand castles and bustling marketplaces. +A brave girl uses her wits and courage to outsmart a cunning wolf in a dark and spooky forest, filled with eerie sounds, rustling leaves, and hidden dangers. +A dragon with shimmering scales and a unicorn with a glowing horn form an unlikely friendship, exploring a magical land of floating islands, sparkling rivers, and enchanted meadows. +A clever tailor tricks a towering giant to save his village, using quick thinking and clever traps in a landscape of rolling hills, quaint cottages, and towering mountains. +A magical tree with golden leaves and whispering branches grants wishes to kind-hearted villagers, its presence bringing warmth and wonder to a serene and picturesque village. +A young prince, transformed into a frog by an evil spell, embarks on a quest to break the curse, hopping through enchanted forests, glittering streams, and ancient ruins. +A curious girl falls down a rabbit hole into a whimsical wonderland filled with talking animals, enchanted tea parties, and peculiar characters, each more fantastical than the last. +A knight, with a sword of light and a heart of gold, rescues a village from an army of trolls, battling through rocky landscapes, dark caves, and roaring waterfalls. +A princess discovers she has magical powers, learning to control them through trials and adventures in a world of ancient magic, mystical creatures, and hidden realms. +A brave child faces a monster under the bed, discovering a secret world of friendly creatures and fantastical adventures hidden beneath the ordinary. +A friendly ghost with a soft, glowing aura helps children find a hidden treasure in an old, creaky mansion filled with dusty books, secret passages, and mysterious artifacts. +A talking squirrel with a mischievous grin leads an adventure through a magical forest filled with towering trees, glowing flowers, and hidden secrets, revealing the wonders of nature. +A young hero discovers a secret portal to a fairy-tale world, stepping into a land of enchanted castles, mythical creatures, and epic quests, each more thrilling than the last. +A group of animals, including a wise owl, a brave rabbit, and a clever fox, work together to outsmart a cunning predator, using their unique skills in a lush and vibrant forest. +A magical creature, with iridescent scales and sparkling eyes, helps a prince find a lost kingdom, guiding him through enchanted landscapes, ancient ruins, and mystical challenges. +A girl with magical shoes that can take her anywhere she wishes embarks on adventures through fantastical lands, from floating islands to glowing forests and ancient cities. +A prince and princess work together to break an ancient curse, facing trials and challenges in a world of dark forests, enchanted castles, and hidden magic. +A young girl befriends a dragon, learning to fly on its back through skies filled with stars, across mountains, and over sparkling seas, discovering the wonders of the world. +A magical library where books come to life, revealing stories of heroes, monsters, and magical lands, each book an adventure waiting to be explored. +A group of children find a secret door to a land of ice and snow, filled with sparkling ice castles, snow-covered forests, and magical creatures, embarking on a wintery adventure. +A fairy, with delicate wings and a glowing aura, grants wishes to kind-hearted villagers, bringing joy and magic to their everyday lives in a charming and idyllic village. +A young hero solves riddles and puzzles to save a kingdom, using wit and bravery to navigate enchanted forests, ancient ruins, and mystical trials. +A group of friends go on a quest to find a lost crown, facing challenges and adventures in a world of enchanted forests, hidden valleys, and ancient castles. +A magical garden with talking flowers, dancing butterflies, and singing birds, where each step reveals a new wonder and a hidden secret. +A prince, disguised as a pauper, discovers true friendship and loyalty, experiencing life in a bustling village filled with colorful characters and heartwarming moments. +A young girl finds a magic mirror that shows the future, using its power to prevent disasters and bring happiness to her kingdom, facing challenges and adventures along the way. +A knight faces trials of strength, courage, and wisdom to prove his worthiness to the king, journeying through enchanted forests, mystical mountains, and ancient ruins. +A princess, disguised as a commoner, explores the world beyond her castle, discovering the joys and challenges of ordinary life in a vibrant and diverse kingdom. +A magical creature, with glowing eyes and a mystical aura, guides a hero through a dangerous journey, offering wisdom and protection in a world of enchanted forests, ancient ruins, and hidden magic. +A group of children befriends a fairy, helping her save her home from dark forces, embarking on adventures through magical forests, sparkling streams, and hidden groves. +A young hero discovers a hidden realm of giants, navigating a world of enormous trees, towering mountains, and giant creatures, each step an adventure. +A talking bird with shimmering feathers and a melodious voice leads an adventure to find a legendary treasure, guiding through enchanted forests, ancient ruins, and mystical challenges. +A prince and his loyal steed on a quest to find a magical artifact, journeying through enchanted forests, mystical mountains, and hidden valleys, each step filled with wonder and danger. +A fairy-tale wedding with enchanted guests, magical decorations, and a grand celebration, where every moment is filled with wonder, joy, and enchantment. +A group of animals, including a wise owl, a brave rabbit, and a clever fox, work together to restore a magical forest, overcoming challenges and discovering hidden wonders. +A young girl discovers a hidden talent that saves her kingdom, using her newfound abilities to face challenges and bring happiness to her people, in a world of magic and wonder. +A brave knight challenges a sorcerer to save a princess, battling through enchanted forests, ancient ruins, and mystical trials, each step filled with danger and adventure. +A group of friends discover a magical world through an old wardrobe, stepping into a land of enchanted forests, mythical creatures, and epic quests, each adventure more thrilling than the last. +A young hero finds a dragon egg and raises the dragon, embarking on adventures through enchanted forests, mystical mountains, and ancient ruins, each step filled with wonder and danger. +A tiny mouse in a colorful dress prepares a surprise birthday party for her forest friends, decorating a cozy treehouse with balloons, ribbons, and a giant cake. +A baby dragon with bright, curious eyes and tiny wings learns to fly for the first time with the help of a friendly bird. +A group of playful fairies throw a tea party in a meadow filled with blooming flowers, where teacups and pastries float in the air and butterflies dance around. +A young princess and her pet bunny explore a magical garden filled with talking flowers, friendly bees, and a sparkling pond. +A group of woodland animals, including a hedgehog, a squirrel, and a deer, build a cozy little house together, complete with tiny furniture and flower decorations. +A young fairy with glittering wings sprinkles magic dust on a sleepy village, bringing dreams of happy adventures to all the children. +A kitten with a tiny crown goes on a magical adventure through a land of oversized furniture and enchanted toys. +A pair of ducklings embark on a journey to find the rainbow’s end, waddling through lush meadows, sparkling streams, and colorful gardens. +A little girl discovers a hidden door in her backyard that leads to a world where all her stuffed animals come to life and have their own adventures. +A group of baby animals, including a fox, a raccoon, and an owl, play hide-and-seek in a forest filled with giggles, rustling leaves, and sparkling fairy lights. +A tiny gnome with a big hat and a friendly smile helps a lost bird find its way home through a forest of giant mushrooms and glowing flowers. +A young unicorn with a rainbow-colored mane discovers a magical meadow where she meets other baby unicorns and they play together under a glittering sky. +A princess and her fluffy puppy go on a picnic by a sparkling lake, with a basket filled with delicious treats and a blanket adorned with magical patterns. +A group of baby dragons discover a field of blooming flowers and play a game of tag, their tiny wings fluttering and their giggles filling the air. +A young girl befriends a tiny fairy who lives in a daisy, and they explore a world where flowers are houses and leaves are slides. +A baby panda discovers a magical bamboo forest where each stalk glows softly, and he makes friends with a wise old turtle. +A young princess and her kitten have a slumber party in a castle, complete with pillow forts, twinkling lights, and bedtime stories. +A group of ducklings find a hidden pond with lily pads that light up, and they play a game of hopscotch across the glowing water. +A curious bunny finds a magical book that brings his drawings to life, and he creates a world filled with friendly creatures and fun adventures. +A pair of baby hedgehogs roll around in a field of dandelions, laughing and playing as the fluffy seeds float around them. +A little girl and her teddy bear find a secret garden where every flower tells a story, and they spend the day listening to the magical tales. +A baby deer with big, bright eyes explores a forest filled with sparkling fireflies and discovers a hidden clearing where all the animals gather for a dance. +A young fairy with a flower petal dress hosts a playdate with other fairies in a meadow filled with bouncing butterflies and singing birds. +A kitten with a golden bell on her collar explores a magical attic where old toys come to life and have their own whimsical adventures. +A young prince and his baby dragon friend build a sandcastle on a beach with seashell towers and a moat that sparkles with magic. +A group of baby animals, including a bunny, a squirrel, and a fox, have a picnic in a meadow filled with blooming flowers and fluttering butterflies. +A tiny elf with a green hat and curly shoes explores a magical mushroom village, meeting other friendly elves and discovering hidden treasures. +A young girl finds a magical mirror that shows her the secret lives of her toys, and she spends the day watching their adorable adventures. +A baby owl with big, curious eyes learns to fly under the guidance of a wise old owl, soaring through a forest filled with twinkling stars and glowing flowers. +A young fairy with a wand that glows softly helps a group of animals prepare for a nighttime festival, decorating the forest with lanterns and sparkling lights. +A kitten and a puppy, both wearing tiny crowns, embark on a royal adventure through a castle filled with secret passages and hidden treasures. +A group of baby animals find a magical treehouse with a slide made of rainbows and a swing that goes higher than the clouds. +A young girl and her pet bunny discover a secret garden where every flower has a friendly face and sings a happy song. +A baby dragon with glittering scales finds a hidden pond where he meets other baby dragons and they play a game of splash and chase. +A young princess and her pet unicorn explore a magical forest where every tree has a door that leads to a different fairy tale world. +A group of baby animals, including a hedgehog, a fox, and a squirrel, discover a hidden clearing where they find a magical seesaw that floats in the air. +A young fairy with a crown of flowers and a wand made of twigs helps her forest friends prepare for a grand feast under the stars. +A kitten with a ribbon around her neck finds a magical garden where butterflies glow in the dark and flowers hum soft lullabies. +A young girl and her teddy bear go on a magical adventure through a world where every toy comes to life and has its own story to tell. +A baby panda with a playful personality finds a bamboo forest where each stalk glows softly, and he makes friends with a wise old turtle. +A young princess and her fluffy puppy have a slumber party in a castle, complete with pillow forts, twinkling lights, and bedtime stories. +A group of ducklings find a hidden pond with lily pads that light up, and they play a game of hopscotch across the glowing water. +A curious bunny finds a magical book that brings his drawings to life, and he creates a world filled with friendly creatures and fun adventures. +A pair of baby hedgehogs roll around in a field of dandelions, laughing and playing as the fluffy seeds float around them. +A little girl and her teddy bear find a secret garden where every flower tells a story, and they spend the day listening to the magical tales. +A baby deer with big, bright eyes explores a forest filled with sparkling fireflies and discovers a hidden clearing where all the animals gather for a dance. +A young fairy with a flower petal dress hosts a playdate with other fairies in a meadow filled with bouncing butterflies and singing birds. +A kitten with a golden bell on her collar explores a magical attic where old toys come to life and have their own whimsical adventures. +A young prince and his baby dragon friend build a sandcastle on a beach with seashell towers and a moat that sparkles with magic. +A group of baby animals, including a bunny, a squirrel, and a fox, have a picnic in a meadow filled with blooming flowers and fluttering butterflies. \ No newline at end of file diff --git a/assets/chatgpt_custom_dog.txt b/assets/chatgpt_custom_dog.txt new file mode 100644 index 0000000000000000000000000000000000000000..77fc3ede6896b9557c159e35f0f47fd1cc35f5f5 --- /dev/null +++ b/assets/chatgpt_custom_dog.txt @@ -0,0 +1,50 @@ +A cat and a dog sitting together on a cozy couch. +A cat and a dog playing with a ball in the backyard. +A cat and a dog sharing a sunny spot on the living room floor. +A cat and a dog sleeping side by side in a pet bed. +A cat and a dog looking out the window at birds. +A cat and a dog eating from their bowls in the kitchen. +A cat and a dog exploring a garden with blooming flowers. +A cat and a dog lying on a rug in front of a fireplace. +A cat and a dog sitting together on a park bench. +A cat and a dog playing with a toy in the living room. +A cat and a dog relaxing on a porch with a view of the yard. +A cat and a dog sitting on a windowsill watching the rain. +A cat and a dog cuddled up on a bed with soft blankets. +A cat and a dog sniffing around a picnic area in the park. +A cat and a dog sitting on a deck overlooking a lake. +A cat and a dog lying together under a tree in the yard. +A cat and a dog playing with a feather toy in the living room. +A cat and a dog sitting on a staircase looking at each other. +A cat and a dog lounging on a sunlit patio. +A cat and a dog exploring a wooded area together. +A cat and a dog sitting side by side on a balcony. +A cat and a dog sharing a pet bed in the bedroom. +A cat and a dog playing chase around the house. +A cat and a dog sitting by the front door waiting for their owner. +A cat and a dog relaxing on a hammock in the backyard. +A cat and a dog watching fish swim in an aquarium. +A cat and a dog lying on a carpet in a cozy den. +A cat and a dog playing in a pile of autumn leaves. +A cat and a dog sitting together on a garden bench. +A cat and a dog exploring a new room together. +A cat and a dog sitting under a table during dinner time. +A cat and a dog playing with a stuffed animal in the living room. +A cat and a dog sitting together on a dock by a pond. +A cat and a dog lounging on the grass in the backyard. +A cat and a dog sitting on the porch steps looking out at the yard. +A cat and a dog napping together in a sunbeam. +A cat and a dog sniffing around a garden bed. +A cat and a dog sitting together on a park bench under a tree. +A cat and a dog playing with a rope toy in the living room. +A cat and a dog relaxing on a swing on the porch. +A cat and a dog exploring a new environment together. +A cat and a dog sitting on a bench at the beach. +A cat and a dog cuddling together on a chair. +A cat and a dog playing in a sandbox in the yard. +A cat and a dog lying on a blanket in the park. +A cat and a dog sitting on a windowsill watching the world go by. +A cat and a dog sniffing around a flower bed in the yard. +A cat and a dog relaxing together on a patio. +A cat and a dog playing hide and seek in the house. +A cat and a dog sitting together on a garden swing. \ No newline at end of file diff --git a/assets/chatgpt_custom_forest.txt b/assets/chatgpt_custom_forest.txt new file mode 100644 index 0000000000000000000000000000000000000000..434b6e005f2a68e7405388c59cb920e23b3a7a9e --- /dev/null +++ b/assets/chatgpt_custom_forest.txt @@ -0,0 +1,50 @@ +A deer is grazing on grass in a sunlit clearing. +A rabbit is hopping through a patch of wildflowers. +A squirrel is gathering acorns near a large oak tree. +A fox is stealthily moving through the underbrush. +A family of ducks is swimming in a tranquil forest pond. +A bear is catching fish in a fast-flowing stream. +A raccoon is climbing a tree to reach a bird's nest. +A group of birds is singing from the branches of a tall pine tree. +A hedgehog is curling up among the fallen leaves. +A pair of owls is perched on a high branch, watching the forest below. +A family of deer is resting under the shade of a large tree. +A bat is hanging upside down from a tree branch, sleeping. +A wolf is howling at the moon in a clearing. +A family of rabbits is playing near their burrow. +A fox is chasing a butterfly through the grass. +A squirrel is running along the branches of a tall tree. +A raccoon is washing its paws in a small stream. +A bear is scratching its back against a tree trunk. +A group of ducks is waddling along the forest floor. +A hedgehog is sniffing around for food in the underbrush. +A pair of owls is hooting softly to each other. +A deer is drinking from a clear forest stream. +A bat is fluttering through the trees as dusk falls. +A wolf is stalking through the forest, searching for prey. +A family of rabbits is nibbling on clover in a meadow. +A fox is resting on a rock, basking in the sun. +A squirrel is leaping from branch to branch. +A raccoon is exploring a hollow log. +A bear is eating berries from a bush. +A group of ducks is quacking and flapping their wings in the pond. +A hedgehog is foraging for insects in the leaf litter. +A pair of owls is watching over their nest. +A deer is wandering through a field of tall grass. +A bat is catching insects in mid-air. +A wolf is drinking from a forest stream. +A family of rabbits is hiding in the tall grass. +A fox is exploring a fallen log. +A squirrel is storing nuts in a tree hollow. +A raccoon is peering into a small cave. +A bear is lying down in a sunny spot, taking a nap. +A group of ducks is preening their feathers on the shore of the pond. +A hedgehog is curling up in a patch of moss. +A pair of owls is swooping down to catch their dinner. +A deer is scratching its ear with its hind leg. +A bat is roosting in a tree cavity. +A wolf is leading its pack through the forest. +A family of rabbits is playing hide and seek among the bushes. +A fox is sniffing the air, alert for any danger. +A squirrel is enjoying an acorn on a high branch. +A raccoon is rummaging through the underbrush for food. \ No newline at end of file diff --git a/assets/chatgpt_custom_forest_vivid.txt b/assets/chatgpt_custom_forest_vivid.txt new file mode 100644 index 0000000000000000000000000000000000000000..6155102b612cd6c83f4a27f4f08e5c3ca69f58b3 --- /dev/null +++ b/assets/chatgpt_custom_forest_vivid.txt @@ -0,0 +1,50 @@ +A deer is peacefully grazing on vibrant green grass in a sunlit clearing, surrounded by glowing fireflies, with a rainbow arching across the sky. +A rabbit is joyfully hopping through a patch of colorful wildflowers, with the warm golden light of the setting sun streaming through the trees. +A squirrel is busily gathering shiny acorns near a majestic, ancient oak tree, with a soft morning fog rolling in. +A fox is stealthily moving through the underbrush, the moonlight casting a mystical glow on its fur as the night sky clears after a rain. +A family of ducks is swimming gracefully in a tranquil forest pond, the water sparkling under the midday sun with a faint rainbow in the mist. +A bear is skillfully catching fish in a fast-flowing, crystal-clear stream, with the first light of dawn breaking through the trees. +A raccoon is climbing a tall tree to reach a bird's nest, the tree's branches swaying gently in the breeze as a light snowfall begins. +A group of birds is singing melodiously from the branches of a tall pine tree, the sky behind them painted with the colors of sunset and streaked with a vibrant rainbow. +A hedgehog is curling up among the fallen autumn leaves, the warm light of the setting sun creating a cozy atmosphere with a hint of fog. +A pair of owls is perched on a high branch, watching the forest below as the stars begin to twinkle in the clear night sky after a light snow. +A family of deer is resting under the shade of a large tree, with soft beams of sunlight filtering through the branches and a rainbow arcing overhead. +A bat is hanging upside down from a tree branch, peacefully sleeping as the twilight sky deepens with the last rays of the sun. +A wolf is howling at the moon in a moonlit clearing, the stars sparkling like diamonds above and the ground lightly dusted with snow. +A family of rabbits is playfully hopping near their burrow, the meadow bathed in the soft glow of dusk with mist rising from the ground. +A fox is joyfully chasing a butterfly through the tall grass, with the sunlight creating a magical aura around them and a rainbow appearing in the distance. +A squirrel is energetically running along the branches of a tall tree, the leaves rustling softly as the first light of dawn breaks. +A raccoon is washing its paws in a sparkling, gently flowing stream, surrounded by lush greenery with a light fog settling over the water. +A bear is scratching its back against a tree trunk, the tree’s bark illuminated by the afternoon sun and a rainbow appearing after a brief shower. +A group of ducks is waddling along the forest floor, the sunlight filtering through the canopy creating a dappled pattern on the ground with a hint of fog. +A hedgehog is sniffing around for food in the underbrush, the forest floor covered in soft, mossy patches as the sun sets in a blaze of color. +A pair of owls is hooting softly to each other under the starlit sky, their eyes reflecting the moonlight with the ground covered in a light dusting of snow. +A deer is drinking from a clear forest stream, the water sparkling as the sun shines brightly above and a rainbow appears in the mist. +A bat is fluttering through the trees as dusk falls, the sky painted in shades of purple and pink with a light fog settling in. +A wolf is stalking through the forest, searching for prey, with the full moon casting a silver glow and the ground lightly covered in snow. +A family of rabbits is nibbling on clover in a meadow, surrounded by a sea of colorful wildflowers as a rainbow arches overhead. +A fox is resting on a sun-warmed rock, basking in the soft afternoon light with a gentle mist rising around it. +A squirrel is leaping from branch to branch, the sunlight highlighting its agile movements with a rainbow appearing in the distance. +A raccoon is exploring a hollow log, the forest floor covered in a soft carpet of leaves with a light fog settling in. +A bear is eating juicy berries from a bush, the sunlight creating a warm, inviting atmosphere with a hint of fog. +A group of ducks is quacking and flapping their wings in the pond, the water sparkling in the sunlight with a rainbow in the background. +A hedgehog is foraging for insects in the leaf litter, the soft light of dawn illuminating the scene with a gentle mist. +A pair of owls is watching over their nest, the forest quiet and peaceful under the starlit sky with the ground covered in a light dusting of snow. +A deer is wandering through a field of tall grass, the sunset casting a golden glow over the landscape with a rainbow arching overhead. +A bat is catching insects in mid-air, the twilight sky providing a dramatic backdrop with a light fog settling in. +A wolf is drinking from a forest stream, the moonlight creating a serene and mystical atmosphere with the ground lightly covered in snow. +A family of rabbits is hiding in the tall grass, the setting sun creating a warm and cozy scene with a hint of fog. +A fox is exploring a fallen log, the forest bathed in the soft light of early morning with a rainbow appearing after a brief shower. +A squirrel is storing nuts in a tree hollow, the branches swaying gently in the breeze with a light fog rolling in. +A raccoon is peering into a small cave, the forest alive with the sounds of twilight and the ground lightly dusted with snow. +A bear is lying down in a sunny spot, taking a nap, with the sun casting a golden glow and a rainbow appearing after a brief shower. +A group of ducks is preening their feathers on the shore of the pond, the water reflecting the clear blue sky with a hint of fog. +A hedgehog is curling up in a patch of moss, the forest floor covered in a soft, green carpet with the first light of dawn breaking. +A pair of owls is swooping down to catch their dinner, the moonlight creating dramatic shadows with the ground lightly covered in snow. +A deer is scratching its ear with its hind leg, the sunlight creating a warm and peaceful scene with a rainbow in the background. +A bat is roosting in a tree cavity, the night sky filled with stars and the ground lightly covered in snow. +A wolf is leading its pack through the forest, the moonlight creating a mysterious and powerful atmosphere with a light fog. +A family of rabbits is playing hide and seek among the bushes, the forest alive with the sounds of nature and the first light of dawn breaking. +A fox is sniffing the air, alert for any danger, with the forest bathed in the soft light of dawn and a rainbow in the distance. +A squirrel is enjoying an acorn on a high branch, the sunlight highlighting its contented expression with a light fog settling in. +A raccoon is rummaging through the underbrush for food, the forest alive with the sounds of twilight and the ground lightly covered in snow. \ No newline at end of file diff --git a/assets/chatgpt_custom_human.txt b/assets/chatgpt_custom_human.txt new file mode 100644 index 0000000000000000000000000000000000000000..8a43a65bd98c5de518828ba00e244521802c1a0d --- /dev/null +++ b/assets/chatgpt_custom_human.txt @@ -0,0 +1,50 @@ +A person playing a guitar by a campfire under a starry sky. +A person riding a bicycle along a tree-lined path with golden autumn leaves falling. +A person reading a book on a park bench surrounded by blooming flowers. +A person doing yoga on a beach at sunrise with gentle waves in the background. +A person painting on an easel in a meadow with colorful wildflowers. +A person running through a field with a kite soaring high in the sky. +A person having a picnic under a cherry blossom tree in full bloom. +A person kayaking on a serene lake with mountains in the distance. +A person playing the violin by a riverside with sunlight reflecting off the water. +A person practicing tai chi in a garden with morning mist rising. +A person dancing ballet in an elegant room with soft, golden lighting. +A person playing the piano in a grand hall with beams of sunlight streaming through. +A person sculpting clay in a studio with fairy lights and soft music playing. +A person writing poetry in a cozy room with a fireplace crackling nearby. +A person singing on a stage with twinkling lights and a dreamy backdrop. +A person drawing in a sketchbook while sitting on a balcony overlooking a sunset. +A person weaving on a loom in a rustic cabin with warm lighting. +A person playing the cello in a sunlit room with large windows. +A person acting on a theater stage with dramatic lighting and set design. +A person creating a mosaic in a garden filled with vibrant flowers. +A person baking bread in a rustic kitchen with sunlight streaming through the windows. +A person picking fresh vegetables in a garden with morning dew glistening. +A person having a tea party in a garden with a beautifully set table and delicate china. +A person cooking a meal over an open fire at a campsite surrounded by nature. +A person decorating a cake in a cozy kitchen with warm, ambient lighting. +A person making homemade pasta in a sunlit kitchen with fresh ingredients. +A person enjoying a picnic with a spread of delicious food under a shady tree. +A person serving drinks at a quaint outdoor café with a charming atmosphere. +A person harvesting fruit in an orchard with sunlight filtering through the trees. +A person grilling food at a backyard barbecue with friends and family gathered around. +A person playing a board game with friends in a cozy living room. +A person fishing at a tranquil lake with the morning mist rising. +A person hiking through a forest with sunlight streaming through the canopy. +A person swimming in a clear, blue pool with a waterfall in the background. +A person practicing archery in a meadow with a beautiful backdrop. +A person roller skating along a beachside boardwalk at sunset. +A person playing badminton in a park with soft evening light. +A person meditating on a mountaintop with a breathtaking view. +A person flying a drone over a scenic landscape with rolling hills. +A person playing volleyball on a sandy beach with the waves crashing nearby. +A person lounging in a hammock between two trees with a book in hand. +A person soaking in a hot tub under the stars with soft lighting around. +A person napping in a sunbeam on a cozy window seat with a blanket. +A person enjoying a bubble bath in a luxurious bathroom with candles and rose petals. +A person watching a movie on a projector in a backyard with twinkling lights. +A person getting a massage in a serene spa setting with calming music. +A person sipping tea on a porch with a scenic mountain view. +A person practicing deep breathing exercises on a cliff overlooking the ocean. +A person listening to music with headphones while lying in a field of flowers. +A person stargazing with a telescope in an open field under a clear night sky. \ No newline at end of file diff --git a/assets/chatgpt_custom_human_activity.txt b/assets/chatgpt_custom_human_activity.txt new file mode 100644 index 0000000000000000000000000000000000000000..661035b0d5f93ae82b3aca6cafb8c5e4cc2bd181 --- /dev/null +++ b/assets/chatgpt_custom_human_activity.txt @@ -0,0 +1,50 @@ +A person washing dishes in a kitchen, wearing gloves and scrubbing a plate under running water. +A person vacuuming a living room, carefully moving the vacuum cleaner over the carpet. +A person folding laundry in a bedroom, neatly stacking clothes on a bed. +A person cooking dinner in a kitchen, chopping vegetables on a cutting board. +A person painting a wall in a living room, using a roller and wearing overalls. +A person mowing the lawn in a backyard, pushing a lawnmower across the grass. +A person ironing clothes in a laundry room, pressing a shirt with an iron. +A person making a bed in a bedroom, straightening the sheets and fluffing the pillows. +A person cleaning windows in a living room, using a spray bottle and a cloth. +A person organizing a closet, arranging clothes on hangers and placing shoes on a rack. +A person jogging in a park, wearing running shoes and listening to music with headphones. +A person gardening in a backyard, planting flowers in a flowerbed. +A person cycling on a bike path, wearing a helmet and riding a bicycle. +A person fishing at a lake, casting a fishing rod and waiting patiently. +A person hiking on a mountain trail, carrying a backpack and using trekking poles. +A person having a picnic in a park, laying out a blanket and setting up food. +A person walking a dog on a leash in a neighborhood, stopping occasionally to let the dog sniff. +A person playing basketball at an outdoor court, dribbling the ball and shooting at the hoop. +A person flying a kite on a beach, holding the string and running to keep the kite in the air. +A person swimming in a pool, doing freestyle strokes and wearing goggles. +A chef preparing a meal in a restaurant kitchen, slicing ingredients and cooking on a stove. +A teacher writing on a chalkboard in a classroom, explaining a lesson to students. +A doctor examining a patient in an office, using a stethoscope and checking vital signs. +A mechanic fixing a car in a garage, using tools to work on the engine. +A construction worker building a wall, laying bricks and applying mortar. +A librarian organizing books on shelves, checking titles and arranging them alphabetically. +A cashier scanning items at a grocery store, ringing up purchases and handling money. +A barista making coffee in a café, steaming milk and brewing espresso. +A firefighter putting out a fire, aiming a hose at flames and wearing protective gear. +A hairdresser cutting a client's hair in a salon, using scissors and a comb. +An artist painting a canvas in a studio, mixing colors on a palette and applying brushstrokes. +A musician playing a guitar on a stage, strumming chords and singing into a microphone. +A writer typing on a laptop at a desk, focused on creating a story. +A photographer taking pictures in a park, adjusting the camera and framing shots. +A dancer practicing ballet in a dance studio, performing pirouettes and wearing ballet shoes. +A sculptor carving a statue out of clay, shaping details with sculpting tools. +A filmmaker directing a scene on a movie set, giving instructions to actors and adjusting the camera. +A graphic designer working on a computer, creating digital illustrations and layouts. +A singer recording in a music studio, singing into a microphone and wearing headphones. +A florist arranging a bouquet, selecting flowers and trimming stems. +A student studying at a desk, reading a textbook and taking notes. +A person learning to play the piano, practicing scales and reading sheet music. +A child painting in an art class, using watercolors and a brush on paper. +A group of people attending a workshop, listening to a speaker and taking notes. +A tutor helping a student with homework, explaining concepts and solving problems together. +A scientist conducting an experiment in a lab, mixing chemicals and recording observations. +A person practicing yoga in a studio, performing various poses and focusing on breathing. +A student presenting a project in a classroom, speaking in front of the class and showing visuals. +A person learning to drive, sitting in a car with an instructor and practicing maneuvers. +A person assembling a puzzle, fitting pieces together on a table. \ No newline at end of file diff --git a/assets/chatgpt_custom_human_fashion.txt b/assets/chatgpt_custom_human_fashion.txt new file mode 100644 index 0000000000000000000000000000000000000000..ecfcab66edb7678eb237847723050d7e2d0fad1c --- /dev/null +++ b/assets/chatgpt_custom_human_fashion.txt @@ -0,0 +1,25 @@ +A woman in a stylish dress walking down a busy street, with high-end stores and neon lights in the background. +A man in a trendy suit taking a selfie in a city square, surrounded by modern buildings and a fountain. +An elegant woman enjoying dinner at a high-end restaurant, with exquisite tableware and delicious food on the table. +A gentleman toasting during a candlelit dinner, with soft lighting and artistic decorations in the background. +A woman in professional sportswear running on a treadmill in a luxury gym, with modern fitness equipment and city views through large windows. +A man lifting weights in the free weights area of an upscale gym, with clean mirrors and various machines in the background. +A woman sunbathing by the pool of a luxury hotel, with a clear pool and comfortable loungers nearby. +A man overlooking the cityscape from the balcony of a five-star hotel, holding a glass of champagne. +A woman in a stunning evening gown walking the red carpet, surrounded by flashing cameras and photographers. +A man in a black tuxedo mingling with guests at a luxurious banquet, with elegant decorations and chandeliers in the background. +A woman shopping in a high-end mall, carrying several designer shopping bags, with shiny marble floors and luxurious stores in the background. +A man trying on a suit in the men's section of an upscale department store, surrounded by neatly arranged clothes and mirrors. +A fashionable woman enjoying a latte at an upscale café, with beautiful coffee art and pastries on the table. +A gentleman reading in an artfully designed café, next to elegantly decorated bookshelves and soft lighting. +A well-dressed man posing in front of a luxury car, with a modern cityscape in the background. +A stylish woman gracefully getting out of a luxury car, surrounded by high-end office buildings and designer stores. +A stylish woman having afternoon tea in a lavish tearoom, with fine china, assorted pastries, and a beautiful chandelier overhead. +A gentleman in a tailored suit enjoying a cup of tea and scones, seated at a beautifully set table with floral arrangements. +A woman in a fashionable outfit browsing books in an upscale bookstore, with cozy reading nooks and artfully arranged shelves. +A man in a smart casual outfit sitting in a plush armchair, reading a book with a cup of coffee on a side table. +A woman in a fashionable sundress enjoying the breeze on the deck of a luxury yacht, with the open sea and clear sky around her. +A man in a casual yet chic outfit steering a luxury yacht, with other elegantly dressed guests relaxing on the deck. +A woman in a stylish outfit admiring a painting in a modern art gallery, with minimalist decor and spotlights on the artworks. +A man in an elegant suit discussing an abstract sculpture with a curator, surrounded by contemporary art pieces. +A man in a formal suit enjoying a gourmet meal at a rooftop restaurant, with ambient lighting and a live pianist playing. \ No newline at end of file diff --git a/assets/chatgpt_custom_ice.txt b/assets/chatgpt_custom_ice.txt new file mode 100644 index 0000000000000000000000000000000000000000..5a264416eec21178de65a7ed5b97a02b97194095 --- /dev/null +++ b/assets/chatgpt_custom_ice.txt @@ -0,0 +1,16 @@ +The Ice Queen stands in her shimmering palace, her gown blending with the frosty surroundings as she surveys her kingdom. +A brave explorer, bundled in furs, navigates a mystical ice world, discovering hidden castles and frozen landscapes. +An ice sculptor chisels a massive block in a workshop filled with intricate sculptures, his breath visible in the chilly air. +A snow princess stands on the balcony of her ice castle, her hair adorned with delicate snowflakes, overlooking her serene realm. +An icy warrior clad in glistening armor patrols the perimeter of a fortress, ready to defend against intruders. +A glacial princess wanders through her crystal cave, her touch leaving a trail of frost as she explores her frozen domain. +A brave explorer chips away at a wall of ice, revealing a hidden chamber with glowing ice crystals. +A girl gently touches an ice wall, feeling the cold surface under her fingertips as it glows softly. +A child slides across a frozen lake, her laughter echoing through the snowy landscape. +A teenager carves a detailed figure out of an ice block, her concentration visible in the cold air. +A girl gently touches an ice wall, marveling at its smooth, cold surface as she explores the ice castle. +An explorer carefully climbs the side of an ice tower, his hands gripping the icy ledges as he ascends. +A mage raises her staff, casting an ice spell that causes the castle walls to shimmer with new frost. +The Ice Queen walks gracefully through the grand hall, her footsteps echoing on the crystalline floor. +A scout lights an ice lantern, its glow illuminating the dark passageways of the ice castle. +A girl discovers a hidden passage behind an ice statue, her curiosity leading her deeper into the ice castle's mysteries. \ No newline at end of file diff --git a/assets/chatgpt_custom_instruments.txt b/assets/chatgpt_custom_instruments.txt new file mode 100644 index 0000000000000000000000000000000000000000..1ff0562d863e17ebb81b9044fe381e2af6f7063d --- /dev/null +++ b/assets/chatgpt_custom_instruments.txt @@ -0,0 +1,50 @@ +A dog playing a piano with its paws in a living room illuminated by fairy lights. +A dog strumming a guitar while sitting on a park bench at sunset. +A dog tapping a drum set in a backyard with twinkling lights overhead. +A dog blowing into a trumpet in a garden filled with blooming flowers. +A dog playing a saxophone under a tree with soft, ambient lighting. +A dog hitting xylophone keys in a cozy room with a fireplace crackling. +A dog playing a harmonica on a porch with the sun setting in the background. +A dog drumming on a set of bongos at a picnic in the park. +A dog strumming a ukulele on a beach at dusk. +A dog playing a tambourine while sitting on a blanket in a meadow. +A cat playing a harp in a sunlit room with soft, flowing curtains. +A cat tapping piano keys with precision in a warmly lit living room. +A cat strumming a tiny guitar on a windowsill overlooking a garden. +A cat plucking at a banjo in a room filled with cozy, ambient light. +A cat playing a flute on a balcony under a moonlit sky. +A cat tapping on a set of drums in a room with twinkling fairy lights. +A cat playing a violin in a garden with morning dew sparkling. +A cat hitting the chimes in a yard with a gentle breeze. +A cat playing a cello in a room with soft, golden light. +A cat strumming a sitar in a peaceful, cozy corner. +A rabbit tapping the keys of a tiny piano in a meadow filled with wildflowers. +A rabbit playing a miniature guitar under a tree with soft light filtering through. +A rabbit hitting the drums with its paws in a garden with blooming flowers. +A rabbit playing a flute on a wooden deck at sunset. +A rabbit strumming a ukulele while sitting on a picnic blanket in the park. +A rabbit tapping a xylophone in a backyard with twinkling string lights. +A rabbit playing a harmonica in a cozy corner with soft ambient lighting. +A rabbit drumming on a set of bongos in a sunlit field. +A rabbit playing a tambourine in a garden with colorful flowers. +A rabbit tapping the chimes in a yard filled with sunlight. +A raccoon playing a piano with its nimble fingers in a moonlit backyard. +A raccoon strumming a guitar while perched on a tree branch. +A raccoon tapping a drum set under a starry night sky. +A raccoon blowing into a saxophone in a forest clearing with fireflies. +A raccoon playing a harmonica while sitting on a park bench at dusk. +A raccoon hitting the xylophone in a garden with soft lighting. +A raccoon playing a flute on a rooftop under a bright, full moon. +A raccoon drumming on a set of bongos in a cozy, warmly lit room. +A raccoon strumming a ukulele while sitting on a log by a campfire. +A raccoon playing a tambourine in a field with a beautiful sunset. +A dog and cat playing a duet on the piano in a room filled with fairy lights. +A dog strumming a guitar while a rabbit taps the drums in a garden. +A cat playing the violin while a raccoon strums a guitar under a tree. +A rabbit and raccoon playing a duet on the xylophone in a meadow. +A dog and raccoon playing harmonicas together in a cozy, warmly lit room. +A cat and rabbit playing the flute and ukulele, respectively, in a sunlit garden. +A dog tapping the bongos while a cat strums a tiny guitar on a picnic blanket. +A raccoon blowing into a saxophone while a rabbit plays the tambourine in a field. +A dog and rabbit playing a duet on the piano in a room with a crackling fireplace. +A cat strumming a guitar while a raccoon taps the chimes in a moonlit backyard. \ No newline at end of file diff --git a/assets/chatgpt_custom_instruments_unseen.txt b/assets/chatgpt_custom_instruments_unseen.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b45a83a502e669e709ce4fec4bed62cccf18791 --- /dev/null +++ b/assets/chatgpt_custom_instruments_unseen.txt @@ -0,0 +1,55 @@ +A dog playing a harp in a dimly lit room with candles flickering around. +A cat playing a double bass in a sunny garden surrounded by butterflies. +A dog tapping a xylophone in a bright room filled with hanging plants. +A cat blowing into a trumpet on a rooftop as the sun sets in the background. +A rabbit strumming a guitar on a hill overlooking a scenic lake at dusk. +A dog playing an accordion in a bustling city park during autumn. +A cat plucking a ukulele in a cozy nook with a gentle rain visible outside the window. +A rabbit playing a flute on a bridge over a tranquil pond. +A raccoon playing a guitar under a blossoming cherry tree. +A dog strumming a guitar on a snowy porch with soft winter light. +A cat playing a keyboard in a colorful alley adorned with street art. +A rabbit tapping a drum in a lush greenhouse. +A dog blowing into a horn at the edge of a forest during a foggy morning. +A cat playing a piano in a small, quaint chapel. +A raccoon playing a violin in a vintage room filled with antique furniture. +A dog playing a saxophone by a quiet stream with autumn leaves falling. +A cat strumming a guitar on a balcony overlooking a bustling cityscape at night. +A rabbit playing a trumpet in a meadow during a vibrant sunrise. +A raccoon tapping a xylophone in a misty garden at dawn. +A dog playing a keyboard in a futuristic room with glowing neon lights. +A dog strumming an acoustic guitar by a lakeside campfire under the stars. +A cat playing an electric guitar in a loft with industrial-style decor and soft, multicolored lights. +A rabbit playing a classical guitar in a botanical garden surrounded by exotic flowers. +A raccoon strumming a bass guitar in a cozy basement with vintage band posters on the walls. +A dog playing a slide guitar on a porch during a gentle rainstorm. +A cat plucking a twelve-string guitar on the roof during a clear sunset. +A rabbit strumming a flamenco guitar in a vibrant street during a local festival. +A dog playing a grand piano in a grand ballroom with chandeliers and elegant dancers. +A cat tapping the keys of a digital piano in a modern apartment overlooking a bustling city. +A rabbit playing an upright piano in a quaint cafe while patrons enjoy their coffee. +A raccoon playing a baby grand piano on a theater stage with dramatic lighting. +A dog performing on a piano in a jazz bar with a dimly lit, intimate atmosphere. +A cat composing on a keyboard in a home studio filled with various musical instruments. +A rabbit playing a miniature piano in a children’s library during storytime. +A raccoon experimenting with sounds on a keyboard in a museum exhibit about the history of music. +A dog playing bongos on a beach during a sunset. +A cat tapping bongos in a cozy living room with a fireplace. +A rabbit playing bongos in a park during a sunny afternoon. +A raccoon drumming on bongos under a starry night sky. +A dog tapping bongos in a backyard with twinkling fairy lights. +A cat strumming a bass guitar in a modern living room. +A rabbit playing an acoustic guitar by a riverside during a calm evening. +A raccoon plucking a guitar in a forest clearing. +A dog playing a baby grand piano in a sunlit conservatory. +A cat playing a digital piano in a tech-themed studio. +A rabbit tapping bongos at a picnic under a big oak tree. +A raccoon strumming a guitar on a hillside with wildflowers. +A dog playing bongos at a summer festival with colorful decorations. +A cat playing an acoustic guitar in a moonlit garden. +A rabbit tapping bongos in a meadow at dawn. +A raccoon strumming a bass guitar in a vintage-themed café. +A dog playing a piano in a music classroom with students watching. +A cat tapping bongos on a balcony overlooking a city at night. +A rabbit strumming a guitar in a quiet forest glade. +A raccoon playing an electric bass in a garage band setting. \ No newline at end of file diff --git a/assets/chatgpt_custom_outdoor.txt b/assets/chatgpt_custom_outdoor.txt new file mode 100644 index 0000000000000000000000000000000000000000..1eda4527aa77213d8370ef7e58d38fba737fa53f --- /dev/null +++ b/assets/chatgpt_custom_outdoor.txt @@ -0,0 +1,50 @@ +A peaceful forest with tall trees and a small wooden cabin. +A blooming meadow filled with colorful wildflowers and a single oak tree. +A bustling city park with children playing and a fountain in the center. +A serene beach with soft sand and a lighthouse in the distance. +A quiet mountain trail with rocky terrain and a wooden bench. +A picturesque lake with clear water and a small rowboat. +A charming village square with cobblestone streets and a market stall. +A tranquil garden with blooming roses and a stone pathway. +A vibrant orchard with apple trees and a rustic wooden cart. +A lush valley with green hills and a grazing herd of sheep. +A majestic waterfall cascading into a clear pool with rocks. +A quiet countryside road lined with wildflowers and a wooden fence. +A beautiful vineyard with rows of grapevines and a quaint farmhouse. +A peaceful riverbank with tall grasses and a small fishing pier. +A scenic overlook with a view of rolling hills and a picnic table. +A colorful carnival with bright lights and a Ferris wheel. +A bustling farmer’s market with fresh produce and lively vendors. +A serene bamboo forest with tall stalks and a winding path. +A historic castle with towering walls and a drawbridge. +A cozy village street with charming houses and a lamppost. +A rustic barn in an open field with hay bales stacked outside. +A calm river with a stone bridge and ducks swimming. +A desert landscape with sandy dunes and a single cactus. +A sunny park with children playing and a kite flying. +A vibrant sunflower field with tall sunflowers and a scarecrow. +A rocky cliff overlooking the ocean with seagulls flying. +A peaceful meadow with grazing deer and wildflowers. +A charming harbor with sailboats and a lighthouse. +A dense forest with a carpet of ferns and a hidden stream. +A quaint village with a cobblestone path and flower boxes. +A serene pond with lily pads and a wooden dock. +A lush garden with a variety of flowers and a birdbath. +A colorful coral reef with tropical fish and seaweed. +A picturesque valley with rolling hills and a clear river. +A tranquil lakeside with canoes and a wooden pier. +A vibrant market street with vendors and colorful awnings. +A historic village square with an old clock tower. +A quiet trail through a dense forest with moss-covered trees. +A scenic mountain pass with a winding road and tall pines. +A bustling port with fishing boats and seagulls. +A peaceful woodland glade with a bubbling brook. +A charming cottage garden with blooming flowers and a picket fence. +A serene riverside with willow trees and a stone bench. +A sunny vineyard with ripe grapes and a rustic barn. +A lush forest path with wildflowers and tall trees. +A picturesque beach with gentle waves and seashells. +A vibrant city street with cafes and outdoor seating. +A calm meadow with a stone path and butterflies. +A scenic countryside road with a wooden fence and grazing cows. +A peaceful park with a pond and paddleboats. \ No newline at end of file diff --git a/assets/chatgpt_custom_rainy.txt b/assets/chatgpt_custom_rainy.txt new file mode 100644 index 0000000000000000000000000000000000000000..2490caf52aea669481321fdd37061d133ab60b59 --- /dev/null +++ b/assets/chatgpt_custom_rainy.txt @@ -0,0 +1,24 @@ +A street with children playing near a playground. +A street with trees lining the sidewalk. +A street with reflections of streetlights on the wet road. +A street with a busker playing guitar on the corner. +A street with puddles forming on the pavement. +A street with a newsstand and people reading newspapers. +A street with a market and vendors selling fruits. +A street with people walking. +A street with a mural painted on a building wall. +A street with outdoor cafes and people dining. +A street with a dog park and owners walking their dogs. +A street with pedestrians and a coffee shop. +A street with cars driving slowly. +A street with a park and benches along the sidewalk. +A street with a fountain in the middle of a square. +A street with a bicycle rental station. +A street with a flower shop and colorful displays. +A street with parked cars and a row of houses. +A street with a view of a distant skyline. +A street with a bus stop and people waiting. +A street with street performers entertaining a crowd. +A street with a bookstore and a display of bestsellers. +A street with a bakery and people buying pastries. +A street with a bicycle lane and cyclists riding by. \ No newline at end of file diff --git a/assets/chatgpt_custom_snowy.txt b/assets/chatgpt_custom_snowy.txt new file mode 100644 index 0000000000000000000000000000000000000000..41b64cae72b2134b13f749cfa595813d1698fe12 --- /dev/null +++ b/assets/chatgpt_custom_snowy.txt @@ -0,0 +1,69 @@ +A street with children playing near a playground. +A street with trees lining the sidewalk. +A street with reflections of streetlights on the wet road. +A street with a busker playing guitar on the corner. +A street with puddles forming on the pavement. +A street with a newsstand and people reading newspapers. +A street with a market and vendors selling fruits. +A street with people walking. +A street with a mural painted on a building wall. +A street with outdoor cafes and people dining. +A street with a dog park and owners walking their dogs. +A street with pedestrians and a coffee shop. +A street with cars driving slowly. +A street with a park and benches along the sidewalk. +A street with a fountain in the middle of a square. +A street with a bicycle rental station. +A street with a flower shop and colorful displays. +A street with parked cars and a row of houses. +A street with a view of a distant skyline. +A street with a bus stop and people waiting. +A street with street performers entertaining a crowd. +A street with a bookstore and a display of bestsellers. +A street with a bakery and people buying pastries. +A street with a bicycle lane and cyclists riding by. +A peaceful forest with tall trees and a small wooden cabin. +A blooming meadow filled with colorful wildflowers and a single oak tree. +A bustling city park with children playing and a fountain in the center. +A quiet mountain trail with rocky terrain and a wooden bench. +A picturesque lake with clear water and a small rowboat. +A charming village square with cobblestone streets and a market stall. +A tranquil garden with blooming roses and a stone pathway. +A vibrant orchard with apple trees and a rustic wooden cart. +A lush valley with green hills and a grazing herd of sheep. +A majestic waterfall cascading into a clear pool with rocks. +A quiet countryside road lined with wildflowers and a wooden fence. +A beautiful vineyard with rows of grapevines and a quaint farmhouse. +A peaceful riverbank with tall grasses and a small fishing pier. +A scenic overlook with a view of rolling hills and a picnic table. +A colorful carnival with bright lights and a Ferris wheel. +A bustling farmer’s market with fresh produce and lively vendors. +A serene bamboo forest with tall stalks and a winding path. +A historic castle with towering walls and a drawbridge. +A cozy village street with charming houses and a lamppost. +A rustic barn in an open field with hay bales stacked outside. +A calm river with a stone bridge and ducks swimming. +A desert landscape with sandy dunes and a single cactus. +A sunny park with children playing and a kite flying. +A vibrant sunflower field with tall sunflowers and a scarecrow. +A peaceful meadow with grazing deer and wildflowers. +A charming harbor with sailboats and a lighthouse. +A dense forest with a carpet of ferns and a hidden stream. +A quaint village with a cobblestone path and flower boxes. +A serene pond with lily pads and a wooden dock. +A lush garden with a variety of flowers and a birdbath. +A picturesque valley with rolling hills and a clear river. +A tranquil lakeside with canoes and a wooden pier. +A vibrant market street with vendors and colorful awnings. +A historic village square with an old clock tower. +A quiet trail through a dense forest with moss-covered trees. +A scenic mountain pass with a winding road and tall pines. +A peaceful woodland glade with a bubbling brook. +A charming cottage garden with blooming flowers and a picket fence. +A serene riverside with willow trees and a stone bench. +A sunny vineyard with ripe grapes and a rustic barn. +A lush forest path with wildflowers and tall trees. +A vibrant city street with cafes and outdoor seating. +A calm meadow with a stone path and butterflies. +A scenic countryside road with a wooden fence and grazing cows. +A peaceful park with a pond and paddleboats. \ No newline at end of file diff --git a/assets/compression_reward.pt b/assets/compression_reward.pt new file mode 100644 index 0000000000000000000000000000000000000000..489dff8cfcdcf9a5eb3dfa0f78954c69215a512c --- /dev/null +++ b/assets/compression_reward.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4834909306ec3fd210805ad9eed6506338417ecf898aab7fa2d13f9c32ea2b2d +size 1857371 diff --git a/assets/hps_custom.txt b/assets/hps_custom.txt new file mode 100644 index 0000000000000000000000000000000000000000..b22a4d9fa9af713e67d2ca4df7402870d44ed6c8 --- /dev/null +++ b/assets/hps_custom.txt @@ -0,0 +1,50 @@ +A man taking a drink from a water fountain. +A smiling man is cooking in his kitchen. +A man smiles as he stirs his food in the pot. +Several people around some motorcycles on the side of a road. +A black and white cat looking out a window over another cat. +A woman in a purple top pulling food out of a oven. +A car driving past a parked motorcycle. +A blue airplane in a blue, cloudless sky. +A man and his dog riding on a bike. +A brown and black dog sticking its head out a window. +An airplane flying past the Moon in the sky. +People leaning out the windows of a train as it goes through the countryside. +there are many people trying to avoid the rain +A woman eating vegetables in front of a stove. +A man hanging his head out of the side of a train. +A man getting food ready while people watch. +A woman taking a photo over the shoulder of a man on a bike. +A plane flies in the sky in front of a silhouette of a moon. +there are many men playing soccer in a field. +A woman forks vegetables out of a bowl into her mouth. +A woman taking a picture of herself in a mirror. +A couple of men riding a motorcycle down a street. +A jet flies in the distance with the moon in the background. +there is a man wearing a suit sitting on a bench +Man talking on personal cell phone on a yellow and black bench. +Two people on a motorcycle with tone taking a photo. +View of tall city buildings with cars and people walking by. +Woman walking down the side walk of a busy night city. +Woman eating an assortment of mixed vegetables in a bowl. +some people driving down the road with their bikes +A brown cat crouches and arches its back in a white sink. +Meats being prepared for cooking on kitchen counter. +Large dog looking at television show in living room. +A man driving a motorcycle with a woman holding a cell phone. +A young woman standing in a kitchen eats a plate of vegetables. +The motorcyclist in a helmet is looking over the side of a bridge. +At night on a street with a group of a bicycle riders riding down the road together. +some people holding umbrellas and standing by a car in the rain +A person is riding his motorcycle on the dirt road. +Eight jars are being filled with orange slices. +there are two woman that are riding motorcycles +There is a cyclist riding above all the pigeons. +Some people are enjoying time on a beach. +A woman eating fresh vegetables from a bowl. +Man is riding a board near a field +A man on a motorcycle riding in the desert. +A group of young bicyclists on a city street at night. +two men on a scooter riding down the roadway +A man in a helmet and jacket riding a motorcycle in the desert. +A woman holding a colorful kite on top of a green field. \ No newline at end of file diff --git a/assets/rainy_reward.pt b/assets/rainy_reward.pt new file mode 100644 index 0000000000000000000000000000000000000000..81f122ca26116b3204478658f184ca9eb7218088 --- /dev/null +++ b/assets/rainy_reward.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfa1e0f38d6a8ed08637a72a141ea26fefe05632f5c160e952aa903f24ff80d3 +size 3715200 diff --git a/assets/sac+logos+ava1-l14-linearMSE.pth b/assets/sac+logos+ava1-l14-linearMSE.pth new file mode 100644 index 0000000000000000000000000000000000000000..aae5780851125baf1a30834c3a715d3866858a4d --- /dev/null +++ b/assets/sac+logos+ava1-l14-linearMSE.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:21dd590f3ccdc646f0d53120778b296013b096a035a2718c9cb0d511bff0f1e0 +size 3714759 diff --git a/assets/simple_animals.txt b/assets/simple_animals.txt new file mode 100644 index 0000000000000000000000000000000000000000..bc9e1176a2eb831541d98dcd810b674a36602a78 --- /dev/null +++ b/assets/simple_animals.txt @@ -0,0 +1,45 @@ +cat +dog +horse +monkey +rabbit +zebra +spider +bird +sheep +deer +cow +goat +lion +tiger +bear +raccoon +fox +wolf +lizard +beetle +ant +butterfly +fish +shark +whale +dolphin +squirrel +mouse +rat +snake +turtle +frog +chicken +duck +goose +bee +pig +turkey +fly +llama +camel +bat +gorilla +hedgehog +kangaroo diff --git a/assets/snowy_reward.pt b/assets/snowy_reward.pt new file mode 100644 index 0000000000000000000000000000000000000000..a338ae4abf5b4e3ba97c07ea14f50c1639a5dc54 --- /dev/null +++ b/assets/snowy_reward.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dae251d2b71dd08b8d43f9ea8b2550ac60159ed02f54bede6162b0108d54f991 +size 3715200