Prgckwb commited on
Commit
f3b72f8
1 Parent(s): 1244abc

Upload 7 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ assets/sea.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Noisescope
3
- emoji: 🦀
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
 
1
  ---
2
  title: Noisescope
3
+ emoji: 😎
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ from pathlib import Path
3
+
4
+ import cv2
5
+ import gradio as gr
6
+ import numpy as np
7
+ import torch
8
+ from PIL import Image, ImageDraw, ImageFont
9
+ from diffusers.schedulers import (
10
+ DDIMScheduler, DDPMScheduler,
11
+ PNDMScheduler
12
+ )
13
+ from gradio.themes import colors
14
+ from torchvision.transforms.functional import to_pil_image, to_tensor
15
+ from tqdm import tqdm
16
+
17
+ SCHEDULER_CLASSES = {
18
+ "PNDMScheduler": PNDMScheduler,
19
+ "DDIMScheduler": DDIMScheduler,
20
+ "DDPMScheduler": DDPMScheduler,
21
+ }
22
+
23
+
24
+ @torch.inference_mode()
25
+ def inference(
26
+ image: Image.Image,
27
+ scheduler_name: str,
28
+ num_timestep: int,
29
+ fps: int = 5,
30
+ seed: int = 1117,
31
+ progress=gr.Progress()
32
+ ):
33
+ progress(0, "Starting the process...")
34
+
35
+ # Set the output directory
36
+ out_dir = Path('out')
37
+ if out_dir.exists():
38
+ # Clean the directory
39
+ for file in out_dir.iterdir():
40
+ file.unlink()
41
+ else:
42
+ out_dir.mkdir()
43
+
44
+ model_id = 'CompVis/stable-diffusion-v1-4'
45
+
46
+ scheduler = SCHEDULER_CLASSES[scheduler_name].from_pretrained(model_id, subfolder='scheduler')
47
+ scheduler.set_timesteps(num_timestep)
48
+ timesteps = reversed(scheduler.timesteps)
49
+
50
+ # Resize image
51
+ img_width, img_height = image.size
52
+ image_tensor = to_tensor(image)
53
+
54
+ # Font info
55
+ # Adjust font size based on the image size
56
+ font_size = int(img_width / 30)
57
+ font = ImageFont.truetype("assets/JetBrainsMono-Bold.ttf", font_size)
58
+
59
+ # Prepare and Add noise
60
+ generator = torch.Generator().manual_seed(seed)
61
+ noise = torch.randn(image_tensor.shape, generator=generator)
62
+
63
+ # Define the codec and create VideoWriter object
64
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
65
+ # fps = 5
66
+
67
+ # Create an in-memory buffer
68
+ out_path = f'{str(out_dir)}/{uuid.uuid4()}.mp4'
69
+ out = cv2.VideoWriter(out_path, fourcc, fps, (img_width, img_height))
70
+
71
+ for i, timestep in enumerate(tqdm(timesteps, desc="Writing video frames")):
72
+ noised_image = scheduler.add_noise(image_tensor, noise, timestep)
73
+ noised_image = to_pil_image(noised_image)
74
+
75
+ # Add caption to the image
76
+ draw = ImageDraw.Draw(noised_image)
77
+ caption = f"Iteration: {i + 1}, Timestep: {timestep.item()}"
78
+
79
+ cap_x = img_width / 4
80
+ cap_y = 10 + (font_size * 2)
81
+
82
+ draw.text(xy=(cap_x, cap_y), text=caption, fill=(255, 255, 255), font=font)
83
+
84
+ noised_image = cv2.cvtColor(np.array(noised_image), cv2.COLOR_RGB2BGR)
85
+ out.write(noised_image) # Write out frame to video
86
+ progress(1 / num_timestep * (i + 1), f"Writing frame {i + 1} of {num_timestep}...")
87
+
88
+ out.release() # Release the video writer
89
+ progress(1, "Video writing completed.")
90
+ return out_path
91
+
92
+
93
+ if __name__ == '__main__':
94
+ iface = gr.Interface(
95
+ fn=inference,
96
+ inputs=[
97
+ gr.Image(type="pil", label="Input Image"),
98
+ gr.Dropdown(
99
+ choices=[name for name in SCHEDULER_CLASSES.keys()],
100
+ label="Scheduler",
101
+ value="PNDMScheduler",
102
+ ),
103
+ gr.Slider(minimum=10, maximum=990, value=50, step=10, label="Total Timesteps"),
104
+ gr.Slider(minimum=1, maximum=60, value=5, step=1, label="fps"),
105
+ gr.Number(value=1117, label="Seed"),
106
+ ],
107
+ outputs=[
108
+ gr.PlayableVideo(label="Process of adding noise")
109
+ ],
110
+ examples=[
111
+ [Image.open("assets/sea.jpg"), "PNDMScheduler", 999, 1117],
112
+ [Image.open("assets/ckwb.jpg"), "PNDMScheduler", 30, 1117],
113
+ [Image.open("assets/dog.jpg"), "PNDMScheduler", 50, 1117],
114
+ ],
115
+ cache_examples=True,
116
+ title="NoiseScope 😎",
117
+ description="Visualize what amount of noise is added at each time step in the forward process of the diffusion model 🧨",
118
+ theme=gr.Theme(primary_hue=colors.green, secondary_hue=colors.yellow),
119
+ allow_flagging="never",
120
+ )
121
+ iface.queue().launch()
assets/JetBrainsMono-Bold.ttf ADDED
Binary file (278 kB). View file
 
assets/ckwb.jpg ADDED
assets/dog.jpg ADDED
assets/sea.jpg ADDED

Git LFS Details

  • SHA256: f3a7bb1639130e5aa3f17b317adc77dcd8134bfd22aac9787d2873f1fb93163d
  • Pointer size: 132 Bytes
  • Size of remote file: 3.78 MB
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ diffusers
4
+ transformers
5
+ safetensors
6
+ accelerate
7
+ opencv-python