nielsgl commited on
Commit
01b6b8d
·
1 Parent(s): 4ac4ef6

update app

Browse files
Files changed (1) hide show
  1. app.py +132 -21
app.py CHANGED
@@ -17,27 +17,30 @@ resolution = 512
17
  # sd_dreambooth_model._diffusion_model = db_diffusion_model
18
 
19
  # checkpoint of the converted Stable Diffusion from KerasCV
20
- model_ckpt = "nielsgl/dreambooth-keras-pug-ace-sd2.1"
21
- pipeline = StableDiffusionPipeline.from_pretrained(model_ckpt)
22
- pipeline.to("cuda")
23
 
24
- unique_id = "puggieace"
25
- class_label = "dog"
26
- prompt = f"A photo of {unique_id} {class_label} in a bucket"
27
  # image = pipeline(prompt, num_inference_steps=50).images[0]
28
 
29
  # generate images
30
- def infer(prompt, negative_prompt, guidance_scale=10, num_inference_steps=50):
31
- neg = negative_prompt if negative_prompt else None
32
- imgs = []
33
- while len(imgs) < 3:
34
- next_prompt = pipeline(prompt, negative_prompt=neg, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, num_images_per_prompt=2)
35
- if not next_prompt.nsfw_content_detected[0]:
36
- imgs.append(next_prompt.images[0])
 
 
 
37
 
38
- return imgs
39
 
40
- output = gr.Gallery(label="Outputs").style(grid=(1,2))
41
 
42
  # customize interface
43
  title = "KerasCV Stable Diffusion Demo on images of Ace."
@@ -49,9 +52,117 @@ examples=[
49
  ["Photo of cute puggieace dog as an astronaut, space and planet in background, ultra realistic, concept art, intricate details, highly detailed, photorealistic, octane render, 8 k, unreal engine. trending on artstation", "", 15, 75],
50
  ["Photo of cute puggieace dog as super hero, futuristic city in background, cinematic light, high dynamic range, insane intricate details, stunning cinema effects, aesthetic, masterpiece, trending on artstation, cartoon art", "", 12, 75],
51
  ]
52
- gr.Interface(infer, inputs=[gr.Textbox(label="Positive Prompt", value="a photo of puggieace dog getting a haircut"),
53
- gr.Textbox(label="Negative Prompt", value="bad anatomy, blurry"),
54
- # gr.Slider(label='Number of gen image', minimum=1, maximum=4, value=2, step=1),
55
- gr.Number(label='Guidance scale', value=12),
56
- gr.Slider(label="Inference Steps",value=50),
57
- ], outputs=[output], title=title, description=description, examples=examples).queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # sd_dreambooth_model._diffusion_model = db_diffusion_model
18
 
19
  # checkpoint of the converted Stable Diffusion from KerasCV
20
+ # model_ckpt = "nielsgl/dreambooth-keras-pug-ace-sd2.1"
21
+ # pipeline = StableDiffusionPipeline.from_pretrained(model_ckpt)
22
+ # pipeline.to("cuda")
23
 
24
+ # unique_id = "puggieace"
25
+ # class_label = "dog"
26
+ # prompt = f"A photo of {unique_id} {class_label} in a bucket"
27
  # image = pipeline(prompt, num_inference_steps=50).images[0]
28
 
29
  # generate images
30
+ # def infer(prompt, negative_prompt, guidance_scale=10, num_inference_steps=50):
31
+ # neg = negative_prompt if negative_prompt else None
32
+ # imgs = []
33
+ # while len(imgs) != 2:
34
+ # next_prompt = pipeline(prompt, negative_prompt=neg, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, num_images_per_prompt=5)
35
+ # for img, is_neg in zip(next_prompt.images, next_prompt.nsfw_content_detected):
36
+ # if not is_neg:
37
+ # imgs.append(img)
38
+ # if len(imgs) == 2:
39
+ # break
40
 
41
+ # return imgs
42
 
43
+ # output = gr.Gallery(label="Outputs").style(grid=(1,2))
44
 
