ZhengPeng7 commited on
Commit
1592dab
1 Parent(s): 65cb7d4

Add BiRefNet-Lite-2K model, add model choosing option in tab_batch.

Browse files
Files changed (2) hide show
  1. app.py +11 -6
  2. app_local.py +11 -6
app.py CHANGED
@@ -61,7 +61,7 @@ def FB_blur_fusion_foreground_estimator(image, F, B, alpha, r=90):
61
  class ImagePreprocessor():
62
  def __init__(self, resolution: Tuple[int, int] = (1024, 1024)) -> None:
63
  self.transform_image = transforms.Compose([
64
- transforms.Resize(resolution), # 1. keep consistent with the cv2.resize used in training 2. redundant with that in path_to_image()
65
  transforms.ToTensor(),
66
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
  ])
@@ -74,6 +74,7 @@ class ImagePreprocessor():
74
  usage_to_weights_file = {
75
  'General': 'BiRefNet',
76
  'General-Lite': 'BiRefNet_lite',
 
77
  'Portrait': 'BiRefNet-portrait',
78
  'DIS': 'BiRefNet-DIS5K',
79
  'HRSOD': 'BiRefNet-HRSOD',
@@ -102,8 +103,8 @@ def predict(images, resolution, weights_file):
102
  try:
103
  resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
104
  except:
105
- resolution = [1024, 1024]
106
- print('Invalid resolution input. Automatically changed to 1024x1024.')
107
 
108
  if isinstance(images, list):
109
  # For tab_batch
@@ -182,7 +183,7 @@ tab_image = gr.Interface(
182
  fn=predict,
183
  inputs=[
184
  gr.Image(label='Upload an image'),
185
- gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`. Higher resolutions can be much slower for inference.", label="Resolution"),
186
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
187
  ],
188
  outputs=ImageSlider(label="BiRefNet's prediction", type="pil"),
@@ -195,7 +196,7 @@ tab_text = gr.Interface(
195
  fn=predict,
196
  inputs=[
197
  gr.Textbox(label="Paste an image URL"),
198
- gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`. Higher resolutions can be much slower for inference.", label="Resolution"),
199
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
200
  ],
201
  outputs=ImageSlider(label="BiRefNet's prediction", type="pil"),
@@ -206,7 +207,11 @@ tab_text = gr.Interface(
206
 
207
  tab_batch = gr.Interface(
208
  fn=predict,
209
- inputs=gr.File(label="Upload multiple images", type="filepath", file_count="multiple"),
 
 
 
 
210
  outputs=[gr.Gallery(label="BiRefNet's predictions"), gr.File(label="Download masked images.")],
211
  api_name="batch",
212
  description=descriptions+'\nTab-batch is partially modified from https://huggingface.co/spaces/NegiTurkey/Multi_Birefnetfor_Background_Removal, thanks to this great work!',
 
61
  class ImagePreprocessor():
62
  def __init__(self, resolution: Tuple[int, int] = (1024, 1024)) -> None:
63
  self.transform_image = transforms.Compose([
64
+ transforms.Resize(resolution),
65
  transforms.ToTensor(),
66
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
  ])
 
74
  usage_to_weights_file = {
75
  'General': 'BiRefNet',
76
  'General-Lite': 'BiRefNet_lite',
77
+ 'General-Lite-2K': 'BiRefNet_lite-2K',
78
  'Portrait': 'BiRefNet-portrait',
79
  'DIS': 'BiRefNet-DIS5K',
80
  'HRSOD': 'BiRefNet-HRSOD',
 
103
  try:
104
  resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
105
  except:
106
+ resolution = (1024, 1024) if weights_file not in ['General-Lite-2K'] else (2560, 1440)
107
+ print('Invalid resolution input. Automatically changed to 1024x1024 or 2K.')
108
 
109
  if isinstance(images, list):
110
  # For tab_batch
 
183
  fn=predict,
184
  inputs=[
185
  gr.Image(label='Upload an image'),
186
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
187
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
188
  ],
189
  outputs=ImageSlider(label="BiRefNet's prediction", type="pil"),
 
196
  fn=predict,
197
  inputs=[
198
  gr.Textbox(label="Paste an image URL"),
199
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
200
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
201
  ],
202
  outputs=ImageSlider(label="BiRefNet's prediction", type="pil"),
 
207
 
208
  tab_batch = gr.Interface(
209
  fn=predict,
210
+ inputs=[
211
+ gr.File(label="Upload multiple images", type="filepath", file_count="multiple"),
212
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
213
+ gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
214
+ ],
215
  outputs=[gr.Gallery(label="BiRefNet's predictions"), gr.File(label="Download masked images.")],
216
  api_name="batch",
217
  description=descriptions+'\nTab-batch is partially modified from https://huggingface.co/spaces/NegiTurkey/Multi_Birefnetfor_Background_Removal, thanks to this great work!',
app_local.py CHANGED
@@ -61,7 +61,7 @@ def FB_blur_fusion_foreground_estimator(image, F, B, alpha, r=90):
61
  class ImagePreprocessor():
62
  def __init__(self, resolution: Tuple[int, int] = (1024, 1024)) -> None:
63
  self.transform_image = transforms.Compose([
64
- transforms.Resize(resolution), # 1. keep consistent with the cv2.resize used in training 2. redundant with that in path_to_image()
65
  transforms.ToTensor(),
66
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
  ])
@@ -74,6 +74,7 @@ class ImagePreprocessor():
74
  usage_to_weights_file = {
75
  'General': 'BiRefNet',
76
  'General-Lite': 'BiRefNet_lite',
 
77
  'Portrait': 'BiRefNet-portrait',
78
  'DIS': 'BiRefNet-DIS5K',
79
  'HRSOD': 'BiRefNet-HRSOD',
@@ -102,8 +103,8 @@ def predict(images, resolution, weights_file):
102
  try:
103
  resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
104
  except:
105
- resolution = [1024, 1024]
106
- print('Invalid resolution input. Automatically changed to 1024x1024.')
107
 
108
  if isinstance(images, list):
109
  # For tab_batch
@@ -182,7 +183,7 @@ tab_image = gr.Interface(
182
  fn=predict,
183
  inputs=[
184
  gr.Image(label='Upload an image'),
185
- gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`. Higher resolutions can be much slower for inference.", label="Resolution"),
186
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
187
  ],
188
  outputs=gr.Image(label="BiRefNet's prediction", type="pil", format='png'),
@@ -195,7 +196,7 @@ tab_text = gr.Interface(
195
  fn=predict,
196
  inputs=[
197
  gr.Textbox(label="Paste an image URL"),
198
- gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`. Higher resolutions can be much slower for inference.", label="Resolution"),
199
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
200
  ],
201
  outputs=gr.Image(label="BiRefNet's prediction", type="pil", format='png'),
@@ -206,7 +207,11 @@ tab_text = gr.Interface(
206
 
207
  tab_batch = gr.Interface(
208
  fn=predict,
209
- inputs=gr.File(label="Upload multiple images", type="filepath", file_count="multiple"),
 
 
 
 
210
  outputs=[gr.Gallery(label="BiRefNet's predictions"), gr.File(label="Download masked images.")],
211
  api_name="batch",
212
  description=descriptions+'\nTab-batch is partially modified from https://huggingface.co/spaces/NegiTurkey/Multi_Birefnetfor_Background_Removal, thanks to this great work!',
 
61
  class ImagePreprocessor():
62
  def __init__(self, resolution: Tuple[int, int] = (1024, 1024)) -> None:
63
  self.transform_image = transforms.Compose([
64
+ transforms.Resize(resolution),
65
  transforms.ToTensor(),
66
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
  ])
 
74
  usage_to_weights_file = {
75
  'General': 'BiRefNet',
76
  'General-Lite': 'BiRefNet_lite',
77
+ 'General-Lite-2K': 'BiRefNet_lite-2K',
78
  'Portrait': 'BiRefNet-portrait',
79
  'DIS': 'BiRefNet-DIS5K',
80
  'HRSOD': 'BiRefNet-HRSOD',
 
103
  try:
104
  resolution = [int(int(reso)//32*32) for reso in resolution.strip().split('x')]
105
  except:
106
+ resolution = (1024, 1024) if weights_file not in ['General-Lite-2K'] else (2560, 1440)
107
+ print('Invalid resolution input. Automatically changed to 1024x1024 or 2K.')
108
 
109
  if isinstance(images, list):
110
  # For tab_batch
 
183
  fn=predict,
184
  inputs=[
185
  gr.Image(label='Upload an image'),
186
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
187
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
188
  ],
189
  outputs=gr.Image(label="BiRefNet's prediction", type="pil", format='png'),
 
196
  fn=predict,
197
  inputs=[
198
  gr.Textbox(label="Paste an image URL"),
199
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
200
  gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
201
  ],
202
  outputs=gr.Image(label="BiRefNet's prediction", type="pil", format='png'),
 
207
 
208
  tab_batch = gr.Interface(
209
  fn=predict,
210
+ inputs=[
211
+ gr.File(label="Upload multiple images", type="filepath", file_count="multiple"),
212
+ gr.Textbox(lines=1, placeholder="Type the resolution (`WxH`) you want, e.g., `1024x1024`.", label="Resolution"),
213
+ gr.Radio(list(usage_to_weights_file.keys()), value='General', label="Weights", info="Choose the weights you want.")
214
+ ],
215
  outputs=[gr.Gallery(label="BiRefNet's predictions"), gr.File(label="Download masked images.")],
216
  api_name="batch",
217
  description=descriptions+'\nTab-batch is partially modified from https://huggingface.co/spaces/NegiTurkey/Multi_Birefnetfor_Background_Removal, thanks to this great work!',