jsu27 commited on
Commit
a6049d1
·
1 Parent(s): 48db291

added upsampling

Browse files
Files changed (3) hide show
  1. app.py +11 -6
  2. requirements.txt +3 -0
  3. upsampling.py +43 -0
app.py CHANGED
@@ -3,12 +3,14 @@ import numpy as np
3
  import torch as th
4
  from imageio import imread
5
  from skimage.transform import resize as imresize
 
6
 
7
- from ema_pytorch import EMA
8
  from decomp_diffusion.model_and_diffusion_util import *
9
  from decomp_diffusion.diffusion.respace import SpacedDiffusion
10
  from decomp_diffusion.gen_image import *
 
11
  from download import download_model
 
12
 
13
  import gradio as gr
14
 
@@ -149,17 +151,19 @@ def combine_components_slice(model, gd, im1, im2, indices=None, sample_method='d
149
  return sample[0].cpu()
150
 
151
 
152
-
153
  def decompose_image_demo(im, model):
154
  sample_method = 'ddim'
155
  result = gen_image_and_components(MODELS[model], GD[sample_method], im, sample_method=sample_method, num_images=1, device=device)
156
- return result.permute(1, 2, 0).numpy()
 
157
 
158
 
159
  def combine_images_demo(im1, im2, model):
160
  sample_method = 'ddim'
161
  result = combine_components_slice(MODELS[model], GD[sample_method], im1, im2, indices='1,0,1,0', sample_method=sample_method, num_images=1, device=device)
162
- return result.permute(1, 2, 0).numpy()
 
 
163
 
164
 
165
  def load_model(dataset, extra_kwargs={}, device='cuda'):
@@ -189,6 +193,7 @@ MODELS = {
189
  'CelebA-HQ': celeb_model
190
  }
191
 
 
192
 
193
  with gr.Blocks() as demo:
194
  gr.Markdown(
@@ -221,7 +226,7 @@ with gr.Blocks() as demo:
221
  )
222
 
223
  with gr.Column():
224
- decomp_output = gr.Image(type='numpy')
225
  decomp_button = gr.Button("Generate")
226
 
227
 
@@ -253,7 +258,7 @@ with gr.Blocks() as demo:
253
 
254
 
255
  with gr.Column(scale=1):
256
- comb_output = gr.Image(type='numpy')
257
  comb_button = gr.Button("Generate")
258
 
259
 
 
3
  import torch as th
4
  from imageio import imread
5
  from skimage.transform import resize as imresize
6
+ from PIL import Image
7
 
 
8
  from decomp_diffusion.model_and_diffusion_util import *
9
  from decomp_diffusion.diffusion.respace import SpacedDiffusion
10
  from decomp_diffusion.gen_image import *
11
+
12
  from download import download_model
13
+ from upsampling import get_pipeline, upscale_image
14
 
15
  import gradio as gr
16
 
 
151
  return sample[0].cpu()
152
 
153
 
 
154
  def decompose_image_demo(im, model):
155
  sample_method = 'ddim'
156
  result = gen_image_and_components(MODELS[model], GD[sample_method], im, sample_method=sample_method, num_images=1, device=device)
157
+ result = Image.fromarray(result.permute(1, 2, 0).numpy())
158
+ return result
159
 
160
 
161
  def combine_images_demo(im1, im2, model):
162
  sample_method = 'ddim'
163
  result = combine_components_slice(MODELS[model], GD[sample_method], im1, im2, indices='1,0,1,0', sample_method=sample_method, num_images=1, device=device)
164
+ result = Image.fromarray(result.permute(1, 2, 0).numpy())
165
+ if model == 'CelebA-HQ':
166
+ return upscale_image(result, pipe)
167
 
168
 
169
  def load_model(dataset, extra_kwargs={}, device='cuda'):
 
193
  'CelebA-HQ': celeb_model
194
  }
195
 
196
+ pipe = get_pipeline()
197
 
198
  with gr.Blocks() as demo:
199
  gr.Markdown(
 
226
  )
227
 
228
  with gr.Column():
229
+ decomp_output = gr.Image(type='pil')
230
  decomp_button = gr.Button("Generate")
231
 
232
 
 
258
 
259
 
260
  with gr.Column(scale=1):
261
+ comb_output = gr.Image(type='pil')
262
  comb_button = gr.Button("Generate")
263
 
264
 
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
  git+https://github.com/jsu27/decomp_diffusion.git
2
  scikit-image==0.19.2
 
 
 
 
1
  git+https://github.com/jsu27/decomp_diffusion.git
2
  scikit-image==0.19.2
3
+ diffusers==0.10.2
4
+ transformers
5
+ pillow
upsampling.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch as th
2
+ from diffusers import IFImg2ImgSuperResolutionPipeline
3
+ from transformers import T5EncoderModel
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ def get_pipeline():
8
+
9
+ text_encoder = T5EncoderModel.from_pretrained(
10
+ "DeepFloyd/IF-I-XL-v1.0",
11
+ subfolder="text_encoder",
12
+ device_map="auto",
13
+ load_in_8bit=True,
14
+ variant="8bit"
15
+ )
16
+
17
+ pipe = IFImg2ImgSuperResolutionPipeline.from_pretrained(
18
+ "DeepFloyd/IF-II-L-v1.0",
19
+ text_encoder=text_encoder,
20
+ variant="fp16",
21
+ torch_dtype=th.float16,
22
+ device_map="auto",
23
+ watermarker=None
24
+ )
25
+ return pipe
26
+
27
+
28
+
29
+ def upscale_image(im, pipe):
30
+ """im is 64x64 PIL image"""
31
+ prompt = ''
32
+ prompt_embeds, negative_embeds = pipe.encode_prompt(prompt)
33
+ generator = th.Generator().manual_seed(0)
34
+
35
+ image = pipe(
36
+ image=original_image,
37
+ original_image=original_image,
38
+ prompt_embeds=prompt_embeds,
39
+ negative_prompt_embeds=negative_embeds,
40
+ generator=generator,
41
+ ).images[0]
42
+
43
+ return image