Package: carvekit
✂️ CarveKit ✂️The higher resolution images from the picture above can be seen in the docs/imgs/compare/ and docs/imgs/input folders.📙 README LanguageRussianEnglish📄 Description:Automated high-quality background removal framework for an image using neural networks.🎆 Features:High QualityBatch ProcessingNVIDIA CUDA and CPU processingFP16 inference: Fast inference with low memory usageEasy inference100% remove.bg compatible FastAPI HTTP APIRemoves background from hairsEasy integration with your code⛱ Try yourself onGoogle Colab⛓️ How does it work?It can be briefly described asThe user selects a picture or a folder with pictures for processingThe photo is preprocessed to ensure the best quality of the output imageUsing machine learning technology, the background of the image is removedImage post-processing to improve the quality of the processed image🎓 Implemented Neural Networks:NetworksTargetAccuracyTracer-B7(default)General(objects, animals, etc)90%(mean F1-Score, DUTS-TE)U^2-netHairs(hairs, people, animals, objects)80.4% (mean F1-Score, DUTS-TE)BASNetGeneral(people, objects)80.3% (mean F1-Score, DUTS-TE)DeepLabV3People, Animals, Cars, etc67.4% (mean IoU, COCO val2017)Recommended parameters for different modelsNetworksSegmentation mask  sizeTrimap parameters (dilation, erosion)tracer_b7640(30, 5)u2net320(30, 5)basnet320(30, 5)deeplabv31024(40, 20)Notes:The final quality may depend on the resolution of your image, the type of scene or object.UseU2-Net for hairsandTracer-B7 for general imagesand correct parameters.It is very important for final quality! Example images was taken by using U2-Net and FBA post-processing.🖼️ Image pre-processing and post-processing methods:🔍 Preprocessing methods:none- No preprocessing methods used.They will be added in the future.✂ Post-processing methods:none- No post-processing methods used.fba(default) - This algorithm improves the borders of the image when removing the background from images with hair, etc. using FBA Matting neural network. This method gives the best result in combination with u2net without any preprocessing methods.🏷 Setup for CPU processing:pip install carvekit --extra-index-url https://download.pytorch.org/whl/cpuThe project has been tested on Python versions ranging from 3.9 to 3.11.7.🏷 Setup for GPU processing:Make sure you have an NVIDIA GPU with 8 GB VRAM.InstallCUDA Toolkit 12.1 and Video Driver for your GPUpip install carvekit --extra-index-url https://download.pytorch.org/whl/cu121The project has been tested on Python versions ranging from 3.9 to 3.11.7.🧰 Interact via code:If you don't need deep configuration or don't want to deal with itimporttorchfromcarvekit.api.highimportHiInterface# Check doc strings for more informationinterface=HiInterface(object_type="hairs-like",# Can be "object" or "hairs-like".batch_size_seg=5,batch_size_matting=1,device='cuda'iftorch.cuda.is_available()else'cpu',seg_mask_size=640,# Use 640 for Tracer B7 and 320 for U2Netmatting_mask_size=2048,trimap_prob_threshold=231,trimap_dilation=30,trimap_erosion_iters=5,fp16=False)images_without_background=interface(['./tests/data/cat.jpg'])cat_wo_bg=images_without_background[0]cat_wo_bg.save('2.png')If you want control everythingimportPIL.Imagefromcarvekit.api.interfaceimportInterfacefromcarvekit.ml.wrap.fba_mattingimportFBAMattingfromcarvekit.ml.wrap.tracer_b7importTracerUniversalB7fromcarvekit.pipelines.postprocessingimportMattingMethodfromcarvekit.pipelines.preprocessingimportPreprocessingStubfromcarvekit.trimap.generatorimportTrimapGenerator# Check doc strings for more informationseg_net=TracerUniversalB7(device='cpu',batch_size=1)fba=FBAMatting(device='cpu',input_tensor_size=2048,batch_size=1)trimap=TrimapGenerator()preprocessing=PreprocessingStub()postprocessing=MattingMethod(matting_module=fba,trimap_generator=trimap,device='cpu')interface=Interface(pre_pipe=preprocessing,post_pipe=postprocessing,seg_pipe=seg_net)image=PIL.Image.open('tests/data/cat.jpg')cat_wo_bg=interface([image])[0]cat_wo_bg.save('2.png')🧰 Running the CLI interface:python3 -m carvekit  -i <input_path> -o <output_path> --device <device>Explanation of args:Usage: carvekit [OPTIONS]

  Performs background removal on specified photos using console interface.

