TensorFlow provides a high-level API that makes it easy to build a neural network. The  layers module enable you to build fully connected layers and convolutional layers, adding activation functions, and applying dropout regularization and batch normilization.

The tf.data API enables you to build input pipelines for an image model might aggregate data from files in a distributed file system, apply random perturbations to each image, and merge randomly selected images into a batch for training.

The Estimators encapsulate training, evaluation, prediction, export for serving. In this tutorial, you’ll learn how to use TensorFlow high-level API to build a convolutional neural network model to classify the Plane or not Plane in the Satellite Imagery dataset.

Dataset


TensorFlow Classify planes or not

The Keggle ‘s Planes in Satellite Imagery dataset consists of 8000 color images of the plane and 24000 color images of non-plane that we use for training. It provided is a zipped directory planesnet.zip that contains the entire dataset as a 20×20 RGB .png image. Each individual image filename follows a specific format: {label} __ {scene id} __ {longitude} _ {latitude}.png.The label valued 1 or 0, representing the “plane” and “no-plane” class, respectively.

Build Convolutional Neural Networks Model


The first step in creating a model function is to write branching code that implements prediction, evaluation, and training.The model function gets invoked whenever someone calls the Estimator’s train, evaluate, or predict methods.
Convolutional Neural Network using High Level API

This is what we will build. There are three convolutional layers and 20x20x3 image as input. We need to connect this to our softmax layer, which will classify the airplane or non-airplane. The last data cube, we reshape it, flatten it out as one big vector. We apply normal dense layers to this vector and end up with our softmax activation and softmax cross entropy loss because this is a classifier.

The model function is your layers. It returns the predictions and the loss. You put the loss into an optimizer. You’ve got this training operation, which the estimator will run in a loop and whatever evaluation metrics you care about.

Load Training and Test Data


First, download data from kaggle and extract label from the file name.Each individual image filename follows a specific format: {label} __ {scene id} __ {longitude} _ {latitude}.png.Valued 1 or 0, representing the “plane” and “no-plane” class, respectively.

If all of your input data fit in memory, the simplest way to create a Dataset from them is to convert them to Tensor objects and use Dataset.from_tensor_slices(). You can process large datasets that do not fit in memory by converting into TFRecord.

The function starts by using the tf.data.Dataset.from_tensor_slices function to create a tf.data.Datasetrepresenting slices of the array. The array is sliced across the first dimension. For example, an array containing the training data has a shape of (27000, 20, 20). Passing this to from_tensor_slices returns a Datasetobject containing 27000 slices, each one a 20×20 image.

Input Function


The training input function will define how your data goes into the model and I use this dataset API. That’s really good because this dataset API is designed for out of memory datasets. You define what your dataset is and then as your model is training the data is loaded and the loading triggers the loading of additional files from disk.

I’m reading images here, The dataset is initialized from files. I apply some loading operations, which will load those files and decompress them. I usually need to shuffle my data into batches because the training always processed by batches and usually are repeated indefinitely and my dataset is done.

Create Estimators


An Estimator is TensorFlow’s high-level representation of a complete model. It handles the details of initialization, logging, saving and restoring, and many other features so you can concentrate on your model.

I advise you to wrap your model into estimator API. That’s just because in an estimator, TensorFlow has written for you a ton of boilerplate code that is not interesting to write, things like regularly outputting checkpoints. If your training crashes after 24 hours, you can restart from where you were, exporting the model at the end so that you have something that is ready to deploy to a serving infrastructure or distributed training. The distributional algorithms of distributed training also baked in into the estimator.

Train the model

Train the model by calling the Estimator’s train method as follows:

Here we wrap up our input_fn in a lambda to capture the arguments.

Evaluate the trained model

The final step is to evaluate the trained model. We can get some statistics on its performance. The following code block evaluates the accuracy of the trained model on the test data:

 

Download this project from GitHub

 

Related Post

Feeding your own data set into the CNN model in TensorFlow
Convert a directory of images to TFRecords
Deep learning model for Car Price prediction using TensorFlow
Importance of Batch Normalization in TensorFlow

 

Leave a Reply