Image Segmentation
BiRefNet
Safetensors
Transformers
background-removal
mask-generation
Dichotomous Image Segmentation
Camouflaged Object Detection
Salient Object Detection
pytorch_model_hub_mixin
model_hub_mixin
custom_code
Instructions to use ZhengPeng7/BiRefNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- BiRefNet
How to use ZhengPeng7/BiRefNet with BiRefNet:
# Option 1: use with transformers from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet", 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") - Transformers
How to use ZhengPeng7/BiRefNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="ZhengPeng7/BiRefNet", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForImageSegmentation model = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download BiRefNet_github/evaluation/evaluate.py from ZhengPeng7/BiRefNet: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/ZhengPeng7/BiRefNet/resolve/f10d16f80ea9b73cd2010793a90bc779ec1f45e1/BiRefNet_github/evaluation/evaluate.py
- Command line
-
hf download hf://ZhengPeng7/BiRefNet@f10d16f80ea9b73cd2010793a90bc779ec1f45e1/BiRefNet_github/evaluation/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/ZhengPeng7/BiRefNet/resolve/f10d16f80ea9b73cd2010793a90bc779ec1f45e1/BiRefNet_github/evaluation/evaluate.py
2.59 kB
| import os | |
| import prettytable as pt | |
| from evaluation.metrics import evaluator | |
| from config import Config | |
| config = Config() | |
| def evaluate(pred_dir, method, testset, only_S_MAE=False, epoch=0): | |
| filename = os.path.join('evaluation', 'eval-{}.txt'.format(method)) | |
| if os.path.exists(filename): | |
| id_suffix = 1 | |
| filename = filename.rstrip('.txt') + '_{}.txt'.format(id_suffix) | |
| while os.path.exists(filename): | |
| id_suffix += 1 | |
| filename = filename.replace('_{}.txt'.format(id_suffix-1), '_{}.txt'.format(id_suffix)) | |
| gt_paths = sorted([ | |
| os.path.join(config.data_root_dir, config.task, testset, 'gt', p) | |
| for p in os.listdir(os.path.join(config.data_root_dir, config.task, testset, 'gt')) | |
| ]) | |
| pred_paths = sorted([os.path.join(pred_dir, method, testset, p) for p in os.listdir(os.path.join(pred_dir, method, testset))]) | |
| with open(filename, 'a+') as file_to_write: | |
| tb = pt.PrettyTable() | |
| field_names = [ | |
| "Dataset", "Method", "maxFm", "wFmeasure", 'MAE', "Smeasure", "meanEm", "maxEm", "meanFm", | |
| "adpEm", "adpFm", 'HCE' | |
| ] | |
| tb.field_names = [name for name in field_names if not only_S_MAE or all(metric not in name for metric in ['Em', 'Fm'])] | |
| em, sm, fm, mae, wfm, hce = evaluator( | |
| gt_paths=gt_paths[:], | |
| pred_paths=pred_paths[:], | |
| metrics=['S', 'MAE', 'E', 'F', 'HCE'][:10*(not only_S_MAE) + 2], # , 'WF' | |
| verbose=config.verbose_eval, | |
| ) | |
| e_max, e_mean, e_adp = em['curve'].max(), em['curve'].mean(), em['adp'].mean() | |
| f_max, f_mean, f_wfm, f_adp = fm['curve'].max(), fm['curve'].mean(), wfm, fm['adp'] | |
| tb.add_row( | |
| [ | |
| method+str(epoch), testset, f_max.round(3), f_wfm.round(3), mae.round(3), sm.round(3), | |
| e_mean.round(3), e_max.round(3), f_mean.round(3), em['adp'].round(3), f_adp.round(3), hce.round(3) | |
| ] if not only_S_MAE else [method, testset, mae.round(3), sm.round(3)] | |
| ) | |
| print(tb) | |
| file_to_write.write(str(tb).replace('+', '|')+'\n') | |
| file_to_write.close() | |
| return {'e_max': e_max, 'e_mean': e_mean, 'e_adp': e_adp, 'sm': sm, 'mae': mae, 'f_max': f_max, 'f_mean': f_mean, 'f_wfm': f_wfm, 'f_adp': f_adp, 'hce': hce} | |
| def main(): | |
| only_S_MAE = False | |
| pred_dir = '.' | |
| method = 'tmp_val' | |
| testsets = 'DIS-VD+DIS-TE1' | |
| for testset in testsets.split('+'): | |
| res_dct = evaluate(pred_dir, method, testset, only_S_MAE=only_S_MAE) | |
| if __name__ == '__main__': | |
| main() | |