Ajuda a proteger a Grande Barreira de Corais com TensorFlow em Kaggle Junte Desafio

Demonstração do Scikit-Learn Model Card Toolkit

Ver no TensorFlow.org Executar no Google Colab Ver no GitHub Baixar caderno

Fundo

Este bloco de notas demonstra como gerar um cartão de modelo usando o Model Card Toolkit com um modelo scikit-learn em um ambiente Jupyter / Colab. Você pode aprender mais sobre modelos de cartões em https://modelcards.withgoogle.com/about .

Configurar

Primeiro precisamos instalar e importar os pacotes necessários.

Atualize para o Pip 20.2 e instale os pacotes

pip install -q --upgrade pip==20.2
pip install -q -U seaborn scikit-learn model-card-toolkit

Você reiniciou o tempo de execução?

Se você estiver usando o Google Colab, na primeira vez que executar a célula acima, deverá reiniciar o tempo de execução (Tempo de execução> Reiniciar tempo de execução ...).

Pacotes de importação

Importamos os pacotes necessários, incluindo scikit-learn.

from datetime import date
from io import BytesIO
from IPython import display
from model_card_toolkit import ModelCardToolkit
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_roc_curve, plot_confusion_matrix

import base64
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import uuid

Carregar dados

Este exemplo utiliza o conjunto de dados de diagnóstico do cancro da mama Wisconsin que scikit-learn pode carregar usando o load_breast_cancer () função.

cancer = load_breast_cancer()

X = pd.DataFrame(cancer.data, columns=cancer.feature_names)
y = pd.Series(cancer.target)

X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train.head()
y_train.head()
28     0
157    1
381    1
436    1
71     1
dtype: int64

Dados do gráfico

Criaremos vários gráficos a partir dos dados que incluiremos no cartão do modelo.

# Utility function that will export a plot to a base-64 encoded string that the model card will accept.

def plot_to_str():
    img = BytesIO()
    plt.savefig(img, format='png')
    return base64.encodebytes(img.getvalue()).decode('utf-8')
# Plot the mean radius feature for both the train and test sets

sns.displot(x=X_train['mean radius'], hue=y_train)
mean_radius_train = plot_to_str()

sns.displot(x=X_test['mean radius'], hue=y_test)
mean_radius_test = plot_to_str()

png

png

# Plot the mean texture feature for both the train and test sets

sns.displot(x=X_train['mean texture'], hue=y_train)
mean_texture_train = plot_to_str()

sns.displot(x=X_test['mean texture'], hue=y_test)
mean_texture_test = plot_to_str()

png

png

Modelo de trem

# Create a classifier and fit the training data

clf = GradientBoostingClassifier().fit(X_train, y_train)

Avalie o modelo

# Plot a ROC curve

plot_roc_curve(clf, X_test, y_test)
roc_curve = plot_to_str()

png

# Plot a confusion matrix

plot_confusion_matrix(clf, X_test, y_test)
confusion_matrix = plot_to_str()

png

Crie um cartão de modelo

Inicializar kit de ferramentas e cartão de modelo

mct = ModelCardToolkit()

model_card = mct.scaffold_assets()

Anotar informações no cartão do modelo

model_card.model_details.name = 'Breast Cancer Wisconsin (Diagnostic) Dataset'
model_card.model_details.overview = (
    'This model predicts whether breast cancer is benign or malignant based on '
    'image measurements.')
model_card.model_details.owners = [
    {'name': 'Model Cards Team', 'contact': 'model-cards@google.com'}
]
model_card.model_details.references = [
    'https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)',
    'https://minds.wisconsin.edu/bitstream/handle/1793/59692/TR1131.pdf'
]
model_card.model_details.version.name = str(uuid.uuid4())
model_card.model_details.version.date = str(date.today())

model_card.considerations.ethical_considerations = [{
    'name': ('Manual selection of image sections to digitize could create '
            'selection bias'),
    'mitigation_strategy': 'Automate the selection process'
}]
model_card.considerations.limitations = ['Breast cancer diagnosis']
model_card.considerations.use_cases = ['Breast cancer diagnosis']
model_card.considerations.users = ['Medical professionals', 'ML researchers']


model_card.model_parameters.data.train.graphics.description = (
  f'{len(X_train)} rows with {len(X_train.columns)} features')
model_card.model_parameters.data.train.graphics.collection = [
    {'image': mean_radius_train},
    {'image': mean_texture_train}
]
model_card.model_parameters.data.eval.graphics.description = (
  f'{len(X_test)} rows with {len(X_test.columns)} features')
model_card.model_parameters.data.eval.graphics.collection = [
    {'image': mean_radius_test},
    {'image': mean_texture_test}
]
model_card.quantitative_analysis.graphics.description = (
  'ROC curve and confusion matrix')
model_card.quantitative_analysis.graphics.collection = [
    {'image': roc_curve},
    {'image': confusion_matrix}
]

mct.update_model_card_json(model_card)

Gerar cartão de modelo

# Return the model card document as an HTML page

html = mct.export_format()

display.display(display.HTML(html))