Image Segmentation
BiRefNet
Safetensors
background-removal
mask-generation
Dichotomous Image Segmentation
Camouflaged Object Detection
Salient Object Detection
pytorch_model_hub_mixin
model_hub_mixin
custom_code
Run this model with HF Jobs
Run ZhengPeng7/BiRefNet_512x512 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/ZhengPeng7/BiRefNet_512x512. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/ZhengPeng7/BiRefNet_512x512 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use ZhengPeng7/BiRefNet_512x512 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- BiRefNet
How to use ZhengPeng7/BiRefNet_512x512 with BiRefNet:
# Option 1: use with transformers from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet_512x512", trust_remote_code=True)# Option 2: use with BiRefNet # Install from https://github.com/ZhengPeng7/BiRefNet from models.birefnet import BiRefNet model = BiRefNet.from_pretrained("ZhengPeng7/BiRefNet_512x512") - Notebooks
- Google Colab
- Kaggle
Commit ·
e94d116
1
Parent(s): 2424c79
Upgrade the ways of importing timm modules (>=1.0.23).
Browse files165 unmodified lines166167168169170171172173373 unmodified lines5475485495505515525533 unmodified lines5575585595605615625631 unmodified line5655665675685695705711 unmodified line5735745755765775785791 unmodified line5815825835845855865872 unmodified lines59059159259359459559617 unmodified lines614615616617618619620574 unmodified lines119511961197119811991200120150 unmodified lines12521253125412551256125712581259126012611262126312643 unmodified lines126812691270127112721273127417 unmodified lines1292129312941295129612971298191 unmodified lines1490149114921493149414951496644 unmodified lines2141214221432144214521462147165 unmodified linesimport torch.nn as nnfrom functools import partialfrom timm.models.layers import DropPath, to_2tuple, trunc_normal_from timm.models.registry import register_modelimport math373 unmodified linesreturn out_dict## @register_modelclass pvt_v2_b0(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b0, self).__init__(3 unmodified lines## @register_modelclass pvt_v2_b1(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b1, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[2, 2, 2, 2], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1)## @register_modelclass pvt_v2_b2(PyramidVisionTransformerImpr):def __init__(self, in_channels=3, **kwargs):super(pvt_v2_b2, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1, in_channels=in_channels)## @register_modelclass pvt_v2_b3(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b3, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 18, 3], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1)## @register_modelclass pvt_v2_b4(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b4, self).__init__(2 unmodified linesdrop_rate=0.0, drop_path_rate=0.1)## @register_modelclass pvt_v2_b5(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b5, self).__init__(17 unmodified linesimport torch.nn.functional as Fimport torch.utils.checkpoint as checkpointimport numpy as npfrom timm.models.layers import DropPath, to_2tuple, trunc_normal_# from config import Config574 unmodified lines# interpolate the position embedding to the corresponding sizeabsolute_pos_embed = F.interpolate(self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic')x = (x + absolute_pos_embed) # B Wh*Ww Couts = []#x.contiguous()]x = x.flatten(2).transpose(1, 2)x = self.pos_drop(x)50 unmodified linesbias=False):super(DeformableConv2d, self).__init__()assert type(kernel_size) == tuple or type(kernel_size) == intkernel_size = kernel_size if type(kernel_size) == tuple else (kernel_size, kernel_size)self.stride = stride if type(stride) == tuple else (stride, stride)self.padding = paddingself.offset_conv = nn.Conv2d(in_channels,2 * kernel_size[0] * kernel_size[1],kernel_size=kernel_size,3 unmodified linesnn.init.constant_(self.offset_conv.weight, 0.)nn.init.constant_(self.offset_conv.bias, 0.)self.modulator_conv = nn.Conv2d(in_channels,1 * kernel_size[0] * kernel_size[1],kernel_size=kernel_size,17 unmodified linesoffset = self.offset_conv(x)#.clamp(-max_offset, max_offset)modulator = 2. * torch.sigmoid(self.modulator_conv(x))x = deform_conv2d(input=x,offset=offset,191 unmodified linesself.conv_out = nn.Conv2d(inter_channels, out_channels, 3, 1, padding=1)self.bn_out = nn.BatchNorm2d(out_channels) if config.batch_size > 1 else nn.Identity()self.conv_resi = nn.Conv2d(in_channels, out_channels, 1, 1, 0)def forward(self, x):644 unmodified linesself.gdt_convs_pred_4 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_pred_3 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_pred_2 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_4 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_3 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_2 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))165 unmodified lines166167168169170171172173373 unmodified lines5475485495505515523 unmodified lines5565575585595605611 unmodified line5635645655665675681 unmodified line5705715725735745751 unmodified line5775785795805815822 unmodified lines58558658758858959017 unmodified lines608609610611612613614574 unmodified lines118911901191119211931194119550 unmodified lines12461247124812491250125112521253125412551256125712583 unmodified lines126212631264126512661267126817 unmodified lines1286128712881289129012911292191 unmodified lines1484148514861487148814891490644 unmodified lines2135213621372138213921402141165 unmodified linesimport torch.nn as nnfrom functools import partialfrom timm.layers import DropPath, to_2tuple, trunc_normal_import math373 unmodified linesreturn out_dictclass pvt_v2_b0(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b0, self).__init__(3 unmodified linesclass pvt_v2_b1(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b1, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[2, 2, 2, 2], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1)class pvt_v2_b2(PyramidVisionTransformerImpr):def __init__(self, in_channels=3, **kwargs):super(pvt_v2_b2, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1, in_channels=in_channels)class pvt_v2_b3(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b3, self).__init__(1 unmodified lineqkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 18, 3], sr_ratios=[8, 4, 2, 1],drop_rate=0.0, drop_path_rate=0.1)class pvt_v2_b4(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b4, self).__init__(2 unmodified linesdrop_rate=0.0, drop_path_rate=0.1)class pvt_v2_b5(PyramidVisionTransformerImpr):def __init__(self, **kwargs):super(pvt_v2_b5, self).__init__(17 unmodified linesimport torch.nn.functional as Fimport torch.utils.checkpoint as checkpointimport numpy as npfrom timm.layers import DropPath, to_2tuple, trunc_normal_# from config import Config574 unmodified lines# interpolate the position embedding to the corresponding sizeabsolute_pos_embed = F.interpolate(self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic')x = (x + absolute_pos_embed) # B Wh*Ww Couts = []#x.contiguous()]x = x.flatten(2).transpose(1, 2)x = self.pos_drop(x)50 unmodified linesbias=False):super(DeformableConv2d, self).__init__()assert type(kernel_size) == tuple or type(kernel_size) == intkernel_size = kernel_size if type(kernel_size) == tuple else (kernel_size, kernel_size)self.stride = stride if type(stride) == tuple else (stride, stride)self.padding = paddingself.offset_conv = nn.Conv2d(in_channels,2 * kernel_size[0] * kernel_size[1],kernel_size=kernel_size,3 unmodified linesnn.init.constant_(self.offset_conv.weight, 0.)nn.init.constant_(self.offset_conv.bias, 0.)self.modulator_conv = nn.Conv2d(in_channels,1 * kernel_size[0] * kernel_size[1],kernel_size=kernel_size,17 unmodified linesoffset = self.offset_conv(x)#.clamp(-max_offset, max_offset)modulator = 2. * torch.sigmoid(self.modulator_conv(x))x = deform_conv2d(input=x,offset=offset,191 unmodified linesself.conv_out = nn.Conv2d(inter_channels, out_channels, 3, 1, padding=1)self.bn_out = nn.BatchNorm2d(out_channels) if config.batch_size > 1 else nn.Identity()self.conv_resi = nn.Conv2d(in_channels, out_channels, 1, 1, 0)def forward(self, x):644 unmodified linesself.gdt_convs_pred_4 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_pred_3 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_pred_2 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_4 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_3 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))self.gdt_convs_attn_2 = nn.Sequential(nn.Conv2d(_N, 1, 1, 1, 0))
birefnet.py+10-16