CoolFace
Apppublic

pepperumo/MVTec_Website

sourceHugging Facemitupdated 2y agoView on Hugging Face
3likes
metrics_calculation.py72 linesDownload Raw Back to root
1import streamlit as st
2import pickle
3
4import plotly.express as px
5import plotly.graph_objects as go
6
7def load_evaluation_metrics(filepath: str):
8    with open(filepath, 'rb') as f:
9        evaluation_metrics = pickle.load(f)
10    return (
11        evaluation_metrics['confusion_matrices'],
12        evaluation_metrics['roc_curves'],
13        evaluation_metrics['auc_scores'],
14        evaluation_metrics['f1_scores']
15    )
16
17def plot_roc_curve(selected_category, roc_curves, auc_scores):
18    fig = go.Figure()
19    roc_data = roc_curves[selected_category]
20    fig.add_trace(go.Scatter(
21        x=roc_data['fpr'],
22        y=roc_data['tpr'],
23        name=selected_category
24    ))
25    fig.add_trace(go.Scatter(
26        x=[0, 1],
27        y=[0, 1],
28        mode='lines',
29        line=dict(dash='dash'),
30        name='Random'
31    ))
32    fig.update_layout(
33        title=f"AUC-ROC Curve - {selected_category}, AUC={auc_scores[selected_category]:.3f}",
34        xaxis_title="False Positive Rate",
35        yaxis_title="True Positive Rate",
36        width=500,
37        height=450,
38        showlegend=False
39    )
40    return fig
41
42def plot_confusion_matrix(selected_category, confusion_matrices, f1_scores):
43    labels = ['OK', 'NOK']
44    conf_matrix = confusion_matrices[selected_category]
45    f1_score = f1_scores[selected_category]
46    fig = px.imshow(
47        conf_matrix,
48        labels=dict(x="True Label", y="Predicted Label"),
49        x=labels,
50        y=labels,
51        color_continuous_scale='Reds',
52        width=500,
53        height=500,
54    )
55    for i in range(len(labels)):
56        for j in range(len(labels)):
57            fig.add_annotation(
58                x=j,
59                y=i,
60                text=str(conf_matrix[i, j]),
61                showarrow=False,
62                font=dict(size=14)
63            )
64    fig.update_layout(
65        title=f"Confusion Matrix - {selected_category}, F1: {f1_score:.3f}",
66        xaxis_title="True Label",
67        yaxis_title="Predicted Label",
68        coloraxis_showscale=False  # This line removes the vertical color scale
69    )
70    return fig
71
72