Fabrice-TIERCELIN commited on
Commit
0c1e4fd
·
verified ·
1 Parent(s): 779f6d3

Optimize memory

Browse files
Files changed (1) hide show
  1. app.py +53 -47
app.py CHANGED
@@ -643,57 +643,63 @@ def worker_start_end(input_image, end_image, image_position, prompts, n_prompt,
643
 
644
  H, W, C = input_image.shape
645
  height, width = find_nearest_bucket(H, W, resolution=resolution)
646
- input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)
647
-
648
- Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}_start.png'))
649
-
650
- input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
651
- input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
652
 
653
- # Processing end image (if provided)
654
- has_end_image = end_image is not None
655
- if has_end_image:
656
- stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Processing end frame ...'))))
657
 
658
- H_end, W_end, C_end = end_image.shape
659
- end_image_np = resize_and_center_crop(end_image, target_width=width, target_height=height)
660
 
661
- Image.fromarray(end_image_np).save(os.path.join(outputs_folder, f'{job_id}_end.png'))
 
662
 
663
- end_image_pt = torch.from_numpy(end_image_np).float() / 127.5 - 1
664
- end_image_pt = end_image_pt.permute(2, 0, 1)[None, :, None]
665
-
666
- # VAE encoding
667
- stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding ...'))))
668
-
669
- if not high_vram:
670
- load_model_as_complete(vae, target_device=gpu)
671
-
672
- start_latent = vae_encode(input_image_pt, vae)
673
-
674
- if has_end_image:
675
- end_latent = vae_encode(end_image_pt, vae)
676
-
677
- # CLIP Vision
678
- stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision encoding ...'))))
679
-
680
- if not high_vram:
681
- load_model_as_complete(image_encoder, target_device=gpu)
682
-
683
- image_encoder_output = hf_clip_vision_encode(input_image_np, feature_extractor, image_encoder)
684
- image_encoder_last_hidden_state = image_encoder_output.last_hidden_state
685
-
686
- if has_end_image:
687
- end_image_encoder_output = hf_clip_vision_encode(end_image_np, feature_extractor, image_encoder)
688
- end_image_encoder_last_hidden_state = end_image_encoder_output.last_hidden_state
689
- # Combine both image embeddings or use a weighted approach
690
- image_encoder_last_hidden_state = (image_encoder_last_hidden_state + end_image_encoder_last_hidden_state) / 2
691
-
692
- # Clean GPU
693
- if not high_vram:
694
- unload_complete_models(
695
- image_encoder
696
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
697
 
698
  # Dtype
699
  image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)
 
643
 
644
  H, W, C = input_image.shape
645
  height, width = find_nearest_bucket(H, W, resolution=resolution)
 
 
 
 
 
 
646
 
647
+ def get_start_latent(input_image, end_image, height, width, vae, gpu, image_encoder, high_vram):
648
+ input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)
 
 
649
 
650
+ Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}_start.png'))
 
651
 
652
+ input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
653
+ input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
654
 
655
+ # Processing end image (if provided)
656
+ has_end_image = end_image is not None
657
+ if has_end_image:
658
+ stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Processing end frame ...'))))
659
+
660
+ H_end, W_end, C_end = end_image.shape
661
+ end_image_np = resize_and_center_crop(end_image, target_width=width, target_height=height)
662
+
663
+ Image.fromarray(end_image_np).save(os.path.join(outputs_folder, f'{job_id}_end.png'))
664
+
665
+ end_image_pt = torch.from_numpy(end_image_np).float() / 127.5 - 1
666
+ end_image_pt = end_image_pt.permute(2, 0, 1)[None, :, None]
667
+
668
+ # VAE encoding
669
+ stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding ...'))))
670
+
671
+ if not high_vram:
672
+ load_model_as_complete(vae, target_device=gpu)
673
+
674
+ start_latent = vae_encode(input_image_pt, vae)
675
+
676
+ if has_end_image:
677
+ end_latent = vae_encode(end_image_pt, vae)
678
+
679
+ # CLIP Vision
680
+ stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision encoding ...'))))
681
+
682
+ if not high_vram:
683
+ load_model_as_complete(image_encoder, target_device=gpu)
684
+
685
+ image_encoder_output = hf_clip_vision_encode(input_image_np, feature_extractor, image_encoder)
686
+ image_encoder_last_hidden_state = image_encoder_output.last_hidden_state
687
+
688
+ if has_end_image:
689
+ end_image_encoder_output = hf_clip_vision_encode(end_image_np, feature_extractor, image_encoder)
690
+ end_image_encoder_last_hidden_state = end_image_encoder_output.last_hidden_state
691
+ # Combine both image embeddings or use a weighted approach
692
+ image_encoder_last_hidden_state = (image_encoder_last_hidden_state + end_image_encoder_last_hidden_state) / 2
693
+
694
+ # Clean GPU
695
+ if not high_vram:
696
+ unload_complete_models(
697
+ image_encoder
698
+ )
699
+
700
+ return [start_latent, image_encoder_last_hidden_state]
701
+
702
+ [start_latent, image_encoder_last_hidden_state] = get_start_latent(input_image, end_image, height, width, vae, gpu, image_encoder, high_vram)
703
 
704
  # Dtype
705
  image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)