Project-MONAI / Project-MONAI/MONAILabel

Add option to run batch inference from slicer UI

Open
#1,437 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
891
Forks
269
Avg merge
15h 41m
Merged PRs (30d)
1

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the Slicer UI's onClickSegmentation handler and the MONAILabelLogic batch_infer entry point, then inspect client.py's request methods. Confirm how existing segmentation configuration and job status are exposed before defining the batch option. Done means users can select batch inference with the relevant configs and see its progress in the UI.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, frontend, machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.