beginner_image_classifier_mnist
Beginner - Image Classifier (MNIST) with Keras Sequential API
Description
This project is a classic "Hello, World!" of deep learning. It demonstrates how to build, train, and evaluate a basic neural network for image classification. The goal is to classify grayscale images of handwritten digits (0 through 9) from the famous MNIST dataset.
We use the tf.keras.Sequential API, which is a straightforward way to build a model by defining a linear stack of layers. This project is perfect for beginners looking to understand the fundamental workflow of a deep learning project.
Functionality
- Load Data: The script automatically downloads and loads the MNIST dataset from Keras, which is already split into training and testing sets.
- Preprocess Data:
- The 28x28 pixel images are flattened into 784-dimensional vectors.
- The pixel values, originally ranging from 0 to 255, are normalized to a range of 0 to 1. This helps improve the training process.
- Build Model: A simple neural network is constructed using the Keras Sequential API. It consists of:
- An input layer.
- A hidden
Denselayer withReLUactivation. - A
Dropoutlayer to help prevent overfitting. - An output
Denselayer withSoftmaxactivation to produce a probability distribution across the 10 digit classes.
- Compile and Train: The model is compiled with an optimizer (
adam), a loss function (sparse_categorical_crossentropy), and a metric to monitor (accuracy). It is then trained on the training data using the.fit()method. - Evaluate: The trained model's performance is evaluated on the unseen test data to get a final measure of its accuracy.
- Predict and Visualize: The script makes predictions on a few sample images from the test set and uses
matplotlibto display the images along with their true and predicted labels.
Architecture
TensorFlow&Keras: The project is built using the high-level Keras API within the TensorFlow framework.- Sequential Model: A simple, linear stack of layers is used to define the network.
- Dense Layers: The core building blocks of this network, where each neuron is connected to every neuron in the previous layer.
- Activation Functions:
ReLU(Rectified Linear Unit): A common activation function for hidden layers that helps with non-linearity.Softmax: Used in the final layer to convert the model's raw output scores into probabilities for each of the 10 classes.
matplotlib: Used to visualize the training progress (loss and accuracy over epochs) and the final prediction results.
How to Run
Prerequisites
Make sure you have Python installed, along with the required libraries. You can install them using pip:
pip install tensorflow numpy matplotlib
Execution
To run the project, navigate to the project directory and execute the following command:
python beginner_image_classifier_mnist.py
The script will print the model's architecture, show the training progress for 10 epochs, and then print the final test accuracy. Finally, it will display plots of the training history and a few sample images with their predictions.
Concepts Covered
- Image Classification: The task of assigning a label to an entire image.
- Neural Networks: The basic structure of a simple feed-forward neural network.
- Dense (Fully Connected) Layers: The fundamental layer type in many neural networks.
- Activation Functions: The role of
ReLUandSoftmax. - Loss Function: Understanding
sparse_categorical_crossentropyfor multi-class classification. - Optimizer: The role of an optimizer like
Adamin the training process. - Training, Validation, and Testing: The standard workflow for training and evaluating a model.
- Overfitting: The concept and how
Dropoutcan help mitigate it.