Repository navigation
Models
The service ships two FastAPI apps, one per model. IS-Net is the newer, recommended model; U²-Net is the original model the project started with. Both expose the same /remove-bg endpoint; you choose the model by choosing which app uvicorn runs.
| IS-Net (new, recommended) | U²-Net (old) | |
|---|---|---|
| App module | service.app:app |
service.app_u2net:app |
| Engine | service/isnet/engine_isnet.py |
service/u2net/engine_u2net.py |
| Network class |
ISNetDIS (model_isnet.py) |
U2NET(3, 1) (model_u2net.py) |
| Weights file | service/isnet/model/isnet-general-use.pth |
service/u2net/model/u2net.pth |
| Weights source | Google Drive | U-2-Net repository |
| Network input size | 1024 × 1024 | 320 × 320 |
| Precision | Full (float32) | float32 |
| Device | CUDA if available, otherwise CPU | CPU |
| Mask refinement | Single pass | 4 passes |
S-Processing-Device header |
Yes | No |
Weight files (*.pth) are listed in .gitignore, so you must download them yourself. Both engines load their weights when the module is imported, so the app fails to start if the file is missing.
IS-Net is the model from Highly Accurate Dichotomous Image Segmentation (DIS) — https://xuebinqin.github.io/dis/index.html. The service uses the general-purpose weights isnet-general-use.pth.
Install the weights
mkdir -p service/isnet/model
gdown 1XHIzgTzY5BQHw140EDIgwIb53K659ENH -O service/isnet/model/isnet-general-use.pthRun
uvicorn service.app:app --host localhost --port 8000Inference settings (the hypar dictionary in engine_isnet.py):
| Key | Value | Meaning |
|---|---|---|
model_path |
./service/isnet/model |
Folder that contains the weights (relative to the working directory). |
restore_model |
isnet-general-use.pth |
Weights file name. |
model_digit |
full |
Floating-point precision: full or half. |
cache_size |
[1024, 1024] |
Resolution the input is resized to before inference. |
input_size |
[1024, 1024] |
Model input size. |
crop_size |
[1024, 1024] |
Crop size (used for training-time augmentation). |
seed |
0 |
Random seed. |
Pipeline
- The image is decoded, converted to a tensor and resized to 1024 × 1024.
- It is normalized (mean
0.5, std1.0per channel) and passed throughISNetDIS. - The most accurate of the network's side outputs is upsampled back to the original size and scaled to 0–255.
- The mask is used to composite the original image over a transparent RGBA canvas.
The device is chosen once at start-up: cuda if torch.cuda.is_available(), otherwise cpu. The engine prints it as Device: cuda or Device: cpu.
U²-Net is the salient-object detection model from https://github.com/xuebinqin/U-2-Net. Download u2net.pth from that repository and place it at service/u2net/model/u2net.pth.
Run
uvicorn service.app_u2net:app --host localhost --port 8000Pipeline
- The image is rescaled to 320 × 320 and converted to a tensor (
RescaleT(320),ToTensorLab). -
U2NET(3, 1)predicts a mask; the first output is normalized to 0–1. - The masking pass is repeated 4 times, each time on the previous result, to refine the cut-out (
remove_bg_mult). - The final mask is resized to the original size and used to composite the image over a transparent canvas.
The U²-Net weights are loaded with map_location="cpu", so this app runs on CPU.
Use IS-Net for new deployments: it works at a higher input resolution, runs on GPU when available and is the model the project calls "new". Keep U²-Net if you already depend on its output or need a CPU-only setup that matches earlier results.
- DIS / IS-Net: https://xuebinqin.github.io/dis/index.html
- U²-Net: https://github.com/xuebinqin/U-2-Net