OriLib commited on
Commit
524ae85
1 Parent(s): 9505c97

Update example_inference.py

Browse files
Files changed (1) hide show
  1. example_inference.py +19 -32
example_inference.py CHANGED
@@ -1,55 +1,42 @@
1
- import os
2
- import numpy as np
3
  from skimage import io
4
- import cv2
5
- import torch
6
- import torch.nn.functional as F
7
- from torchvision.transforms.functional import normalize
8
  from briarmbg import BriaRMBG
9
-
10
 
11
  def example_inference():
12
 
13
- input_size=[1024,1024]
14
- net=BriaRMBG()
15
-
16
- model_path = "./model.pth"
17
- im_path = "./example_image.jpg"
18
- result_path = "."
19
 
 
20
  if torch.cuda.is_available():
21
- net.load_state_dict(torch.load(model_path))
22
- net=net.cuda()
23
  else:
24
  net.load_state_dict(torch.load(model_path,map_location="cpu"))
25
  net.eval()
26
 
27
  # prepare input
28
- im = io.imread(im_path)
29
- if len(im.shape) < 3:
30
- im = im[:, :, np.newaxis]
31
- im_size=im.shape[0:2]
32
- im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)
33
- im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=input_size, mode='bilinear').type(torch.uint8)
34
- image = torch.divide(im_tensor,255.0)
35
- image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])
36
 
37
  if torch.cuda.is_available():
38
  image=image.cuda()
39
-
40
- #inference
41
  result=net(image)
42
 
43
  # post process
44
- result = torch.squeeze(F.interpolate(result[0][0], size=im_size, mode='bilinear') ,0)
45
- ma = torch.max(result)
46
- mi = torch.min(result)
47
- result = (result-mi)/(ma-mi)
48
 
49
  # save result
50
- im_name=im_path.split('/')[-1].split('.')[0]
51
- im_array = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)
52
- cv2.imwrite(os.path.join(result_path, im_name+".png"), im_array)
 
 
53
 
54
 
55
  if __name__ == "__main__":
 
 
 
1
  from skimage import io
2
+ import torch, os
3
+ from PIL import Image
 
 
4
  from briarmbg import BriaRMBG
5
+ from utilities import preprocess_image, postprocess_image
6
 
7
  def example_inference():
8
 
9
+ model_path = f"{os.path.dirname(__file__)}/model.pth"
10
+ im_path = f"{os.path.dirname(__file__)}/example_input.jpg"
 
 
 
 
11
 
12
+ net = BriaRMBG()
13
  if torch.cuda.is_available():
14
+ net.load_state_dict(torch.load(model_path)).cuda()
 
15
  else:
16
  net.load_state_dict(torch.load(model_path,map_location="cpu"))
17
  net.eval()
18
 
19
  # prepare input
20
+ model_input_size = [1024,1024]
21
+ orig_im = io.imread(im_path)
22
+ orig_im_size = orig_im.shape[0:2]
23
+ image = preprocess_image(orig_im, model_input_size)
 
 
 
 
24
 
25
  if torch.cuda.is_available():
26
  image=image.cuda()
27
+
28
+ # inference
29
  result=net(image)
30
 
31
  # post process
32
+ result_image = postprocess_image(result[0][0], orig_im_size)
 
 
 
33
 
34
  # save result
35
+ pil_im = Image.fromarray(result_image)
36
+ no_bg_image = Image.new("RGBA", pil_im.size, (0,0,0,0))
37
+ orig_image = Image.open(im_path)
38
+ no_bg_image.paste(orig_image, mask=pil_im)
39
+ no_bg_image.save("example_image_no_bg.png")
40
 
41
 
42
  if __name__ == "__main__":