xinwei89 commited on
Commit
084c97d
β€’
1 Parent(s): f5f79e8

dynamic choosing of model versions

Browse files
Files changed (17) hide show
  1. app.py +3 -3
  2. backend.py +9 -4
  3. {building_model_weight β†’ building_model_weights}/README.md +0 -0
  4. {building_model_weight β†’ building_model_weights}/_annotations.coco.json +0 -0
  5. {building_model_weight β†’ building_model_weights}/buildings_poc_cfg.yml +0 -0
  6. {building_model_weight β†’ building_model_weights}/buildingv1_best.pth +0 -0
  7. {building_model_weight β†’ building_model_weights}/buildingv1_cfg.yaml +0 -0
  8. building_model_weight/buildingv2instances_predictions.pth β†’ building_model_weights/buildingv2_instances_predictions.pth +0 -0
  9. building_model_weight/buildingv2model_best.pth β†’ building_model_weights/buildingv2_model_best.pth +0 -0
  10. building_model_weight/buildingv2model_best_iteration_71.pth β†’ building_model_weights/buildingv2_model_best_iteration_71.pth +0 -0
  11. building_model_weight/buildingv2model_final.pth β†’ building_model_weights/buildingv2_model_final.pth +0 -0
  12. building_model_weight/buildingv2sixmaps_building_level_category_20122023_cfg.yaml β†’ building_model_weights/buildingv2_sixmaps_building_level_category_20122023_cfg.yaml +0 -0
  13. {building_model_weight β†’ building_model_weights}/buildingv3_binary_17112023.pth +0 -0
  14. {building_model_weight β†’ building_model_weights}/buildingv3_binary_cfg.yaml +0 -0
  15. {building_model_weight β†’ building_model_weights}/model_final.pth +0 -0
  16. {building_model_weight β†’ building_model_weights}/tree_cfg.yml +0 -0
  17. {building_model_weight β†’ building_model_weights}/tree_model.pth +0 -0
app.py CHANGED
@@ -25,13 +25,13 @@ with gr.Blocks() as demo:
25
  tree_row, building_row , lcz_row = gr.Row(), gr.Row(), gr.Row()
26
  # tree_col, building_col = gr.Column(elem_id="tree_col"), gr.Column(elem_id="building_col")
27
  with tree_row as tree_options:
28
- tree_version_dropdown = gr.Dropdown(choices=["treev1", "treev2"], label="Tree Detection Version", value="treev2", visible=True, interactive=True)
29
  tree_pth_dropdown = gr.Dropdown(choices=list_pth_files_in_directory("tree_model_weights", "v2"), label="Select a tree model file", visible=True, interactive=True)
30
  tree_threshold_slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label='Set confidence threshold "%" for trees', visible=True, interactive=True)
31
 
32
  with building_row as building_options:
33
- building_version_dropdown = gr.Dropdown(choices=["buildingv1", "buildingv2"], label="Building Detection Version", value="buildingv1", visible=True, interactive=True)
34
- building_pth_dropdown = gr.Dropdown(choices=list_pth_files_in_directory("building_model_weight", "v1"), label="Select a building model file", visible=True, interactive=True)
35
  building_threshold_slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label='Set confidence threshold "%" for buildings', visible=True, interactive=True)
36
 
37
  with lcz_row as lcz_options:
 
25
  tree_row, building_row , lcz_row = gr.Row(), gr.Row(), gr.Row()
26
  # tree_col, building_col = gr.Column(elem_id="tree_col"), gr.Column(elem_id="building_col")
27
  with tree_row as tree_options:
28
+ tree_version_dropdown = gr.Dropdown(choices=list_cfg_file_versions("tree_model_weights"), label="Tree Detection Version", value="treev2", visible=True, interactive=True)
29
  tree_pth_dropdown = gr.Dropdown(choices=list_pth_files_in_directory("tree_model_weights", "v2"), label="Select a tree model file", visible=True, interactive=True)
30
  tree_threshold_slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label='Set confidence threshold "%" for trees', visible=True, interactive=True)
31
 
32
  with building_row as building_options:
33
+ building_version_dropdown = gr.Dropdown(choices=list_cfg_file_versions("building_model_weights"), label="Building Detection Version", value="buildingv1", visible=True, interactive=True)
34
+ building_pth_dropdown = gr.Dropdown(choices=list_pth_files_in_directory("building_model_weights", "v1"), label="Select a building model file", visible=True, interactive=True)
35
  building_threshold_slider = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label='Set confidence threshold "%" for buildings', visible=True, interactive=True)
36
 
37
  with lcz_row as lcz_options:
backend.py CHANGED
@@ -29,6 +29,11 @@ from detectron2.checkpoint import DetectionCheckpointer
29
  from detectron2.utils.visualizer import ColorMode
30
  from detectron2.structures import Instances
31
 
 
 
 
 
 
32
 
33
  def list_pth_files_in_directory(directory, version="v1"):
34
  files = os.listdir(directory)
@@ -57,7 +62,7 @@ def update_path_options(version):
57
  if "tree" in version:
58
  directory = "tree_model_weights"
59
  else:
60
- directory = "building_model_weight"
61
  return gr.Dropdown(choices=list_pth_files_in_directory(directory, version), label=f"Select a {version.split('v')[0]} model file", visible=True, interactive=True)
62
 
63
  # Model for trees
@@ -74,9 +79,9 @@ def tree_model(tree_version_dropdown, tree_pth_dropdown, tree_threshold, device=
74
  # Model for buildings
75
  def building_model(building_version_dropdown, building_pth_dropdown, building_threshold, device="cpu"):
76
  building_cfg = get_cfg()
77
- building_cfg.merge_from_file(get_version_cfg_yml(f"building_model_weight/{building_version_dropdown}"))
78
  building_cfg.MODEL.DEVICE=device
79
- building_cfg.MODEL.WEIGHTS = f"building_model_weight/{building_pth_dropdown}"
80
  building_cfg.MODEL.ROI_HEADS.NUM_CLASSES = 8 # TODO change this
81
  building_cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = building_threshold
82
  building_predictor = DefaultPredictor(building_cfg)
@@ -162,7 +167,7 @@ def visualize_image(im, mode, tree_threshold, building_threshold, color_mode, tr
162
  instances = lcz_instances if mode == "LCZ" else combine_instances(instances, LCZ_instances)
163
 
164
  # Assuming 'urban-small_train' is intended for both Trees and Buildings
165
- metadata = get_metadata("urban-small_train", "building_model_weight/_annotations.coco.json")
166
  visualizer = Visualizer(im[:, :, ::-1], metadata=metadata, scale=0.5, instance_mode=color_mode)
167
 
168
  output_image = visualizer.draw_instance_predictions(instances)
 
29
  from detectron2.utils.visualizer import ColorMode
30
  from detectron2.structures import Instances
31
 
32
+ def list_cfg_file_versions(directory):
33
+ files = os.listdir(directory)
34
+ # return files that contains substring version and end with .yml
35
+ cfg_files = [f.split("_")[0] for f in files if (f.endswith(".yml") or f.endswith(".yaml")) and f.startswith(f"{directory.split('_')[0]}v")]
36
+ return cfg_files
37
 
38
  def list_pth_files_in_directory(directory, version="v1"):
39
  files = os.listdir(directory)
 
62
  if "tree" in version:
63
  directory = "tree_model_weights"
64
  else:
65
+ directory = "building_model_weights"
66
  return gr.Dropdown(choices=list_pth_files_in_directory(directory, version), label=f"Select a {version.split('v')[0]} model file", visible=True, interactive=True)
67
 
68
  # Model for trees
 
79
  # Model for buildings
80
  def building_model(building_version_dropdown, building_pth_dropdown, building_threshold, device="cpu"):
81
  building_cfg = get_cfg()
82
+ building_cfg.merge_from_file(get_version_cfg_yml(f"building_model_weights/{building_version_dropdown}"))
83
  building_cfg.MODEL.DEVICE=device
84
+ building_cfg.MODEL.WEIGHTS = f"building_model_weights/{building_pth_dropdown}"
85
  building_cfg.MODEL.ROI_HEADS.NUM_CLASSES = 8 # TODO change this
86
  building_cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = building_threshold
87
  building_predictor = DefaultPredictor(building_cfg)
 
167
  instances = lcz_instances if mode == "LCZ" else combine_instances(instances, LCZ_instances)
168
 
169
  # Assuming 'urban-small_train' is intended for both Trees and Buildings
170
+ metadata = get_metadata("urban-small_train", "building_model_weights/_annotations.coco.json")
171
  visualizer = Visualizer(im[:, :, ::-1], metadata=metadata, scale=0.5, instance_mode=color_mode)
172
 
173
  output_image = visualizer.draw_instance_predictions(instances)
{building_model_weight β†’ building_model_weights}/README.md RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/_annotations.coco.json RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/buildings_poc_cfg.yml RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/buildingv1_best.pth RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/buildingv1_cfg.yaml RENAMED
File without changes
building_model_weight/buildingv2instances_predictions.pth β†’ building_model_weights/buildingv2_instances_predictions.pth RENAMED
File without changes
building_model_weight/buildingv2model_best.pth β†’ building_model_weights/buildingv2_model_best.pth RENAMED
File without changes
building_model_weight/buildingv2model_best_iteration_71.pth β†’ building_model_weights/buildingv2_model_best_iteration_71.pth RENAMED
File without changes
building_model_weight/buildingv2model_final.pth β†’ building_model_weights/buildingv2_model_final.pth RENAMED
File without changes
building_model_weight/buildingv2sixmaps_building_level_category_20122023_cfg.yaml β†’ building_model_weights/buildingv2_sixmaps_building_level_category_20122023_cfg.yaml RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/buildingv3_binary_17112023.pth RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/buildingv3_binary_cfg.yaml RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/model_final.pth RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/tree_cfg.yml RENAMED
File without changes
{building_model_weight β†’ building_model_weights}/tree_model.pth RENAMED
File without changes