DEV Community

Ahirton Lopes
Ahirton Lopes

Posted on

Fashion Forward: Leveraging Keras 3.0 for Beginner-Friendly Deep Learning

Exploring the New Features of Keras 3.0 with CNNs on Fashion MNIST

In this post, we will explore the new features introduced in Keras 3.0, which was showcased at Google I/O 2024. We will also dive into a practical example of using Convolutional Neural Networks (CNNs) on the Fashion MNIST dataset, illustrating how to implement these updates effectively.

Image description

Ref. https://blog.gopenai.com/tensorflow-vs-pytorch-vs-keras-9161988c19b9

What’s New in Keras 3.0?

Keras 3.0 brings several improvements and updates that enhance the usability and performance of the library. Here are some of the key features:

Pros of Keras 3.0 Updates:

  1. Integration with TensorFlow: Keras is now fully integrated with TensorFlow, allowing for better performance and streamlined workflows. This makes it easier to access the latest TensorFlow features while using Keras.

  2. Improved API: The updated API provides more clarity and consistency, making it easier for developers to implement models without extensive boilerplate code.

  3. Enhanced Performance: Keras 3.0 includes optimizations that improve training speed and resource management, allowing for more efficient model training.

  4. New Features: New functionalities, such as improved model serialization and advanced preprocessing utilities, make it easier to manage data and models.

Cons of Keras 3.0 Updates:

  1. Learning Curve: For users accustomed to the previous versions, transitioning to Keras 3.0 may require some adjustment, especially with the new API and integrated functionalities.

  2. Potential Compatibility Issues: Existing codebases may face compatibility issues when migrating to the new version, necessitating code revisions and testing.

  3. Documentation Updates: While the documentation is continuously improving, some users may find that certain sections lag behind in terms of clarity or examples for the new features.

Before we dive into the code, let’s take a look at the images from the Fashion MNIST dataset. Below are some samples that illustrate the different categories of clothing we will be classifying.

Practical Example: CNN on Fashion MNIST

Let's implement a simple CNN using Keras 3.0 to classify images from the Fashion MNIST dataset. Below is the complete code, including data preprocessing and model creation.

Image description

Importing Libraries

import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D
from tensorflow.keras.utils import to_categorical
from matplotlib import pyplot as plt
from random import randint
Enter fullscreen mode Exit fullscreen mode

Loading and Preprocessing the Data

# Load the Fashion MNIST dataset
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

# Reshape the data to fit the model's input shape
img_rows, img_cols = 28, 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

# Convert class vectors to binary class matrices (one-hot encoding)
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
Enter fullscreen mode Exit fullscreen mode

Visualizing Sample Images

# Display a random sample image from the training set
plt.imshow(x_train[randint(0, x_train.shape[0])], cmap='Blues_r')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Building the CNN Model

# Define the CNN model
model = Sequential()

# First convolutional layer
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))

# First pooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))

# Second convolutional layer
model.add(Conv2D(64, (3, 3), activation='relu'))

# Second pooling layer
model.add(MaxPooling2D(pool_size=(2, 2)))

# Flatten the output
model.add(Flatten())

# Fully connected layer
model.add(Dense(128, activation='relu'))

# Output layer with 10 neurons for 10 classes
model.add(Dense(10, activation='softmax'))

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

# Model summary
model.summary()
Enter fullscreen mode Exit fullscreen mode

Conclusion

Keras 3.0 introduces exciting advancements that enhance the deep learning workflow, making it more efficient and user-friendly. While there may be some challenges during the transition, the benefits of improved performance, better integration, and a clearer API make it worthwhile. By leveraging the new features, we can build powerful models with greater ease.

Feel free to try the code above, and let me know your thoughts on Keras 3.0!

"For those interested in exploring the code further, you can find the complete example on Google Colab: Fashion MNIST CNN Example. Feel free to run the notebook and experiment with the model!"

Top comments (0)