<jupyter_start><jupyter_text>Computer Vision Tutorial Convolutional Neural Networks---In this notebook, we train a CNN to classify images from the CIFAR-10 database. 1. Load CIFAR-10 Database<jupyter_code>import tensorflow as tf 

import matplotlib.pyplot as plt
import numpy as np


import os
import keras.backend.tensorflow_backend as KTF

os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth=True
session = tf.compat.v1.Session(config=config)

KTF.set_session(session)
import keras
from keras.datasets import cifar10

# load the pre-shuffled train and test data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()<jupyter_output>Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 191s 1us/step<jupyter_text>2. Visualize the First 24 Training Images<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(20,5))
for i in range(36):
    ax = fig.add_subplot(3, 12, i + 1, xticks=[], yticks=[])
    ax.imshow(np.squeeze(x_train[i]))<jupyter_output><empty_output><jupyter_text>3. Rescale the Images by Dividing Every Pixel in Every Image by 255In fact, the cost function has the shape of a bowl, but it can be an elongated bowl if the features have verydifferent scales. Figure below shows Gradient Descent on a training set where features 1 and 2 have thesame scale (on the left), and on a training set where feature 1 has much smaller values than feature 2 (onthe right).** Tip: ** When using Gradient Descent, you should ensure that all features have a similar scale to speed up training or else it will take much longer to converge.![feature scaling](normalized.jpg)<jupyter_code># rescale [0,255] --> [0,1]
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255<jupyter_output><empty_output><jupyter_text>4.  Break Dataset into Training, Testing, and Validation Sets<jupyter_code>from keras.utils import np_utils

# one-hot encode the labels
num_classes = len(np.unique(y_train))
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# break training set into training and validation sets
(x_train, x_valid) = x_train[5000:], x_train[:5000]
(y_train, y_valid) = y_train[5000:], y_train[:5000]

# print shape of training set
print('x_train shape:', x_train.shape)

# print number of training, validation, and test images
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print(x_valid.shape[0], 'validation samples')<jupyter_output>x_train shape: (45000, 32, 32, 3)
45000 train samples
10000 test samples
5000 validation samples<jupyter_text>5. Define the Model Architecture ![feature scaling](cnn-schema1.jpg)<jupyter_code>from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense , Dropout,Conv2D, MaxPooling2D, Flatten

model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, padding='same', activation='relu', 
                        input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))

model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 32, 32, 16)        208       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 16, 16, 16)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 16, 16, 32)        2080      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 8, 8, 32)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 8, 8, 64)          8256      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 4, 4, 64)          0         
________________________________________________________[...]<jupyter_text>6. Compile the Model<jupyter_code># compile the model
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>7. Train the Model<jupyter_code>from keras.callbacks import ModelCheckpoint   

# train the model
checkpointer = ModelCheckpoint(filepath='model.weights.best.hdf5', verbose=1, save_best_only=True)

hist = model.fit(x_train, y_train, batch_size=200, epochs=100,
          validation_data=(x_valid, y_valid), callbacks=[checkpointer], 
          verbose=2, shuffle=True)<jupyter_output>Train on 45000 samples, validate on 5000 samples
Epoch 1/100

Epoch 00001: val_loss improved from inf to 1.63054, saving model to model.weights.best.hdf5
45000/45000 - 8s - loss: 1.8135 - accuracy: 0.3493 - val_loss: 1.6305 - val_accuracy: 0.4190
Epoch 2/100

Epoch 00002: val_loss improved from 1.63054 to 1.31626, saving model to model.weights.best.hdf5
45000/45000 - 5s - loss: 1.4909 - accuracy: 0.4710 - val_loss: 1.3163 - val_accuracy: 0.5446
Epoch 3/100

Epoch 00003: val_loss improved from 1.31626 to 1.22462, saving model to model.weights.best.hdf5
45000/45000 - 5s - loss: 1.3520 - accuracy: 0.5197 - val_loss: 1.2246 - val_accuracy: 0.5782
Epoch 4/100

Epoch 00004: val_loss improved from 1.22462 to 1.13643, saving model to model.weights.best.hdf5
45000/45000 - 5s - loss: 1.2589 - accuracy: 0.5515 - val_loss: 1.1364 - val_accuracy: 0.6064
Epoch 5/100

Epoch 00005: val_loss did not improve from 1.13643
45000/45000 - 5s - loss: 1.1773 - accuracy: 0.5816 - val_loss: 1.1406 - val_accurac[...]<jupyter_text>8. Load the Model with the Best Validation Accuracy<jupyter_code># load the weights that yielded the best validation accuracy
model.load_weights('model.weights.best.hdf5')<jupyter_output><empty_output><jupyter_text>9. Calculate Classification Accuracy on Test Set<jupyter_code># evaluate and print test accuracy
score = model.evaluate(x_test, y_test, verbose=0)
print('\n', 'Test accuracy:', score[1])<jupyter_output>Test accuracy: 0.76<jupyter_text>10. Visualize Some PredictionsThis may give you some insight into why the network is misclassifying certain objects.<jupyter_code># get predictions on the test set
y_hat = model.predict(x_test)

# define text labels (source: https://www.cs.toronto.edu/~kriz/cifar.html)
cifar10_labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
# plot a random sample of test images, their predicted labels, and ground truth
fig = plt.figure(figsize=(20, 8))
for i, idx in enumerate(np.random.choice(x_test.shape[0], size=32, replace=False)):
    ax = fig.add_subplot(4, 8, i + 1, xticks=[], yticks=[])
    ax.imshow(np.squeeze(x_test[idx]))
    pred_idx = np.argmax(y_hat[idx])
    true_idx = np.argmax(y_test[idx])
    ax.set_title("{} ({})".format(cifar10_labels[pred_idx], cifar10_labels[true_idx]),
                 color=("green" if pred_idx == true_idx else "red"))<jupyter_output><empty_output><jupyter_text><jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12275409836065573, 'fraction_numerical': 0.06609836065573771, 'mean_word_length': 5.292079207920792, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '79101', 'license': 'MIT', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2503, 'n_words': 657, 'path': '02-cifar/cifar10_cnn_simple.ipynb', 'repo_name': 'achuwilson/dl4vision', 'size': '7625'}