Update README.md
Browse files
README.md
CHANGED
|
@@ -3,42 +3,49 @@ library_name: keras
|
|
| 3 |
tags:
|
| 4 |
- Image Classification
|
| 5 |
---
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
tags:
|
| 4 |
- Image Classification
|
| 5 |
---
|
| 6 |
+
# Cifar-CNN (Teeny-Tiny Castle)
|
| 7 |
+
|
| 8 |
+
This model is part of a tutorial tied to the [Teeny-Tiny Castle](https://github.com/Nkluge-correa/TeenyTinyCastle), an open-source repository containing educational tools for AI Ethics and Safety research.
|
| 9 |
+
|
| 10 |
+
## How to Use
|
| 11 |
+
|
| 12 |
+
```python
|
| 13 |
+
import numpy as np
|
| 14 |
+
import tensorflow as tf
|
| 15 |
+
import matplotlib.pyplot as plt
|
| 16 |
+
from huggingface_hub import from_pretrained_keras
|
| 17 |
+
|
| 18 |
+
# Download the CIFAR-10 dataset
|
| 19 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
|
| 20 |
+
|
| 21 |
+
class_names = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
|
| 22 |
+
'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
|
| 23 |
+
|
| 24 |
+
plt.figure(figsize=[10, 10])
|
| 25 |
+
for i in range(25):
|
| 26 |
+
plt.subplot(5, 5, i+1)
|
| 27 |
+
plt.xticks([])
|
| 28 |
+
plt.yticks([])
|
| 29 |
+
plt.grid(False)
|
| 30 |
+
plt.imshow(x_test[i], cmap=plt.cm.binary)
|
| 31 |
+
plt.xlabel(class_names[y_test[i][0]])
|
| 32 |
+
|
| 33 |
+
plt.show()
|
| 34 |
+
|
| 35 |
+
# Load the model from the Hub
|
| 36 |
+
model = from_pretrained_keras("AiresPucrs/Cifar-CNN")
|
| 37 |
+
model.compile(
|
| 38 |
+
loss=tf.keras.losses.CategoricalCrossentropy(),
|
| 39 |
+
metrics=['categorical_accuracy']
|
| 40 |
+
)
|
| 41 |
+
x_train = x_train.astype('float32')
|
| 42 |
+
x_train = x_train / 255.
|
| 43 |
+
y_train = tf.keras.utils.to_categorical(y_train, 10)
|
| 44 |
+
x_test = x_test.astype('float32')
|
| 45 |
+
x_test = x_test / 255.
|
| 46 |
+
y_test = tf.keras.utils.to_categorical(y_test, 10)
|
| 47 |
+
test_loss_score, test_acc_score = model.evaluate(x_test, y_test, verbose=0)
|
| 48 |
+
model.summary()
|
| 49 |
+
print(f'Loss: {round(test_loss_score, 2)}.')
|
| 50 |
+
print(f'Accuracy: {round(test_acc_score * 100, 2)} %.')
|
| 51 |
+
```
|