This project aims to build a machine learning model using TensorFlow to classify images from the Fashion MNIST dataset. The Fashion MNIST dataset consists of 70,000 grayscale images in 10 categories. The images show individual articles of clothing at low resolution (28x28 pixels), as seen in the figure below.
The Fashion MNIST dataset contains:
- 60,000 training images
- 10,000 test images
Each image is a 28x28 pixel grayscale image, associated with a label from 10 classes:
- T-shirt/top
- Trouser
- Pullover
- Dress
- Coat
- Sandal
- Shirt
- Sneaker
- Bag
- Ankle boot
We start by importing the necessary libraries:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Load the Fashion MNIST dataset from TensorFlow's Keras API:
fmnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = fmnist.load_data()
Check the shape and content of the data:
index = 0
np.set_printoptions(linewidth=320)
print(f'LABEL: {training_labels[index]}')
print(f'\nIMAGE PIXEL ARRAY:\n {training_images[index]}')
plt.imshow(training_images[index], cmap='Greys')
Normalize the images to have values between 0 and 1:
training_images = training_images / 255.0
test_images = test_images / 255.0
Define the neural network model:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
Compile the model with an optimizer, loss function, and metrics:
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Train the model with the training data:
model.fit(training_images, training_labels, epochs=10)
Evaluate the model using the test data:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
Use the trained model to make predictions:
predictions = model.predict(test_images)
The model is evaluated on the test set, achieving an accuracy of approximately X% (replace X with the actual accuracy achieved).
This project demonstrates how to build and train a neural network model to classify images from the Fashion MNIST dataset using TensorFlow. The model can be further improved by experimenting with different architectures, optimizers, and hyperparameters.
- TensorFlow
- NumPy
- Matplotlib
- Clone this repository.
- Install the required packages using
pip install -r requirements.txt
. - Run the Jupyter notebook to execute the code and train the model.
- Experiment with different neural network architectures.
- Implement data augmentation to improve model robustness.
- Explore transfer learning with pre-trained models.