Convolutional Neural Network (CNN)| CIFAR 10 TensorFlow

by DataFlair Team ·
Published May 21, 2018
· Updated September 15, 2018

1. Objective – TensorFlow CNN

In our previous Tensorflow tutorial, we discussed MNIST with TensorFlow. Today we’ll be learning how to build a Convolutional Neural Network (CNN) using TensorFlow in CIFAR 10 Model. Moreover, in this Convolution Neural Network Tutorial, we will see CIFAR 10 CNN TensorFlow model architecture and also the predictions for this model. Along with this, we will learn training and launching of CIFAR 10 model with TensorFlow Convolutional Neural Network example.
So, let’s begin the Convolutional Neural Network (CNN) in TensorFlow.

Convolutional Neural Network (CNN) TensorFlow

2. Working With Convolutional Neural Network

Before we start, it’ll be good to understand the working of a convolutional neural network. Basically, we will be working on the CIFAR 10 dataset, which is a dataset used for object recognition and consists of 60,000 32×32 images which contain one of the ten object classes including aeroplane, automobile, car, bird, dog, frog, horse, ship, and truck.Read Tensorflow Applications | Learn Various Uses of Tensorflow

3. CIFAR 10 TensorFlow Model Architecture

This Convolutional neural network Model achieves a peak performance of about 86% accuracy within a few hours of training time on a GPU. Following is a list of the files you’ll be needing: cifar10_input.pyReads the native CIFAR-10 binary file format.cifar10.py Builds the CIFAR-10 model.cifar10_train.pyTrains a CIFAR-10 model on a CPU or GPU.cifar10_multi_gpu_train.pyTrains a CIFAR-10 model on multiple GPUs.cifar10_eval.pyEvaluates the predictive performance of a CIFAR-10 model.Explore TensorFlow Features

a. Inputs

Basically, the input part of the CIFAR 10 CNN TensorFlow model is built by the functions inputs() and distorted_inputs() which read images from the CIFAR 10 binary data files. These files contain fixed byte length records, so you can use tf.FixedLengthRecordReader. You can look at Reading Data to learn more about how the Reader class works.First, crop the images are up to 24 x 24 pixels. To make the model insensitive to the dynamic range they are approximately whitened. For training, you can apply a series of random distortions like flipping the image horizontally, changing the brightness or the contrast, to artificially increase the data set size

4. Prediction for CIFAR 10 Model

The prediction part of the CIFAR 10 Convolutional Neural Network model is constructed by the inference() function which adds operations to compute the logic of the predictions. The following are the layers you need to build for the model to work properly:Let’s discuss Tensorflow Pros and Cons

Conv1convolution and rectified linear activation.

Pool1max pooling.

Norm1local response normalization.

Conv2convolution and rectified linear activation.

Norm2local response normalization.

Pool2max pooling.

Local3fully connected layer with rectified linear activation.

Local4fully connected layer with rectified linear activation.

Softmax_linearlinear transformation to produce logic.

Prediction of CIFAR-10 CNN

5. Training the CIFAR 10 Model

Now, we can use multinomial logistic regression, which is softmax regression. Softmax regression applies a nonlinearity to the output of the network and calculates the cross-entropy between the normalized predictions and the label index as described in the previous articles. The sum of the cross-entropy loss is the objective function of the model and all these weight decay terms, as returned by the loss() function.Read TensorFlow API Documentation | Use Of TensorFlow APICIFAR 10 Loss

6. Launching the CIFAR 10 CNN Model

Now, you can launch and run the training operation with the script.python cifar10_train.pyHere, the reported loss is the average loss of the most recent batch. This loss is the sum of the cross-entropy and all weight decay terms.cifar10_train.py periodically saves all model parameters in checkpoint files but it does not evaluate the model. cifar10_eval.py use the checkpoint file to measure the predictive performance (see Evaluating a Model below).TensorBoard provides the functionality of displaying data exported periodically from cifar10_train.py via a tf.summary.FileWriter.Learn TensorBoard

7. Evaluation of CNN Model

By the given below script, a model is evaluated. It uses all 10,000 images in the evaluation set of CIFAR 10 and constructs a model with the inference. It calculates the precision at how often the top prediction matches the label of the image.python cifar10_eval.pySo, do not run the evaluation and training binary on the same GPU as you can run out of memory.

Revise TensorFlow Image Recognition Using – Python & C++Now, a model copy might be trained on a stale copy of the model parameters, so employing asynchronous updates of model parameters leads to not so good training performance. Also, employing fully synchronous updates will be as slow as the slowest model replica.

9. Launching and Training the Model on Multiple GPU Cards

You can use multiple GPU cards installed on your system to train the model faster with the cifar10_multi_gpu_train.py script. Code ExampleNow, you can browse through the following code for a better understanding.

10. Conclusion

Hence, in this TensorFlow Convolutional Neural Network tutorial, we have seen TensorFlow Model Architecture, prediction of CIFAR 10 Model, and code with the example of CNN. Moreover, the example code is a reference for those who find the implementation hard, so that you can directly run it through Linux. At last, we saw training and launching of the CNN model. Finally, we discussed it with multiple GPU cards. Furthermore, if you have any query regarding Convolutional Neural Network, feel free to ask in the comment section. See also –TensorFlow MobileFor reference

No Responses

In the below line as referenced from your code::
x_batch, y_true_batch, _, cls_batch = data.train.next_batch(batch_size)
When I type “data” I do not get any suggestion from PyCharm IDE. and I get “unressolved reference “data”. Can you please share your views on this please. I am using python 3.6 in windows environment,and python 3.5 in Ubuntu. Both has tensorflow version as 1.9.0. Can you please suggest me