Spaces:
Running
on
Zero
Running
on
Zero
Update gradio_app.py
Browse files- gradio_app.py +34 -41
gradio_app.py
CHANGED
|
@@ -10,11 +10,7 @@ from attn_ctrl.attention_control import (AttentionStore,
|
|
| 10 |
register_temporal_self_attention_control,
|
| 11 |
register_temporal_self_attention_flip_control,
|
| 12 |
)
|
| 13 |
-
from torch.amp import autocast
|
| 14 |
-
import gc
|
| 15 |
-
|
| 16 |
-
# Set PYTORCH_CUDA_ALLOC_CONF
|
| 17 |
-
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
|
| 18 |
|
| 19 |
# Set up device
|
| 20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -32,7 +28,7 @@ pipe = FrameInterpolationWithNoiseInjectionPipeline.from_pretrained(
|
|
| 32 |
scheduler=noise_scheduler,
|
| 33 |
variant="fp16",
|
| 34 |
torch_dtype=torch.float16,
|
| 35 |
-
)
|
| 36 |
ref_unet = pipe.ori_unet
|
| 37 |
|
| 38 |
# Compute delta w
|
|
@@ -41,14 +37,14 @@ finetuned_unet = UNetSpatioTemporalConditionModel.from_pretrained(
|
|
| 41 |
checkpoint_dir,
|
| 42 |
subfolder="unet",
|
| 43 |
torch_dtype=torch.float16,
|
| 44 |
-
)
|
| 45 |
assert finetuned_unet.config.num_frames == 14
|
| 46 |
ori_unet = UNetSpatioTemporalConditionModel.from_pretrained(
|
| 47 |
"stabilityai/stable-video-diffusion-img2vid",
|
| 48 |
subfolder="unet",
|
| 49 |
variant='fp16',
|
| 50 |
torch_dtype=torch.float16,
|
| 51 |
-
)
|
| 52 |
|
| 53 |
finetuned_state_dict = finetuned_unet.state_dict()
|
| 54 |
ori_state_dict = ori_unet.state_dict()
|
|
@@ -68,7 +64,6 @@ register_temporal_self_attention_flip_control(pipe.unet, controller, controller_
|
|
| 68 |
def cuda_memory_cleanup():
|
| 69 |
torch.cuda.empty_cache()
|
| 70 |
torch.cuda.ipc_collect()
|
| 71 |
-
gc.collect()
|
| 72 |
|
| 73 |
def check_outputs_folder(folder_path):
|
| 74 |
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
|
@@ -87,51 +82,47 @@ def check_outputs_folder(folder_path):
|
|
| 87 |
@torch.no_grad()
|
| 88 |
def infer(frame1_path, frame2_path):
|
| 89 |
seed = 42
|
| 90 |
-
num_inference_steps =
|
| 91 |
noise_injection_steps = 0
|
| 92 |
noise_injection_ratio = 0.5
|
| 93 |
weighted_average = False
|
|
|
|
| 94 |
|
| 95 |
generator = torch.Generator(device)
|
| 96 |
if seed is not None:
|
| 97 |
generator = generator.manual_seed(seed)
|
| 98 |
|
| 99 |
frame1 = load_image(frame1_path)
|
| 100 |
-
frame1 = frame1.resize((
|
| 101 |
|
| 102 |
frame2 = load_image(frame2_path)
|
| 103 |
-
frame2 = frame2.resize((
|
| 104 |
|
| 105 |
-
# Clear CUDA cache
|
| 106 |
cuda_memory_cleanup()
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
else:
|
| 132 |
-
return f"An error occurred: {str(e)}"
|
| 133 |
-
finally:
|
| 134 |
-
cuda_memory_cleanup()
|
| 135 |
|
| 136 |
with gr.Blocks() as demo:
|
| 137 |
with gr.Column():
|
|
@@ -151,4 +142,6 @@ with gr.Blocks() as demo:
|
|
| 151 |
show_api=False
|
| 152 |
)
|
| 153 |
|
| 154 |
-
demo.
|
|
|
|
|
|
|
|
|
| 10 |
register_temporal_self_attention_control,
|
| 11 |
register_temporal_self_attention_flip_control,
|
| 12 |
)
|
| 13 |
+
from torch.cuda.amp import autocast
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Set up device
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 28 |
scheduler=noise_scheduler,
|
| 29 |
variant="fp16",
|
| 30 |
torch_dtype=torch.float16,
|
| 31 |
+
)
|
| 32 |
ref_unet = pipe.ori_unet
|
| 33 |
|
| 34 |
# Compute delta w
|
|
|
|
| 37 |
checkpoint_dir,
|
| 38 |
subfolder="unet",
|
| 39 |
torch_dtype=torch.float16,
|
| 40 |
+
)
|
| 41 |
assert finetuned_unet.config.num_frames == 14
|
| 42 |
ori_unet = UNetSpatioTemporalConditionModel.from_pretrained(
|
| 43 |
"stabilityai/stable-video-diffusion-img2vid",
|
| 44 |
subfolder="unet",
|
| 45 |
variant='fp16',
|
| 46 |
torch_dtype=torch.float16,
|
| 47 |
+
)
|
| 48 |
|
| 49 |
finetuned_state_dict = finetuned_unet.state_dict()
|
| 50 |
ori_state_dict = ori_unet.state_dict()
|
|
|
|
| 64 |
def cuda_memory_cleanup():
|
| 65 |
torch.cuda.empty_cache()
|
| 66 |
torch.cuda.ipc_collect()
|
|
|
|
| 67 |
|
| 68 |
def check_outputs_folder(folder_path):
|
| 69 |
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
|
|
|
| 82 |
@torch.no_grad()
|
| 83 |
def infer(frame1_path, frame2_path):
|
| 84 |
seed = 42
|
| 85 |
+
num_inference_steps = 10
|
| 86 |
noise_injection_steps = 0
|
| 87 |
noise_injection_ratio = 0.5
|
| 88 |
weighted_average = False
|
| 89 |
+
decode_chunk_size = 8
|
| 90 |
|
| 91 |
generator = torch.Generator(device)
|
| 92 |
if seed is not None:
|
| 93 |
generator = generator.manual_seed(seed)
|
| 94 |
|
| 95 |
frame1 = load_image(frame1_path)
|
| 96 |
+
frame1 = frame1.resize((512, 288))
|
| 97 |
|
| 98 |
frame2 = load_image(frame2_path)
|
| 99 |
+
frame2 = frame2.resize((512, 288))
|
| 100 |
|
|
|
|
| 101 |
cuda_memory_cleanup()
|
| 102 |
|
| 103 |
+
with autocast():
|
| 104 |
+
frames = pipe(image1=frame1, image2=frame2,
|
| 105 |
+
num_inference_steps=num_inference_steps,
|
| 106 |
+
generator=generator,
|
| 107 |
+
weighted_average=weighted_average,
|
| 108 |
+
noise_injection_steps=noise_injection_steps,
|
| 109 |
+
noise_injection_ratio=noise_injection_ratio,
|
| 110 |
+
decode_chunk_size=decode_chunk_size
|
| 111 |
+
).frames[0]
|
| 112 |
+
|
| 113 |
+
frames = [frame.cpu() for frame in frames]
|
| 114 |
+
|
| 115 |
+
out_dir = "result"
|
| 116 |
+
check_outputs_folder(out_dir)
|
| 117 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 118 |
+
out_path = "result/video_result.gif"
|
| 119 |
+
|
| 120 |
+
return "done"
|
| 121 |
+
|
| 122 |
+
@torch.no_grad()
|
| 123 |
+
def load_model():
|
| 124 |
+
global pipe
|
| 125 |
+
pipe = pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
with gr.Column():
|
|
|
|
| 142 |
show_api=False
|
| 143 |
)
|
| 144 |
|
| 145 |
+
demo.load(load_model)
|
| 146 |
+
|
| 147 |
+
demo.queue(max_size=1).launch(show_api=False, show_error=True)
|