Spaces:
Sleeping
Sleeping
update requirements
Browse files- app.py +29 -4
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -9,15 +9,30 @@ import spaces
|
|
| 9 |
import torch
|
| 10 |
import random
|
| 11 |
from PIL import Image
|
| 12 |
-
|
| 13 |
from pipeline import GenSIRR
|
| 14 |
from diffusers.utils import load_image
|
| 15 |
-
|
| 16 |
from optimization import optimize_pipeline_
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
from huggingface_hub import hf_hub_download
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def load_deepspeed_weights(model, checkpoint_path) -> None:
|
| 22 |
"""Load LoRA weights from a DeepSpeed ZeRO Stage 2 checkpoint into the model."""
|
| 23 |
tensor_path = checkpoint_path
|
|
@@ -81,10 +96,20 @@ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5
|
|
| 81 |
if randomize_seed:
|
| 82 |
seed = random.randint(0, MAX_SEED)
|
| 83 |
|
| 84 |
-
|
| 85 |
input_image = input_image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
image = pipe(
|
| 87 |
-
image=
|
| 88 |
width = input_image.size[0],
|
| 89 |
height = input_image.size[1],
|
| 90 |
num_inference_steps=steps,
|
|
|
|
| 9 |
import torch
|
| 10 |
import random
|
| 11 |
from PIL import Image
|
| 12 |
+
import torchvision.transforms.functional as TF
|
| 13 |
from pipeline import GenSIRR
|
| 14 |
from diffusers.utils import load_image
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
from optimization import optimize_pipeline_
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
from huggingface_hub import hf_hub_download
|
| 20 |
|
| 21 |
+
def pad_for_model(image: torch.Tensor, multiple: int) -> Tuple[torch.Tensor, Tuple[int, int]]:
|
| 22 |
+
"""Pad the tensor image so height/width are divisible by ``multiple``."""
|
| 23 |
+
if multiple <= 0:
|
| 24 |
+
raise ValueError("round_multiple must be > 0")
|
| 25 |
+
|
| 26 |
+
height, width = image.shape[-2:]
|
| 27 |
+
pad_h = (multiple - height % multiple) % multiple
|
| 28 |
+
pad_w = (multiple - width % multiple) % multiple
|
| 29 |
+
if pad_h == 0 and pad_w == 0:
|
| 30 |
+
return image, (0, 0)
|
| 31 |
+
|
| 32 |
+
padded = F.pad(image.unsqueeze(0), (0, pad_w, 0, pad_h), mode="reflect").squeeze(0)
|
| 33 |
+
return padded, (pad_h, pad_w)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
def load_deepspeed_weights(model, checkpoint_path) -> None:
|
| 37 |
"""Load LoRA weights from a DeepSpeed ZeRO Stage 2 checkpoint into the model."""
|
| 38 |
tensor_path = checkpoint_path
|
|
|
|
| 96 |
if randomize_seed:
|
| 97 |
seed = random.randint(0, MAX_SEED)
|
| 98 |
|
| 99 |
+
size = 768
|
| 100 |
input_image = input_image.convert("RGB")
|
| 101 |
+
if input_image.width < input_image.height:
|
| 102 |
+
input_image = input_image.resize((size, int(size * input_image.height / input_image.width)))
|
| 103 |
+
else:
|
| 104 |
+
input_image = input_image.resize((int(size * input_image.width / input_image.height), size))
|
| 105 |
+
tensor = TF.to_tensor(img)
|
| 106 |
+
|
| 107 |
+
original_size = tensor.shape[-2:]
|
| 108 |
+
padded_tensor, padding = pad_for_model(tensor, args.round_multiple)
|
| 109 |
+
batch_cpu = padded_tensor.unsqueeze(0)
|
| 110 |
+
batch_device = batch_cpu.to('cuda')
|
| 111 |
image = pipe(
|
| 112 |
+
image=batch_device,
|
| 113 |
width = input_image.size[0],
|
| 114 |
height = input_image.size[1],
|
| 115 |
num_inference_steps=steps,
|
requirements.txt
CHANGED
|
@@ -3,4 +3,5 @@ git+https://github.com/huggingface/diffusers.git
|
|
| 3 |
accelerate
|
| 4 |
safetensors
|
| 5 |
sentencepiece
|
| 6 |
-
peft
|
|
|
|
|
|
| 3 |
accelerate
|
| 4 |
safetensors
|
| 5 |
sentencepiece
|
| 6 |
+
peft
|
| 7 |
+
torchvision
|