Spaces:
Runtime error
Runtime error
Optimize memory
Browse files
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 |
-
|
| 654 |
-
|
| 655 |
-
if has_end_image:
|
| 656 |
-
stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Processing end frame ...'))))
|
| 657 |
|
| 658 |
-
|
| 659 |
-
end_image_np = resize_and_center_crop(end_image, target_width=width, target_height=height)
|
| 660 |
|
| 661 |
-
|
|
|
|
| 662 |
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 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)
|