#!/usr/bin/env python # coding: utf-8 # # Semantic Segmentation Input # ## Install Dependencies # In[1]: get_ipython().system('pip install -r requirements.txt') # ## Setup Imports # In[2]: from auxiliary.nifti.io import read_nifti from rich import print as pprint from panoptica import ( InputType, Panoptica_Evaluator, ConnectedComponentsInstanceApproximator, NaiveThresholdMatching, ) # ## Load Data # To demonstrate we use a reference and predicition of spine a segmentation without instances. # # ![semantic_figure](figures/semantic.png) # In[3]: ref_masks = read_nifti("./spine_seg/semantic/ref.nii.gz") pred_masks = read_nifti("./spine_seg/semantic/pred.nii.gz") # To use your own data please replace the example data with your own data. # # In ordner to successfully load your data please use NIFTI files and the following file designation within the "semantic" folder: # # ```panoptica/spine_seg/semantic/``` # # - Reference data ("ref.nii.gz") # - Prediction data ("pred.nii.gz") # # ## Run Evaluation # In[4]: evaluator = Panoptica_Evaluator( expected_input=InputType.SEMANTIC, instance_approximator=ConnectedComponentsInstanceApproximator(), instance_matcher=NaiveThresholdMatching(), ) # ## Inspect Results # The results object allows access to individual metrics and provides helper methods for further processing # # In[5]: # print all results result, intermediate_steps_data = evaluator.evaluate( pred_masks, ref_masks, verbose=False )["ungrouped"] print(result) # In[6]: # get specific metric, e.g. pq pprint(f"{result.pq=}") # In[7]: # get dict for further processing, e.g. for pandas pprint("results dict: ", result.to_dict()) # In[8]: # To inspect different phases, just use the returned intermediate_steps_data object import numpy as np intermediate_steps_data.original_prediction_arr # yields input prediction array intermediate_steps_data.original_reference_arr # yields input reference array intermediate_steps_data.prediction_arr( InputType.MATCHED_INSTANCE ) # yields prediction array after instances have been matched intermediate_steps_data.reference_arr( InputType.MATCHED_INSTANCE ) # yields reference array after instances have been matched # This works with all InputType for i in InputType: print(i) pred = intermediate_steps_data.prediction_arr(i) ref = intermediate_steps_data.reference_arr(i) print("Prediction array shape =", pred.shape, "unique_values=", np.unique(pred)) print("Reference array shape =", ref.shape, "unique_values=", np.unique(ref)) print()