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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
sys.path.append('..')
|
| 3 |
+
import argparse
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
parser = argparse.ArgumentParser(description="Generate a video from a text prompt using CogVideoX")
|
| 7 |
+
parser.add_argument("--prompt", type=str, required=True, help="The description of the video to be generated")
|
| 8 |
+
parser.add_argument("--first_image", type=str,required=True, help="The path of the video for controlnet processing.",)
|
| 9 |
+
parser.add_argument("--last_image", type=str,required=True, help="The path of the video for controlnet processing.",)
|
| 10 |
+
|
| 11 |
+
parser.add_argument("--pretrained_model_name_or_path", type=str, default="THUDM/CogVideoX-5b", help="The path of the pre-trained model to be used")
|
| 12 |
+
parser.add_argument("--EF_Net_model_path", type=str, default="TheDenk/cogvideox-5b-controlnet-hed-v1", help="The path of the controlnet pre-trained model to be used")
|
| 13 |
+
parser.add_argument("--EF_Net_weights", type=float, default=1.0, help="Strenght of controlnet")
|
| 14 |
+
parser.add_argument("--EF_Net_guidance_start", type=float, default=0.0, help="The stage when the controlnet starts to be applied")
|
| 15 |
+
parser.add_argument("--EF_Net_guidance_end", type=float, default=1.0, help="The stage when the controlnet end to be applied")
|
| 16 |
+
|
| 17 |
+
parser.add_argument("--out_path", type=str, default="./output.mp4", help="The path where the generated video will be saved")
|
| 18 |
+
parser.add_argument("--guidance_scale", type=float, default=6.0, help="The scale for classifier-free guidance")
|
| 19 |
+
parser.add_argument("--dtype", type=str, default="bfloat16", help="The data type for computation (e.g., 'float16' or 'bfloat16')")
|
| 20 |
+
parser.add_argument("--seed", type=int, default=42, help="The seed for reproducibility")
|
| 21 |
+
|
| 22 |
+
args = parser.parse_args()
|
| 23 |
+
|
| 24 |
+
import time
|
| 25 |
+
import torch
|
| 26 |
+
import numpy as np
|
| 27 |
+
from transformers import T5EncoderModel, T5Tokenizer
|
| 28 |
+
from diffusers import (
|
| 29 |
+
CogVideoXDDIMScheduler,
|
| 30 |
+
CogVideoXDPMScheduler,
|
| 31 |
+
AutoencoderKLCogVideoX
|
| 32 |
+
)
|
| 33 |
+
from diffusers.utils import export_to_video, load_image
|
| 34 |
+
from cogvideo_Sci_Fi_inbetweening_pipeline import CogVideoXEFNetInbetweeningPipeline
|
| 35 |
+
from cogvideo_transformer import CustomCogVideoXTransformer3DModel
|
| 36 |
+
from cogvideo_EF_Net import CogVideoX_EF_Net
|
| 37 |
+
import cv2
|
| 38 |
+
import os
|
| 39 |
+
import sys
|
| 40 |
+
from decord import VideoReader
|
| 41 |
+
|
| 42 |
+
@torch.no_grad()
|
| 43 |
+
def generate_video(
|
| 44 |
+
prompt: str,
|
| 45 |
+
first_image: str,
|
| 46 |
+
last_image: str,
|
| 47 |
+
pretrained_model_name_or_path: str,
|
| 48 |
+
EF_Net_model_path: str,
|
| 49 |
+
EF_Net_weights: float = 1.0,
|
| 50 |
+
EF_Net_guidance_start: float = 0.0,
|
| 51 |
+
EF_Net_guidance_end: float = 1.0,
|
| 52 |
+
out_path: str = "./output.mp4",
|
| 53 |
+
guidance_scale: float = 6.0,
|
| 54 |
+
dtype: torch.dtype = torch.bfloat16,
|
| 55 |
+
seed: int = 42,
|
| 56 |
+
):
|
| 57 |
+
"""
|
| 58 |
+
Parameters:
|
| 59 |
+
- prompt (str): The description of the video to be generated.
|
| 60 |
+
- first_image (str): The start frame.
|
| 61 |
+
- last_image (str): The end frame.
|
| 62 |
+
- pretrained_model_name_or_path (str): The path of the pre-trained model to be used.
|
| 63 |
+
- transformer_model_path (str): The path of the pre-trained transformer to be used.
|
| 64 |
+
- EF_Net_model_path (str): The path of the pre-trained EF-Net model to be used.
|
| 65 |
+
- EF_Net_weights (float): Strenght of EF-Net
|
| 66 |
+
- EF_Net_guidance_start (float): The stage when the EF-Net starts to be applied
|
| 67 |
+
- EF_Net_guidance_end (float): The stage when the EF-Net end to be applied
|
| 68 |
+
- out_path (str): The path where the generated video will be saved.
|
| 69 |
+
- guidance_scale (float): The scale for classifier-free guidance. Higher values can lead to better alignment with the prompt.
|
| 70 |
+
- dtype (torch.dtype): The data type for computation (default is torch.bfloat16).
|
| 71 |
+
- seed (int): The seed for reproducibility.
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
# 1. Load the pre-trained CogVideoX-I2V-5B model.
|
| 75 |
+
tokenizer = T5Tokenizer.from_pretrained(pretrained_model_name_or_path, subfolder="tokenizer")
|
| 76 |
+
text_encoder = T5EncoderModel.from_pretrained(pretrained_model_name_or_path, subfolder="text_encoder")
|
| 77 |
+
transformer = CustomCogVideoXTransformer3DModel.from_pretrained(pretrained_model_name_or_path, subfolder="transformer")
|
| 78 |
+
vae = AutoencoderKLCogVideoX.from_pretrained(pretrained_model_name_or_path, subfolder="vae")
|
| 79 |
+
scheduler = CogVideoXDDIMScheduler.from_pretrained(pretrained_model_name_or_path, subfolder="scheduler")
|
| 80 |
+
|
| 81 |
+
# 2. Load the pre-trained EF_Net
|
| 82 |
+
EF_Net = CogVideoX_EF_Net(num_layers=4, downscale_coef=8, in_channels=2, num_attention_heads=48,).requires_grad_(False).eval()
|
| 83 |
+
ckpt = torch.load(EF_Net_model_path, map_location='cpu', weights_only=False)
|
| 84 |
+
EF_Net_state_dict = {}
|
| 85 |
+
for name, params in ckpt['state_dict'].items():
|
| 86 |
+
EF_Net_state_dict[name] = params
|
| 87 |
+
m, u = EF_Net.load_state_dict(EF_Net_state_dict, strict=False)
|
| 88 |
+
print(f'[ Weights from pretrained EF-Net was loaded into EF-Net ] [M: {len(m)} | U: {len(u)}]')
|
| 89 |
+
|
| 90 |
+
#3. Load the prompt (Can be modified independently according to specific needs.)
|
| 91 |
+
with open(prompt, 'r', encoding='utf-8') as file:
|
| 92 |
+
prompt = file.read()
|
| 93 |
+
prompt = prompt.strip()
|
| 94 |
+
|
| 95 |
+
# 4. Combine as a pipeline
|
| 96 |
+
pipe = CogVideoXEFNetInbetweeningPipeline(
|
| 97 |
+
tokenizer=tokenizer,
|
| 98 |
+
text_encoder=text_encoder,
|
| 99 |
+
transformer=transformer,
|
| 100 |
+
vae=vae,
|
| 101 |
+
EF_Net=EF_Net,
|
| 102 |
+
scheduler=scheduler,
|
| 103 |
+
)
|
| 104 |
+
pipe.scheduler = CogVideoXDDIMScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
| 105 |
+
|
| 106 |
+
# 5. Enable CPU offload for the model.
|
| 107 |
+
# turn off if you have multiple GPUs or enough GPU memory(such as H100) and it will cost less time in inference
|
| 108 |
+
# and enable to("cuda")
|
| 109 |
+
|
| 110 |
+
pipe.to("cuda")
|
| 111 |
+
pipe = pipe.to(dtype=dtype)
|
| 112 |
+
#pipe.enable_sequential_cpu_offload()
|
| 113 |
+
|
| 114 |
+
pipe.vae.enable_slicing()
|
| 115 |
+
pipe.vae.enable_tiling()
|
| 116 |
+
|
| 117 |
+
# 6. Generate the video frames based on the start and end frames, as well as the text prompt
|
| 118 |
+
|
| 119 |
+
first_image = load_image(first_image)
|
| 120 |
+
last_image = load_image(last_image)
|
| 121 |
+
|
| 122 |
+
start_time = time.time()
|
| 123 |
+
|
| 124 |
+
video_generate = pipe(
|
| 125 |
+
first_image=first_image,
|
| 126 |
+
last_image=last_image,
|
| 127 |
+
prompt=prompt,
|
| 128 |
+
num_frames=49,
|
| 129 |
+
use_dynamic_cfg=False,
|
| 130 |
+
guidance_scale=guidance_scale,
|
| 131 |
+
generator=torch.Generator().manual_seed(seed), # Set the seed for reproducibility
|
| 132 |
+
EF_Net_weights=EF_Net_weights,
|
| 133 |
+
EF_Net_guidance_start=EF_Net_guidance_start,
|
| 134 |
+
EF_Net_guidance_end=EF_Net_guidance_end,
|
| 135 |
+
).frames[0]
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
export_to_video(video_generate, out_path, fps=7)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
|
| 143 |
+
dtype = torch.float16 if args.dtype == "float16" else torch.bfloat16
|
| 144 |
+
generate_video(
|
| 145 |
+
prompt=args.prompt,
|
| 146 |
+
first_image=args.first_image,
|
| 147 |
+
last_image=args.last_image,
|
| 148 |
+
pretrained_model_name_or_path=args.pretrained_model_name_or_path,
|
| 149 |
+
EF_Net_model_path=args.EF_Net_model_path,
|
| 150 |
+
EF_Net_weights=args.EF_Net_weights,
|
| 151 |
+
EF_Net_guidance_start=args.EF_Net_guidance_start,
|
| 152 |
+
EF_Net_guidance_end=args.EF_Net_guidance_end,
|
| 153 |
+
out_path=args.out_path,
|
| 154 |
+
guidance_scale=args.guidance_scale,
|
| 155 |
+
dtype=dtype,
|
| 156 |
+
seed=args.seed,
|
| 157 |
+
)
|
Sci_Fi_frame_inbetweening.sh
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export CUDA_VISIBLE_DEVICES=6
|
| 2 |
+
EVAL_DIR=testing_data
|
| 3 |
+
MODEL_NAME=CogVideoX-5b-I2V
|
| 4 |
+
OUT_DIR=outputs
|
| 5 |
+
|
| 6 |
+
mkdir -p $OUT_DIR
|
| 7 |
+
for example_dir in $(ls -d $EVAL_DIR/*)
|
| 8 |
+
do
|
| 9 |
+
example_name=$(EVAL_DIR $example_dir)
|
| 10 |
+
echo $example_name
|
| 11 |
+
|
| 12 |
+
out_fn=$OUT_DIR/$example_name'.mp4'
|
| 13 |
+
python Sci_Fi_frame_inbetweening.py \
|
| 14 |
+
--first_image=$example_dir/start.jpg \
|
| 15 |
+
--last_image=$example_dir/end.jpg \
|
| 16 |
+
--EF_Net_model_path='EF_Net/EF_Net.pt' \
|
| 17 |
+
--pretrained_model_name_or_path=$MODEL_NAME \
|
| 18 |
+
--prompt=$example_dir/prompt.txt \
|
| 19 |
+
--out_path=$out_fn \
|
| 20 |
+
--EF_Net_weights=1.0 \
|
| 21 |
+
|
| 22 |
+
done
|