jhj0517 commited on
Commit
bb21642
1 Parent(s): a3a4de0

Refactor parameters as function

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from gradio_image_prompter import ImagePrompter
 
3
  import os
4
  import yaml
5
 
@@ -30,6 +31,27 @@ class App:
30
  gr.Accordion(visible=mode == AUTOMATIC_MODE),
31
  ]
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def launch(self):
34
  _mask_hparams = self.hparams["mask_hparams"]
35
 
@@ -47,23 +69,7 @@ class App:
47
  choices=self.sam_inf.available_models)
48
 
49
  with gr.Accordion("Mask Parameters", open=False) as acc_mask_hparams:
50
- nb_points_per_side = gr.Number(label="points_per_side ", value=_mask_hparams["points_per_side"],
51
- interactive=True)
52
- nb_points_per_batch = gr.Number(label="points_per_batch ", value=_mask_hparams["points_per_batch"],
53
- interactive=True)
54
- sld_pred_iou_thresh = gr.Slider(label="pred_iou_thresh ", value=_mask_hparams["pred_iou_thresh"],
55
- minimum=0, maximum=1, interactive=True)
56
- sld_stability_score_thresh = gr.Slider(label="stability_score_thresh ", value=_mask_hparams["stability_score_thresh"],
57
- minimum=0, maximum=1, interactive=True)
58
- sld_stability_score_offset = gr.Slider(label="stability_score_offset ", value=_mask_hparams["stability_score_offset"],
59
- minimum=0, maximum=1)
60
- nb_crop_n_layers = gr.Number(label="crop_n_layers ", value=_mask_hparams["crop_n_layers"],)
61
- sld_box_nms_thresh = gr.Slider(label="box_nms_thresh ", value=_mask_hparams["box_nms_thresh"],
62
- minimum=0, maximum=1)
63
- nb_crop_n_points_downscale_factor = gr.Number(label="crop_n_points_downscale_factor ",
64
- value=_mask_hparams["crop_n_points_downscale_factor"],)
65
- nb_min_mask_region_area = gr.Number(label="min_mask_region_area ", value=_mask_hparams["min_mask_region_area"],)
66
- cb_use_m2m = gr.Checkbox(label="use_m2m ", value=_mask_hparams["use_m2m"])
67
 
68
  cb_multimask_output = gr.Checkbox(label="multimask_output", value=_mask_hparams["multimask_output"])
69
 
@@ -77,16 +83,13 @@ class App:
77
 
78
  sources = [img_input, img_input_prompter, dd_input_modes]
79
  model_params = [dd_models]
80
- mask_hparams = [nb_points_per_side, nb_points_per_batch, sld_pred_iou_thresh,
81
- sld_stability_score_thresh, sld_stability_score_offset, nb_crop_n_layers,
82
- sld_box_nms_thresh, nb_crop_n_points_downscale_factor, nb_min_mask_region_area,
83
- cb_use_m2m, cb_multimask_output]
84
 
85
  btn_generate.click(fn=self.sam_inf.divide_layer,
86
- inputs=sources + model_params + mask_hparams, outputs=[gallery_output, output_file])
87
  btn_open_folder.click(fn=lambda: open_folder(os.path.join(OUTPUT_DIR)),
88
  inputs=None, outputs=None)
89
-
90
  dd_input_modes.change(fn=self.on_mode_change,
91
  inputs=[dd_input_modes],
92
  outputs=[img_input, img_input_prompter, acc_mask_hparams])
 
1
  import gradio as gr
2
  from gradio_image_prompter import ImagePrompter
3
+ from typing import List, Dict, Optional, Union
4
  import os
5
  import yaml
6
 
 
31
  gr.Accordion(visible=mode == AUTOMATIC_MODE),
32
  ]
33
 
34
+ def mask_parameters(self,
35
+ hparams: Optional[Dict] = None):
36
+ if hparams is None:
37
+ hparams = self.hparams["mask_hparams"]
38
+ mask_components = [
39
+ gr.Number(label="points_per_side ", value=hparams["points_per_side"], interactive=True),
40
+ gr.Number(label="points_per_batch ", value=hparams["points_per_batch"], interactive=True),
41
+ gr.Slider(label="pred_iou_thresh ", value=hparams["pred_iou_thresh"], minimum=0, maximum=1,
42
+ interactive=True),
43
+ gr.Slider(label="stability_score_thresh ", value=hparams["stability_score_thresh"], minimum=0,
44
+ maximum=1, interactive=True),
45
+ gr.Slider(label="stability_score_offset ", value=hparams["stability_score_offset"], minimum=0,
46
+ maximum=1),
47
+ gr.Number(label="crop_n_layers ", value=hparams["crop_n_layers"]),
48
+ gr.Slider(label="box_nms_thresh ", value=hparams["box_nms_thresh"], minimum=0, maximum=1),
49
+ gr.Number(label="crop_n_points_downscale_factor ", value=hparams["crop_n_points_downscale_factor"]),
50
+ gr.Number(label="min_mask_region_area ", value=hparams["min_mask_region_area"]),
51
+ gr.Checkbox(label="use_m2m ", value=hparams["use_m2m"])
52
+ ]
53
+ return mask_components
54
+
55
  def launch(self):
56
  _mask_hparams = self.hparams["mask_hparams"]
57
 
 
69
  choices=self.sam_inf.available_models)
70
 
71
  with gr.Accordion("Mask Parameters", open=False) as acc_mask_hparams:
72
+ mask_hparams_component = self.mask_parameters(_mask_hparams)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  cb_multimask_output = gr.Checkbox(label="multimask_output", value=_mask_hparams["multimask_output"])
75
 
 
83
 
84
  sources = [img_input, img_input_prompter, dd_input_modes]
85
  model_params = [dd_models]
86
+ mask_hparams = mask_hparams_component + [cb_multimask_output]
87
+ input_params = sources + model_params + mask_hparams
 
 
88
 
89
  btn_generate.click(fn=self.sam_inf.divide_layer,
90
+ inputs=input_params, outputs=[gallery_output, output_file])
91
  btn_open_folder.click(fn=lambda: open_folder(os.path.join(OUTPUT_DIR)),
92
  inputs=None, outputs=None)
 
93
  dd_input_modes.change(fn=self.on_mode_change,
94
  inputs=[dd_input_modes],
95
  outputs=[img_input, img_input_prompter, acc_mask_hparams])