How to run monailabel autosegmentation using python interactor

Dear community,
I am trying to run the monailabel autosegmentation for many samples so I would like to learn how to run infer using the python interactor.
I found this discussion on github answered by wonderful @lassoan, informing how logic can be used to run the module on python interactor.

With the given information, I was able to connect to the server but I could not make the autosegmentation to work.

I guess the infer can be done with following line but I was not able to figure out what image_in is for.
result_file, params = infer(model, image_in)

I tried putting my session folder as the image_in address but it did not worked.

#MONAILABEL
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

#check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# <FAILED>
# def infer(self, model, image_in, params={}, label_in=None, file=None, session_id=None):
session_image = "C:/Users/username/.cache/monailabel/sessions"
result_file, params = logic.infer("deepedit_seg", image_in = session_image)

Can somebody kindly explain to me what the image_in is supposed to be?

I am always greatful for all your help and thank you in advance.

I figured it out!!

I got confused what image_in was because I misread this and thought the image_in was supposed to be different from the file.

infer(model, image_id, params, label_in=None, file=None, session_id=None )[source]

Run Infer

Parameters

  • model – Name of Model
  • image_id – Image Id
  • params – Additional configs/json params as part of Infer request
  • label_in – File path for label mask which is needed to run Inference (e.g. In case of Scribbles)
  • file – File path for Image (use raw image instead of image_id)
  • session_id – Session ID (use existing session id instead of image_id)

Returns

response_file (label mask), response_body (json result/output params)

After all, image_in represented the path to my volume. (Thank you so much @rbumm!!)
So something like this would be image_in,

image_in = “C:/Users/username/Desktop/sampleFile.nrrd”

Now, if you want your processed volume to be autosegmented, not the original volume, you have to save currently edited volume as a temporary file.

Monailabel does that for you when you use it on Slicer, but you have to do this part manually when using the python interactor.

So, after having the server ready, you can get the server address and do as following.

# Save currently working scene
import time
import tempfile

def saveVolTemp():
	# Temporary folder path
	tempVolDir = logic.tmpdir
	# Select the volume node you are trying to work with
	volNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
	image_id = volNode.GetName()
	# Absolute path of the temporary volume file
	in_file = tempfile.NamedTemporaryFile(suffix= '.nrrd', dir = tempVolDir).name
	logic.reportProgress(5)
	# save the volume node
	start = time.time()
	slicer.util.saveNode(volNode, in_file)
	logging.info(f"Saved Input Node into {in_file} in {time.time() - start:3.1f}s")
	logic.reportProgress(30)
	return tempVolDir, image_id, in_file

# get MONAILABEL logic
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

# check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# Save the volume and get the path
tempVolDir, image_id, in_file = saveVolTemp()

# infer
result_file, params = logic.infer("deepedit_seg", in_file)

# load the autosegmented segmentation file onto Slicer
slicer.util.loadSegmentation(result_file)

I not good at python so please feel free to correct me if there is anything wrong or if this is a hard way aroud.
Thank you always, and I really hope this can help somebody.

1 Like

Great, @hourglassnam, I tested this and it works fine.

Just needed to remove logic.reportProgress calls from saveVolTemp() and make a change for tempVolDir:

 # Save currently working scene
import time
import tempfile
def saveVolTemp():
	# Temporary folder path
	tempVolDir = slicer.app.temporaryPath + "/YourProgramName/"
	# Select the volume node you are trying to work with
	volNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
	image_id = volNode.GetName()
	# Absolute path of the temporary volume file
	in_file = tempfile.NamedTemporaryFile(suffix= '.nrrd', dir = tempVolDir).name

	# save the volume node
	start = time.time()
	slicer.util.saveNode(volNode, in_file)
	logging.info(f"Saved Input Node into {in_file} in {time.time() - start:3.1f}s")
	return tempVolDir, image_id, in_file

# get MONAILABEL logic
logic = slicer.util.getModuleLogic('MONAILabel')

# connect to server
server_add = "http://127.0.0.1:8000"
logic.setServer(server_url=server_add)

# check if Monailabel is connected correctly
MONAILabelClient = logic.info()
print(MONAILabelClient)

# Save the volume and get the path
tempVolDir, image_id, in_file = saveVolTemp()

# infer
result_file, params = logic.infer("deepedit_seg", in_file)

# load the autosegmented segmentation file onto Slicer
slicer.util.loadSegmentation(result_file)
1 Like

Great!! Thank you so much for checking it:))