Spaces:
Running
on
Zero
Running
on
Zero
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
from huggingface_hub import PyTorchModelHubMixin | |
import numpy as np | |
from PIL import Image | |
class REBNCONV(nn.Module): | |
def __init__(self, in_ch=3, out_ch=3, dirate=1, stride=1): | |
super(REBNCONV, self).__init__() | |
self.conv_s1 = nn.Conv2d( | |
in_ch, out_ch, 3, padding=1 * dirate, dilation=1 * dirate, stride=stride | |
) | |
self.bn_s1 = nn.BatchNorm2d(out_ch) | |
self.relu_s1 = nn.ReLU(inplace=True) | |
def forward(self, x): | |
hx = x | |
xout = self.relu_s1(self.bn_s1(self.conv_s1(hx))) | |
return xout | |
def _upsample_like(src, tar): | |
src = F.interpolate(src, size=tar.shape[2:], mode="bilinear") | |
return src | |
class RSU7(nn.Module): | |
def __init__(self, in_ch=3, mid_ch=12, out_ch=3, img_size=512): | |
super(RSU7, self).__init__() | |
self.in_ch = in_ch | |
self.mid_ch = mid_ch | |
self.out_ch = out_ch | |
self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) | |
self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) | |
self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) | |
self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) | |
self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) | |
self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) | |
self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) | |
self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) | |
self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) | |
self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1) | |
self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True) | |
self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=1) | |
self.rebnconv7 = REBNCONV(mid_ch, mid_ch, dirate=2) | |
self.rebnconv6d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) | |
self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) | |
self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) | |
self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) | |
self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) | |
self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) | |
def forward(self, x): | |
hx = x | |
hxin = self.rebnconvin(hx) | |
hx1 = self.rebnconv1(hxin) | |
hx = self.pool1(hx1) | |
hx2 = self.rebnconv2(hx) | |
hx = self.pool2(hx2) | |
hx3 = self.rebnconv3(hx) | |
hx = self.pool3(hx3) | |
hx4 = self.rebnconv4(hx) | |
hx = self.pool4(hx4) | |
hx5 = self.rebnconv5(hx) | |
hx = self.pool5(hx5) | |
hx6 = self.rebnconv6(hx) | |
hx7 = self.rebnconv7(hx6) | |
hx6d = self.rebnconv6d(torch.cat((hx7, hx6), 1)) | |
hx6dup = _upsample_like(hx6d, hx5) | |
hx5d = self.rebnconv5d(torch.cat((hx6dup, hx5), 1)) | |
hx5dup = _upsample_like(hx5d, hx4) | |
hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1)) | |
hx4dup = _upsample_like(hx4d, hx3) | |
hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) | |
hx3dup = _upsample_like(hx3d, hx2) | |
hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) | |
hx2dup = _upsample_like(hx2d, hx1) | |
hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1)) | |
return hx1d + hxin | |
class BriaRMBG(nn.Module, PyTorchModelHubMixin): | |
def __init__(self, config: dict = {"in_ch": 3, "out_ch": 1}): | |
super(BriaRMBG, self).__init__() | |
self.config = config | |
# Simplified architecture for fallback | |
self.stage1 = RSU7(3, 32, 64) | |
self.stage2 = RSU7(64, 32, 128) | |
self.stage3 = RSU7(128, 64, 256) | |
self.stage4 = RSU7(256, 128, 512) | |
# Decoder | |
self.stage4d = RSU7(1024, 128, 256) | |
self.stage3d = RSU7(512, 64, 128) | |
self.stage2d = RSU7(256, 32, 64) | |
self.stage1d = RSU7(128, 16, 64) | |
self.side1 = nn.Conv2d(64, 1, 3, padding=1) | |
self.side2 = nn.Conv2d(64, 1, 3, padding=1) | |
self.side3 = nn.Conv2d(128, 1, 3, padding=1) | |
self.side4 = nn.Conv2d(256, 1, 3, padding=1) | |
self.side5 = nn.Conv2d(512, 1, 3, padding=1) | |
self.side6 = nn.Conv2d(256, 1, 3, padding=1) | |
self.outconv = nn.Conv2d(6, 1, 1) | |
def forward(self, x): | |
hx = x | |
# Encoder | |
hx1 = self.stage1(hx) | |
hx = F.max_pool2d(hx1, 2, stride=2, ceil_mode=True) | |
hx2 = self.stage2(hx) | |
hx = F.max_pool2d(hx2, 2, stride=2, ceil_mode=True) | |
hx3 = self.stage3(hx) | |
hx = F.max_pool2d(hx3, 2, stride=2, ceil_mode=True) | |
hx4 = self.stage4(hx) | |
hx = F.max_pool2d(hx4, 2, stride=2, ceil_mode=True) | |
# Decoder | |
hx4d = self.stage4d(torch.cat((hx, hx4), 1)) | |
hx4dup = _upsample_like(hx4d, hx3) | |
hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1)) | |
hx3dup = _upsample_like(hx3d, hx2) | |
hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1)) | |
hx2dup = _upsample_like(hx2d, hx1) | |
hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1)) | |
# Side outputs | |
side1 = self.side1(hx1d) | |
side2 = _upsample_like(self.side2(hx2d), side1) | |
side3 = _upsample_like(self.side3(hx3d), side1) | |
side4 = _upsample_like(self.side4(hx4d), side1) | |
side5 = _upsample_like(self.side5(hx), side1) | |
side6 = _upsample_like(self.side6(hx4d), side1) | |
# Fusion | |
out = self.outconv(torch.cat((side1, side2, side3, side4, side5, side6), 1)) | |
return torch.sigmoid(out) | |
def simple_background_removal(image): | |
""" | |
Simple fallback background removal using edge detection and thresholding | |
""" | |
if isinstance(image, np.ndarray): | |
img = image | |
else: | |
img = np.array(image) | |
# Convert to grayscale | |
gray = np.mean(img, axis=2) | |
# Simple edge detection | |
from scipy import ndimage | |
edges = ndimage.sobel(gray) | |
# Create a simple mask based on intensity | |
mask = np.ones_like(gray) | |
# Simple thresholding - assume foreground is in center and has more edges | |
h, w = gray.shape | |
center_mask = np.zeros_like(gray) | |
center_mask[h//4:3*h//4, w//4:3*w//4] = 1 | |
# Combine edge information with center bias | |
mask = (edges > np.percentile(edges, 70)) * center_mask | |
mask = ndimage.binary_fill_holes(mask) | |
mask = ndimage.gaussian_filter(mask.astype(float), sigma=2) | |
return mask |