Upload folder using huggingface_hub
Browse files- inference2.py +16 -9
- internals/data/task.py +3 -0
- internals/pipelines/replace_background.py +8 -1
- internals/pipelines/upscaler.py +59 -21
- internals/util/image.py +3 -3
- internals/util/lora_style.py +1 -2
inference2.py
CHANGED
@@ -7,18 +7,17 @@ from internals.data.task import ModelType, Task, TaskType
|
|
7 |
from internals.pipelines.inpainter import InPainter
|
8 |
from internals.pipelines.object_remove import ObjectRemoval
|
9 |
from internals.pipelines.prompt_modifier import PromptModifier
|
10 |
-
from internals.pipelines.remove_background import RemoveBackground,
|
|
|
11 |
from internals.pipelines.replace_background import ReplaceBackground
|
12 |
from internals.pipelines.safety_checker import SafetyChecker
|
13 |
from internals.pipelines.upscaler import Upscaler
|
14 |
from internals.util.avatar import Avatar
|
15 |
from internals.util.cache import auto_clear_cuda_and_gc, clear_cuda
|
16 |
-
from internals.util.commons import construct_default_s3_url, upload_image,
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
set_root_dir,
|
21 |
-
)
|
22 |
from internals.util.failure_hander import FailureHandler
|
23 |
from internals.util.slack import Slack
|
24 |
|
@@ -135,12 +134,20 @@ def upscale_image(task: Task):
|
|
135 |
if task.get_modelType() == ModelType.ANIME:
|
136 |
print("Using Anime model")
|
137 |
out_img = upscaler.upscale_anime(
|
138 |
-
image=task.get_imageUrl(),
|
|
|
|
|
|
|
|
|
139 |
)
|
140 |
else:
|
141 |
print("Using Real model")
|
142 |
out_img = upscaler.upscale(
|
143 |
-
image=task.get_imageUrl(),
|
|
|
|
|
|
|
|
|
144 |
)
|
145 |
|
146 |
upload_image(BytesIO(out_img), output_key)
|
|
|
7 |
from internals.pipelines.inpainter import InPainter
|
8 |
from internals.pipelines.object_remove import ObjectRemoval
|
9 |
from internals.pipelines.prompt_modifier import PromptModifier
|
10 |
+
from internals.pipelines.remove_background import (RemoveBackground,
|
11 |
+
RemoveBackgroundV2)
|
12 |
from internals.pipelines.replace_background import ReplaceBackground
|
13 |
from internals.pipelines.safety_checker import SafetyChecker
|
14 |
from internals.pipelines.upscaler import Upscaler
|
15 |
from internals.util.avatar import Avatar
|
16 |
from internals.util.cache import auto_clear_cuda_and_gc, clear_cuda
|
17 |
+
from internals.util.commons import (construct_default_s3_url, upload_image,
|
18 |
+
upload_images)
|
19 |
+
from internals.util.config import (num_return_sequences, set_configs_from_task,
|
20 |
+
set_root_dir)
|
|
|
|
|
21 |
from internals.util.failure_hander import FailureHandler
|
22 |
from internals.util.slack import Slack
|
23 |
|
|
|
134 |
if task.get_modelType() == ModelType.ANIME:
|
135 |
print("Using Anime model")
|
136 |
out_img = upscaler.upscale_anime(
|
137 |
+
image=task.get_imageUrl(),
|
138 |
+
width=task.get_width(),
|
139 |
+
height=task.get_height(),
|
140 |
+
face_enhance=task.get_face_enhance(),
|
141 |
+
resize_dimension=task.get_resize_dimension(),
|
142 |
)
|
143 |
else:
|
144 |
print("Using Real model")
|
145 |
out_img = upscaler.upscale(
|
146 |
+
image=task.get_imageUrl(),
|
147 |
+
width=task.get_width(),
|
148 |
+
height=task.get_height(),
|
149 |
+
face_enhance=task.get_face_enhance(),
|
150 |
+
resize_dimension=task.get_resize_dimension(),
|
151 |
)
|
152 |
|
153 |
upload_image(BytesIO(out_img), output_key)
|
internals/data/task.py
CHANGED
@@ -112,6 +112,9 @@ class Task:
|
|
112 |
def get_resize_dimension(self) -> int:
|
113 |
return self.__data.get("resize_dimension", 1024)
|
114 |
|
|
|
|
|
|
|
115 |
def get_ti_guidance_scale(self) -> float:
|
116 |
return self.__data.get("ti_guidance_scale", 7.5)
|
117 |
|
|
|
112 |
def get_resize_dimension(self) -> int:
|
113 |
return self.__data.get("resize_dimension", 1024)
|
114 |
|
115 |
+
def get_face_enhance(self) -> bool:
|
116 |
+
return self.__data.get("up_face_enhance", False)
|
117 |
+
|
118 |
def get_ti_guidance_scale(self) -> float:
|
119 |
return self.__data.get("ti_guidance_scale", 7.5)
|
120 |
|
internals/pipelines/replace_background.py
CHANGED
@@ -106,7 +106,14 @@ class ReplaceBackground:
|
|
106 |
if not has_nsfw:
|
107 |
for i in range(len(images)):
|
108 |
images[i].paste(image, (0, 0), image)
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
images[i] = Image.open(BytesIO(out_bytes)).convert("RGB")
|
111 |
|
112 |
return (images, has_nsfw)
|
|
|
106 |
if not has_nsfw:
|
107 |
for i in range(len(images)):
|
108 |
images[i].paste(image, (0, 0), image)
|
109 |
+
w, h = images[i].size
|
110 |
+
out_bytes = self.upscaler.upscale(
|
111 |
+
image=images[i],
|
112 |
+
width=w,
|
113 |
+
height=h,
|
114 |
+
face_enhance=False,
|
115 |
+
resize_dimension=resize_dimension,
|
116 |
+
)
|
117 |
images[i] = Image.open(BytesIO(out_bytes)).convert("RGB")
|
118 |
|
119 |
return (images, has_nsfw)
|
internals/pipelines/upscaler.py
CHANGED
@@ -6,6 +6,7 @@ from typing import Union
|
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
9 |
from basicsr.utils.download_util import load_file_from_url
|
10 |
from gfpgan import GFPGANer
|
11 |
from PIL import Image
|
@@ -17,7 +18,7 @@ from internals.util.config import get_root_dir
|
|
17 |
|
18 |
|
19 |
class Upscaler:
|
20 |
-
__model_esrgan_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.
|
21 |
__model_esrgan_anime_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"
|
22 |
__model_gfpgan_url = (
|
23 |
"https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth"
|
@@ -41,21 +42,39 @@ class Upscaler:
|
|
41 |
)
|
42 |
self.__loaded = True
|
43 |
|
44 |
-
def upscale(
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
num_in_ch=3,
|
47 |
num_out_ch=3,
|
48 |
num_feat=64,
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
)
|
53 |
return self.__internal_upscale(
|
54 |
-
image,
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
)
|
56 |
|
57 |
def upscale_anime(
|
58 |
-
self,
|
|
|
|
|
|
|
|
|
|
|
59 |
) -> bytes:
|
60 |
model = RRDBNet(
|
61 |
num_in_ch=3,
|
@@ -66,7 +85,13 @@ class Upscaler:
|
|
66 |
scale=4,
|
67 |
)
|
68 |
return self.__internal_upscale(
|
69 |
-
image,
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
|
72 |
def __preload_model(self, url: str, download_dir: Path):
|
@@ -85,25 +110,34 @@ class Upscaler:
|
|
85 |
self,
|
86 |
image,
|
87 |
resize_dimension: int,
|
|
|
|
|
|
|
88 |
model_path: str,
|
89 |
-
|
90 |
) -> bytes:
|
91 |
if type(image) is str:
|
92 |
image = download_image(image)
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
image_array = np.frombuffer(image, dtype=np.uint8)
|
100 |
-
input_image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
|
101 |
dimension = min(input_image.shape[0], input_image.shape[1])
|
102 |
scale = max(math.floor(resize_dimension / dimension), 2)
|
103 |
|
104 |
os.chdir(str(Path.home() / ".cache"))
|
105 |
upsampler = RealESRGANer(
|
106 |
-
scale=4,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
)
|
108 |
face_enhancer = GFPGANer(
|
109 |
model_path=self.__model_path_gfpgan,
|
@@ -113,9 +147,13 @@ class Upscaler:
|
|
113 |
bg_upsampler=upsampler,
|
114 |
)
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
119 |
os.chdir(get_root_dir())
|
|
|
120 |
out_bytes = cv2.imencode(".png", output)[1].tobytes()
|
121 |
return out_bytes
|
|
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
9 |
+
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
10 |
from basicsr.utils.download_util import load_file_from_url
|
11 |
from gfpgan import GFPGANer
|
12 |
from PIL import Image
|
|
|
18 |
|
19 |
|
20 |
class Upscaler:
|
21 |
+
__model_esrgan_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth"
|
22 |
__model_esrgan_anime_url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"
|
23 |
__model_gfpgan_url = (
|
24 |
"https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth"
|
|
|
42 |
)
|
43 |
self.__loaded = True
|
44 |
|
45 |
+
def upscale(
|
46 |
+
self,
|
47 |
+
image: Union[str, Image.Image],
|
48 |
+
width: int,
|
49 |
+
height: int,
|
50 |
+
face_enhance: bool,
|
51 |
+
resize_dimension: int,
|
52 |
+
) -> bytes:
|
53 |
+
model = SRVGGNetCompact(
|
54 |
num_in_ch=3,
|
55 |
num_out_ch=3,
|
56 |
num_feat=64,
|
57 |
+
num_conv=32,
|
58 |
+
upscale=4,
|
59 |
+
act_type="prelu",
|
60 |
)
|
61 |
return self.__internal_upscale(
|
62 |
+
image,
|
63 |
+
resize_dimension,
|
64 |
+
face_enhance,
|
65 |
+
width,
|
66 |
+
height,
|
67 |
+
self.__model_path,
|
68 |
+
model,
|
69 |
)
|
70 |
|
71 |
def upscale_anime(
|
72 |
+
self,
|
73 |
+
image: Union[str, Image.Image],
|
74 |
+
width: int,
|
75 |
+
height: int,
|
76 |
+
face_enhance: bool,
|
77 |
+
resize_dimension: int,
|
78 |
) -> bytes:
|
79 |
model = RRDBNet(
|
80 |
num_in_ch=3,
|
|
|
85 |
scale=4,
|
86 |
)
|
87 |
return self.__internal_upscale(
|
88 |
+
image,
|
89 |
+
resize_dimension,
|
90 |
+
face_enhance,
|
91 |
+
width,
|
92 |
+
height,
|
93 |
+
self.__model_path_anime,
|
94 |
+
model,
|
95 |
)
|
96 |
|
97 |
def __preload_model(self, url: str, download_dir: Path):
|
|
|
110 |
self,
|
111 |
image,
|
112 |
resize_dimension: int,
|
113 |
+
face_enhance: bool,
|
114 |
+
width: int,
|
115 |
+
height: int,
|
116 |
model_path: str,
|
117 |
+
model,
|
118 |
) -> bytes:
|
119 |
if type(image) is str:
|
120 |
image = download_image(image)
|
121 |
+
w, h = image.size
|
122 |
+
if max(w, h) > 1536:
|
123 |
+
image = ImageUtil.resize_image(image, dimension=1536)
|
124 |
|
125 |
+
in_path = str(Path.home() / ".cache" / "input_upscale.png")
|
126 |
+
image.save(in_path)
|
127 |
+
input_image = cv2.imread(in_path, cv2.IMREAD_UNCHANGED)
|
|
|
|
|
128 |
dimension = min(input_image.shape[0], input_image.shape[1])
|
129 |
scale = max(math.floor(resize_dimension / dimension), 2)
|
130 |
|
131 |
os.chdir(str(Path.home() / ".cache"))
|
132 |
upsampler = RealESRGANer(
|
133 |
+
scale=4,
|
134 |
+
model_path=model_path,
|
135 |
+
model=model,
|
136 |
+
half=False,
|
137 |
+
gpu_id="0",
|
138 |
+
tile=0,
|
139 |
+
tile_pad=10,
|
140 |
+
pre_pad=0,
|
141 |
)
|
142 |
face_enhancer = GFPGANer(
|
143 |
model_path=self.__model_path_gfpgan,
|
|
|
147 |
bg_upsampler=upsampler,
|
148 |
)
|
149 |
|
150 |
+
if face_enhance:
|
151 |
+
_, _, output = face_enhancer.enhance(
|
152 |
+
input_image, has_aligned=False, only_center_face=False, paste_back=True
|
153 |
+
)
|
154 |
+
else:
|
155 |
+
output, _ = upsampler.enhance(input_image, outscale=scale)
|
156 |
os.chdir(get_root_dir())
|
157 |
+
cv2.imwrite("out.png", output)
|
158 |
out_bytes = cv2.imencode(".png", output)[1].tobytes()
|
159 |
return out_bytes
|
internals/util/image.py
CHANGED
@@ -9,12 +9,12 @@ def to_bytes(image: Image.Image) -> bytes:
|
|
9 |
return output.getvalue()
|
10 |
|
11 |
|
12 |
-
def
|
13 |
iw, ih = image.size
|
14 |
if iw > ih:
|
15 |
-
image = image.resize((
|
16 |
else:
|
17 |
-
image = image.resize((int(
|
18 |
return image
|
19 |
|
20 |
|
|
|
9 |
return output.getvalue()
|
10 |
|
11 |
|
12 |
+
def resize_image(image: Image.Image, dimension: int = 512) -> Image.Image:
|
13 |
iw, ih = image.size
|
14 |
if iw > ih:
|
15 |
+
image = image.resize((dimension, int(dimension * ih / iw)))
|
16 |
else:
|
17 |
+
image = image.resize((int(dimension * iw / ih), dimension))
|
18 |
return image
|
19 |
|
20 |
|
internals/util/lora_style.py
CHANGED
@@ -176,5 +176,4 @@ class LoraStyle:
|
|
176 |
|
177 |
@staticmethod
|
178 |
def unload_lora_weights(pipe):
|
179 |
-
pipe.
|
180 |
-
pipe._remove_text_encoder_monkey_patch()
|
|
|
176 |
|
177 |
@staticmethod
|
178 |
def unload_lora_weights(pipe):
|
179 |
+
pipe.unload_lora_weights()
|
|