<jupyter_start><jupyter_text>Computer Vision Day 1 - Dense networksWe will train a dense network to classify CIFAR10 images. This code was inspired by the [TensorFlow 2 quickstart for beginners](https://www.tensorflow.org/tutorials/quickstart/beginner). How to use it?The easiest way is to use Google Colab* Go to https://colab.research.google.com* Select `File > open notebook` and upload the notebook* If you want to use a GPU go to `Runtime > Change runtime type`* Start!Or just press the button below and get started!       Run in Google Colab        View source on GitHub   This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:1. Build a neural network that classifies images.2. Train this neural network.3. And, finally, evaluate the accuracy of the model.<jupyter_code>import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}")<jupyter_output><empty_output><jupyter_text>Load and prepare the [cifar10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset. Convert the samples from integers to floating-point numbers.**Questions*** Why do we divide by 255?* Can you explain the shape?<jupyter_code>cifar10 = tf.keras.datasets.cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print(x_train.shape)<jupyter_output><empty_output><jupyter_text>Create a modelFor available layers in Keras see [here](https://www.tensorflow.org/api_docs/python/tf/keras/layers).It is easiest to use a softmax activation in the last layer for multiclass classification problems. However, while using the softmax activition makes the model output more directly interpretable, this approach is discouraged as it's impossible to provide an exact and numerically stable loss calculation for all models whenusing a softmax output. Alternatively you can use a linear activation in the last layer and then apply `tf.nn.softmax(...)` on the predictions.<jupyter_code>model = tf.keras.models.Sequential([
  ...
])
#@title Example Model
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(32, 32, 3)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

# # Example convolutional neural network.
# model = tf.keras.models.Sequential([
#   tf.keras.layers.InputLayer(input_shape=(32,32,3)),
#   tf.keras.layers.Conv2D(16, (3, 3), activation='relu'),
#   tf.keras.layers.MaxPooling2D((2,2)),
#   tf.keras.layers.Dropout(0.2),
#   tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
#   tf.keras.layers.MaxPooling2D((2,2)),
#   tf.keras.layers.Dropout(0.2),
#   tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
#   tf.keras.layers.MaxPooling2D((2,2)),
#   tf.keras.layers.Dropout(0.2),
#   tf.keras.layers.GlobalAveragePooling2D(),
#   tf.keras.layers.Dense(10, activation='softmax')
# ])<jupyter_output><empty_output><jupyter_text>Make some example predictions<jupyter_code>predictions = model(x_train[:1]).numpy()
predictions<jupyter_output><empty_output><jupyter_text>The `losses.SparseCategoricalCrossentropy` loss takes a vector of logits and a `True` index and returns a scalar loss for each example.<jupyter_code>loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)<jupyter_output><empty_output><jupyter_text>This loss is equal to the negative log probability of the true class:It is zero if the model is sure of the correct class.This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to `-tf.math.log(1/10) ~= 2.3`.<jupyter_code>model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
model.summary()
print(f"Loss on the train set: {loss_fn(y_train[:1], predictions).numpy():.2f}")
print(f"Accuracy on the train set: {model.evaluate(x_train,  y_train, verbose=0)[1]:.2f}")<jupyter_output><empty_output><jupyter_text>Next, fit the model parameters using the `model.fit` method.<jupyter_code>model.fit(..., 
          ..., 
          epochs=...
          )
#@title Example answer
# model.fit(x_train, y_train, 
#           epochs=20, 
#           validation_split=0.2,
#           callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1),])<jupyter_output><empty_output><jupyter_text>The `Model.evaluate` method checks the models performance.<jupyter_code>model.evaluate(x_test,  y_test, verbose=2)<jupyter_output><empty_output><jupyter_text>Look at some predictionsClass names for cifar10 can be found [here](https://www.tensorflow.org/api_docs/python/tf/keras/datasets/cifar10/load_data).<jupyter_code>import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
# Get predictions
preds_test = model.predict(x_test).argmax(axis=1)

# define class names
class_names = {0: 'airplane',
               1:	'automobile',
               2:	'bird',
               3:	'cat',
               4:	'deer',
               5:	'dog',
               6:	'frog',
               7:	'horse',
               8:	'ship',
               9:	'truck'}<jupyter_output><empty_output><jupyter_text>Look at some examples and plot the confusion matrix.<jupyter_code>index = 16
plt.title(f"Prediction: {class_names[preds_test[index]]}; True label: {class_names[y_test.ravel()[index]]}")
plt.imshow(x_test[index])
keys = [k for k in class_names.keys()]
names = [v for v in class_names.values()]
cm = confusion_matrix(y_test.ravel(), preds_test, labels = keys)

plt.figure(figsize=(10, 10))
ax = plt.subplot()
ConfusionMatrixDisplay(cm, display_labels=names).plot(values_format='d', ax=ax)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12870771899392888, 'fraction_numerical': 0.020988725065047703, 'mean_word_length': 4.714568880079287, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 6, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '569185', 'license': 'Apache-2.0', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1921, 'n_words': 468, 'path': '1_intro.ipynb', 'repo_name': 'Digital-Health-UMCU/tensorflow_demo', 'size': '5765'}