Book Image

Hands-On Convolutional Neural Networks with TensorFlow

By : Iffat Zafar, Giounona Tzanidou, Richard Burton, Nimesh Patel, Leonardo Araujo
Book Image

Hands-On Convolutional Neural Networks with TensorFlow

By: Iffat Zafar, Giounona Tzanidou, Richard Burton, Nimesh Patel, Leonardo Araujo

Overview of this book

Convolutional Neural Networks (CNN) are one of the most popular architectures used in computer vision apps. This book is an introduction to CNNs through solving real-world problems in deep learning while teaching you their implementation in popular Python library - TensorFlow. By the end of the book, you will be training CNNs in no time! We start with an overview of popular machine learning and deep learning models, and then get you set up with a TensorFlow development environment. This environment is the basis for implementing and training deep learning models in later chapters. Then, you will use Convolutional Neural Networks to work on problems such as image classification, object detection, and semantic segmentation. After that, you will use transfer learning to see how these models can solve other deep learning problems. You will also get a taste of implementing generative models such as autoencoders and generative adversarial networks. Later on, you will see useful tips on machine learning best practices and troubleshooting. Finally, you will learn how to apply your models on large datasets of millions of images.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Image classification with TensorFlow


In this section, we will show you how to implement a relatively simple CNN architecture. We will also look at how to train it to classify the CIFAR-10 dataset.

 

 

Start by importing all the necessary libraries:

import fire 
import numpy as np 
import os 
import tensorflow as tf 
from tf.keras.datasets import cifar10 

We will define a Python class that will implement the training process. The class name is Train, and it implements two methods: build_graph and train. The train function is fired when the main program is executed:

class Train:  

   __x_ = []
     __y_ = []
     __logits = []
     __loss = []
     __train_step = []
     __merged_summary_op = []
     __saver = []
     __session = []
     __writer = []
     __is_training = []
     __loss_val = []
     __train_summary = []
     __val_summary = []

   def __init__(self):
         pass 

   def build_graph(self): 

   [...] 

   def train(self, save_dir='./save', batch_size=500): 

[...] 

if __name__...