wenpeng commited on
Commit
af4f502
·
1 Parent(s): d902f9c
app.py CHANGED
@@ -3,20 +3,25 @@ import inpaint.infer_model as inpaint
3
  import sod.infer_model as sod
4
  import numpy as np
5
  import torch
 
6
  # import os
7
  # cmd = 'sh download.sh'
8
  # os.system(cmd)
9
 
10
  device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
 
11
  inpaint_model = inpaint.IVModel(device=device)
12
  sod_model = sod.IVModel(device=torch.device("cpu"))
13
  def sod_inpaint(img):
14
  img = img[:,:,::-1]
15
  res = sod_model.forward(img,None)
 
16
  res = np.uint8(res)
17
  res = inpaint_model.forward(res,None)
18
  res = np.uint8(res)
 
19
  return res[:,:,::-1]
20
 
21
- iface = gr.Interface(fn=sod_inpaint, inputs="image", outputs="image", examples='examples', title='显著物体消除', description='这是一个图像API,功能是自动把画面中的显著物体消除', theme='huggingface')
 
22
  iface.launch()
 
3
  import sod.infer_model as sod
4
  import numpy as np
5
  import torch
6
+ import glob
7
  # import os
8
  # cmd = 'sh download.sh'
9
  # os.system(cmd)
10
 
11
  device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
12
+ print(device)
13
  inpaint_model = inpaint.IVModel(device=device)
14
  sod_model = sod.IVModel(device=torch.device("cpu"))
15
  def sod_inpaint(img):
16
  img = img[:,:,::-1]
17
  res = sod_model.forward(img,None)
18
+ print('sod complete!')
19
  res = np.uint8(res)
20
  res = inpaint_model.forward(res,None)
21
  res = np.uint8(res)
22
+ print('inpaint complete!')
23
  return res[:,:,::-1]
24
 
25
+ examples = glob.glob('examples/*.*')
26
+ iface = gr.Interface(fn=sod_inpaint, inputs="image", outputs="image", examples=examples, title='显著物体消除', description='这是一个图像API,功能是自动把画面中的显著物体消除', theme='huggingface')
27
  iface.launch()
examples/SOD003.jpeg DELETED
Binary file (86.4 kB)
 
inpaint/infer_model.py CHANGED
@@ -45,13 +45,13 @@ class IVModel():
45
  self.__first_forward__()
46
 
47
 
48
- def __first_forward__(self, input_size=(2048, 4096, 3)):
49
  # 调用forward()严格控制最大显存
50
  print('initialize Inpaint Model...')
51
  _ = self.forward(np.random.rand(*input_size) * 255, None)
52
  print('initialize Complete!')
53
 
54
- def __resize_tensor__(self, image, max_size=1024, scale_factor=8):
55
  h, w = image.size()[2:]
56
  if max(h, w) > max_size:
57
  if h < w:
@@ -68,7 +68,7 @@ class IVModel():
68
  img_t = img_t.permute(2, 0, 1).unsqueeze(0)
69
  img_t = img_t / 255.
70
  img_t_for_net = self.__resize_tensor__(img_t).to(self.device) # 为了控制最大显存容量
71
- img_t_for_out = self.__resize_tensor__(img_t, max_size=2048).to(self.device) # 为了控制最大显存容量
72
  return img_t_for_net, img_t_for_out
73
 
74
  def forward(self, img, json_data):
 
45
  self.__first_forward__()
46
 
47
 
48
+ def __first_forward__(self, input_size=(512, 1024, 3)):
49
  # 调用forward()严格控制最大显存
50
  print('initialize Inpaint Model...')
51
  _ = self.forward(np.random.rand(*input_size) * 255, None)
52
  print('initialize Complete!')
53
 
54
+ def __resize_tensor__(self, image, max_size=512, scale_factor=8):
55
  h, w = image.size()[2:]
56
  if max(h, w) > max_size:
57
  if h < w:
 
68
  img_t = img_t.permute(2, 0, 1).unsqueeze(0)
69
  img_t = img_t / 255.
70
  img_t_for_net = self.__resize_tensor__(img_t).to(self.device) # 为了控制最大显存容量
71
+ img_t_for_out = self.__resize_tensor__(img_t, max_size=512).to(self.device) # 为了控制最大显存容量
72
  return img_t_for_net, img_t_for_out
73
 
74
  def forward(self, img, json_data):
sod/infer_model.py CHANGED
@@ -49,13 +49,13 @@ class IVModel():
49
  self.__first_forward__()
50
 
51
 
52
- def __first_forward__(self, input_size=(2048, 2048, 3)):
53
  # 调用forward()严格控制最大显存
54
  print('initialize Sod Model...')
55
  _ = self.forward(np.random.rand(*input_size) * 255, None)
56
  print('initialize Complete!')
57
 
58
- def __resize_tensor__(self, image, max_size=1024):
59
  h, w = image.size()[2:]
60
  if max(h, w) > max_size:
61
  if h < w:
@@ -76,7 +76,7 @@ class IVModel():
76
  img_t = self.input_preprocess_tensor(img)
77
  shape = [torch.as_tensor([img_t.shape[2]]), torch.as_tensor([img_t.shape[3]])]
78
  h, w = img_t.shape[2], img_t.shape[3]
79
- img_t_temp = F.interpolate(img_t, (1024, 1024), mode='area')
80
  with torch.no_grad():
81
  res = self.net(img_t_temp, shape=shape)
82
  res = F.interpolate(res[0],size=shape, mode='bilinear')
 
49
  self.__first_forward__()
50
 
51
 
52
+ def __first_forward__(self, input_size=(512, 512, 3)):
53
  # 调用forward()严格控制最大显存
54
  print('initialize Sod Model...')
55
  _ = self.forward(np.random.rand(*input_size) * 255, None)
56
  print('initialize Complete!')
57
 
58
+ def __resize_tensor__(self, image, max_size=512):
59
  h, w = image.size()[2:]
60
  if max(h, w) > max_size:
61
  if h < w:
 
76
  img_t = self.input_preprocess_tensor(img)
77
  shape = [torch.as_tensor([img_t.shape[2]]), torch.as_tensor([img_t.shape[3]])]
78
  h, w = img_t.shape[2], img_t.shape[3]
79
+ img_t_temp = F.interpolate(img_t, (512, 512), mode='area')
80
  with torch.no_grad():
81
  res = self.net(img_t_temp, shape=shape)
82
  res = F.interpolate(res[0],size=shape, mode='bilinear')