This code visualizes model performance results from the MERMAID image classification model.
Loading package libraries and setting parameters
The following libraries and parameters are used throughout the code.
Show the code
rm(list =ls()) #remove past stored objectsoptions(scipen =999) #turn off scientific notation#Package librarieslibrary(readxl)library(tidyverse)library(ggplot2)library(mermaidr)library(ggtext)library(xfun)library(plotly) #for turning the confusion matrices into interactive plotslibrary(htmlwidgets) #saving interactive plots as html fileslibrary(DT) #for interactive data tables
Loading results files
Show the code
### Report with the overall performanceoverallClassReportTBL <-read_excel(path ="../data/classifier_metrics.xlsx",sheet ="Metrics")### Report with the performance per label (UUIDs)labelClassReportTBL <-read_excel(path ="../data/classifier_metrics.xlsx",sheet ="Classification_Report")### Label mapping --> UUID to characterlabelMapTBL <-read_csv(file ="../data/LabelMap.csv")### MERMAID benthic attributesbenthicAttTBL <-mermaid_get_reference(reference ="benthicattributes") %>%select(name, parent) %>%rename(ba = name)### User testing results (for confusion matrices)userResTBL <-read_csv(file ="../data/anonymizedUserTestingResults.csv")
Prepare data
Show the code
overallClassReportTBL <- overallClassReportTBL %>%filter(...1%in%c("precision", "recall", "f1_score")) %>%pivot_wider(names_from = ...1,values_from =`0.0`) %>%mutate(CoralFocus3Label ="<b>Overall</b>",ba ="Overall",tlc ="Overall") %>%rename(`f1-score`= f1_score)labelClassReportTBL <- labelClassReportTBL %>%rename(label_id = ...1) %>%filter(!label_id %in%c("accuracy", "weighted avg", "macro avg")) %>%left_join(labelMapTBL, by ="label_id") %>%select(CoralFocus3Label, `f1-score`, precision, recall)#### Get the top level categories for each of the labels## Extract just the benthic attribute from the labellabelClassReportTBL <- labelClassReportTBL %>%mutate(ba =gsub(pattern =paste(" -",c("Branching","Foliose","Encrusting","Plates or tables","Massive","Digitate"),collapse ="|"),replacement ="",x = CoralFocus3Label))### Get all the unique benthic attributes and assign them to the top level categoriesuniqueBaParentTBL <- labelClassReportTBL %>%select(ba) %>%distinct() # Function to find the top-level categoryfind_top_level <-function(ba, lookup_table) { parent <- lookup_table$parent[lookup_table$ba == ba]if (is.na(parent)) {return(ba) # If no parent, the current ba is the top-level category } else {# Recursively find the top-level categoryreturn(find_top_level(parent, lookup_table)) }}uniqueBaTlcTBL <- uniqueBaParentTBL %>%rowwise() %>%mutate(tlc =find_top_level(ba, benthicAttTBL)) %>%ungroup()labelClassReportTBL <- labelClassReportTBL %>%left_join(uniqueBaTlcTBL, by ="ba")##Get the maximum f1 score by tlc to order tlc as a factorf1ScoreByTlcTBL <- labelClassReportTBL %>%group_by(tlc) %>%summarise(max_f1_score =max(`f1-score`)) %>%ungroup() %>%arrange(desc(max_f1_score))allClassReportTBL <-bind_rows(labelClassReportTBL, overallClassReportTBL) ### Add an asterisk to all the user labels that are not represented in the modeluserResTBL <- userResTBL %>%mutate(ba_user =ifelse(ba_user %in% labelClassReportTBL$ba,as.character(ba_user),paste0(ba_user, "*")))
Plot the results - horizontal barplots by label
Horizontal barplot with labels organized into top level categories