nkanungo commited on
Commit
c46f708
·
1 Parent(s): 1d25ad8

Update Era_s20_updt.py

Browse files
Files changed (1) hide show
  1. Era_s20_updt.py +12 -14
Era_s20_updt.py CHANGED
@@ -1,6 +1,5 @@
1
- #!pip install -q --upgrade transformers==4.25.1 diffusers ftfy accelerate
2
- #import transformers as t
3
- #assert t.__version__=='4.25.1', "Transformers version should be as specified"
4
 
5
 
6
  import torch
@@ -25,7 +24,7 @@ import torch.nn.functional as F
25
 
26
  torch.manual_seed(1)
27
 
28
- #if not (Path.home()/'.cache/huggingface'/'token').exists(): notebook_login()
29
 
30
  # Supress some unnecessary warnings when loading the CLIPTextModel
31
  logging.set_verbosity_error()
@@ -88,7 +87,7 @@ vae = vae.to(torch_device)
88
  text_encoder = text_encoder.to(torch_device)
89
  unet = unet.to(torch_device);
90
 
91
- embeds_folder = Path('stable_diffusion_experiment/paintings_embed')
92
  file_names = [path.name for path in embeds_folder.glob('*') if path.is_file()]
93
  print(file_names)
94
 
@@ -193,7 +192,7 @@ def get_output_embeds(input_embeddings):
193
  def generate_with_embs_custom(text_embeddings,seed):
194
  height = 512 # default height of Stable Diffusion
195
  width = 512 # default width of Stable Diffusion
196
- num_inference_steps = 30 # Number of denoising steps
197
  guidance_scale = 7.5 # Scale for classifier-free guidance
198
  generator = torch.manual_seed(seed) # Seed generator to create the inital latent noise
199
  batch_size = 1
@@ -242,7 +241,7 @@ def generate_with_embs_custom(text_embeddings,seed):
242
  # ref_latent = pil_to_latent(ref_image)
243
 
244
  ## Guidance through Custom Loss Function
245
- def custom_loss(latent):
246
  error = F.mse_loss(0.5*latent,0.8*ref_latent)
247
  return error
248
 
@@ -279,11 +278,11 @@ class Styles_paintings():
279
  def generate_styles_with_custom_loss(self, image):
280
  height = 512 # default height of Stable Diffusion
281
  width = 512 # default width of Stable Diffusion
282
- num_inference_steps = 50 #@param # Number of denoising steps
283
  guidance_scale = 8 #@param # Scale for classifier-free guidance
284
  batch_size = 1
285
  custom_loss_scale = 200 #@param
286
- #print('image shape there is',image.size)
287
  self.output_styles_with_custom_loss = []
288
  #ref_image = Image.open('C:/Users/shivs/Downloads/ig.jpg').resize((512,512))
289
  ref_latent = pil_to_latent(image)
@@ -344,7 +343,7 @@ class Styles_paintings():
344
  #denoised_images = vae.decode((1 / 0.18215) * latents_x0).sample / 2 + 0.5 # range (0, 1)
345
 
346
  # Calculate loss
347
- loss = custom_loss(latents_x0) * custom_loss_scale
348
  #loss = blue_loss(denoised_images) * blue_loss_scale
349
 
350
  # Occasionally print it out
@@ -362,14 +361,13 @@ class Styles_paintings():
362
 
363
  self.output_styles_with_custom_loss.append(latents_to_pil(latents)[0])
364
 
365
- def generate_final_image(im1,in_prompt):
366
  paintings = Styles_paintings(in_prompt)
367
  paintings.generate_styles()
368
  r_image = im1.resize((512,512))
369
  print('image shape is',r_image.size)
370
  paintings.generate_styles_with_custom_loss(r_image)
371
 
372
- print(len(paintings.output_styles))
373
- print(len(paintings.output_styles_with_custom_loss))
374
 
