app.py: defining the correct device
Browse files
app.py
CHANGED
|
@@ -1,76 +1,79 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from torchvision.transforms import Compose, ToTensor, Resize, Normalize
|
| 5 |
-
import numpy as np
|
| 6 |
-
import imageio
|
| 7 |
-
import tempfile
|
| 8 |
-
|
| 9 |
-
from utils.utils import denorm
|
| 10 |
-
from model.hub import MultiInputResShiftHub
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"-
|
| 67 |
-
"
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
demo.launch(max_threads=1)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from torchvision.transforms import Compose, ToTensor, Resize, Normalize
|
| 5 |
+
import numpy as np
|
| 6 |
+
import imageio
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
from utils.utils import denorm
|
| 10 |
+
from model.hub import MultiInputResShiftHub
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
|
| 15 |
+
model = MultiInputResShiftHub.from_pretrained("vfontech/Multiple-Input-Resshift-VFI")
|
| 16 |
+
model.requires_grad_(False).to(device).eval()
|
| 17 |
+
|
| 18 |
+
transform = Compose([
|
| 19 |
+
Resize((256, 448)),
|
| 20 |
+
ToTensor(),
|
| 21 |
+
Normalize(mean=[0.5]*3, std=[0.5]*3),
|
| 22 |
+
])
|
| 23 |
+
|
| 24 |
+
def to_numpy(img_tensor):
|
| 25 |
+
img_np = denorm(img_tensor, mean=[0.5]*3, std=[0.5]*3).squeeze().permute(1, 2, 0).cpu().numpy()
|
| 26 |
+
img_np = np.clip(img_np, 0, 1)
|
| 27 |
+
return (img_np * 255).astype(np.uint8)
|
| 28 |
+
|
| 29 |
+
def interpolate(img0_pil, img2_pil, tau, num_samples):
|
| 30 |
+
img0 = transform(img0_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 31 |
+
img2 = transform(img2_pil.convert("RGB")).unsqueeze(0).to(device)
|
| 32 |
+
|
| 33 |
+
if num_samples == 1:
|
| 34 |
+
# Unique image
|
| 35 |
+
img1 = model.reverse_process([img0, img2], tau)
|
| 36 |
+
return Image.fromarray(to_numpy(img1)), None
|
| 37 |
+
else:
|
| 38 |
+
# Múltiples imágenes → video
|
| 39 |
+
frames = [to_numpy(img0)]
|
| 40 |
+
for t in np.linspace(0, 1, num_samples):
|
| 41 |
+
img = model.reverse_process([img0, img2], float(t))
|
| 42 |
+
frames.append(to_numpy(img))
|
| 43 |
+
frames.append(to_numpy(img2))
|
| 44 |
+
|
| 45 |
+
temp_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 46 |
+
imageio.mimsave(temp_path, frames, fps=8)
|
| 47 |
+
return None, temp_path
|
| 48 |
+
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=interpolate,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Image(type="pil", label="Initial Image (frame1)"),
|
| 53 |
+
gr.Image(type="pil", label="Final Image (frame3)"),
|
| 54 |
+
gr.Slider(0.0, 1.0, step=0.05, value=0.5, label="Tau Value (only if Num Samples = 1)"),
|
| 55 |
+
gr.Slider(1, 15, step=1, value=1, label="Number of Samples"),
|
| 56 |
+
],
|
| 57 |
+
outputs=[
|
| 58 |
+
gr.Image(label="Interpolated Image (if num_samples = 1)"),
|
| 59 |
+
gr.Video(label="Interpolation in video (if num_samples > 1)"),
|
| 60 |
+
],
|
| 61 |
+
title="Multi-Input ResShift Diffusion VFI",
|
| 62 |
+
description=(
|
| 63 |
+
"📄 [arXiv Paper](https://arxiv.org/pdf/2504.05402) • "
|
| 64 |
+
"🤗 [Model](https://huggingface.co/vfontech/Multiple-Input-Resshift-VFI) • "
|
| 65 |
+
"🧪 [Colab](https://colab.research.google.com/drive/1MGYycbNMW6Mxu5MUqw_RW_xxiVeHK5Aa#scrollTo=EKaYCioiP3tQ) • "
|
| 66 |
+
"🌐 [GitHub](https://github.com/VicFonch/Multi-Input-Resshift-Diffusion-VFI)\n\n"
|
| 67 |
+
"Video interpolation using Conditional Residual Diffusion.\n"
|
| 68 |
+
"- All images are resized to 256x448.\n"
|
| 69 |
+
"- If `Number of Samples` = 1, generates only one intermediate image with the given Tau value.\n"
|
| 70 |
+
"- If `Number of Samples` > 1, ignores Tau and generates a sequence of interpolated images."
|
| 71 |
+
),
|
| 72 |
+
examples=[
|
| 73 |
+
["_data/example_images/frame1.png", "_data/example_images/frame3.png", 0.5],
|
| 74 |
+
],
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
demo.queue(max_size=12)
|
| 79 |
demo.launch(max_threads=1)
|