How to customize model inference
Custom Inference Settings
settings = { 'confidence_threshold': 0.5 }
m = MyModel(model_dir=model_dir, custom_inference_settings=settings)def predict(self, image_path: str, settings: Dict[str, Any]) -> List[sly.nn.PredictionMask]:
confidence_threshold = settings.get("confidence_threshold", 0.5)
image = cv2.imread(image_path) # BGR
####### CUSTOM CODE FOR MY MODEL STARTS (e.g. DETECTRON2) #######
outputs = self.predictor(image) # get predictions from Detectron2 model
pred_classes = outputs["instances"].pred_classes.detach().cpu().numpy()
pred_class_names = [self.class_names[pred_class] for pred_class in pred_classes]
pred_scores = outputs["instances"].scores.detach().cpu().numpy().tolist()
pred_masks = outputs["instances"].pred_masks.detach().cpu().numpy()
####### CUSTOM CODE FOR MY MODEL ENDS (e.g. DETECTRON2) ########
results = []
for score, class_name, mask in zip(pred_scores, pred_class_names, pred_masks):
# filter predictions by confidence
if score >= confidence_threshold:
results.append(sly.nn.PredictionMask(class_name, mask, score))
return results
Model Information
Sliding window mode
Model files storage
Model meta for multitask models
Last updated