Views
No views yet
1import numpy as np
2import tensorflow as tf
3import matplotlib.pyplot as plt
4from huggingface_hub import from_pretrained_keras
5
6# Download the CIFAR-10 dataset
7(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
8
9class_names = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
10 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
11
12plt.figure(figsize=[10, 10])
13for i in range(25):
14 plt.subplot(5, 5, i+1)
15 plt.xticks([])
16 plt.yticks([])
17 plt.grid(False)
18 plt.imshow(x_test[i], cmap=plt.cm.binary)
19 plt.xlabel(class_names[y_test[i][0]])
20
21plt.show()
22
23# Load the model from the Hub
24model = from_pretrained_keras("AiresPucrs/Cifar-CNN")
25model.compile(
26 loss=tf.keras.losses.CategoricalCrossentropy(),
27 metrics=['categorical_accuracy']
28 )
29x_train = x_train.astype('float32')
30x_train = x_train / 255.
31y_train = tf.keras.utils.to_categorical(y_train, 10)
32x_test = x_test.astype('float32')
33x_test = x_test / 255.
34y_test = tf.keras.utils.to_categorical(y_test, 10)
35test_loss_score, test_acc_score = model.evaluate(x_test, y_test, verbose=0)
36model.summary()
37print(f'Loss: {round(test_loss_score, 2)}.')
38print(f'Accuracy: {round(test_acc_score * 100, 2)} %.')