375
- return [paintings.output_styles[0]], [paintings.output_styles[1]],[paintings.output_styles[2]],[paintings.output_styles[3]],[paintings.output_styles[4]],[paintings.output_styles_with_custom_loss[0]],[paintings.output_styles_with_custom_loss[1]],[paintings.output_styles_with_custom_loss[2]],[paintings.output_styles_with_custom_loss[3]],[paintings.output_styles_with_custom_loss[4]]
 
1
+ # import transformers as t
2
+ # assert t.__version__=='4.25.1', "Transformers version should be as specified"
 
3
 
4
 
5
  import torch
 
24
 
25
  torch.manual_seed(1)
26
 
27
+ if not (Path.home()/'.cache/huggingface'/'token').exists(): notebook_login()
28
 
29
  # Supress some unnecessary warnings when loading the CLIPTextModel
30
  logging.set_verbosity_error()
 
87
  text_encoder = text_encoder.to(torch_device)
88
  unet = unet.to(torch_device);
89
 
90
+ embeds_folder = Path('C:/Users/shivs/Downloads/paintings_embed')
91
  file_names = [path.name for path in embeds_folder.glob('*') if path.is_file()]
92
  print(file_names)
93
 
 
192
  def generate_with_embs_custom(text_embeddings,seed):
193
  height = 512 # default height of Stable Diffusion
194
  width = 512 # default width of Stable Diffusion
195
+ num_inference_steps = 10 # Number of denoising steps
196
  guidance_scale = 7.5 # Scale for classifier-free guidance
197
  generator = torch.manual_seed(seed) # Seed generator to create the inital latent noise
198
  batch_size = 1
 
241
  # ref_latent = pil_to_latent(ref_image)
242
 
243
  ## Guidance through Custom Loss Function
244
+ def custom_loss(latent,ref_latent):
245
  error = F.mse_loss(0.5*latent,0.8*ref_latent)
246
  return error
247
 
 
278
  def generate_styles_with_custom_loss(self, image):
279
  height = 512 # default height of Stable Diffusion
280
  width = 512 # default width of Stable Diffusion
281
+ num_inference_steps = 20 #@param # Number of denoising steps
282
  guidance_scale = 8 #@param # Scale for classifier-free guidance
283
  batch_size = 1
284
  custom_loss_scale = 200 #@param
285
+ print('image shape there is',image.size)
286
  self.output_styles_with_custom_loss = []
287
  #ref_image = Image.open('C:/Users/shivs/Downloads/ig.jpg').resize((512,512))
288
  ref_latent = pil_to_latent(image)
 
343
  #denoised_images = vae.decode((1 / 0.18215) * latents_x0).sample / 2 + 0.5 # range (0, 1)
344
 
345
  # Calculate loss
346
+ loss = custom_loss(latents_x0,ref_latent) * custom_loss_scale
347
  #loss = blue_loss(denoised_images) * blue_loss_scale
348
 
349
  # Occasionally print it out
 
361
 
362
  self.output_styles_with_custom_loss.append(latents_to_pil(latents)[0])
363
 
364
+ def generate_final_image(im1,in_prompt="an oil painting of an baby girl with flowers in a park"):
365
  paintings = Styles_paintings(in_prompt)
366
  paintings.generate_styles()
367
  r_image = im1.resize((512,512))
368
  print('image shape is',r_image.size)
369
  paintings.generate_styles_with_custom_loss(r_image)
370
 
371
+ #print(len(paintings.output_styles))
 
372
 
373
+ return [paintings.output_styles[0]], [paintings.output_styles[1]],[paintings.output_styles[2]],[paintings.output_styles[3]],[paintings.output_styles[4]], [paintings.output_styles_with_custom_loss[0]],[paintings.output_styles_with_custom_loss[1]],[paintings.output_styles_with_custom_loss[2]],[paintings.output_styles_with_custom_loss[3]],[paintings.output_styles_with_custom_loss[4]]