Tumgik
datafever · 3 years
Text
Using Tensorflow's Sub-classing API to build CNN models
Hello Everyone! In this article, I'm gonna show you how you can build CNN models with Tensorflow's Subclassing API. Tensorflow's Subclassing API is an high-level API for researchers to create advanced deep learning models. It is a bit hard when compared to Tensorflow's Sequential API because you have to define everthing from scratch in Tensorflow's Subclassing API. to get started, you just need Tensorflow installed.
Step -1 (Import Libraries)
first step is to import necessary libraries. So, we will start by importing:-
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
Step -2 (Prepare Dataset)
In this project, we will be using MNIST dataset. In case you don't know what MNIST dataset is? MNIST Dataset is a collection of 32 x 32 x 1 images of handwritten digits. Now, lets prepare the dataset. Here's the code,
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Add a channels dimension
x_train = x_train[...,tf.newaxis].astype("float32")
x_test = x_test[...,tf.newaxis].astype("float32")
first, we are loading MNIST dataset as (x_train, y_train),(x_test, y_test). then we are normalizing the values of X to the scale of 0 to 1. Later, we're adding new axis to the variable 'X'. We are doing this to add a Single Channel. Currently Our Images are Gray Scaled. So we are adding new channel to the image to make it 2D image. And later converting the values to 'float32'.
Next Step is to create a Tensorflow Dataset to send data to the Model. To do this, here's the code,
train_ds = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
In the above code, we creating a Tensorflow Dataset and then shuffling and batching the dataset.
Step -3 (Model Production)
Here comes the most difference-making part. from this step onwards, our steps of making a model with Tensorflow's Subclassing API changes. Now, we will use keras.Model to define the model. Our Structure of the Model is too Simple. Here's an Overview,
2D Convolutional Layer with 32 filters, 3 kernel size and reLU activation
Flatten Layer
Dense layer with 128 Nodes and reLU activation
Dense layer with 10 Nodes
Here's the code,
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10)
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
# Create an instance of the model
model = MyModel()
Now, our model is ready and nest step is to choose an optimizer and loss function. In this case, I will use Adam optimizer and SparseCategoricalCrossentropy loss function. Code for this is below,
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optimizer = tf.keras.optimizers.Adam()
Next step is to select metrics to measure the loss and the accuracy of the model. These metrics accumulate the values over epochs and then print the overall result.
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
Now, we have to create a function to train the model. We will be using tf.GradientTape() to train the model. we will use @tf.function decorator to define that this function is part of the model. here's the code for that,
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
# training=True is only needed if there are layers with different
# behavior during training versus inference (e.g. Dropout).
predictions = model(images, training=True)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
Now we have to create a function to test the model. Same as train function, we will be using tf.function as decorator for the test function,
@tf.function
def test_step(images, labels):
# training=False is only needed if there are layers with different
# behavior during training versus inference (e.g. Dropout).
predictions = model(images, training=False)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
Our Last & Most important step is to Train the Model and Evaluate the model performance.
EPOCHS = 5
for epoch in range(EPOCHS):
# Reset the metrics at the start of the next epoch
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for images, labels in train_ds:
train_step(images, labels)
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
print(
f'Epoch {epoch + 1}, '
f'Loss: {train_loss.result()}, '
f'Accuracy: {train_accuracy.result() * 100}, '
f'Test Loss: {test_loss.result()}, '
f'Test Accuracy: {test_accuracy.result() * 100}'
)
the code above is very simple. first, we are defining the number of EPOCHS. then we are resetting the states if train & test's loss and accuracy. then we are obtaining our dataset from train_ds and test_ds, which are nothing but Tensorflow Dataset. Later, we are training the model and testing the model performance.
The model accuracy is about 98%. This is good accuracy for such a simple model. This Code is originally provided by Tensorflow on their website.
Hope you learned something new and enjoyed reading this article. I'll come up soon with another interesting article.
2 notes · View notes