This task consists of two parts. First, set up and evaluate a convolutional neural network.
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
layers = keras.layers
print("keras", keras.__version__)
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
x_train = (x_train - 128.) / 128.
x_test = (x_test - 128.) / 128.
y_train_one_hot = tf.keras.utils.to_categorical(y_train)
y_test_one_hot = tf.keras.utils.to_categorical(y_test)
model = keras.models.Sequential([
layers.Convolution2D(16, kernel_size=(5, 5), padding="same", activation='elu', input_shape=(32, 32, 3)),
layers.Convolution2D(16, kernel_size=(3, 3), padding="same", activation='elu'),
layers.Convolution2D(32, kernel_size=(3, 3), padding="same", strides=(2, 2), activation='elu'),
layers.Convolution2D(32, kernel_size=(3, 3), padding="same", activation='elu'),
layers.Convolution2D(64, kernel_size=(3, 3), padding="same", strides=(2, 2), activation='elu'),
layers.Convolution2D(64, kernel_size=(3, 3), padding="same", activation='elu'),
layers.GlobalMaxPooling2D(),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
print(model.summary())
model.compile(
loss='categorical_crossentropy',
optimizer=keras.optimizers.Adam(1e-3),
metrics=['accuracy'])
results = model.fit(x_train, y_train_one_hot,
batch_size=32,
epochs=30,
verbose=1,
validation_split=0.1,
callbacks = [keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=3, verbose=1, min_lr=1e-5)],
)
plt.figure(1, (12, 4))
plt.subplot(1, 2, 1)
plt.plot(results.history['loss'])
plt.plot(results.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.subplot(1, 2, 2)
plt.plot(results.history['accuracy'])
plt.plot(results.history['val_accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.tight_layout()
def plot_prediction(X, Y, Y_predict, fname=False):
"""
Plot image X along with predicted probabilities Y_predict.
X: CIFAR image, shape = (32, 32, 3)
Y: CIFAR label, one-hot encoded, shape = (10)
Y_predict: predicted probabilities, shape = (10)
"""
X = 128 * X + 128
labels = np.array(['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck'])
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
# plot image
ax1.imshow(X.astype('uint8'), origin='upper')
ax1.set(xticks=[], yticks=[])
# plot probabilities
ax2.barh(np.arange(10), Y_predict, align='center')
ax2.set(xlim=(0, 1), xlabel='Score', yticks=[])
for i in range(10):
c = 'red' if (i == np.argmax(Y)) else 'black'
ax2.text(0.05, i, labels[i].capitalize(), ha='left', va='center', color=c)
Visualize the activations in the first two layers of your network for two input images of choice and describe what you see.