Options:
  -i ./2.jpg                   Path to input file or dir  [required]
  -o ./2.png                   Path to output file or dir
  --pre none                   Preprocessing method
  --post fba                   Postprocessing method.
  --net tracer_b7              Segmentation Network. Check README for more info.
  --recursive                  Enables recursive search for images in a folder
  --batch_size 10              Batch Size for list of images to be loaded to
                               RAM

  --batch_size_seg 5           Batch size for list of images to be processed
                               by segmentation network

  --batch_size_mat 1           Batch size for list of images to be processed
                               by matting network

  --seg_mask_size 640          The size of the input image for the
                               segmentation neural network. Use 640 for Tracer B7 and 320 for U2Net

  --matting_mask_size 2048     The size of the input image for the matting
                               neural network.
  --trimap_dilation 30       The size of the offset radius from the
                                  object mask in pixels when forming an
                                  unknown area
  --trimap_erosion 5        The number of iterations of erosion that the
                                  object's mask will be subjected to before
                                  forming an unknown area
  --trimap_prob_threshold 231
                                  Probability threshold at which the
                                  prob_filter and prob_as_unknown_area
                                  operations will be applied

  --device cpu                 Processing Device.
  --fp16                       Enables mixed precision processing. Use only with CUDA. CPU support is experimental!
  --help                       Show this message and exit.📦 Running the Framework / FastAPI HTTP API server via Docker:Using the API via docker is afastand non-complex way to have a working API.Our docker images are available onDocker Hub.Version tags are the same as the releases of the project with suffixes-cpuand-cudafor CPU and CUDA versions respectively.Important Notes:Docker image has default front-end at/url and FastAPI backend with docs at/docsurl.Authentication isenabledby default.Token keys are reseton every container restart if ENV variables are not set.Seedocker-compose.<device>.ymlfor more information.You can see your access keys in the docker container logs.There are examples of interaction with the API.Seedocs/code_examples/pythonfor more details🔨 Creating and running a container:Installdocker-composeRundocker-compose -f docker-compose.cpu.yml up -d# For CPU ProcessingRundocker-compose -f docker-compose.cuda.yml up -d# For GPU ProcessingAlso you can mount folders from your host machine to docker container
and use the CLI interface inside the docker container to process
files in this folder.Building a docker image on Windows is not officially supported. You can try using WSL2 or "Linux Containers Mode" but I haven't tested this.☑️ Testing☑️ Testing with local environmentpip install -r requirements_test.txtpytest☑️ Testing with DockerRundocker-compose -f docker-compose.cpu.yml run carvekit_api pytest# For testing on CPURundocker-compose -f docker-compose.cuda.yml run carvekit_api pytest# For testing on GPU👪 Credits:More info💵 SupportYou can thank me for developing this project and buy me a small cup of coffee ☕BlockchainCryptocurrencyNetworkWalletEthereumETH / USDT / USDC / BNB / DogecoinMainnet0x7Ab1B8015020242D2a9bC48F09b2F34b994bc2F8EthereumETH / USDT / USDC / BNB / DogecoinBSC (Binance Smart Chain)0x7Ab1B8015020242D2a9bC48F09b2F34b994bc2F8BitcoinBTC-bc1qmf4qedujhhvcsg8kxpg5zzc2s3jvqssmu7mmhqZCashZEC-t1d7b9WxdboGFrcVVHG2ZuwWBgWEKhNUbtmTronTRX-TH12CADSqSTcNZPvG77GVmYKAe4nrrJB5XMoneroXMRMainnet48w2pDYgPtPenwqgnNneEUC9Qt1EE6eD5MucLvU3FGpY3SABudDa4ce5bT1t32oBwchysRCUimCkZVsD1HQRBbxVLF9GTh3TONTON-EQCznqTdfOKI3L06QX-3Q802tBL0ecSWIKfkSjU-qsoy0CWE📧FeedbackI will be glad to receive feedback on the project and suggestions for integration.For all questions write:farvard34@gmail.com