Introduction to Keras, Part One: Data Loading | by Samhita Alla | Towards Data Science

Keras is a Deep Learning API of TensorFlow 2.0 used for easy and fast experimentation. It is simple to understand, flexible to extend and deploy, and powerful enough to build any neural network.
With the increase in the usage of deep learning to solve real-time problems, it has become quite a necessity to lessen the time consumed to build robust machine learning algorithms, i.e., the time taken from designing an algorithm to putting it into practice to generate the desired model has to be minimal.
Keras has been designed for this very purpose. It is a high-level deep learning API running on top of TensorFlow, a machine learning/deep learning framework. It provides an easy to use, modular, and an organised interface to solve deep learning problems. It is simple to understand and is expressive. It is a flexible API that promotes innovative research in the fields of deep learning.
Keras can be used by researchers and engineers alike to build small and large deep learning applications. With Keras, you can model any deep learning algorithm within no time!
Data Loading is the first step that goes into modeling a deep learning problem. In this article, you will learn the various data loading techniques that you can avail of, to load data into your workspace with ease.
Keras is available in the TensorFlow library. You can directly import it using the following command:
from tensorflow import keras
Raw data cannot be directly processed by a Keras model. The data has to be converted into a suitable format to enable the model to interpret. For example, text has to be converted to numeric tensors, images to floating-point tensors, so on and so forth.
Keras in general accepts three types of inputs — NumPy arrays, TensorFlow Dataset objects, and Python generators.
A NumPy array pertains to a low-level array representation of the data, a Dataset object produces a high-level representation, and a Generator pertains to a batch of data with certain properties.
Here’s an overview of a wide variety of data loading techniques to load several types of data in Keras.
Keras comes with a few in-built datasets — cleaned and vectorized, to help you build simple deep learning models. MNIST, CIFAR10, CIFAR100, IMDB, Fashion MNIST, Reuters newswire, and Boston housing price datasets are available within Keras.
For example, to load MNIST dataset — a dataset comprising handwritten digits each of size 28 * 28 pixels, you can run the code:
This gives the following output:
(60000, 28, 28)(60000,)(10000, 28, 28)(10000,)
You’ve got both train and test datasets by importing the dataset from tf.keras.datasets library. The path parameter is to create a local cache of the MNIST dataset, stored as a compressed NumPy file. The output here tells us that there are 60000 train and 10000 test images.
To load images from a URL, use the get_file() method to fetch the data by passing the URL as an arguement. This stores the data in a local directory.
To load images from a local directory, use image_dataset_from_directory() method to convert the directory to a valid dataset to be used by a deep learning model.
On running the code, you get the following output:
Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz228818944/228813984 [==============================] - 2s 0us/stepFound 3670 files belonging to 5 classes.<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>
image_size and batch_size parameters specify the size of an image and the number of dataset batches respectively.
Now check the shape of training data and labels present in the dataset.
On running this code, you get the following output:
(64, 180, 180, 3)(64,)
Each RGB image is of size 180 * 180, and each batch has 64 such images. Data labels is a 1D array specifying a label against every image.
Using matplotlib library, you can show the images present in the dataset.
An Image Generator class can be used to specify certain traits to the images in a dataset. Using the data_dir which you generated in section 2.2. Loading Images, initialize an image generator and apply it onto the dataset.
The parameters used in ImageDataGenerator class are as follows:
rescale is the rescaling factor,
rotation_range specifies the degree to be used for random rotations of the images,
width_shift_range specifies the upper bound by which the image is to be randomly shifted, either left or right,
height_shift_range specifies the upper bound by which the image is to be randomly shifted, either up or down,
and vertical_flip flips the image vertically.
On running the code, you get the following output:
Found 3670 images belonging to 5 classes.(32, 256, 256, 3)(32, 5)
Here’s how the image data now looks like:
To load a file comprising text present locally, you can run the following command:
data = open("sample_data/mnist_test.csv").read()
If the data is to be fetched from a URL or is present in a local directory, run the following commands:
Here, you are importing a tarred directory. You are later untarring it and fetching the directory which has the actual dataset.
On running this code, you get the following output:
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz84131840/84125825 [==============================] - 2s 0us/step
Now print the directories present in dataset_dir.
os.listdir(dataset_dir)
You’ll receive the following output:
['neg','urls_unsup.txt','labeledBow.feat','urls_neg.txt','unsup','urls_pos.txt','pos','unsupBow.feat']
To get to know the data, pick a random file and read the content.
open(os.path.join(dataset_dir, 'train/urls_neg.txt'), 'r').read()
The output you get is:
http://www.imdb.com/title/tt0064354/usercommentshttp://www.imdb.com/title/tt0100680/usercommentshttp://www.imdb.com/title/tt0100680/usercommentshttp://www.imdb.com/title/tt0100680/usercommentshttp://www.imdb.com/title/tt0047200/usercomments...
CSV data can be loaded using Python’s pandas library. You can run the below code snippet to load a CSV into your workspace.
The read_csv() method converts a CSV file to a pandas object. You can print the first 5 values in the dataset using the method head().
Length  Diameter  Height  ...  Viscera weight  Shell weight  Age0   0.435     0.335   0.110  ...          0.0775        0.0965    71   0.585     0.450   0.125  ...          0.2075        0.2250    62   0.655     0.510   0.160  ...          0.2825        0.3700   143   0.545     0.425   0.125  ...          0.1495        0.2600   164   0.545     0.420   0.130  ...          0.1695        0.2300   13[5 rows x 8 columns]
You can also convert a pandas object to a numpy array as follows:
The output you see is:
array([[ 0.435 ,  0.335 ,  0.11  , ...,  0.0775,  0.0965,  7.    ],[ 0.585 ,  0.45  ,  0.125 , ...,  0.2075,  0.225 ,  6.    ],[ 0.655 ,  0.51  ,  0.16  , ...,  0.2825,  0.37  , 14.    ],...,])
You get a 2D numpy array.
Again, you can also convert a pandas object to a Dataset object as follows:
The from_tensor_slices() method creates a separate row for each input. Age attribute here, is considered as the dataset label.
The output you get on running the above code is:
tf.Tensor([0.435  0.335  0.11   0.334  0.1355 0.0775 0.0965 7.    ], shape=(8,), dtype=float64)tf.Tensor(7, shape=(), dtype=int64)
For ease of training a model, you can also convert a numpy array into a Dataset object. First, initialize a numpy array and pass it as an argument to from_tensor_slices() method.
tf.data.Dataset.from_tensor_slices(([[1, 2, 0.5, 0.3], [0.3, 0.4, 6, 7]], ['Lily', 'Jasmine']))
This creates a Dataset object as follows:
<TensorSliceDataset shapes: ((4,), ()), types: (tf.float32, tf.string)>
Consider a case where you are building your dataset using a Python function.
def repetition(x):    i = 0    while i <= x:        yield i        i += 1
On calling this function with an argument 5, you get 0, 1, 2, 3, 4, and 5. Say, you want to build a dataset from this — where the dataset has 4 rows and each row has 5 items.
The from_generator() method in the code snippet creates such a dataset using the function repetition. batch(5) indicates the length per batch and take(4), the number of batches.
On running this code, you get the following output:
[0 1 2 3 4][5 0 1 2 3][4 5 0 1 2][3 4 5 0 1]
Altogether, here’s the full-fledged code:
Data Loading is a significant preliminary step to build any deep learning model. Depending on the data that you want to train your model using, you have to load and preprocess it.
In this article, you learned how to load images, text, csv(s), pandas objects and numpy arrays using Keras.
In the next part of this series, you shall learn the techniques used for Data Preprocessing in Keras.
Thanks for reading!