Spaces:
Runtime error
Runtime error
Update app.py (#5)
Browse files- Update app.py (c1a8afc0f0d4d87e0fb45a40b6257e71ed69f4b1)
- Upload briarmbg.py (e1ab39d8ea2cfd2f26f97f54edda4e5ae9739d2d)
- app.py +136 -9
- briarmbg.py +462 -0
app.py
CHANGED
|
@@ -9,11 +9,15 @@ from huggingface_hub import hf_hub_download
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import random
|
|
|
|
| 12 |
|
| 13 |
model_id = "Wan-AI/Wan2.1-VACE-14B-diffusers"
|
| 14 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
| 15 |
pipe = WanVACEPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=2.0)
|
| 18 |
|
| 19 |
pipe.load_lora_weights(
|
|
@@ -89,6 +93,91 @@ def remove_alpha_channel(image: Image.Image) -> Image.Image:
|
|
| 89 |
return image
|
| 90 |
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
|
| 93 |
min_slider_h, max_slider_h,
|
| 94 |
min_slider_w, max_slider_w,
|
|
@@ -228,20 +317,26 @@ def prepare_video_and_mask_Random2V(images: List[Image.Image], frame_indices: Li
|
|
| 228 |
def get_duration(gallery_images, mode, prompt, height, width,
|
| 229 |
negative_prompt, duration_seconds,
|
| 230 |
guidance_scale, steps,
|
| 231 |
-
seed, randomize_seed,
|
| 232 |
progress):
|
|
|
|
|
|
|
| 233 |
if steps > 4 and duration_seconds > 2:
|
| 234 |
-
|
| 235 |
elif steps > 4 or duration_seconds > 2:
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
@spaces.GPU(duration=get_duration)
|
| 241 |
def generate_video(gallery_images, mode, prompt, height, width,
|
| 242 |
negative_prompt=default_negative_prompt, duration_seconds = 2,
|
| 243 |
guidance_scale = 1, steps = 4,
|
| 244 |
-
seed = 42, randomize_seed = False,
|
| 245 |
progress=gr.Progress(track_tqdm=True)):
|
| 246 |
"""
|
| 247 |
Generate a video from gallery images using the selected mode.
|
|
@@ -258,6 +353,7 @@ def generate_video(gallery_images, mode, prompt, height, width,
|
|
| 258 |
steps (int): Number of inference steps
|
| 259 |
seed (int): Random seed for reproducible results
|
| 260 |
randomize_seed (bool): Whether to use a random seed
|
|
|
|
| 261 |
progress (gr.Progress): Gradio progress tracker
|
| 262 |
|
| 263 |
Returns:
|
|
@@ -266,8 +362,20 @@ def generate_video(gallery_images, mode, prompt, height, width,
|
|
| 266 |
if gallery_images is None or len(gallery_images) == 0:
|
| 267 |
raise gr.Error("Please upload at least one image to the gallery.")
|
| 268 |
else:
|
| 269 |
-
#
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
if mode == "FLF2V" and len(gallery_images) >= 2:
|
| 273 |
gallery_images = gallery_images[:2]
|
|
@@ -377,6 +485,14 @@ with gr.Blocks() as demo:
|
|
| 377 |
|
| 378 |
with gr.Accordion("Advanced Settings", open=False):
|
| 379 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 381 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 382 |
with gr.Row():
|
|
@@ -390,6 +506,10 @@ with gr.Blocks() as demo:
|
|
| 390 |
with gr.Column():
|
| 391 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
# Update prompt when mode changes
|
| 394 |
mode_radio.change(
|
| 395 |
fn=update_prompt_from_mode,
|
|
@@ -397,6 +517,13 @@ with gr.Blocks() as demo:
|
|
| 397 |
outputs=[prompt_input]
|
| 398 |
)
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
# Update dimensions when gallery changes
|
| 401 |
gallery_component.change(
|
| 402 |
fn=handle_gallery_upload_for_dims_wan,
|
|
@@ -407,7 +534,7 @@ with gr.Blocks() as demo:
|
|
| 407 |
ui_inputs = [
|
| 408 |
gallery_component, mode_radio, prompt_input, height_input, width_input,
|
| 409 |
negative_prompt_input, duration_seconds_input,
|
| 410 |
-
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 411 |
]
|
| 412 |
|
| 413 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import random
|
| 12 |
+
from briarmbg import BriaRMBG
|
| 13 |
|
| 14 |
model_id = "Wan-AI/Wan2.1-VACE-14B-diffusers"
|
| 15 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
| 16 |
pipe = WanVACEPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16).to("cuda")
|
| 17 |
|
| 18 |
+
# Initialize background removal model
|
| 19 |
+
rmbg = BriaRMBG.from_pretrained("briaai/RMBG-1.4").to("cuda", dtype=torch.float32)
|
| 20 |
+
|
| 21 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=2.0)
|
| 22 |
|
| 23 |
pipe.load_lora_weights(
|
|
|
|
| 93 |
return image
|
| 94 |
|
| 95 |
|
| 96 |
+
@torch.inference_mode()
|
| 97 |
+
def numpy2pytorch(imgs):
|
| 98 |
+
h = torch.from_numpy(np.stack(imgs, axis=0)).float() / 127.0 - 1.0 # so that 127 must be strictly 0.0
|
| 99 |
+
h = h.movedim(-1, 1)
|
| 100 |
+
return h
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@torch.inference_mode()
|
| 104 |
+
def pytorch2numpy(imgs, quant=True):
|
| 105 |
+
results = []
|
| 106 |
+
for x in imgs:
|
| 107 |
+
y = x.movedim(0, -1)
|
| 108 |
+
|
| 109 |
+
if quant:
|
| 110 |
+
y = y * 127.5 + 127.5
|
| 111 |
+
y = y.detach().float().cpu().numpy().clip(0, 255).astype(np.uint8)
|
| 112 |
+
else:
|
| 113 |
+
y = y * 0.5 + 0.5
|
| 114 |
+
y = y.detach().float().cpu().numpy().clip(0, 1).astype(np.float32)
|
| 115 |
+
|
| 116 |
+
results.append(y)
|
| 117 |
+
return results
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def resize_without_crop(image, target_width, target_height):
|
| 121 |
+
pil_image = Image.fromarray(image)
|
| 122 |
+
resized_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
|
| 123 |
+
return np.array(resized_image)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
@torch.inference_mode()
|
| 127 |
+
def run_rmbg(img, sigma=0.0):
|
| 128 |
+
"""
|
| 129 |
+
Remove background from image using BriaRMBG model.
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
img (np.ndarray): Input image as numpy array (H, W, C)
|
| 133 |
+
sigma (float): Noise parameter for blending
|
| 134 |
+
|
| 135 |
+
Returns:
|
| 136 |
+
tuple: (result_image, alpha_mask) where result_image is the image with background removed
|
| 137 |
+
"""
|
| 138 |
+
H, W, C = img.shape
|
| 139 |
+
assert C == 3
|
| 140 |
+
k = (256.0 / float(H * W)) ** 0.5
|
| 141 |
+
feed = resize_without_crop(img, int(64 * round(W * k)), int(64 * round(H * k)))
|
| 142 |
+
feed = numpy2pytorch([feed]).to(device="cuda", dtype=torch.float32)
|
| 143 |
+
alpha = rmbg(feed)[0][0]
|
| 144 |
+
alpha = torch.nn.functional.interpolate(alpha, size=(H, W), mode="bilinear")
|
| 145 |
+
alpha = alpha.movedim(1, -1)[0]
|
| 146 |
+
alpha = alpha.detach().float().cpu().numpy().clip(0, 1)
|
| 147 |
+
result = 127 + (img.astype(np.float32) - 127 + sigma) * alpha
|
| 148 |
+
return result.clip(0, 255).astype(np.uint8), alpha
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def remove_background_from_image(image: Image.Image) -> Image.Image:
|
| 152 |
+
"""
|
| 153 |
+
Remove background from PIL Image using RMBG model.
|
| 154 |
+
|
| 155 |
+
Args:
|
| 156 |
+
image (Image.Image): Input PIL image
|
| 157 |
+
|
| 158 |
+
Returns:
|
| 159 |
+
Image.Image: Image with background removed (transparent background)
|
| 160 |
+
"""
|
| 161 |
+
# Convert PIL to numpy array
|
| 162 |
+
img_array = np.array(image)
|
| 163 |
+
|
| 164 |
+
# Remove background using RMBG
|
| 165 |
+
result_array, alpha_mask = run_rmbg(img_array)
|
| 166 |
+
|
| 167 |
+
# Convert back to PIL with alpha channel
|
| 168 |
+
result_image = Image.fromarray(result_array)
|
| 169 |
+
|
| 170 |
+
# Create RGBA image with alpha mask
|
| 171 |
+
if result_image.mode != 'RGBA':
|
| 172 |
+
result_image = result_image.convert('RGBA')
|
| 173 |
+
|
| 174 |
+
# Apply alpha mask
|
| 175 |
+
alpha_pil = Image.fromarray((alpha_mask * 255).astype(np.uint8), mode='L')
|
| 176 |
+
result_image.putalpha(alpha_pil)
|
| 177 |
+
|
| 178 |
+
return result_image
|
| 179 |
+
|
| 180 |
+
|
| 181 |
def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
|
| 182 |
min_slider_h, max_slider_h,
|
| 183 |
min_slider_w, max_slider_w,
|
|
|
|
| 317 |
def get_duration(gallery_images, mode, prompt, height, width,
|
| 318 |
negative_prompt, duration_seconds,
|
| 319 |
guidance_scale, steps,
|
| 320 |
+
seed, randomize_seed, remove_bg,
|
| 321 |
progress):
|
| 322 |
+
# Add extra time if background removal is enabled
|
| 323 |
+
base_duration = 60
|
| 324 |
if steps > 4 and duration_seconds > 2:
|
| 325 |
+
base_duration = 90
|
| 326 |
elif steps > 4 or duration_seconds > 2:
|
| 327 |
+
base_duration = 75
|
| 328 |
+
|
| 329 |
+
# Add extra time for background removal processing
|
| 330 |
+
if mode == "Ref2V" and remove_bg:
|
| 331 |
+
base_duration += 30
|
| 332 |
+
|
| 333 |
+
return base_duration
|
| 334 |
|
| 335 |
@spaces.GPU(duration=get_duration)
|
| 336 |
def generate_video(gallery_images, mode, prompt, height, width,
|
| 337 |
negative_prompt=default_negative_prompt, duration_seconds = 2,
|
| 338 |
guidance_scale = 1, steps = 4,
|
| 339 |
+
seed = 42, randomize_seed = False, remove_bg = False,
|
| 340 |
progress=gr.Progress(track_tqdm=True)):
|
| 341 |
"""
|
| 342 |
Generate a video from gallery images using the selected mode.
|
|
|
|
| 353 |
steps (int): Number of inference steps
|
| 354 |
seed (int): Random seed for reproducible results
|
| 355 |
randomize_seed (bool): Whether to use a random seed
|
| 356 |
+
remove_bg (bool): Whether to remove background from images (Ref2V mode only)
|
| 357 |
progress (gr.Progress): Gradio progress tracker
|
| 358 |
|
| 359 |
Returns:
|
|
|
|
| 362 |
if gallery_images is None or len(gallery_images) == 0:
|
| 363 |
raise gr.Error("Please upload at least one image to the gallery.")
|
| 364 |
else:
|
| 365 |
+
# Process images: remove background if requested (Ref2V mode only), then remove alpha channels
|
| 366 |
+
processed_images = []
|
| 367 |
+
for img in gallery_images:
|
| 368 |
+
image = img[0] # Extract PIL image from gallery format
|
| 369 |
+
|
| 370 |
+
# Apply background removal only for Ref2V mode if checkbox is checked
|
| 371 |
+
if mode == "Ref2V" and remove_bg:
|
| 372 |
+
image = remove_background_from_image(image)
|
| 373 |
+
|
| 374 |
+
# Always remove alpha channels to ensure RGB format
|
| 375 |
+
image = remove_alpha_channel(image)
|
| 376 |
+
processed_images.append(image)
|
| 377 |
+
|
| 378 |
+
gallery_images = processed_images
|
| 379 |
|
| 380 |
if mode == "FLF2V" and len(gallery_images) >= 2:
|
| 381 |
gallery_images = gallery_images[:2]
|
|
|
|
| 485 |
|
| 486 |
with gr.Accordion("Advanced Settings", open=False):
|
| 487 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 488 |
+
|
| 489 |
+
# Background removal checkbox (only for Ref2V mode)
|
| 490 |
+
remove_bg_checkbox = gr.Checkbox(
|
| 491 |
+
label="Remove Background (Ref2V mode only)",
|
| 492 |
+
value=False,
|
| 493 |
+
info="Automatically remove background from input images when using Ref2V mode"
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 497 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 498 |
with gr.Row():
|
|
|
|
| 506 |
with gr.Column():
|
| 507 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 508 |
|
| 509 |
+
# Function to update checkbox visibility based on mode
|
| 510 |
+
def update_bg_removal_visibility(mode):
|
| 511 |
+
return gr.update(visible=(mode == "Ref2V"))
|
| 512 |
+
|
| 513 |
# Update prompt when mode changes
|
| 514 |
mode_radio.change(
|
| 515 |
fn=update_prompt_from_mode,
|
|
|
|
| 517 |
outputs=[prompt_input]
|
| 518 |
)
|
| 519 |
|
| 520 |
+
# Update background removal checkbox visibility when mode changes
|
| 521 |
+
mode_radio.change(
|
| 522 |
+
fn=update_bg_removal_visibility,
|
| 523 |
+
inputs=[mode_radio],
|
| 524 |
+
outputs=[remove_bg_checkbox]
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
# Update dimensions when gallery changes
|
| 528 |
gallery_component.change(
|
| 529 |
fn=handle_gallery_upload_for_dims_wan,
|
|
|
|
| 534 |
ui_inputs = [
|
| 535 |
gallery_component, mode_radio, prompt_input, height_input, width_input,
|
| 536 |
negative_prompt_input, duration_seconds_input,
|
| 537 |
+
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox, remove_bg_checkbox
|
| 538 |
]
|
| 539 |
|
| 540 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
briarmbg.py
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RMBG1.4 (diffusers implementation)
|
| 2 |
+
# Found on huggingface space of several projects
|
| 3 |
+
# Not sure which project is the source of this file
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class REBNCONV(nn.Module):
|
| 12 |
+
def __init__(self, in_ch=3, out_ch=3, dirate=1, stride=1):
|
| 13 |
+
super(REBNCONV, self).__init__()
|
| 14 |
+
|
| 15 |
+
self.conv_s1 = nn.Conv2d(
|
| 16 |
+
in_ch, out_ch, 3, padding=1 * dirate, dilation=1 * dirate, stride=stride
|
| 17 |
+
)
|
| 18 |
+
self.bn_s1 = nn.BatchNorm2d(out_ch)
|
| 19 |
+
self.relu_s1 = nn.ReLU(inplace=True)
|
| 20 |
+
|
| 21 |
+
def forward(self, x):
|
| 22 |
+
hx = x
|
| 23 |
+
xout = self.relu_s1(self.bn_s1(self.conv_s1(hx)))
|
| 24 |
+
|
| 25 |
+
return xout
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _upsample_like(src, tar):
|
| 29 |
+
src = F.interpolate(src, size=tar.shape[2:], mode="bilinear")
|
| 30 |
+
return src
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
### RSU-7 ###
|
| 34 |
+
class RSU7(nn.Module):
|
| 35 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3, img_size=512):
|
| 36 |
+
super(RSU7, self).__init__()
|
| 37 |
+
|
| 38 |
+
self.in_ch = in_ch
|
| 39 |
+
self.mid_ch = mid_ch
|
| 40 |
+
self.out_ch = out_ch
|
| 41 |
+
|
| 42 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) ## 1 -> 1/2
|
| 43 |
+
|
| 44 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
| 45 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 46 |
+
|
| 47 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 48 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 49 |
+
|
| 50 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 51 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 52 |
+
|
| 53 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 54 |
+
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 55 |
+
|
| 56 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 57 |
+
self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 58 |
+
|
| 59 |
+
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 60 |
+
|
| 61 |
+
self.rebnconv7 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
| 62 |
+
|
| 63 |
+
self.rebnconv6d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 64 |
+
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 65 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 66 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 67 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 68 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
| 69 |
+
|
| 70 |
+
def forward(self, x):
|
| 71 |
+
b, c, h, w = x.shape
|
| 72 |
+
|
| 73 |
+
hx = x
|
| 74 |
+
hxin = self.rebnconvin(hx)
|
| 75 |
+
|
| 76 |
+
hx1 = self.rebnconv1(hxin)
|
| 77 |
+
hx = self.pool1(hx1)
|
| 78 |
+
|
| 79 |
+
hx2 = self.rebnconv2(hx)
|
| 80 |
+
hx = self.pool2(hx2)
|
| 81 |
+
|
| 82 |
+
hx3 = self.rebnconv3(hx)
|
| 83 |
+
hx = self.pool3(hx3)
|
| 84 |
+
|
| 85 |
+
hx4 = self.rebnconv4(hx)
|
| 86 |
+
hx = self.pool4(hx4)
|
| 87 |
+
|
| 88 |
+
hx5 = self.rebnconv5(hx)
|
| 89 |
+
hx = self.pool5(hx5)
|
| 90 |
+
|
| 91 |
+
hx6 = self.rebnconv6(hx)
|
| 92 |
+
|
| 93 |
+
hx7 = self.rebnconv7(hx6)
|
| 94 |
+
|
| 95 |
+
hx6d = self.rebnconv6d(torch.cat((hx7, hx6), 1))
|
| 96 |
+
hx6dup = _upsample_like(hx6d, hx5)
|
| 97 |
+
|
| 98 |
+
hx5d = self.rebnconv5d(torch.cat((hx6dup, hx5), 1))
|
| 99 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
| 100 |
+
|
| 101 |
+
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1))
|
| 102 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
| 103 |
+
|
| 104 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
| 105 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
| 106 |
+
|
| 107 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
| 108 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
| 109 |
+
|
| 110 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
| 111 |
+
|
| 112 |
+
return hx1d + hxin
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
### RSU-6 ###
|
| 116 |
+
class RSU6(nn.Module):
|
| 117 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
| 118 |
+
super(RSU6, self).__init__()
|
| 119 |
+
|
| 120 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
| 121 |
+
|
| 122 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
| 123 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 124 |
+
|
| 125 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 126 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 127 |
+
|
| 128 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 129 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 130 |
+
|
| 131 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 132 |
+
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 133 |
+
|
| 134 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 135 |
+
|
| 136 |
+
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
| 137 |
+
|
| 138 |
+
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 139 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 140 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 141 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 142 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
| 143 |
+
|
| 144 |
+
def forward(self, x):
|
| 145 |
+
hx = x
|
| 146 |
+
|
| 147 |
+
hxin = self.rebnconvin(hx)
|
| 148 |
+
|
| 149 |
+
hx1 = self.rebnconv1(hxin)
|
| 150 |
+
hx = self.pool1(hx1)
|
| 151 |
+
|
| 152 |
+
hx2 = self.rebnconv2(hx)
|
| 153 |
+
hx = self.pool2(hx2)
|
| 154 |
+
|
| 155 |
+
hx3 = self.rebnconv3(hx)
|
| 156 |
+
hx = self.pool3(hx3)
|
| 157 |
+
|
| 158 |
+
hx4 = self.rebnconv4(hx)
|
| 159 |
+
hx = self.pool4(hx4)
|
| 160 |
+
|
| 161 |
+
hx5 = self.rebnconv5(hx)
|
| 162 |
+
|
| 163 |
+
hx6 = self.rebnconv6(hx5)
|
| 164 |
+
|
| 165 |
+
hx5d = self.rebnconv5d(torch.cat((hx6, hx5), 1))
|
| 166 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
| 167 |
+
|
| 168 |
+
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1))
|
| 169 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
| 170 |
+
|
| 171 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
| 172 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
| 173 |
+
|
| 174 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
| 175 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
| 176 |
+
|
| 177 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
| 178 |
+
|
| 179 |
+
return hx1d + hxin
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
### RSU-5 ###
|
| 183 |
+
class RSU5(nn.Module):
|
| 184 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
| 185 |
+
super(RSU5, self).__init__()
|
| 186 |
+
|
| 187 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
| 188 |
+
|
| 189 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
| 190 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 191 |
+
|
| 192 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 193 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 194 |
+
|
| 195 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 196 |
+
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 197 |
+
|
| 198 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 199 |
+
|
| 200 |
+
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
| 201 |
+
|
| 202 |
+
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 203 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 204 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 205 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
| 206 |
+
|
| 207 |
+
def forward(self, x):
|
| 208 |
+
hx = x
|
| 209 |
+
|
| 210 |
+
hxin = self.rebnconvin(hx)
|
| 211 |
+
|
| 212 |
+
hx1 = self.rebnconv1(hxin)
|
| 213 |
+
hx = self.pool1(hx1)
|
| 214 |
+
|
| 215 |
+
hx2 = self.rebnconv2(hx)
|
| 216 |
+
hx = self.pool2(hx2)
|
| 217 |
+
|
| 218 |
+
hx3 = self.rebnconv3(hx)
|
| 219 |
+
hx = self.pool3(hx3)
|
| 220 |
+
|
| 221 |
+
hx4 = self.rebnconv4(hx)
|
| 222 |
+
|
| 223 |
+
hx5 = self.rebnconv5(hx4)
|
| 224 |
+
|
| 225 |
+
hx4d = self.rebnconv4d(torch.cat((hx5, hx4), 1))
|
| 226 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
| 227 |
+
|
| 228 |
+
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1))
|
| 229 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
| 230 |
+
|
| 231 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
| 232 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
| 233 |
+
|
| 234 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
| 235 |
+
|
| 236 |
+
return hx1d + hxin
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
### RSU-4 ###
|
| 240 |
+
class RSU4(nn.Module):
|
| 241 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
| 242 |
+
super(RSU4, self).__init__()
|
| 243 |
+
|
| 244 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
| 245 |
+
|
| 246 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
| 247 |
+
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 248 |
+
|
| 249 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 250 |
+
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 251 |
+
|
| 252 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1)
|
| 253 |
+
|
| 254 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
| 255 |
+
|
| 256 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 257 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1)
|
| 258 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
| 259 |
+
|
| 260 |
+
def forward(self, x):
|
| 261 |
+
hx = x
|
| 262 |
+
|
| 263 |
+
hxin = self.rebnconvin(hx)
|
| 264 |
+
|
| 265 |
+
hx1 = self.rebnconv1(hxin)
|
| 266 |
+
hx = self.pool1(hx1)
|
| 267 |
+
|
| 268 |
+
hx2 = self.rebnconv2(hx)
|
| 269 |
+
hx = self.pool2(hx2)
|
| 270 |
+
|
| 271 |
+
hx3 = self.rebnconv3(hx)
|
| 272 |
+
|
| 273 |
+
hx4 = self.rebnconv4(hx3)
|
| 274 |
+
|
| 275 |
+
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1))
|
| 276 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
| 277 |
+
|
| 278 |
+
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1))
|
| 279 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
| 280 |
+
|
| 281 |
+
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1))
|
| 282 |
+
|
| 283 |
+
return hx1d + hxin
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
### RSU-4F ###
|
| 287 |
+
class RSU4F(nn.Module):
|
| 288 |
+
def __init__(self, in_ch=3, mid_ch=12, out_ch=3):
|
| 289 |
+
super(RSU4F, self).__init__()
|
| 290 |
+
|
| 291 |
+
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1)
|
| 292 |
+
|
| 293 |
+
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1)
|
| 294 |
+
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=2)
|
| 295 |
+
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=4)
|
| 296 |
+
|
| 297 |
+
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=8)
|
| 298 |
+
|
| 299 |
+
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=4)
|
| 300 |
+
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=2)
|
| 301 |
+
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1)
|
| 302 |
+
|
| 303 |
+
def forward(self, x):
|
| 304 |
+
hx = x
|
| 305 |
+
|
| 306 |
+
hxin = self.rebnconvin(hx)
|
| 307 |
+
|
| 308 |
+
hx1 = self.rebnconv1(hxin)
|
| 309 |
+
hx2 = self.rebnconv2(hx1)
|
| 310 |
+
hx3 = self.rebnconv3(hx2)
|
| 311 |
+
|
| 312 |
+
hx4 = self.rebnconv4(hx3)
|
| 313 |
+
|
| 314 |
+
hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1))
|
| 315 |
+
hx2d = self.rebnconv2d(torch.cat((hx3d, hx2), 1))
|
| 316 |
+
hx1d = self.rebnconv1d(torch.cat((hx2d, hx1), 1))
|
| 317 |
+
|
| 318 |
+
return hx1d + hxin
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
class myrebnconv(nn.Module):
|
| 322 |
+
def __init__(
|
| 323 |
+
self,
|
| 324 |
+
in_ch=3,
|
| 325 |
+
out_ch=1,
|
| 326 |
+
kernel_size=3,
|
| 327 |
+
stride=1,
|
| 328 |
+
padding=1,
|
| 329 |
+
dilation=1,
|
| 330 |
+
groups=1,
|
| 331 |
+
):
|
| 332 |
+
super(myrebnconv, self).__init__()
|
| 333 |
+
|
| 334 |
+
self.conv = nn.Conv2d(
|
| 335 |
+
in_ch,
|
| 336 |
+
out_ch,
|
| 337 |
+
kernel_size=kernel_size,
|
| 338 |
+
stride=stride,
|
| 339 |
+
padding=padding,
|
| 340 |
+
dilation=dilation,
|
| 341 |
+
groups=groups,
|
| 342 |
+
)
|
| 343 |
+
self.bn = nn.BatchNorm2d(out_ch)
|
| 344 |
+
self.rl = nn.ReLU(inplace=True)
|
| 345 |
+
|
| 346 |
+
def forward(self, x):
|
| 347 |
+
return self.rl(self.bn(self.conv(x)))
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
class BriaRMBG(nn.Module, PyTorchModelHubMixin):
|
| 351 |
+
def __init__(self, config: dict = {"in_ch": 3, "out_ch": 1}):
|
| 352 |
+
super(BriaRMBG, self).__init__()
|
| 353 |
+
in_ch = config["in_ch"]
|
| 354 |
+
out_ch = config["out_ch"]
|
| 355 |
+
self.conv_in = nn.Conv2d(in_ch, 64, 3, stride=2, padding=1)
|
| 356 |
+
self.pool_in = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 357 |
+
|
| 358 |
+
self.stage1 = RSU7(64, 32, 64)
|
| 359 |
+
self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 360 |
+
|
| 361 |
+
self.stage2 = RSU6(64, 32, 128)
|
| 362 |
+
self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 363 |
+
|
| 364 |
+
self.stage3 = RSU5(128, 64, 256)
|
| 365 |
+
self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 366 |
+
|
| 367 |
+
self.stage4 = RSU4(256, 128, 512)
|
| 368 |
+
self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 369 |
+
|
| 370 |
+
self.stage5 = RSU4F(512, 256, 512)
|
| 371 |
+
self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
|
| 372 |
+
|
| 373 |
+
self.stage6 = RSU4F(512, 256, 512)
|
| 374 |
+
|
| 375 |
+
# decoder
|
| 376 |
+
self.stage5d = RSU4F(1024, 256, 512)
|
| 377 |
+
self.stage4d = RSU4(1024, 128, 256)
|
| 378 |
+
self.stage3d = RSU5(512, 64, 128)
|
| 379 |
+
self.stage2d = RSU6(256, 32, 64)
|
| 380 |
+
self.stage1d = RSU7(128, 16, 64)
|
| 381 |
+
|
| 382 |
+
self.side1 = nn.Conv2d(64, out_ch, 3, padding=1)
|
| 383 |
+
self.side2 = nn.Conv2d(64, out_ch, 3, padding=1)
|
| 384 |
+
self.side3 = nn.Conv2d(128, out_ch, 3, padding=1)
|
| 385 |
+
self.side4 = nn.Conv2d(256, out_ch, 3, padding=1)
|
| 386 |
+
self.side5 = nn.Conv2d(512, out_ch, 3, padding=1)
|
| 387 |
+
self.side6 = nn.Conv2d(512, out_ch, 3, padding=1)
|
| 388 |
+
|
| 389 |
+
# self.outconv = nn.Conv2d(6*out_ch,out_ch,1)
|
| 390 |
+
|
| 391 |
+
def forward(self, x):
|
| 392 |
+
hx = x
|
| 393 |
+
|
| 394 |
+
hxin = self.conv_in(hx)
|
| 395 |
+
# hx = self.pool_in(hxin)
|
| 396 |
+
|
| 397 |
+
# stage 1
|
| 398 |
+
hx1 = self.stage1(hxin)
|
| 399 |
+
hx = self.pool12(hx1)
|
| 400 |
+
|
| 401 |
+
# stage 2
|
| 402 |
+
hx2 = self.stage2(hx)
|
| 403 |
+
hx = self.pool23(hx2)
|
| 404 |
+
|
| 405 |
+
# stage 3
|
| 406 |
+
hx3 = self.stage3(hx)
|
| 407 |
+
hx = self.pool34(hx3)
|
| 408 |
+
|
| 409 |
+
# stage 4
|
| 410 |
+
hx4 = self.stage4(hx)
|
| 411 |
+
hx = self.pool45(hx4)
|
| 412 |
+
|
| 413 |
+
# stage 5
|
| 414 |
+
hx5 = self.stage5(hx)
|
| 415 |
+
hx = self.pool56(hx5)
|
| 416 |
+
|
| 417 |
+
# stage 6
|
| 418 |
+
hx6 = self.stage6(hx)
|
| 419 |
+
hx6up = _upsample_like(hx6, hx5)
|
| 420 |
+
|
| 421 |
+
# -------------------- decoder --------------------
|
| 422 |
+
hx5d = self.stage5d(torch.cat((hx6up, hx5), 1))
|
| 423 |
+
hx5dup = _upsample_like(hx5d, hx4)
|
| 424 |
+
|
| 425 |
+
hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1))
|
| 426 |
+
hx4dup = _upsample_like(hx4d, hx3)
|
| 427 |
+
|
| 428 |
+
hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1))
|
| 429 |
+
hx3dup = _upsample_like(hx3d, hx2)
|
| 430 |
+
|
| 431 |
+
hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1))
|
| 432 |
+
hx2dup = _upsample_like(hx2d, hx1)
|
| 433 |
+
|
| 434 |
+
hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1))
|
| 435 |
+
|
| 436 |
+
# side output
|
| 437 |
+
d1 = self.side1(hx1d)
|
| 438 |
+
d1 = _upsample_like(d1, x)
|
| 439 |
+
|
| 440 |
+
d2 = self.side2(hx2d)
|
| 441 |
+
d2 = _upsample_like(d2, x)
|
| 442 |
+
|
| 443 |
+
d3 = self.side3(hx3d)
|
| 444 |
+
d3 = _upsample_like(d3, x)
|
| 445 |
+
|
| 446 |
+
d4 = self.side4(hx4d)
|
| 447 |
+
d4 = _upsample_like(d4, x)
|
| 448 |
+
|
| 449 |
+
d5 = self.side5(hx5d)
|
| 450 |
+
d5 = _upsample_like(d5, x)
|
| 451 |
+
|
| 452 |
+
d6 = self.side6(hx6)
|
| 453 |
+
d6 = _upsample_like(d6, x)
|
| 454 |
+
|
| 455 |
+
return [
|
| 456 |
+
F.sigmoid(d1),
|
| 457 |
+
F.sigmoid(d2),
|
| 458 |
+
F.sigmoid(d3),
|
| 459 |
+
F.sigmoid(d4),
|
| 460 |
+
F.sigmoid(d5),
|
| 461 |
+
F.sigmoid(d6),
|
| 462 |
+
], [hx1d, hx2d, hx3d, hx4d, hx5d, hx6]
|