This topic describes how to evaluate a model based on metrics such as the accuracy rate and recall rate, and how to view and compare model evaluation results.
Prerequisites
- A model is created and associated with a training job. For more information, see Model management.
- A persistent volume claim (PVC) is created. For more information, see Mount a statically provisioned NAS volume in the console or Mount an OSS bucket as a statically provisioned volume in the console.
Create an evaluation job
View evaluation metrics
Compare evaluation metrics
Write evaluation job code
Perform the following steps to write custom evaluation job code. For more information about the sample code, see Sample evaluation job code.
Sample evaluation job code
The following sample code is used to evaluate the model trained based on the MNIST
data set of TensorFlow 1.15.
from kubeai.evaluate.evaluator import Evaluator
from abc import ABC
from kubeai.api import KubeAI
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, models
class CNN(object):
def __init__(self):
model = models.Sequential()
model.add(layers.Conv2D(
32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
self.model = model
class TensorflowEvaluatorDemo(Evaluator, ABC):
def preprocess_dataset(self): # Preprocess the data set.
with np.load(self.dataset_dir) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
train_images = x_train.reshape((60000, 28, 28, 1))
test_images = x_test.reshape((10000, 28, 28, 1))
train_images, test_images = train_images / 255.0, test_images / 255.0
train_images, train_labels = train_images, y_train
test_images, test_labels = test_images, y_test
test_loader = {
"test_images" : test_images,
"test_labels" : test_labels
}
return test_loader
def load_model(self): # Load the model.
latest = tf.train.latest_checkpoint(self.model_dir)
self.cnn = CNN()
self.model = self.cnn.model
self.model.load_weights(latest)
def evaluate_model(self, dataset): # Evaluate the model.
metrics = Utils.evaluate_function_classification_tensorflow1(model=self.model, evaluate_x=dataset["test_images"], evaluate_y=dataset["test_labels"])
predictions = self.model.predict(dataset["test_images"])
pred = []
for arr in predictions:
pred.append(np.argmax(arr))
pred = np.array(pred)
confusion_matrix = mt.confusion_matrix(dataset["test_labels"], pred)
metrics["Confusion_matrix"] = str(confusion_matrix)
return metrics
def report_metrics(self, metrics): # Export the evaluation model.
print(metrics)
Utils.ROC_plot(fpr=metrics["ROC"]["fpr"], tpr=metrics["ROC"]["tpr"], report_dir=self.report_dir)
print("Here is the customer-defined report method")
if __name__ == '__main__':
tensorflow_evaluator = TensorflowEvaluatorDemo() # Create an API client and reference the created Evaluator object. Then, call the Evaluate method to run the evaluation job.
KubeAI.evaluate(tensorflow_evaluator)
# KubeAI.test(tensorflow_evaluator, model_dir, dataset_dir, report_dir) # Perform the test on your on-premises machine.