Project-MONAI / MONAILabel

MONAI Label is an intelligent open source image labeling and learning tool.
https://docs.monai.io/projects/label
Apache License 2.0
615 stars 196 forks source link

Add option to run batch inference from slicer UI #1437

Open AHarouni opened 1 year ago

AHarouni commented 1 year ago

Describe the solution you'd like From slicer UI user can trigger segmentation with multiple config parameters that he can set. Similarly we need an option to easily call batch inference with the same configs. Moreover, we should have a progress bar showing the progress of the batch infer job.

Describe alternatives you've considered I can use the monai label swagger UI to call batch infer but I then need to type in all the configs which is error prone.

Additional context I managed to hack a solution in the slicer client by having a pop up to ask if I should do batch infer or run single image. Cleaner option is to add a new button along with may be more parameters related to batch inference as number of gpus to use. Code below works on my end

    def onClickSegmentation(self): # AEH added batch inference support 
  .... initial code is as is 
            model = self.ui.segmentationModelSelector.currentText
            image_file = self.current_sample["id"]
            params = self.getParamsFromConfig("infer", model)

            ###############  adding batch inference 
            if not slicer.util.confirmOkCancelDisplay("Run batch single image ? (cancel will run batch inference"):
                return self.logic.batch_infer(model, params, session_id=self.getSessionId())
  ..... the rest of function is unchanged 

added function below in class MONAILabelLogic(ScriptedLoadableModuleLogic):

    def batch_infer(self, model, params={}, session_id=None):
        return self._client().batch_infer(model, params,session_id)

in the client.py file I added the batch_infer function call

    def batch_infer(self, model, params, session_id=None):
        """
        Run Infer

        :param model: Name of Model
        :param params: Additional configs/json params as part of Infer request
        :param session_id: Session ID (use existing session id instead of image_id)
        """
        selector = "/batch/infer/{}".format(
            MONAILabelUtils.urllib_quote_plus(model),
        )
        if session_id:
            selector += f"&session_id={MONAILabelUtils.urllib_quote_plus(session_id)}"

        params = self._update_client_id(params)
        #fields = {"params": json.dumps(params) if params else "{}"}

        status, response, _, _ = MONAILabelUtils.http_method(
            "POST", self._server_url, selector, params, headers=self._headers
        )
        if status != 200:
            raise MONAILabelClientException(
                MONAILabelError.SERVER_ERROR,
                f"Status: {status}; Response: {bytes_to_str(response)}",
            )

        response = bytes_to_str(response)
        logging.debug(f"Response: {response}")
        return json.loads(response)
SachidanandAlle commented 1 year ago

please use CURL command (Try APIs) to run any of the background tasks.. you don't need 3D slicer

SachidanandAlle commented 1 year ago

And yes.. client.py can have batch_infer function.. if possible please raise a PR to add this functionality for client.py