Simple CNN Architecture for Image Classification
Required Libraries
import tensorflow as tf
from tensorflow.keras import layers, models
Define the CNN Model
# Define the CNN model
def create_simple_cnn(input_shape, num_classes):
model = models.Sequential([
# Convolutional layers
layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
# Flatten the 3D feature maps to 1D
layers.Flatten(),
# Fully connected layers
layers.Dense(64, activation='relu'),
layers.Dense(num_classes, activation='softmax') # Output layer with softmax activation for classification
])
return model
Prepare the Data
# Example: Load dataset (replace with your data loading/preprocessing)
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize pixel values to 0-1 range and add channel dimension (for grayscale images)
train_images = train_images.reshape((60000, 28, 28, 1)) / 255.0
test_images = test_images.reshape((10000, 28, 28, 1)) / 255.0
# Convert labels to categorical format
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
Compile and Train the Model
# Create an instance of the model
model = create_simple_cnn((28, 28, 1), 10) # Input shape (28x28 grayscale images), 10 classes for MNIST
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_data=(test_images, test_labels))
Evaluate the Model
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
Explanation:
- Conv2D Layers: Perform 2D convolution operations to extract features from input images. Each layer applies a set of filters to the input.
- MaxPooling2D Layers: Downsamples the feature maps, reducing their spatial dimensions to capture important features.
- Flatten Layer: Converts the 2D feature maps into a 1D vector to input into fully connected layers.
- Dense Layers: Fully connected layers that perform classification based on learned features.
- Softmax Activation: Used in the output layer for multi-class classification tasks, providing probability scores for each class.
This example sets up a basic CNN using TensorFlow for image classification on the MNIST dataset. Adjust the model architecture, input shape, and number of classes according to your specific task and dataset.