45
  # customize interface
46
  title = "KerasCV Stable Diffusion Demo on images of Ace."
 
52
  ["Photo of cute puggieace dog as an astronaut, space and planet in background, ultra realistic, concept art, intricate details, highly detailed, photorealistic, octane render, 8 k, unreal engine. trending on artstation", "", 15, 75],
53
  ["Photo of cute puggieace dog as super hero, futuristic city in background, cinematic light, high dynamic range, insane intricate details, stunning cinema effects, aesthetic, masterpiece, trending on artstation, cartoon art", "", 12, 75],
54
  ]
55
+
56
+ card_interface = gr.Blocks(gr.Markdown(
57
+ """
58
+ # KerasCV Stable Diffusion in Diffusers 🧨🤗
59
+
60
+ DreamBooth model for the `puggieace` concept trained by nielsgl on the `nielsgl/dreambooth-ace` dataset.
61
+ It can be used by modifying the `instance_prompt`: **a photo of puggieace**.
62
+
63
+ The examples are from 2 different Keras CV models (`StableDiffusion` and `StableDiffusionV2`, corresponding to Stable Diffusion V1.4 and V2.1, respectively) trained on the same dataset (`nielsgl/dreambooth-ace`).
64
+
65
+ ## Description
66
+
67
+ The Stable Diffusion V2 pipeline contained in the corresponding repository (`nielsgl/dreambooth-keras-pug-ace-sd2.1`) was created using a modified version of [this Space](https://huggingface.co/spaces/sayakpaul/convert-kerascv-sd-diffusers) for StableDiffusionV2 from KerasCV. The purpose is to convert the KerasCV Stable Diffusion weights in a way that is compatible with [Diffusers](https://github.com/huggingface/diffusers). This allows users to fine-tune using KerasCV and use the fine-tuned weights in Diffusers taking advantage of its nifty features (like [schedulers](https://huggingface.co/docs/diffusers/main/en/using-diffusers/schedulers), [fast attention](https://huggingface.co/docs/diffusers/optimization/fp16), etc.).
68
+ This model was created as part of the Keras DreamBooth Sprint 🔥. Visit the [organisation page](https://huggingface.co/keras-dreambooth) for instructions on how to take part!
69
+
70
+ ## Examples
71
+
72
+ ### Stable Diffusion V1.4
73
+
74
+ > Portrait of puggieace dog as a Roman Emperor, city in background
75
+
76
+ ![Portrait of puggieace dog as a Roman Emperor, city in background, ultra realistic, intricate details, eerie, highly detailed, photorealistic, octane render, 8 k, unreal engine. art by artgerm and greg rutkowski and charlie bowater and magali villeneuve and alphonse mucha](examples/emperor-1.4.jpeg)
77
+
78
+ > Photo of puggieace dog wearing sunglasses on the beach, sunset in background, golden hour
79
+
80
+ ![Photo of puggieace dog wearing sunglasses on the beach, sunset in background, golden hour](examples/beach-1.4.jpg)
81
+
82
+ > Photo of cute puggieace dog as an astronaut, planet and spaceship in background
83
+
84
+ ![Photo of cute puggieace dog as an astronaut, planet and spaceship in background, ultra realistic, intricate details, highly detailed, photorealistic, octane render, 8 k, unreal engine. trending on artstation](examples/astronaut-1.4.jpg)
85
+
86
+ ### Stable Diffusion V2.1
87
+
88
+ > Portrait painting of a cute puggieace dog as a samurai
89
+
90
+ ![Portrait painting of a cute puggieace dog as a samurai, ultra realistic, concept art, intricate details, eerie, highly detailed, photorealistic, octane render, 8 k, unreal engine. art by artgerm and greg rutkowski and charlie bowater and magali villeneuve and alphonse mucha](examples/samurai-2.1.jpg)
91
+
92
+ > Photo of cute puggieace dog as an astronaut, space and planet in background
93
+
94
+ ![Photo of cute puggieace dog as an astronaut, space and planet in background, ultra realistic, concept art, intricate details, highly detailed, photorealistic, octane render, 8 k, unreal engine. art by artgerm and greg rutkowski and charlie bowater, trending on artstation](examples/astronaut-2.1.jpg)
95
+
96
+ > A photo of a cute puggieace dog getting a haircut in a barbershop
97
+
98
+ ![A photo of a cute puggieace dog getting a haircut in a barbershop, ultra realistic, intricate details, highly detailed, photorealistic, octane render, 8 k, unreal engine. art by artgerm and greg rutkowski and charlie bowater and magali villeneuve and alphonse mucha](examples/haircut-2.1.jpg)
99
+
100
+ > Portrait photo of puggieace dog in New York
101
+
102
+ ![Portrait photo of puggieace dog in New York, city and skyscrapers in background, highly detailed, photorealistic, hdr, 4k](examples/ny-2.1.jpg)
103
+
104
+ > Portrait of puggieace dog as a Roman Emperor, city in background
105
+
106
+ ![Portrait of puggieace dog as a Roman Emperor, city in background, ultra realistic, intricate details, eerie, highly detailed, photorealistic, octane render, 8 k, unreal engine. art by artgerm and greg rutkowski and charlie bowater and magali villeneuve and alphonse mucha](examples/emperor-2.1.jpg)
107
+
108
+
109
+ ## Usage with Stable Diffusion V1.4
110
+
111
+ ```python
112
+ from huggingface_hub import from_pretrained_keras
113
+ import keras_cv
114
+ import matplotlib.pyplot as plt
115
+
116
+
117
+ model = keras_cv.models.StableDiffusion(img_width=512, img_height=512, jit_compile=True)
118
+ model._diffusion_model = from_pretrained_keras("nielsgl/dreambooth-pug-ace")
119
+ model._text_encoder = from_pretrained_keras("nielsgl/dreambooth-pug-ace-text-encoder")
120
+
121
+ images = model.text_to_image("a photo of puggieace dog on the beach", batch_size=3)
122
+ plt.imshow(image[0])
123
+ ```
124
+
125
+ ## Usage with Stable Diffusion V2.1
126
+
127
+ ```python
128
+ from diffusers import StableDiffusionPipeline
129
+
130
+ pipeline = StableDiffusionPipeline.from_pretrained('nielsgl/dreambooth-keras-pug-ace-sd2.1')
131
+ image = pipeline().images[0]
132
+ image
133
+ ```
134
+
135
+ ### Training hyperparameters
136
+
137
+ The following hyperparameters were used during training for Stable Diffusion v1.4:
138
+
139
+ | Hyperparameters | Value |
140
+ | :-- | :-- |
141
+ | name | RMSprop |
142
+ | weight_decay | None |
143
+ | clipnorm | None |
144
+ | global_clipnorm | None |
145
+ | clipvalue | None |
146
+ | use_ema | False |
147
+ | ema_momentum | 0.99 |
148
+ | ema_overwrite_frequency | 100 |
149
+ | jit_compile | True |
150
+ | is_legacy_optimizer | False |
151
+ | learning_rate | 0.0010000000474974513 |
152
+ | rho | 0.9 |
153
+ | momentum | 0.0 |
154
+ | epsilon | 1e-07 |
155
+ | centered | False |
156
+ | training_precision | float32 |
157
+ """
158
+ ))
159
+
160
+ # demo_interface = gr.Interface(infer, inputs=[gr.Textbox(label="Positive Prompt", value="a photo of puggieace dog getting a haircut"),
161
+ # gr.Textbox(label="Negative Prompt", value="bad anatomy, blurry"),
162
+ # # gr.Slider(label='Number of gen image', minimum=1, maximum=4, value=2, step=1),
163
+ # gr.Number(label='Guidance scale', value=12),
164
+ # gr.Slider(label="Inference Steps",value=50),
165
+ # ], outputs=[output], title=title, description=description, examples=examples).queue()
166
+
167
+
168
+ gr.TabbedInterface([card_interface], ["Model Card"]).launch()