ZFNet CNN for CIFAR-10 Image Classification in Python
Introduction to ZFNet for CIFAR-10 Classification
ZFNet, introduced by Zeiler and Fergus in 2013, is a Convolutional Neural Network (CNN) architecture that achieved state-of-the-art results on the CIFAR-10 image classification dataset. In this presentation, we will explore how to implement ZFNet using Python and the Pandas library for data preprocessing and manipulation.
import pandas as pd
import numpy as np
from keras.datasets import cifar10
Loading the CIFAR-10 Dataset
The CIFAR-10 dataset consists of 60,000 32x32 color images in 10 classes, with 6,000 images per class. We can load the dataset using the Keras library.
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
Data Preprocessing
Before feeding the data to the ZFNet model, we need to preprocess it. This typically involves normalization and reshaping the data to the expected input format.
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
X_train = X_train.reshape(-1, 32, 32, 3)
X_test = X_test.reshape(-1, 32, 32, 3)
One-Hot Encoding the Labels
Since the labels in the CIFAR-10 dataset are integers, we need to one-hot encode them before using them as targets for the classification task.
from keras.utils import to_categorical
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
Defining the ZFNet Architecture
ZFNet is a CNN architecture that consists of several convolutional, pooling, and fully connected layers. We can define the architecture using the Keras library.
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
model = Sequential([
Conv2D(96, (7, 7), strides=(2, 2), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(pool_size=(3, 3), strides=(2, 2)),
# ... (Add more layers here)
])
Compiling the Model
Once the model architecture is defined, we need to compile it with an optimizer, loss function, and evaluation metrics.
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Training the Model
We can train the ZFNet model on the CIFAR-10 dataset using the fit method from Keras.
model.fit(X_train, y_train,
batch_size=64,
epochs=50,
validation_data=(X_test, y_test))
Evaluating the Model
After training, we can evaluate the model's performance on the test set using the evaluate method.
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f'Test accuracy: {test_acc * 100:.2f}%')
Making Predictions
To make predictions on new data, we can use the predict method of the trained model.
import matplotlib.pyplot as plt
# Load a new image
new_image = ... # Load image data here
# Preprocess the new image
new_image = new_image.reshape(1, 32, 32, 3)
new_image = new_image.astype('float32') / 255
# Make a prediction
prediction = model.predict(new_image)
class_idx = np.argmax(prediction)
class_name = cifar10.load_data()[1].class_names[class_idx]
# Display the image and prediction
plt.imshow(new_image.reshape(32, 32, 3))
plt.title(f'Prediction: {class_name}')
plt.show()
Data Augmentation
Data augmentation can be used to artificially increase the size of the training dataset and improve model performance. We can use the ImageDataGenerator class from Keras for this purpose.
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = datagen.flow(X_train, y_train, batch_size=64)
Transfer Learning with ZFNet
Transfer learning is a technique where we can use the pre-trained weights from a model trained on a large dataset and fine-tune it on our specific task. This can lead to better performance and faster convergence.
from keras.applications import ZFNet
base_model = ZFNet(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
# Freeze the base model layers
for layer in base_model.layers:
layer.trainable = False
# Add custom classification layers
x = base_model.output
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(10, activation='softmax')(x)
# Create the transfer learning model
transfer_model = Model(inputs=base_model.input, outputs=predictions)
# Compile and train the model
transfer_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
transfer_model.fit(train_generator, epochs=10, validation_data=(X_test, y_test))
ZFNet Visualization
We can visualize the learned filters and feature maps of the ZFNet model to gain insights into its internal representations.
from keras.models import Model
# Create a model that outputs the activations of a specific layer
layer_idx = 2 # Index of the layer to visualize
layer_outputs = [layer.output for layer in model.layers[:layer_idx+1]]
activation_model = Model(inputs=model.input, outputs=layer_outputs)
# Visualize the activations
img = X_test[0].reshape(1, 32, 32, 3)
activations = activation_model.predict(img)
# Plot the activations
for i, activation in enumerate(activations):
plt.subplot(1, len(activations), i+1)
plt.imshow(activation[0, :, :, :])
plt.title(f'Layer {i}')
plt.show()
ZFNet Performance Analysis
We can analyze the performance of the ZFNet model by evaluating its accuracy, precision, recall, and F1-score on the test set.
from sklearn.metrics import precision_score, recall_score, f1_score
y_pred = model.predict(X_test)
y_pred = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_test, axis=1)
accuracy = (y_pred == y_true).mean()
precision = precision_score(y_true, y_pred, average='macro')
recall = recall_score(y_true, y_pred, average='macro')
f1 = f1_score(y_true, y_pred, average='macro')
print(f'Accuracy: {accuracy:.4f}')
print(f'Precision: {precision:.4f}')
print(f'Recall: {recall:.4f}')
print(f'F1-score: {f1:.4f}')
Additional Resources
For further reading and exploration, here are some additional resources on ZFNet and related topics from ArXiv.org:
- Zeiler, M. D., & Fergus, R. (2013). Visualizing and Understanding Convolutional Networks. arXiv:1311.2901 [cs.CV] https://arxiv.org/abs/1311.2901
- Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). ImageNet Classification with Deep Convolutional Neural Networks. arXiv:1202.2683 [cs.CV] https://arxiv.org/abs/1202.2683
- Simonyan, K., & Zisserman, A. (2014). Very Deep Convolutional Networks for Large-Scale Image Recognition. arXiv:1409.1556 [cs.CV] https://arxiv.org/abs/1409.1556
- Szegedy, C., Liu, W., Jia, Y., Sermanet, P., Reed, S., Anguelov, D., ... & Rabinovich, A. (2015). Going Deeper with Convolutions. arXiv:1409.4842 [cs.CV] https://arxiv.org/abs/1409.4842
- He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep Residual Learning for Image Recognition. arXiv:1512.03385 [cs.CV] https://arxiv.org/abs/1512.03385
These papers cover the original ZFNet architecture, the AlexNet architecture that inspired ZFNet, deeper CNN architectures like VGGNet and GoogLeNet, and the groundbreaking ResNet architecture, which built upon the ideas from previous architectures like ZFNet.