Build a Simple Neural Network using Numpy

In this article, we will discuss how to make a simple neural network using NumPy.

1. Import Libraries

First, we will import all the packages that we will need. We will need numpyh5py (for loading dataset stored in H5 file), and matplotlib (for plotting).

``````import numpy as np
import matplotlib.pyplot as plt
import h5py
``````

2. Data Preparation

The data is available in (“.h5”) format and contain training and test set of images labeled as cat or non-cat. The dataset is available in github repo for download. Load the dataset using the following function:

``````def load_dataset():
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
train_x = np.array(train_dataset["train_set_x"][:])
train_y = np.array(train_dataset["train_set_y"][:])
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
test_x = np.array(test_dataset["test_set_x"][:])
test_y = np.array(test_dataset["test_set_y"][:])
classes = np.array(test_dataset["list_classes"][:])

train_y = train_y.reshape((1, train_y.shape[0]))
test_y = test_y.reshape((1, test_y.shape[0]))

return train_x, train_y, test_x, test_y, classes
``````

We can analyze the data by looking at their shape.

``````train_x, train_y, test_x, test_y, classes = load_dataset()

print ("Train X shape: " + str(train_x.shape))
print ("Train Y shape: " + str(train_y.shape))
print ("Test X shape: " + str(test_x.shape))
print ("Test Y shape: " + str(test_y.shape))
``````

We have 209 train image where each image is square (height = 64px) and (width = 64px) and have 3 channels (RGB). Similarly, we have 50 test images of the same dimension.

Let us visualize the image. You can change the index to see different images.

``````# change index for another image
index = 2
plt.imshow(train_x[index]
``````

**Data Preprocessing: **The common data preprocessing for image data involves:

1. Figure out the dimensions and shapes of the data (m_train, m_test, num_px, …)
2. Reshape the datasets such that each example is now a vector of size (height * width * channel, 1)
3. “Standardize” the data

First, we need to flatten the image. This can be done by reshaping the images of shape (height, width, channel) in a numpy-array of shape (height ∗ width ∗channel, 1).

``````train_x = train_x.reshape(train_x.shape[0], -1).T
test_x = test_x.reshape(test_x.shape[0], -1).T

print ("Train X shape: " + str(train_x.shape))
print ("Train Y shape: " + str(train_y.shape))
print ("Test X shape: " + str(test_x.shape))
print ("Test Y shape: " + str(test_y.shape))
``````

Standardize the data: The common preprocessing step in machine learning is to center and standardize the dataset. For the given picture datasets, it can be done by dividing every row of the dataset by 255 (the maximum value of a pixel channel).

``````train_x = train_x/255.
test_x = test_x/255.
``````

Now we will build a simple neural network model that can correctly classify pictures as cat or non-cat.

#neural-networks #numpy #image-classification

18.35 GEEK