Spaces:
Runtime error
Runtime error
Commit
·
9633e6a
1
Parent(s):
d6b8beb
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ image_pipe = DDPMPipeline.from_pretrained(pipeline_name).to(device)
|
|
| 14 |
|
| 15 |
# Set up the scheduler
|
| 16 |
scheduler = DDIMScheduler.from_pretrained(pipeline_name)
|
| 17 |
-
scheduler.set_timesteps(num_inference_steps=
|
| 18 |
|
| 19 |
def color_loss(images, target_color=(0.1, 0.9, 0.5)):
|
| 20 |
"""Given a target color (R, G, B) return a loss for how far away on average
|
|
@@ -26,49 +26,24 @@ def color_loss(images, target_color=(0.1, 0.9, 0.5)):
|
|
| 26 |
|
| 27 |
|
| 28 |
def generate(color, guidance_loss_scale):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
noise_pred = image_pipe.unet(model_input, t)["sample"]
|
| 48 |
-
|
| 49 |
-
# Set requires grad on x (shortcut method - we're doing this AFTER the unet)
|
| 50 |
-
x = x.detach().requires_grad_()
|
| 51 |
-
|
| 52 |
-
# Get the predicted x0:
|
| 53 |
-
x0 = scheduler.step(noise_pred, t, x).pred_original_sample
|
| 54 |
-
|
| 55 |
-
# Calculate loss
|
| 56 |
-
loss = color_loss(x0, target_color) * guidance_loss_scale
|
| 57 |
-
|
| 58 |
-
# Get gradient
|
| 59 |
-
cond_grad = -torch.autograd.grad(loss, x)[0]
|
| 60 |
-
|
| 61 |
-
# Modify x based on this gradient
|
| 62 |
-
x = x.detach() + cond_grad
|
| 63 |
-
|
| 64 |
-
# Now step with scheduler
|
| 65 |
-
x = scheduler.step(noise_pred, t, x).prev_sample
|
| 66 |
-
|
| 67 |
-
# Return the final output as an image (or image grid if there are more than one images)
|
| 68 |
-
grid = torchvision.utils.make_grid(x, nrow=4)
|
| 69 |
-
im = grid.permute(1, 2, 0).cpu().clip(-1, 1)*0.5 + 0.5
|
| 70 |
-
return Image.fromarray(np.array(im*255).astype(np.uint8))
|
| 71 |
-
|
| 72 |
|
| 73 |
inputs = [
|
| 74 |
gr.ColorPicker(label="color", value='55FFAA'), # Add any inputs you need here
|
|
@@ -83,6 +58,7 @@ demo = gr.Interface(
|
|
| 83 |
examples=[
|
| 84 |
["#BB2266"],["#44CCAA"] # You can provide some example inputs to get people started
|
| 85 |
],
|
|
|
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
-
demo.launch()
|
|
|
|
| 14 |
|
| 15 |
# Set up the scheduler
|
| 16 |
scheduler = DDIMScheduler.from_pretrained(pipeline_name)
|
| 17 |
+
scheduler.set_timesteps(num_inference_steps=20)
|
| 18 |
|
| 19 |
def color_loss(images, target_color=(0.1, 0.9, 0.5)):
|
| 20 |
"""Given a target color (R, G, B) return a loss for how far away on average
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def generate(color, guidance_loss_scale):
|
| 29 |
+
target_color = ImageColor.getcolor(color, "RGB") # Target color as RGB
|
| 30 |
+
target_color = [a/255 for a in target_color] # Rescale from (0, 255) to (0, 1)
|
| 31 |
+
x = torch.randn(1, 3, 256, 256).to(device)
|
| 32 |
+
for i, t in tqdm(enumerate(scheduler.timesteps)):
|
| 33 |
+
model_input = scheduler.scale_model_input(x, t)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
noise_pred = image_pipe.unet(model_input, t)["sample"]
|
| 36 |
+
x = x.detach().requires_grad_()
|
| 37 |
+
x0 = scheduler.step(noise_pred, t, x).pred_original_sample
|
| 38 |
+
loss = color_loss(x0, target_color) * guidance_loss_scale
|
| 39 |
+
cond_grad = -torch.autograd.grad(loss, x)[0]
|
| 40 |
+
x = x.detach() + cond_grad
|
| 41 |
+
x = scheduler.step(noise_pred, t, x).prev_sample
|
| 42 |
+
grid = torchvision.utils.make_grid(x, nrow=4)
|
| 43 |
+
im = grid.permute(1, 2, 0).cpu().clip(-1, 1)*0.5 + 0.5
|
| 44 |
+
im = Image.fromarray(np.array(im*255).astype(np.uint8))
|
| 45 |
+
im.save('test.jpeg')
|
| 46 |
+
return im
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
inputs = [
|
| 49 |
gr.ColorPicker(label="color", value='55FFAA'), # Add any inputs you need here
|
|
|
|
| 58 |
examples=[
|
| 59 |
["#BB2266"],["#44CCAA"] # You can provide some example inputs to get people started
|
| 60 |
],
|
| 61 |
+
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch(enable_queue=True)
|