Package: lapixdl
LAPiX DL - Utils for Computer Vision Deep Learning researchThis package contains utilitary functions to support train and evaluation of Deep Learning models applied to images.Three computer vision approaches are covered: Segmentation, Detection and Classification.How to useFor Model EvaluationThis module exports the following functions for model evaluation:fromlapixdl.evaluation.evaluateimportevaluate_segmentationfromlapixdl.evaluation.evaluateimportevaluate_detectionfromlapixdl.evaluation.evaluateimportevaluate_classificationAll model evaluation methods need two iterators:one for the ground truth itens and one for the predictions.These iterators must be sorted equaly, assuring that the ground truth and the prediction of the same sample are at the same position.Example of segmentation model evaluation usingPyTorch:fromlapixdl.evaluation.evaluateimportevaluate_segmentationclasses=['background','object']# Iterator for GT masks# `dl` is a PyTorch DataLoaderdefgt_mask_iterator_from_dl(dl):forimgs,masksiniter(dl):formaskinmasks:yieldmask# Iterator for prediction masks# `predict` a function that, given an image, predicts the mask.defpred_mask_iterator_from_dl(dl,predict):forimgs,masksiniter(dl):forimginimgs:yieldpredict(img)gt_masks=gt_mask_iterator_from_dl(validation_dl)pred_masks=pred_mask_iterator_from_dl(validation_dl,prediction_function)# Calculates and shows metricseval=evaluate_segmentation(gt_masks,pred_masks,classes)# Shows confusion matrix and returns its Figure and Axesfig,axes=eval.show_confusion_matrix()Examples with third librariesHow to log the results of LAPiX DL evaluations in the Weights & Biases platformAboutWeights & Biases.fromlapixdl.evaluation.evaluateimportevaluate_segmentationimportwandb# init wandb ......eval_test=evaluate_segmentation(gt_masks,pred_masks,categories)...# If you want to log everythingwandb.log({'test_evaluation':eval_test.to_dict()['By Class']})# If you want to choose specific categories to logselected_cats=['A','B','C']metrics_by_cat={k:vfork,vineval_test.to_dict()['By Class'].items()ifkinselected_cats}wandb.log({'test_evaluation':metrics_by_cat})Computing using GPU withtorchmetricsAbouttorchmetrics.The lapixdl package calculates the confusion matrix first (on the CPU), which
this will be slower than calculating usingtorchmetricswhich usespytorchtensors. So a trick here, to not calculate each metric separately intorchmetrics, is to calculate aconfusion matrixusingtorchmetricsand then calculate all the metrics at once usinglapixdl.A simple example for a Segmentation case:importtorchmetricsfromlapixdl.evaluation.modelimportSegmentationMetricsclasses=['background','object']confMat=torchmetrics.ConfusionMatrix(reduce="macro",mdmc_reduce="global",num_classes=len(classes))confusion_matrix=confMat(pred,target)confusion_matrix=confusion_matrix.numpy()metrics=SegmentationMetrics(classes=classes,confusion_matrix=confusion_matrix)For Results VisualizationThis module exports the following functions for results visualization:fromlapixdl.evaluation.visualizeimportshow_segmentationsfromlapixdl.evaluation.visualizeimportshow_classificationsfromlapixdl.evaluation.visualizeimportshow_detectionsThe available color maps are theones from matplotlib.For Data ConversionThis module exports the functions for data conversion.fromlapixdl.convertimportlabelbox_to_lapixfromlapixdl.convertimportlabelbox_to_cocoExample of conversion fromLabelboxtoCOCOlabels format:importjsonfromlapixdl.formatsimportlabelbox_to_coco# A map categories between labelbox schematic id and category IDmap_categories={'<schematic id from labelbox>':1# category id}# The categories section in the COCO formatcategories_coco=[{'supercategory':None,'name':'example_category','id':1}]# Convert it and create the COCO OD datacoco_dict=labelbox_to_coco('labelbox_export_file.json',map_categories,categories_coco,target='object detection',image_shape=(1200,1600))# Saves converted jsonwithopen('./coco.json','w')asout_file:json.dump(coco_dict,out_file)