5 Best Ways to Use TensorFlow for Fashion MNIST Dataset in Python

Rate this post

πŸ’‘ Problem Formulation: The task is to build a neural network model that can accurately classify images from the Fashion MNIST dataset, which contains 70,000 grayscale images of 10 different categories of clothing. The input is a 28×28 pixel grayscale image, and the desired output is the correct label for the image, indicating the type of garment it represents.

Method 1: Using a Simple Sequential Model

TensorFlow’s Sequential API allows for easy stacking of layers to create a neural network. For the Fashion MNIST dataset, a basic model might consist of several densely connected layers. This method is great for beginners to understand the concept of neural networks.

Here’s an example:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense

# Load the Fashion MNIST dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Normalize the images
train_images = train_images / 255.0
test_images = test_images / 255.0

# Build the Sequential model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(train_images, train_labels, epochs=10)

The output should be the training process output with the final epoch showing the accuracy on the training set.

This code snippet uses TensorFlow and Keras to build and train a simple neural network on the Fashion MNIST dataset. The Flatten layer transforms the 28×28 image into a 1D array, and the Dense layers perform classification. The model uses the Adam optimizer and is trained for 10 epochs.

Method 2: Implementing Convolutional Neural Networks (CNN)

Convolutional Neural Networks (CNNs) are particularly well-suited to image classification tasks. In a CNN, convolutional and pooling layers are used to automatically learn spatial hierarchies of features from input images, which can significantly boost the accuracy for tasks like Fashion MNIST.

Here’s an example:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Load and prepare the dataset as before

# Build the CNN model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D((2, 2)),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile, train, and evaluate the model as before

The output should be the training process output showing accuracy improvements compared to the simple sequential model.

The layer Conv2D applies filtering to extract features, and MaxPooling2D reduces dimensionality, which helps the neural network understand the images better. The model ends with Flatten and Dense layers for classification.

Method 3: Data Augmentation Integration

Data augmentation expands the dataset with additional training examples created by modifying existing data. It can reduce overfitting and improve model generalization. TensorFlow’s ImageDataGenerator provides an elegant way to implement data augmentation.

Here’s an example:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Load and prepare the dataset as before

# Create an ImageDataGenerator for data augmentation
datagen = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True
)

# Train the model using the generated augmented images
model.fit(datagen.flow(train_images, train_labels, batch_size=32), epochs=10)

The output will demonstrate the training process, potentially with lower overfitting due to augmented data variability.

This snippet uses ImageDataGenerator to augment the training set with random transformations, increasing the diversity of the training data. The model is then fitted on these augmented images which helps it to generalize better when making predictions.

Method 4: Hyperparameter Tuning with Keras Tuner

Hyperparameter tuning can help in finding the optimal architecture for the neural network model. Keras Tuner is a library by TensorFlow for hyperparameter tuning that allows us to conduct an automated search for the best set of hyperparameters for our model.

Here’s an example:

from kerastuner.tuners import RandomSearch

# Define a model-building function for Keras Tuner
def build_model(hp):
    model = Sequential()
    model.add(Flatten(input_shape=(28, 28)))
    
    # Tune the number of units in the Dense layer
    # Choose an optimal value between 32-512
    hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
    model.add(Dense(units=hp_units, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    
    # Tune the learning rate for the optimizer
    # Choose an optimal value from 0.01, 0.001, or 0.0001
    hp_learning_rate = hp.Choice('learning_rate', values=[0.01, 0.001, 0.0001])
    
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    return model

# Create a tuner object
tuner = RandomSearch(
    build_model,
    objective='val_accuracy',
    max_trials=10,
    executions_per_trial=1,
    directory='my_dir',
    project_name='hparam_tuning')

# Perform hyperparameter tuning
tuner.search(train_images, train_labels, epochs=10, validation_split=0.2)

The output will include the best found hyperparameters and validation accuracy after tuning.

This code employs Keras Tuner to search through different combinations of hyperparameters to find the most effective ones. The hp.Int and hp.Choice functions are used to define search spaces for different hyperparameters like the number of units in the Dense layer and learning rate.

Bonus One-Liner Method 5: Pretrained Networks

Using pretrained networks with transfer learning allows the utilization of a pre-trained model as a feature extractor and customizing the final few layers for our specific task. TensorFlow Hub provides access to numerous pretrained models which can be adapted for the Fashion MNIST dataset with minimal effort.

Here’s an example:

import tensorflow_hub as hub

# Load the Fashion MNIST dataset and preprocess as required by the pre-trained model

# Load a pretrained model from TensorFlow Hub
model = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4", input_shape=(28, 28, 1)),
    Dense(10, activation='softmax')
])

# Compile, train, and evaluate the model as before

The output will show the enhanced performance metrics achieved through transfer learning using the pretrained network.

This concise example introduces transfer learning with TensorFlow Hub. The hub.KerasLayer feature allows for easy integration of a pre-trained model, MobileNetV2 in this scenario, as a part of a larger sequential model.

Summary/Discussion

  • Method 1: Using a Simple Sequential Model. Strengths: Simple and easy to implement. Weaknesses: May not capture complex features in images.
  • Method 2: Implementing Convolutional Neural Networks (CNN). Strengths: Better at image recognition due to feature extraction. Weaknesses: Requires more computation and tuning.
  • Method 3: Data Augmentation Integration. Strengths: Can reduce overfitting and improve the model’s generalization. Weaknesses: Increased training time.
  • Method 4: Hyperparameter Tuning with Keras Tuner. Strengths: Can find the best model configuration. Weaknesses: Computationally intensive process.
  • Bonus Method 5: Pretrained Networks. Strengths: Leverages existing models for quick results. Weaknesses: Might not fully adapt to specific data characteristics.