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