diff --git a/configs/man-skiing.yaml b/configs/man-skiing.yaml index 3089b11..a2ff273 100644 --- a/configs/man-skiing.yaml +++ b/configs/man-skiing.yaml @@ -14,7 +14,7 @@ validation_data: prompts: - "mickey mouse is skiing on the snow" - "spider man is skiing on the beach, cartoon style" - - "wonder woman, wearing a cowboy hat, is skiing”" + - "wonder woman, wearing a cowboy hat, is skiing" - "a man, wearing pink clothes, is skiing at sunset" video_length: 24 width: 512 diff --git a/notebooks/Tune-A-Video.ipynb b/notebooks/Tune-A-Video.ipynb index 276fd05..ee77a84 100644 --- a/notebooks/Tune-A-Video.ipynb +++ b/notebooks/Tune-A-Video.ipynb @@ -1 +1,385 @@ -{"cells":[{"cell_type":"markdown","metadata":{"id":"fZ_xQvU70UQc"},"source":["# Tune-A-Video\n","\n","**[Tune-A-Video: One-Shot Tuning of Image Diffusion Models for Text-to-Video Generation](https://arxiv.org/abs/2212.11565)** \n","[Jay Zhangjie Wu](https://zhangjiewu.github.io/), \n","[Yixiao Ge](https://geyixiao.com/), \n","[Xintao Wang](https://xinntao.github.io/), \n","[Stan Weixian Lei](), \n","[Yuchao Gu](https://ycgu.site/), \n","[Wynne Hsu](https://www.comp.nus.edu.sg/~whsu/), \n","[Ying Shan](https://scholar.google.com/citations?user=4oXBp9UAAAAJ&hl=en), \n","[Xiaohu Qie](https://scholar.google.com/citations?user=mk-F69UAAAAJ&hl=en), \n","[Mike Zheng Shou](https://sites.google.com/view/showlab) \n","\n","[![Project Website](https://img.shields.io/badge/Project-Website-orange)](https://tuneavideo.github.io/)\n","[![arXiv](https://img.shields.io/badge/arXiv-2212.11565-b31b1b.svg)](https://arxiv.org/abs/2212.11565)\n","[![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/Tune-A-Video-library/Tune-A-Video-Training-UI)\n","[![GitHub](https://img.shields.io/github/stars/showlab/Tune-A-Video?style=social)](https://github.com/showlab/Tune-A-Video)"]},{"cell_type":"markdown","metadata":{"id":"wnTMyW41cC1E"},"source":["## Setup"]},{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":1617,"status":"ok","timestamp":1675847464041,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"XU7NuMAA2drw","outputId":"6652be79-2978-490c-80f1-a5a0afe8a165"},"outputs":[{"name":"stdout","output_type":"stream","text":["Tesla T4, 15360 MiB, 15109 MiB\n"]}],"source":["#@markdown Check type of GPU and VRAM available.\n","!nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader"]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":48415,"status":"ok","timestamp":1675847514322,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"D1PRgre3Gt5U","outputId":"85079eb4-f9f9-4d4c-bbb5-c1fcc829c0ce"},"outputs":[{"name":"stdout","output_type":"stream","text":["Cloning into '/content/Tune-A-Video'...\n","remote: Enumerating objects: 42, done.\u001b[K\n","remote: Counting objects: 100% (42/42), done.\u001b[K\n","remote: Compressing objects: 100% (32/32), done.\u001b[K\n","remote: Total 42 (delta 9), reused 32 (delta 6), pack-reused 0\u001b[K\n","Unpacking objects: 100% (42/42), 796.30 KiB | 11.06 MiB/s, done.\n","/content/Tune-A-Video\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 MB\u001b[0m \u001b[31m11.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m145.0/145.0 KB\u001b[0m \u001b[31m14.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n"," Building wheel for lit (setup.py) ... \u001b[?25l\u001b[?25hdone\n"]}],"source":["#@title Install requirements\n","\n","!git clone https://github.com/showlab/Tune-A-Video.git /content/Tune-A-Video\n","%cd /content/Tune-A-Video \n","# %pip install -r requirements.txt\n","%pip install -q -U --pre triton\n","%pip install -q diffusers[torch]==0.11.1 transformers==4.26.0 bitsandbytes==0.35.4 \\\n","decord accelerate omegaconf einops ftfy gradio imageio-ffmpeg xformers"]},{"cell_type":"code","execution_count":3,"metadata":{"cellView":"form","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":82042,"status":"ok","timestamp":1675847618104,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"m6I6kZNG3Inb","outputId":"378860d5-2fa0-4aa5-d5fb-0e960254a93a"},"outputs":[{"name":"stdout","output_type":"stream","text":["Updated git hooks.\n","Git LFS initialized.\n","Cloning into 'checkpoints/CompVis/stable-diffusion-v1-4'...\n","remote: Enumerating objects: 719, done.\u001b[K\n","remote: Total 719 (delta 0), reused 0 (delta 0), pack-reused 719\u001b[K\n","Receiving objects: 100% (719/719), 679.70 KiB | 1.42 MiB/s, done.\n","Resolving deltas: 100% (119/119), done.\n","Filtering content: 100% (4/4), 5.10 GiB | 66.30 MiB/s, done.\n","[*] MODEL_NAME=./checkpoints/CompVis/stable-diffusion-v1-4\n"]}],"source":["#@title Download pretrained model\n","\n","#@markdown Name/Path of the initial model.\n","MODEL_NAME = \"CompVis/stable-diffusion-v1-4\" #@param {type:\"string\"}\n","\n","#@markdown If model should be download from a remote repo. Untick it if the model is loaded from a local path.\n","download_pretrained_model = True #@param {type:\"boolean\"}\n","if download_pretrained_model:\n"," !git lfs install\n"," !git clone https://huggingface.co/$MODEL_NAME checkpoints/$MODEL_NAME\n"," MODEL_NAME = f\"./checkpoints/{MODEL_NAME}\"\n","print(f\"[*] MODEL_NAME={MODEL_NAME}\")"]},{"cell_type":"markdown","metadata":{"id":"qn5ILIyDJIcX"},"source":["## Usage\n"]},{"cell_type":"markdown","metadata":{"id":"REmFAHfz9Y_X"},"source":["### Training\n"]},{"cell_type":"code","execution_count":4,"metadata":{"cellView":"form","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":556,"status":"ok","timestamp":1675848002295,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"Rxg0y5MBudmd","outputId":"06cebc9e-27fe-4073-f370-0900d555a628"},"outputs":[{"name":"stdout","output_type":"stream","text":["[*] Weights will be saved at outputs/man-surfing\n"]}],"source":["#@markdown If model weights should be saved directly in google drive (takes around 4-5 GB).\n","save_to_gdrive = False #@param {type:\"boolean\"}\n","if save_to_gdrive:\n"," from google.colab import drive\n"," drive.mount('/content/drive')\n","\n","#@markdown Enter the directory name to save model at.\n","\n","OUTPUT_DIR = \"outputs/man-surfing\" #@param {type:\"string\"}\n","if save_to_gdrive:\n"," OUTPUT_DIR = \"/content/drive/MyDrive/\" + OUTPUT_DIR\n","\n","print(f\"[*] Weights will be saved at {OUTPUT_DIR}\")\n","\n","!mkdir -p $OUTPUT_DIR\n"]},{"cell_type":"code","execution_count":5,"metadata":{"cellView":"form","colab":{"base_uri":"https://localhost:8080/","height":39},"executionInfo":{"elapsed":3955,"status":"ok","timestamp":1675848008563,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"32gYIDDR1aCp","outputId":"74be2d00-844e-4a8b-8773-1aecd9cb427f"},"outputs":[{"data":{"text/html":["\n"," \n"," \n"," Upload widget is only available when the cell has been executed in the\n"," current browser session. Please rerun this cell to enable.\n"," \n"," "],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["#@markdown Upload your video by running this cell.\n","\n","#@markdown OR\n","\n","#@markdown You can use the file manager on the left panel to upload (drag and drop) to `data` folder.\n","\n","import os\n","from google.colab import files\n","import shutil\n","\n","uploaded = files.upload()\n","for filename in uploaded.keys():\n"," dst_path = os.path.join(\"data\", filename)\n"," shutil.move(filename, dst_path)"]},{"cell_type":"code","execution_count":6,"metadata":{"executionInfo":{"elapsed":1,"status":"ok","timestamp":1675848018997,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"wGGFFpNcR2d_"},"outputs":[],"source":["#@markdown Train config\n","\n","from omegaconf import OmegaConf\n","\n","CONFIG_NAME = \"configs/man-surfing.yaml\" #@param {type:\"string\"}\n","\n","train_video_path = \"data/man-surfing.mp4\" #@param {type:\"string\"}\n","train_prompt = \"a man is surfing\" #@param {type:\"string\"}\n","video_length = 8 #@param {type:\"number\"}\n","width = 512 #@param {type:\"number\"}\n","height = 512 #@param {type:\"number\"}\n","learning_rate = 3e-5 #@param {type:\"number\"}\n","train_steps = 300 #@param {type:\"number\"}\n","\n","config = {\n"," \"pretrained_model_path\": MODEL_NAME,\n"," \"output_dir\": OUTPUT_DIR,\n"," \"train_data\": {\n"," \"video_path\": train_video_path,\n"," \"prompt\": train_prompt,\n"," \"n_sample_frames\": video_length,\n"," \"width\": width,\n"," \"height\": height,\n"," \"sample_start_idx\": 0,\n"," \"sample_frame_rate\": 1,\n"," },\n"," \"validation_data\": {\n"," \"prompts\": [\n"," \"a panda is surfing\",\n"," \"a boy, wearing a birthday hat, is surfing\",\n"," \"a raccoon is surfing, cartoon style\",\n"," \"Iron Man is surfing in the desert\",\n"," ],\n"," \"video_length\": video_length,\n"," \"width\": width,\n"," \"height\": height,\n"," \"num_inference_steps\": 20,\n"," \"guidance_scale\": 7.5,\n"," },\n"," \"learning_rate\": learning_rate,\n"," \"train_batch_size\": 1,\n"," \"max_train_steps\": train_steps,\n"," \"checkpointing_steps\": 1000,\n"," \"validation_steps\": 1000,\n"," \"trainable_modules\": [\n"," \"attn1.to_q\",\n"," \"attn2.to_q\",\n"," \"attn_temp\",\n"," ],\n"," \"seed\": 33,\n"," \"mixed_precision\": \"fp16\",\n"," \"use_8bit_adam\": False,\n"," \"gradient_checkpointing\": True,\n"," \"enable_xformers_memory_efficient_attention\": True,\n","}\n","\n","OmegaConf.save(config, CONFIG_NAME)"]},{"cell_type":"code","execution_count":7,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":1109123,"status":"ok","timestamp":1675849135846,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"jjcSXTp-u-Eg","outputId":"dbe96b90-9d60-407e-a277-7ccaaa218a5a"},"outputs":[{"name":"stdout","output_type":"stream","text":["WARNING:accelerate.commands.launch:The following values were not passed to `accelerate launch` and had defaults used instead:\n","\t`--num_processes` was set to a value of `1`\n","\t`--num_machines` was set to a value of `1`\n","\t`--mixed_precision` was set to a value of `'no'`\n","\t`--dynamo_backend` was set to a value of `'no'`\n","To avoid this warning pass in values for each of the problematic parameters or run `accelerate config`.\n","INFO:__main__:Distributed environment: NO\n","Num processes: 1\n","Process index: 0\n","Local process index: 0\n","Device: cuda\n","Mixed precision type: fp16\n","\n","{'prediction_type', 'variance_type'} was not found in config. Values will be initialized to default values.\n","{'norm_num_groups'} was not found in config. Values will be initialized to default values.\n","{'use_linear_projection', 'upcast_attention', 'class_embed_type', 'mid_block_type', 'resnet_time_scale_shift', 'dual_cross_attention', 'only_cross_attention', 'num_class_embeds'} was not found in config. Values will be initialized to default values.\n","{'prediction_type'} was not found in config. Values will be initialized to default values.\n","INFO:__main__:***** Running training *****\n","INFO:__main__: Num examples = 1\n","INFO:__main__: Num Epochs = 300\n","INFO:__main__: Instantaneous batch size per device = 1\n","INFO:__main__: Total train batch size (w. parallel, distributed & accumulation) = 1\n","INFO:__main__: Gradient Accumulation steps = 1\n","INFO:__main__: Total optimization steps = 300\n","Steps: 100% 300/300 [17:06<00:00, 3.43s/it, lr=3e-5, step_loss=0.0428]{'prediction_type'} was not found in config. Values will be initialized to default values.\n","Configuration saved in outputs/man-surfing/2023-02-08T09-20-46/model_index.json\n","Configuration saved in outputs/man-surfing/2023-02-08T09-20-46/vae/config.json\n","Model weights saved in outputs/man-surfing/2023-02-08T09-20-46/vae/diffusion_pytorch_model.bin\n","Configuration saved in outputs/man-surfing/2023-02-08T09-20-46/unet/config.json\n","Model weights saved in outputs/man-surfing/2023-02-08T09-20-46/unet/diffusion_pytorch_model.bin\n","Configuration saved in outputs/man-surfing/2023-02-08T09-20-46/scheduler/scheduler_config.json\n","Steps: 100% 300/300 [17:22<00:00, 3.48s/it, lr=3e-5, step_loss=0.0428]\n"]}],"source":["!accelerate launch train_tuneavideo.py --config=$CONFIG_NAME"]},{"cell_type":"markdown","metadata":{"id":"ToNG4fd_dTbF"},"source":["### Inference"]},{"cell_type":"code","execution_count":13,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":3,"status":"ok","timestamp":1675849329160,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"89Az5NUxOWdy","outputId":"80d6f2ad-f710-4fbf-d115-38667a620b24"},"outputs":[{"name":"stdout","output_type":"stream","text":["[*] WEIGHTS_DIR=outputs/man-surfing/2023-02-08T09-20-46\n"]}],"source":["#@markdown Specify the weights directory to use (leave blank for latest)\n","WEIGHTS_DIR = \"\" #@param {type:\"string\"}\n","if WEIGHTS_DIR == \"\":\n"," from natsort import natsorted\n"," from glob import glob\n"," import os\n"," WEIGHTS_DIR = natsorted(glob(OUTPUT_DIR + os.sep + \"*\"))[-1]\n","print(f\"[*] WEIGHTS_DIR={WEIGHTS_DIR}\")"]},{"cell_type":"code","execution_count":14,"metadata":{"executionInfo":{"elapsed":20083,"status":"ok","timestamp":1675849351518,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"91bsSFv2Punm"},"outputs":[],"source":["import torch\n","from torch import autocast\n","from diffusers import DDIMScheduler\n","from tuneavideo.pipelines.pipeline_tuneavideo import TuneAVideoPipeline\n","from tuneavideo.models.unet import UNet3DConditionModel\n","from tuneavideo.util import save_videos_grid\n","\n","\n","unet = UNet3DConditionModel.from_pretrained(WEIGHTS_DIR, subfolder='unet', torch_dtype=torch.float16).to('cuda')\n","scheduler = DDIMScheduler.from_pretrained(MODEL_NAME, subfolder='scheduler')\n","pipe = TuneAVideoPipeline.from_pretrained(MODEL_NAME, unet=unet, scheduler=scheduler, torch_dtype=torch.float16).to(\"cuda\")\n","pipe.enable_xformers_memory_efficient_attention()\n","g_cuda = None"]},{"cell_type":"code","execution_count":18,"metadata":{"cellView":"form","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":4,"status":"ok","timestamp":1675849749322,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"oIzkltjpVO_f","outputId":"44604d9a-db00-48e4-8ea2-6c9378532e2e"},"outputs":[{"data":{"text/plain":[""]},"execution_count":18,"metadata":{},"output_type":"execute_result"}],"source":["#@markdown Can set random seed here for reproducibility.\n","g_cuda = torch.Generator(device='cuda')\n","seed = 1234 #@param {type:\"number\"}\n","g_cuda.manual_seed(seed)"]},{"cell_type":"code","execution_count":16,"metadata":{"cellView":"form","colab":{"base_uri":"https://localhost:8080/","height":49,"referenced_widgets":["49097988b4a4457baeae21183c14bdd3","a49725f4344a49e0a1c974ff795c76b4","fb35a24428564299a99d51004cccdfa0","dfef05b6c77e480e8a3184169190d936","a08b256dc4f244e98bd0a23a8eacb34f","bb8aa031ddb24d238f6f747c2bffc32d","3ae8a0b6018c485ebd33af4bd976b4ed","4439a02ffe694feb9c2b3caf27d3cffb","b8b58e20668142038672a28ec3f429e3","1a24a695cf60498baded5a9194c8b521","5fa928aea7804dac903c454c6fc1e668"]},"executionInfo":{"elapsed":81227,"status":"ok","timestamp":1675849439987,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"K6xoHWSsbcS3","outputId":"94e7ff06-2997-45a6-d948-c7ddeacdb50d","scrolled":false},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"49097988b4a4457baeae21183c14bdd3","version_major":2,"version_minor":0},"text/plain":[" 0%| | 0/50 [00:00 {\n"," if (!google.colab.kernel.accessAllowed && !cache) {\n"," return;\n"," }\n"," element.appendChild(document.createTextNode(''));\n"," const url = await google.colab.kernel.proxyPort(port, {cache});\n","\n"," const external_link = document.createElement('div');\n"," external_link.innerHTML = `\n","
\n"," Running on \n"," https://localhost:${port}${path}\n"," \n","
\n"," `;\n"," element.appendChild(external_link);\n","\n"," const iframe = document.createElement('iframe');\n"," iframe.src = new URL(path, url).toString();\n"," iframe.height = height;\n"," iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n"," iframe.width = width;\n"," iframe.style.border = 0;\n"," element.appendChild(iframe);\n"," })(7860, \"/\", \"100%\", 500, false, window.element)"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"dac39fd4df474209a7e17a311991b574","version_major":2,"version_minor":0},"text/plain":[" 0%| | 0/50 [00:00 t h w c')\n"," frames = (frames * 255).to(torch.uint8).numpy()\n"," out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)\n"," writer = imageio.get_writer(out_file.name, fps=3)\n"," for frame in frames:\n"," writer.append_data(frame)\n"," writer.close()\n","\n"," return out_file.name\n","\n","with gr.Blocks() as demo:\n"," with gr.Row():\n"," with gr.Column():\n"," prompt = gr.Textbox(label=\"Prompt\", value=\"a panda is surfing\")\n"," negative_prompt = gr.Textbox(label=\"Negative Prompt\", value=\"\")\n"," run = gr.Button(value=\"Generate\")\n"," with gr.Row():\n"," num_samples = gr.Number(label=\"Number of Samples\", value=1)\n"," guidance_scale = gr.Number(label=\"Guidance Scale\", value=7.5)\n"," with gr.Row():\n"," video_length = gr.Number(label=\"Video Length\", value=8)\n"," height = gr.Number(label=\"Height\", value=512)\n"," width = gr.Number(label=\"Width\", value=512)\n"," num_inference_steps = gr.Slider(label=\"Steps\", value=50)\n"," with gr.Column():\n"," result = gr.Video(label='Result')\n","\n"," run.click(inference, inputs=[prompt, negative_prompt, num_samples, video_length, height, width, num_inference_steps, guidance_scale], outputs=result)\n","\n","demo.launch(debug=True)"]},{"cell_type":"code","execution_count":20,"metadata":{"executionInfo":{"elapsed":505,"status":"ok","timestamp":1675849885899,"user":{"displayName":"Tune-A-Video Show Lab","userId":"17565151047989293791"},"user_tz":-480},"id":"jXgi8HM4c-DA"},"outputs":[],"source":["#@markdown Free runtime memory\n","exit()"]}],"metadata":{"accelerator":"GPU","colab":{"provenance":[{"file_id":"https://github.com/ShivamShrirao/diffusers/blob/main/examples/dreambooth/DreamBooth_Stable_Diffusion.ipynb","timestamp":1675778115635}]},"gpuClass":"standard","kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.13-final"},"vscode":{"interpreter":{"hash":"e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"}},"widgets":{"application/vnd.jupyter.widget-state+json":{"04e2b3130df34ce393553119ec19e8a8":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_d79e656551a74cdc858bfae4537e78cb","placeholder":"​","style":"IPY_MODEL_df19cbfa4d3945468db15f5f90cbfa2f","value":"100%"}},"1a24a695cf60498baded5a9194c8b521":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"3ae8a0b6018c485ebd33af4bd976b4ed":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"4439a02ffe694feb9c2b3caf27d3cffb":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"49097988b4a4457baeae21183c14bdd3":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_a49725f4344a49e0a1c974ff795c76b4","IPY_MODEL_fb35a24428564299a99d51004cccdfa0","IPY_MODEL_dfef05b6c77e480e8a3184169190d936"],"layout":"IPY_MODEL_a08b256dc4f244e98bd0a23a8eacb34f"}},"5fa928aea7804dac903c454c6fc1e668":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"82cf82f3836d4a95957533f7e21be784":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"8a5058a18b6f41a097d89838ad401182":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"a08b256dc4f244e98bd0a23a8eacb34f":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"a49725f4344a49e0a1c974ff795c76b4":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_bb8aa031ddb24d238f6f747c2bffc32d","placeholder":"​","style":"IPY_MODEL_3ae8a0b6018c485ebd33af4bd976b4ed","value":"100%"}},"b8b58e20668142038672a28ec3f429e3":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"bb8aa031ddb24d238f6f747c2bffc32d":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"c20e3cbd34d34a9ba062ec2700d965c0":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ProgressView","bar_style":"success","description":"","description_tooltip":null,"layout":"IPY_MODEL_8a5058a18b6f41a097d89838ad401182","max":50,"min":0,"orientation":"horizontal","style":"IPY_MODEL_cec1fb17540d4a869b455f843c42d13a","value":50}},"c6dc464fdf364f6db3440cd0ec5414a4":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_dd2624557cf24536b5f973fcfe7c8dae","placeholder":"​","style":"IPY_MODEL_82cf82f3836d4a95957533f7e21be784","value":" 50/50 [01:20<00:00, 1.56s/it]"}},"cec1fb17540d4a869b455f843c42d13a":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"d79e656551a74cdc858bfae4537e78cb":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"dac39fd4df474209a7e17a311991b574":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_04e2b3130df34ce393553119ec19e8a8","IPY_MODEL_c20e3cbd34d34a9ba062ec2700d965c0","IPY_MODEL_c6dc464fdf364f6db3440cd0ec5414a4"],"layout":"IPY_MODEL_dc2318668fca49b0871711f709e41da1"}},"dc2318668fca49b0871711f709e41da1":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"dd2624557cf24536b5f973fcfe7c8dae":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"df19cbfa4d3945468db15f5f90cbfa2f":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"dfef05b6c77e480e8a3184169190d936":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_1a24a695cf60498baded5a9194c8b521","placeholder":"​","style":"IPY_MODEL_5fa928aea7804dac903c454c6fc1e668","value":" 50/50 [01:16<00:00, 1.64s/it]"}},"fb35a24428564299a99d51004cccdfa0":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ProgressView","bar_style":"success","description":"","description_tooltip":null,"layout":"IPY_MODEL_4439a02ffe694feb9c2b3caf27d3cffb","max":50,"min":0,"orientation":"horizontal","style":"IPY_MODEL_b8b58e20668142038672a28ec3f429e3","value":50}}}}},"nbformat":4,"nbformat_minor":0} \ No newline at end of file +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "fZ_xQvU70UQc" + }, + "source": [ + "# Tune-A-Video\n", + "\n", + "**[Tune-A-Video: One-Shot Tuning of Image Diffusion Models for Text-to-Video Generation](https://arxiv.org/abs/2212.11565)** \n", + "[Jay Zhangjie Wu](https://zhangjiewu.github.io/), \n", + "[Yixiao Ge](https://geyixiao.com/), \n", + "[Xintao Wang](https://xinntao.github.io/), \n", + "[Stan Weixian Lei](), \n", + "[Yuchao Gu](https://ycgu.site/), \n", + "[Wynne Hsu](https://www.comp.nus.edu.sg/~whsu/), \n", + "[Ying Shan](https://scholar.google.com/citations?user=4oXBp9UAAAAJ&hl=en), \n", + "[Xiaohu Qie](https://scholar.google.com/citations?user=mk-F69UAAAAJ&hl=en), \n", + "[Mike Zheng Shou](https://sites.google.com/view/showlab) \n", + "\n", + "[![Project Website](https://img.shields.io/badge/Project-Website-orange)](https://tuneavideo.github.io/)\n", + "[![arXiv](https://img.shields.io/badge/arXiv-2212.11565-b31b1b.svg)](https://arxiv.org/abs/2212.11565)\n", + "[![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/Tune-A-Video-library/Tune-A-Video-Training-UI)\n", + "[![GitHub](https://img.shields.io/github/stars/showlab/Tune-A-Video?style=social)](https://github.com/showlab/Tune-A-Video)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wnTMyW41cC1E" + }, + "source": [ + "## Setup" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "XU7NuMAA2drw" + }, + "outputs": [], + "source": [ + "#@markdown Check type of GPU and VRAM available.\n", + "!nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "D1PRgre3Gt5U" + }, + "outputs": [], + "source": [ + "#@title Install requirements\n", + "\n", + "!git clone https://github.com/showlab/Tune-A-Video.git /content/Tune-A-Video\n", + "%cd /content/Tune-A-Video \n", + "# %pip install -r requirements.txt\n", + "%pip install -q -U --pre triton\n", + "%pip install -q diffusers[torch]==0.11.1 transformers==4.26.0 bitsandbytes==0.35.4 \\\n", + "decord accelerate omegaconf einops ftfy gradio imageio-ffmpeg xformers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "m6I6kZNG3Inb" + }, + "outputs": [], + "source": [ + "#@title Download pretrained model\n", + "\n", + "#@markdown Name/Path of the initial model.\n", + "MODEL_NAME = \"CompVis/stable-diffusion-v1-4\" #@param {type:\"string\"}\n", + "\n", + "#@markdown If model should be download from a remote repo. Untick it if the model is loaded from a local path.\n", + "download_pretrained_model = True #@param {type:\"boolean\"}\n", + "if download_pretrained_model:\n", + " !git lfs install\n", + " !git clone https://huggingface.co/$MODEL_NAME checkpoints/$MODEL_NAME\n", + " MODEL_NAME = f\"./checkpoints/{MODEL_NAME}\"\n", + "print(f\"[*] MODEL_NAME={MODEL_NAME}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qn5ILIyDJIcX" + }, + "source": [ + "## Usage\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "REmFAHfz9Y_X" + }, + "source": [ + "### Training\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Rxg0y5MBudmd" + }, + "outputs": [], + "source": [ + "#@markdown If model weights should be saved directly in google drive (takes around 4-5 GB).\n", + "save_to_gdrive = False #@param {type:\"boolean\"}\n", + "if save_to_gdrive:\n", + " from google.colab import drive\n", + " drive.mount('/content/drive')\n", + "\n", + "#@markdown Enter the directory name to save model at.\n", + "\n", + "OUTPUT_DIR = \"outputs/man-skiing\" #@param {type:\"string\"}\n", + "if save_to_gdrive:\n", + " OUTPUT_DIR = \"/content/drive/MyDrive/\" + OUTPUT_DIR\n", + "\n", + "print(f\"[*] Weights will be saved at {OUTPUT_DIR}\")\n", + "\n", + "!mkdir -p $OUTPUT_DIR\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "32gYIDDR1aCp" + }, + "outputs": [], + "source": [ + "#@markdown Upload your video by running this cell.\n", + "\n", + "#@markdown OR\n", + "\n", + "#@markdown You can use the file manager on the left panel to upload (drag and drop) to `data` folder.\n", + "\n", + "import os\n", + "from google.colab import files\n", + "import shutil\n", + "\n", + "uploaded = files.upload()\n", + "for filename in uploaded.keys():\n", + " dst_path = os.path.join(\"data\", filename)\n", + " shutil.move(filename, dst_path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "wGGFFpNcR2d_" + }, + "outputs": [], + "source": [ + "#@markdown Train config\n", + "\n", + "from omegaconf import OmegaConf\n", + "\n", + "CONFIG_NAME = \"configs/man-skiing.yaml\" #@param {type:\"string\"}\n", + "\n", + "train_video_path = \"data/man-skiing.mp4\" #@param {type:\"string\"}\n", + "train_prompt = \"a man is skiing\" #@param {type:\"string\"}\n", + "video_length = 8 #@param {type:\"number\"}\n", + "width = 512 #@param {type:\"number\"}\n", + "height = 512 #@param {type:\"number\"}\n", + "learning_rate = 3e-5 #@param {type:\"number\"}\n", + "train_steps = 300 #@param {type:\"number\"}\n", + "\n", + "config = {\n", + " \"pretrained_model_path\": MODEL_NAME,\n", + " \"output_dir\": OUTPUT_DIR,\n", + " \"train_data\": {\n", + " \"video_path\": train_video_path,\n", + " \"prompt\": train_prompt,\n", + " \"n_sample_frames\": video_length,\n", + " \"width\": width,\n", + " \"height\": height,\n", + " \"sample_start_idx\": 0,\n", + " \"sample_frame_rate\": 2,\n", + " },\n", + " \"validation_data\": {\n", + " \"prompts\": [\n", + " \"mickey mouse is skiing on the snow\",\n", + " \"spider man is skiing on the beach, cartoon style\",\n", + " \"wonder woman, wearing a cowboy hat, is skiing\",\n", + " \"a man, wearing pink clothes, is skiing at sunset\",\n", + " ],\n", + " \"video_length\": video_length,\n", + " \"width\": width,\n", + " \"height\": height,\n", + " \"num_inference_steps\": 20,\n", + " \"guidance_scale\": 12.5,\n", + " \"use_inv_latent\": True,\n", + " \"num_inv_steps\": 50,\n", + " },\n", + " \"learning_rate\": learning_rate,\n", + " \"train_batch_size\": 1,\n", + " \"max_train_steps\": train_steps,\n", + " \"checkpointing_steps\": 1000,\n", + " \"validation_steps\": 100,\n", + " \"trainable_modules\": [\n", + " \"attn1.to_q\",\n", + " \"attn2.to_q\",\n", + " \"attn_temp\",\n", + " ],\n", + " \"seed\": 33,\n", + " \"mixed_precision\": \"fp16\",\n", + " \"use_8bit_adam\": False,\n", + " \"gradient_checkpointing\": True,\n", + " \"enable_xformers_memory_efficient_attention\": True,\n", + "}\n", + "\n", + "OmegaConf.save(config, CONFIG_NAME)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "jjcSXTp-u-Eg" + }, + "outputs": [], + "source": [ + "!accelerate launch train_tuneavideo.py --config=$CONFIG_NAME" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ToNG4fd_dTbF" + }, + "source": [ + "### Inference" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "91bsSFv2Punm" + }, + "outputs": [], + "source": [ + "import torch\n", + "from torch import autocast\n", + "from diffusers import DDIMScheduler\n", + "from tuneavideo.pipelines.pipeline_tuneavideo import TuneAVideoPipeline\n", + "from tuneavideo.models.unet import UNet3DConditionModel\n", + "from tuneavideo.util import save_videos_grid\n", + "\n", + "\n", + "unet = UNet3DConditionModel.from_pretrained(OUTPUT_DIR, subfolder='unet', torch_dtype=torch.float16).to('cuda')\n", + "scheduler = DDIMScheduler.from_pretrained(MODEL_NAME, subfolder='scheduler')\n", + "pipe = TuneAVideoPipeline.from_pretrained(MODEL_NAME, unet=unet, scheduler=scheduler, torch_dtype=torch.float16).to(\"cuda\")\n", + "pipe.enable_xformers_memory_efficient_attention()\n", + "pipe.enable_vae_slicing()\n", + "\n", + "g_cuda = None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "oIzkltjpVO_f" + }, + "outputs": [], + "source": [ + "#@markdown Can set random seed here for reproducibility.\n", + "g_cuda = torch.Generator(device='cuda')\n", + "seed = 1234 #@param {type:\"number\"}\n", + "g_cuda.manual_seed(seed)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "K6xoHWSsbcS3", + "scrolled": false + }, + "outputs": [], + "source": [ + "#@markdown Run for generating videos.\n", + "\n", + "prompt = \"iron man is skiing\" #@param {type:\"string\"}\n", + "negative_prompt = \"\" #@param {type:\"string\"}\n", + "use_inv_latent = True #@param {type:\"boolean\"}\n", + "inv_latent_path = \"\" #@param {type:\"string\"}\n", + "num_samples = 1 #@param {type:\"number\"}\n", + "guidance_scale = 12.5 #@param {type:\"number\"}\n", + "num_inference_steps = 50 #@param {type:\"number\"}\n", + "video_length = 8 #@param {type:\"number\"}\n", + "height = 512 #@param {type:\"number\"}\n", + "width = 512 #@param {type:\"number\"}\n", + "\n", + "ddim_inv_latent = None\n", + "if use_inv_latent and inv_latent_path == \"\":\n", + " from natsort import natsorted\n", + " from glob import glob\n", + " import os\n", + " inv_latent_path = natsorted(glob(f\"{OUTPUT_DIR}/inv_latents/*\"))[-1]\n", + " ddim_inv_latent = torch.load(inv_latent_path).to(torch.float16)\n", + " print(f\"DDIM inversion latent loaded from {inv_latent_path}\")\n", + "\n", + "with autocast(\"cuda\"), torch.inference_mode():\n", + " videos = pipe(\n", + " prompt, \n", + " latents=ddim_inv_latent,\n", + " video_length=video_length, \n", + " height=height, \n", + " width=width, \n", + " negative_prompt=negative_prompt,\n", + " num_videos_per_prompt=num_samples,\n", + " num_inference_steps=num_inference_steps, \n", + " guidance_scale=guidance_scale,\n", + " generator=g_cuda\n", + " ).videos\n", + "\n", + "save_dir = \"./results\" #@param {type:\"string\"}\n", + "save_path = f\"{save_dir}/{prompt}.gif\"\n", + "save_videos_grid(videos, save_path)\n", + "\n", + "# display\n", + "from IPython.display import Image, display\n", + "display(Image(filename=save_path))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "jXgi8HM4c-DA" + }, + "outputs": [], + "source": [ + "#@markdown Free runtime memory\n", + "exit()" + ] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "provenance": [] + }, + "gpuClass": "standard", + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.13-final" + }, + "vscode": { + "interpreter": { + "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file