{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/mnt/workspace/ysh/miniconda3/envs/consisid/lib/python3.12/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers\n", " warnings.warn(f\"Importing from {__name__} is deprecated, please import via timm.layers\", FutureWarning)\n", "/mnt/workspace/ysh/miniconda3/envs/consisid/lib/python3.12/site-packages/timm/models/hub.py:4: FutureWarning: Importing from timm.models.hub is deprecated, please import via timm.models\n", " warnings.warn(f\"Importing from {__name__} is deprecated, please import via timm.models\", FutureWarning)\n" ] } ], "source": [ "import os\n", "import json\n", "import torch\n", "import numpy as np\n", "import PIL\n", "from PIL import Image\n", "from IPython.display import HTML\n", "from pyramid_dit import PyramidDiTForVideoGeneration\n", "from IPython.display import Image as ipython_image\n", "from diffusers.utils import load_image, export_to_video, export_to_gif" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using temporal causal attention\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c335b12f18da4834843ad6f92bf7eab6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading checkpoint shards: 0%| | 0/2 [00:00\n", " \n", " \n", " \"\"\"\n", " \n", " html += f\"\"\"\n", " \"\"\"\n", " return HTML(html)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#### Text-to-Video" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prompt = \"A movie trailer featuring the adventures of the 30 year old space man wearing a red wool knitted motorcycle helmet, blue sky, salt desert, cinematic style, shot on 35mm film, vivid colors\"\n", "\n", "# used for 384p model variant\n", "# width = 640\n", "# height = 384\n", "\n", "# used for 768p model variant\n", "width = 1280\n", "height = 768\n", "\n", "temp = 16 # temp in [1, 31] <=> frame in [1, 241] <=> duration in [0, 10s]\n", "# Noting that, for the 384p version, only supports maximum 5s generation (temp = 16)\n", "\n", "with torch.no_grad(), torch.amp.autocast('cuda', enabled=True if model_dtype != 'fp32' else False, dtype=torch_dtype):\n", " frames = model.generate(\n", " prompt=prompt,\n", " num_inference_steps=[20, 20, 20],\n", " video_num_inference_steps=[10, 10, 10],\n", " height=height,\n", " width=width,\n", " temp=temp,\n", " guidance_scale=7.0, # The guidance for the first frame, set it to 7 for 384p variant\n", " video_guidance_scale=5.0, # The guidance for the other video latent\n", " output_type=\"pil\",\n", " save_memory=True, # If you have enough GPU memory, set it to `False` to improve vae decoding speed\n", " )\n", "\n", "export_to_video(frames, \"./text_to_video_sample.mp4\", fps=24)\n", "show_video(None, \"./text_to_video_sample.mp4\", \"70%\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "#### Image-to-Video" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image_path = 'assets/the_great_wall.jpg'\n", "image = Image.open(image_path).convert(\"RGB\")\n", "\n", "# used for 384p model variant\n", "# width = 640\n", "# height = 384\n", "\n", "# used for 768p model variant\n", "width = 1280\n", "height = 768\n", "\n", "temp = 16\n", "image = image.resize((width, height))\n", "image = resize_crop_image(image, width, height)\n", "\n", "display(image)\n", "\n", "prompt = \"FPV flying over the Great Wall\"\n", "\n", "with torch.no_grad(), torch.amp.autocast('cuda', enabled=True if model_dtype != 'fp32' else False, dtype=torch_dtype):\n", " frames = model.generate_i2v(\n", " prompt=prompt,\n", " input_image=image,\n", " num_inference_steps=[10, 10, 10],\n", " temp=temp,\n", " guidance_scale=7.0,\n", " video_guidance_scale=4.0,\n", " output_type=\"pil\",\n", " save_memory=True, # If you have enough GPU memory, set it to `False` to improve vae decoding speed\n", " )\n", "\n", "export_to_video(frames, \"./image_to_video_sample.mp4\", fps=24)\n", "show_video(None, \"./image_to_video_sample.mp4\", \"70%\")" ] } ], "metadata": { "kernelspec": { "display_name": "consisid", "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.12.2" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }