alrichardbollans commited on
Commit
5fe3fb1
·
1 Parent(s): 3564118

Add some url variables and model params that might be changeable in future iterations

Browse files
Files changed (1) hide show
  1. python_utils/get_model.py +47 -9
python_utils/get_model.py CHANGED
@@ -1,9 +1,14 @@
1
  import urllib.request
2
  import tempfile
3
 
4
-
5
  OPTIMAL_NMS_THRESHOLD = 0.7
6
- _model_config_url = "https://huggingface.co/TZProject/final_tz_segmentor/resolve/main/final_model_config.yaml"
 
 
 
 
 
7
  def get_set_up():
8
  import torch
9
  TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
@@ -14,7 +19,20 @@ def get_set_up():
14
 
15
  # print("detectron2:", detectron2.__version__)
16
 
17
- def load_model():
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # return None
19
  import torch
20
  from detectron2.engine import DefaultPredictor
@@ -32,12 +50,13 @@ def load_model():
32
  else:
33
  cfg.MODEL.DEVICE = 'cuda'
34
 
35
- ## when rerouting to use the final model (final_tz_segmentor) USE_FED_LOSS has to be set to False
36
- ## this setting requires the training data to calculate class imbalance that the app will not have access to and cause a runtime error
37
- ## some messages will appear when using the model that certain weights are not being used
38
- ## but these are used during training and not inference and shouldn't affect the model performance
39
- ## code below
40
- cfg.MODEL.ROI_BOX_HEAD.USE_FED_LOSS = False
 
41
 
42
  predictor = DefaultPredictor(cfg)
43
 
@@ -78,6 +97,25 @@ def mask_nms(masks, scores, nms_threshold=OPTIMAL_NMS_THRESHOLD):
78
  return masks_kept
79
 
80
  def apply_nms(prediction, mask=False, cls_agnostic_nms=OPTIMAL_NMS_THRESHOLD):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  from torchvision.ops import nms
82
  from detectron2.structures import Instances
83
 
 
1
  import urllib.request
2
  import tempfile
3
 
4
+ ## Urls and model variables that might change.
5
  OPTIMAL_NMS_THRESHOLD = 0.7
6
+ model_page = "https://huggingface.co/TZProject/final_tz_segmentor"
7
+ _model_config_url = model_page+"/resolve/main/final_model_config.yaml"
8
+ MODEL_VERSION = "v1.0"
9
+ discussion_url = 'https://huggingface.co/spaces/TZProject/TZSeedApp/discussions'
10
+ github_repo_url = 'https://github.com/JATamura/TZSegmenting'
11
+
12
  def get_set_up():
13
  import torch
14
  TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
 
19
 
20
  # print("detectron2:", detectron2.__version__)
21
 
22
+ def load_model(using_final_model:bool=True):
23
+ """
24
+ Load and configure a Detectron2 model predictor. The method creates a configuration
25
+ object, merges it with a specified configuration file fetched from a remote URL,
26
+ and initializes the model using the `DefaultPredictor` from Detectron2. The model
27
+ will be set up to run on either a GPU or CPU depending on the system's capabilities.
28
+
29
+ :param using_final_model: A flag to indicate whether the final model should be used with
30
+ specific configurations. When this is set to True, adjustments are made to suppress
31
+ settings that are relevant during training but cause runtime errors during inference.
32
+ This includes disabling certain loss calculations that depend on training data.
33
+
34
+ :return: A Detectron2 predictor object configured and ready for inference.
35
+ """
36
  # return None
37
  import torch
38
  from detectron2.engine import DefaultPredictor
 
50
  else:
51
  cfg.MODEL.DEVICE = 'cuda'
52
 
53
+ if using_final_model:
54
+ ## when rerouting to use the final model (final_tz_segmentor) USE_FED_LOSS has to be set to False
55
+ ## this setting requires the training data to calculate class imbalance that the app will not have access to and cause a runtime error
56
+ ## some messages will appear when using the model that certain weights are not being used
57
+ ## but these are used during training and not inference and shouldn't affect the model performance
58
+ ## code below
59
+ cfg.MODEL.ROI_BOX_HEAD.USE_FED_LOSS = False
60
 
61
  predictor = DefaultPredictor(cfg)
62
 
 
97
  return masks_kept
98
 
99
  def apply_nms(prediction, mask=False, cls_agnostic_nms=OPTIMAL_NMS_THRESHOLD):
100
+ """
101
+ Applies Non-Maximum Suppression (NMS) to filter redundant bounding boxes
102
+ from the prediction output produced by an object detection model. The method is compatible
103
+ with instances containing bounding boxes, scores, classes, and optionally mask predictions.
104
+ The NMS operation is performed based on a specific IoU threshold, configurable via the
105
+ `cls_agnostic_nms` parameter. If mask mode is enabled, a mask-based NMS step will be applied
106
+ to filter out redundant masks among the predictions.
107
+
108
+ :param prediction: The dictionary containing detection results, where the
109
+ "instances" key points to an `Instances` object. This object should
110
+ contain attributes including bounding boxes, scores, predicted classes,
111
+ and optionally predicted masks.
112
+ :type prediction: Dict[str, Instances]
113
+ :param mask: A boolean flag indicating whether NMS should additionally be
114
+ applied to instance masks. Defaults to ``False``.
115
+ :type mask: bool
116
+ :param cls_agnostic_nms: An IoU threshold for NMS, reflecting the
117
+ level of overlap above which boxes are considered for suppression.
118
+ Defaults to ``"""
119
  from torchvision.ops import nms
120
  from detectron2.structures import Instances
121