EeeeeeeH commited on
Commit
06769fe
1 Parent(s): 2dccad5
Files changed (1) hide show
  1. app.py +34 -25
app.py CHANGED
@@ -8,52 +8,61 @@ from diffusers import DDIMScheduler
8
 
9
  device = 'mps' if torch.backends.mps.is_available() else 'cuda' if torch.cuda.is_available() else 'cpu'
10
 
11
- pipeline_name = 'johnowhitaker/sd-class-wikiart-from-bedrooms'
 
12
  image_pipe = DDPMPipeline.from_pretrained(pipeline_name).to(device)
13
 
 
14
  scheduler = DDIMScheduler.from_pretrained(pipeline_name)
15
  scheduler.set_timesteps(num_inference_steps=20)
16
 
 
17
  def color_loss(images, target_color=(0.1, 0.9, 0.5)):
18
- target = torch.tensor(target_color).to(images.device) * 2 - 1
19
- target = target[None, :, None, None]
20
- error = torch.abs(images - target).mean()
 
 
21
  return error
22
 
 
23
  def generate(color, guidance_loss_scale):
24
- target_color = ImageColor.getcolor(color, "RGB")
25
- target_color = [a / 255 for a in target_color]
26
- x = torch.randn(1, 3, 256, 256).to(device)
27
- for i, t in enumerate(scheduler.timesteps):
28
- model_input = scheduler.scale_model_input(x, t)
29
- with torch.no_grad():
30
- noise_pred = image_pipe.unet(model_input, t)["sample"]
31
- x = x.detach().requires_grad_()
32
- x0 = scheduler.step(noise_pred, t, x).pred_original_sample
33
- loss = color_loss(x0, target_color) * guidance_loss_scale
34
- cond_grad = -torch.autograd.grad(loss, x)[0]
35
- x = x.detach() + cond_grad
36
- x = scheduler.step(noise_pred, t, x).prev_sample
37
- grid = torchvision.utils.make_grid(x, nrow=4)
38
- im = grid.permute(1, 2, 0).cpu().clip(-1, 1) * 0.5 + 0.5
39
- im = Image.fromarray(np.array(im * 255).astype(np.uint8))
40
- im.save("test.jpeg")
41
- return im
42
 
 
43
  inputs = [
44
- gr.ColorPicker(label="Color", value = '55FFAA'),
45
- gr.Slider(label="Guidance Loss Scale", minimum=0, maximum=30, value=1)
46
  ]
47
  outputs = gr.Image(label="result")
48
 
 
49
  demo = gr.Interface(
50
  fn=generate,
51
  inputs=inputs,
52
  outputs=outputs,
53
  examples=[
54
- ["#BB2266", 3],["#44CCAA", 5]
55
  ],
56
  )
57
 
 
58
  if __name__ == "__main__":
59
  demo.launch(enable_queue=True)
 
8
 
9
  device = 'mps' if torch.backends.mps.is_available() else 'cuda' if torch.cuda.is_available() else 'cpu'
10
 
11
+ # Load the pretrained pipeline
12
+ pipeline_name = 'EeeeeeeH/Trial2'
13
  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=20)
18
 
19
+ # The guidance function
20
  def color_loss(images, target_color=(0.1, 0.9, 0.5)):
21
+ """Given a target color (R, G, B) return a loss for how far away on average
22
+ the images' pixels are from that color. Defaults to a light teal: (0.1, 0.9, 0.5) """
23
+ target = torch.tensor(target_color).to(images.device) * 2 - 1 # Map target color to (-1, 1)
24
+ target = target[None, :, None, None] # Get shape right to work with the images (b, c, h, w)
25
+ error = torch.abs(images - target).mean() # Mean absolute difference between the image pixels and the target color
26
  return error
27
 
28
+ # And the core function to generate an image given the relevant inputs
29
  def generate(color, guidance_loss_scale):
30
+ target_color = ImageColor.getcolor(color, "RGB") # Target color as RGB
31
+ target_color = [a/255 for a in target_color] # Rescale from (0, 255) to (0, 1)
32
+ x = torch.randn(1, 3, 256, 256).to(device)
33
+ for i, t in enumerate(scheduler.timesteps):
34
+ model_input = scheduler.scale_model_input(x, t)
35
+ with torch.no_grad():
36
+ noise_pred = image_pipe.unet(model_input, t)["sample"]
37
+ x = x.detach().requires_grad_()
38
+ x0 = scheduler.step(noise_pred, t, x).pred_original_sample
39
+ loss = color_loss(x0, target_color) * guidance_loss_scale
40
+ cond_grad = -torch.autograd.grad(loss, x)[0]
41
+ x = x.detach() + cond_grad
42
+ x = scheduler.step(noise_pred, t, x).prev_sample
43
+ grid = torchvision.utils.make_grid(x, nrow=4)
44
+ im = grid.permute(1, 2, 0).cpu().clip(-1, 1)*0.5 + 0.5
45
+ im = Image.fromarray(np.array(im*255).astype(np.uint8))
46
+ im.save('test.jpeg')
47
+ return im
48
 
49
+ # See the gradio docs for the types of inputs and outputs available
50
  inputs = [
51
+ gr.ColorPicker(label="color", value='55FFAA'), # Add any inputs you need here
52
+ gr.Slider(label="guidance_scale", minimum=0, maximum=30, value=3)
53
  ]
54
  outputs = gr.Image(label="result")
55
 
56
+ # Setting up a minimal interface to our function:
57
  demo = gr.Interface(
58
  fn=generate,
59
  inputs=inputs,
60
  outputs=outputs,
61
  examples=[
62
+ ["#BB2266", 3],["#44CCAA", 5] # You can provide some example inputs to get people started
63
  ],
64
  )
65
 
66
+ # And launching
67
  if __name__ == "__main__":
68
  demo.launch(enable_queue=True)