How to train a CNN using tf

This recipe helps you train a CNN using tf

Recipe Objective

How to train a CNN using tf?

The CNN is nothing but the convolution neural networks, it contains the images data. Here in our example we are going to use images data named as "CIFAR10" data which consist of 60,000 color images in 10 classes in which 6000 images per class. There two sets in which the data is being divided, 50,000 images are in training set where 10,000 images are in testing set. There is no overlap between the classes as they are mutually exclusive.

Step 1 - Import library

import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt

Step 2 - Load Dataset

(training_images_data, training_labels_data), (testing_images_data, testing_labels_data) = datasets.cifar10.load_data()

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 2s 0us/step

Step 3 - Normalize the data

training_images_data, testing_images_data = training_images_data / 255.0, testing_images_data / 255.0

Step 4 - Verify the data

names_classes = ['airplane', 'vehicles', 'birds', 'animal-cats', 'animal-deer', 'animal-dog', 'animal-frog', 'animal-horse', 'ship', 'vehicles-truck'] plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(training_images_data[i], cmap=plt.cm.binary)

Step 5 - Build CNN model

CNN_model = models.Sequential() CNN_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) CNN_model.add(layers.MaxPooling2D((2, 2))) CNN_model.add(layers.Conv2D(64, (3, 3), activation='relu')) CNN_model.add(layers.MaxPooling2D((2, 2))) CNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))

Step 6 - Check the summary of model

CNN_model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     
=================================================================
Total params: 56,320
Trainable params: 56,320
Non-trainable params: 0
_________________________________________________________________

Step 7 - Add the dense layers

CNN_model.add(layers.Flatten()) CNN_model.add(layers.Dense(64, activation='relu')) CNN_model.add(layers.Dense(10)) CNN_model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                65600     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
_________________________________________________________________
flatten_1 (Flatten)          (None, 10)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 64)                704       
_________________________________________________________________
dense_3 (Dense)              (None, 10)                650       
=================================================================
Total params: 123,924
Trainable params: 123,924
Non-trainable params: 0
_________________________________________________________________

Step 8 - Train our Model

CNN_model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) history_model = CNN_model.fit(training_images_data, training_labels_data, epochs=10, validation_data=(testing_images_data, testing_labels_data))

Epoch 1/10
1563/1563 [==============================] - 68s 43ms/step - loss: 1.7811 - accuracy: 0.3324 - val_loss: 1.2650 - val_accuracy: 0.5381
Epoch 2/10
1563/1563 [==============================] - 65s 42ms/step - loss: 1.2315 - accuracy: 0.5578 - val_loss: 1.1343 - val_accuracy: 0.5967
Epoch 3/10
1563/1563 [==============================] - 64s 41ms/step - loss: 1.0499 - accuracy: 0.6307 - val_loss: 0.9888 - val_accuracy: 0.6536
Epoch 4/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.9325 - accuracy: 0.6677 - val_loss: 0.9913 - val_accuracy: 0.6578
Epoch 5/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.8421 - accuracy: 0.7041 - val_loss: 0.9118 - val_accuracy: 0.6846
Epoch 6/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.7929 - accuracy: 0.7223 - val_loss: 0.9092 - val_accuracy: 0.6850
Epoch 7/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.7191 - accuracy: 0.7469 - val_loss: 0.8908 - val_accuracy: 0.6999
Epoch 8/10
1563/1563 [==============================] - 65s 41ms/step - loss: 0.6794 - accuracy: 0.7593 - val_loss: 0.9160 - val_accuracy: 0.6854
Epoch 9/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.6401 - accuracy: 0.7737 - val_loss: 0.9232 - val_accuracy: 0.6938
Epoch 10/10
1563/1563 [==============================] - 65s 42ms/step - loss: 0.5925 - accuracy: 0.7947 - val_loss: 0.9415 - val_accuracy: 0.6871

Step 9 - Evaluate the model

plt.plot(history_model.history['accuracy'], label='accuracy') plt.plot(history_model.history['val_accuracy'], label = 'val_accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.ylim([0.5, 1]) plt.legend(loc='lower right') testing_loss, testing_acc = CNN_model.evaluate(testing_images_data, testing_labels_data, verbose=2) {"mode":"full","isActive":false}

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling