File size: 2,381 Bytes
44b2a9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""

Simple usage example for ISNet Background Remover

Shows how to use the model with one-line loading

"""
from PIL import Image
from skimage import io
import torch
import torch.nn.functional as F
from transformers import AutoModelForImageSegmentation
from torchvision.transforms.functional import normalize
import numpy as np

def preprocess_image(im: np.ndarray, model_input_size: list) -> torch.Tensor:
    """Preprocess image for model input"""
    if len(im.shape) < 3:
        im = im[:, :, np.newaxis]
    im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)
    im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=model_input_size, mode='bilinear')
    image = torch.divide(im_tensor,255.0)
    image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])
    return image

def postprocess_image(result: torch.Tensor, im_size: list)-> np.ndarray:
    """Postprocess model output to get mask"""
    result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear') ,0)
    ma = torch.max(result)
    mi = torch.min(result)
    result = (result-mi)/(ma-mi)
    im_array = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)
    im_array = np.squeeze(im_array)
    return im_array

def main():
    # One-line model loading with trust_remote_code=True
    model = AutoModelForImageSegmentation.from_pretrained("mateenahmed/isnet-background-remover", trust_remote_code=True)
    
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)
    
    # Example image URL
    image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
    orig_im = io.imread(image_path)
    orig_im_size = orig_im.shape[0:2]
    model_input_size = [1024, 1024]
    
    # Preprocess image
    image = preprocess_image(orig_im, model_input_size).to(device)
    
    # Inference 
    result = model(image)
    
    # Post process
    result_image = postprocess_image(result, orig_im_size)
    
    # Save result
    pil_mask_im = Image.fromarray(result_image)
    orig_image = Image.open(image_path)
    no_bg_image = orig_image.copy()
    no_bg_image.putalpha(pil_mask_im)
    no_bg_image.save("output_no_bg.png")
    
    print("✅ Background removed! Check output_no_bg.png")

if __name__ == "__main__":
    main()