vfontech commited on
Commit
0887e6b
·
verified ·
1 Parent(s): b9664b6

app.py: defining the correct device

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