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|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import matplotlib
|
| 6 |
+
matplotlib.use('Agg')
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
class SentinelNoiseSchedule:
|
| 10 |
+
def __init__(self, timesteps=1000, z=2.0):
|
| 11 |
+
self.timesteps = timesteps
|
| 12 |
+
self.z = z
|
| 13 |
+
self.betas = self._sentinel_schedule()
|
| 14 |
+
self.alphas = 1.0 - self.betas
|
| 15 |
+
self.alpha_bars = torch.cumprod(self.alphas, dim=0)
|
| 16 |
+
|
| 17 |
+
def _sentinel_schedule(self):
|
| 18 |
+
n = torch.arange(1, self.timesteps + 1, dtype=torch.float64)
|
| 19 |
+
t_norm = n / self.timesteps
|
| 20 |
+
beta = torch.zeros_like(n)
|
| 21 |
+
for i in range(self.timesteps):
|
| 22 |
+
t = t_norm[i].item()
|
| 23 |
+
if t < 0.5:
|
| 24 |
+
beta[i] = 0.0001 + 0.01 * (2 * t) ** (1 / (2 * t + 0.01))
|
| 25 |
+
else:
|
| 26 |
+
beta[i] = 0.01 + 0.02 * ((2 * t - 1) ** (2 * t - 1))
|
| 27 |
+
return torch.clamp(beta, 0.0001, 0.999).float()
|
| 28 |
+
|
| 29 |
+
def add_noise(self, x, t):
|
| 30 |
+
sqrt_alpha_bar = torch.sqrt(self.alpha_bars[t])
|
| 31 |
+
sqrt_one_minus = torch.sqrt(1.0 - self.alpha_bars[t])
|
| 32 |
+
noise = torch.randn_like(x)
|
| 33 |
+
return sqrt_alpha_bar.view(-1,1,1,1) * x + sqrt_one_minus.view(-1,1,1,1) * noise, noise
|
| 34 |
+
|
| 35 |
+
def visualize_schedule(timesteps, z):
|
| 36 |
+
"""Visualize Sentinel noise schedule."""
|
| 37 |
+
schedule = SentinelNoiseSchedule(timesteps, z)
|
| 38 |
+
|
| 39 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
|
| 40 |
+
|
| 41 |
+
t = np.arange(timesteps)
|
| 42 |
+
axes[0].plot(t, schedule.betas.numpy(), linewidth=2, color='purple')
|
| 43 |
+
axes[0].set_title('Sentinel β Schedule (Super-Exponential)')
|
| 44 |
+
axes[0].set_xlabel('Timestep')
|
| 45 |
+
axes[0].set_ylabel('β')
|
| 46 |
+
axes[0].grid(True, alpha=0.3)
|
| 47 |
+
|
| 48 |
+
axes[1].plot(t, schedule.alpha_bars.numpy(), linewidth=2, color='blue')
|
| 49 |
+
axes[1].set_title('ᾱ (Cumulative Product)')
|
| 50 |
+
axes[1].set_xlabel('Timestep')
|
| 51 |
+
axes[1].set_ylabel('ᾱ')
|
| 52 |
+
axes[1].grid(True, alpha=0.3)
|
| 53 |
+
|
| 54 |
+
# Compare with cosine schedule
|
| 55 |
+
cos_betas = np.cos(np.linspace(0, np.pi/2, timesteps)) ** 2 * 0.02
|
| 56 |
+
axes[2].plot(t, schedule.betas.numpy(), label='Sentinel', linewidth=2, color='purple')
|
| 57 |
+
axes[2].plot(t, cos_betas, label='Cosine', linewidth=2, color='orange', linestyle='--')
|
| 58 |
+
axes[2].set_title('Schedule Comparison')
|
| 59 |
+
axes[2].set_xlabel('Timestep')
|
| 60 |
+
axes[2].set_ylabel('β')
|
| 61 |
+
axes[2].legend()
|
| 62 |
+
axes[2].grid(True, alpha=0.3)
|
| 63 |
+
|
| 64 |
+
plt.tight_layout()
|
| 65 |
+
plt.savefig('/tmp/diffusion_sched.png', dpi=150)
|
| 66 |
+
plt.close()
|
| 67 |
+
|
| 68 |
+
return '/tmp/diffusion_sched.png'
|
| 69 |
+
|
| 70 |
+
def add_noise_demo(image_size, timesteps, step, z):
|
| 71 |
+
"""Demo noise addition on synthetic image."""
|
| 72 |
+
schedule = SentinelNoiseSchedule(timesteps, z)
|
| 73 |
+
|
| 74 |
+
# Create synthetic image (colored pattern)
|
| 75 |
+
img = torch.zeros(1, 3, image_size, image_size)
|
| 76 |
+
for c in range(3):
|
| 77 |
+
for i in range(image_size):
|
| 78 |
+
for j in range(image_size):
|
| 79 |
+
img[0, c, i, j] = np.sin(i * 0.3 + c) * np.cos(j * 0.3 + c) * 0.5 + 0.5
|
| 80 |
+
|
| 81 |
+
t = torch.tensor([step])
|
| 82 |
+
noisy_img, noise = schedule.add_noise(img, t)
|
| 83 |
+
|
| 84 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
| 85 |
+
|
| 86 |
+
def show_tensor(ax, tensor, title):
|
| 87 |
+
arr = tensor[0].permute(1, 2, 0).numpy()
|
| 88 |
+
arr = np.clip(arr, 0, 1)
|
| 89 |
+
ax.imshow(arr)
|
| 90 |
+
ax.set_title(title)
|
| 91 |
+
ax.axis('off')
|
| 92 |
+
|
| 93 |
+
show_tensor(axes[0], img, 'Original Image')
|
| 94 |
+
show_tensor(axes[1], noisy_img, f'Noisy (t={step}, β={schedule.betas[step]:.4f})')
|
| 95 |
+
show_tensor(axes[2], noise * 0.3 + 0.5, 'Noise (scaled)')
|
| 96 |
+
|
| 97 |
+
plt.tight_layout()
|
| 98 |
+
plt.savefig('/tmp/diffusion_noise.png', dpi=150)
|
| 99 |
+
plt.close()
|
| 100 |
+
|
| 101 |
+
info = f"""
|
| 102 |
+
## Sentinel Diffusion Noise Addition
|
| 103 |
+
|
| 104 |
+
| Property | Value |
|
| 105 |
+
|----------|-------|
|
| 106 |
+
| Timestep | {step}/{timesteps} |
|
| 107 |
+
| β (noise level) | {schedule.betas[step]:.6f} |
|
| 108 |
+
| ᾱ (signal retained) | {schedule.alpha_bars[step]:.6f} |
|
| 109 |
+
| Schedule type | **Super-exponential** |
|
| 110 |
+
|
| 111 |
+
### Key Innovation
|
| 112 |
+
Sentinel noise schedule uses **super-exponential growth** of β:
|
| 113 |
+
- Early steps: small noise (preserve structure)
|
| 114 |
+
- Late steps: rapid increase (destroy structure)
|
| 115 |
+
- Sharper transitions than cosine/linear schedules
|
| 116 |
+
"""
|
| 117 |
+
return '/tmp/diffusion_noise.png', info
|
| 118 |
+
|
| 119 |
+
with gr.Blocks(title="Sentinel Diffusion Model") as demo:
|
| 120 |
+
gr.Markdown("""
|
| 121 |
+
# 🎨 Sentinel Diffusion Model
|
| 122 |
+
|
| 123 |
+
**Super-exponential noise schedule for sharper transitions.**
|
| 124 |
+
|
| 125 |
+
The Sentinel partition function F(z) = Σ zⁿ/nⁿ inspires a noise schedule
|
| 126 |
+
with super-exponential β growth — potentially requiring fewer steps.
|
| 127 |
+
""")
|
| 128 |
+
|
| 129 |
+
with gr.Tab("Noise Schedule"):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
ts_sched = gr.Slider(100, 2000, value=1000, step=100, label="Timesteps")
|
| 132 |
+
z_sched = gr.Slider(0.5, 5.0, value=2.0, label="z Parameter")
|
| 133 |
+
btn_sched = gr.Button("Visualize Schedule", variant="primary")
|
| 134 |
+
img_sched = gr.Image()
|
| 135 |
+
btn_sched.click(visualize_schedule, [ts_sched, z_sched], img_sched)
|
| 136 |
+
|
| 137 |
+
with gr.Tab("Noise Addition Demo"):
|
| 138 |
+
with gr.Row():
|
| 139 |
+
img_size = gr.Slider(16, 128, value=64, step=16, label="Image Size")
|
| 140 |
+
ts_noise = gr.Slider(100, 2000, value=1000, step=100, label="Total Timesteps")
|
| 141 |
+
step_noise = gr.Slider(0, 999, value=500, label="Current Step")
|
| 142 |
+
z_noise = gr.Slider(0.5, 5.0, value=2.0, label="z Parameter")
|
| 143 |
+
btn_noise = gr.Button("Add Noise", variant="primary")
|
| 144 |
+
img_noise = gr.Image()
|
| 145 |
+
info_noise = gr.Markdown()
|
| 146 |
+
btn_noise.click(add_noise_demo, [img_size, ts_noise, step_noise, z_noise], [img_noise, info_noise])
|
| 147 |
+
|
| 148 |
+
gr.Markdown("""
|
| 149 |
+
## About Sentinel Diffusion
|
| 150 |
+
|
| 151 |
+
- **Noise schedule**: Super-exponential β growth (from partition function)
|
| 152 |
+
- **Transition**: Sharper than cosine/linear (phase-like)
|
| 153 |
+
- **Structure preservation**: Strong early, weak late
|
| 154 |
+
- **Potential**: Fewer diffusion steps needed
|
| 155 |
+
|
| 156 |
+
[Model Repo](https://huggingface.co/5dimension/sentinel-diffusion)
|
| 157 |
+
""")
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|