<jupyter_start><jupyter_text>Training a Computer to Differentiate Cats<jupyter_code>!pip install keras
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Dropout
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD, Adam
from keras import regularizers
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import h5py

%matplotlib inline
np.random.seed(1)
plt.rcParams['figure.figsize'] = (8.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

np.random.seed(1)<jupyter_output><empty_output><jupyter_text>1 - Overview of the data setFirst of all, we'll load the data. After loading, here's a basic overview;- a training set of m_train images labeled as cat (y=1) or non-cat (y=0)- a test set of m_test images labeled as cat or non-cat- each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).<jupyter_code>train_dataset = h5py.File('train.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # train set labels

test_dataset = h5py.File('test.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
classes
train_set_y = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
print(train_set_y.shape)
print(test_set_y.shape)<jupyter_output>(1, 209)
(1, 50)<jupyter_text>Each line of train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the index value and re-run to see other images.<jupyter_code># Example of a picture
index = 56
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +  "' picture.")<jupyter_output>y = [1], it's a 'cat' picture.<jupyter_text>2 - Data preprocessingCommon steps for pre-processing a new dataset are:- Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)- Reshape the datasets such that each example is now a vector of size (num_px * num_px * 3, 1)- "Standardize" the dataMany software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs.  2.1 Dimensions of our data<jupyter_code>m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

print ("Dataset dimensions:")
print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))<jupyter_output>train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)<jupyter_text>2.2 Reshaping data For convenience, we now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px $*$ num_px $*$ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b$*$c$*$d, a) is to use:    X_flatten = X.reshape(X.shape[0], -1).T       X.T is the transpose of X<jupyter_code># Reshape the training and test examples

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1)
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1)
train_set_y = train_set_y.T
test_set_y = test_set_y.T

print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))<jupyter_output>train_set_x_flatten shape: (209, 12288)
train_set_y shape: (209, 1)
test_set_x_flatten shape: (50, 12288)
test_set_y shape: (50, 1)<jupyter_text>2.3 Standardizing data One common preprocessing step in machine learning is to center and standardize your dataset, meaning that you substract the mean of the whole numpy array from each example, and then divide each example by the standard deviation of the whole numpy array. But for picture datasets, it is simpler and more convenient and works almost as well to just divide every row of the dataset by 255 (the maximum value of a pixel channel).Let's standardize our dataset.<jupyter_code>train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.
train_set_x.shape<jupyter_output><empty_output><jupyter_text>3 - Modeling<jupyter_code>from numpy.random import seed
seed(1)
#from tensorflow import set_random_seed
#set_random_seed(2)
# Utility function
def evaluate_this_model(model, epochs):
    history = model.fit(train_set_x, train_set_y, epochs=epochs)
    results = model.evaluate(test_set_x, test_set_y)
    
    plt.plot(np.squeeze(history.history["loss"]))
    plt.ylabel('cost')
    plt.xlabel('iterations (per tens)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()
    
    print("\n\nAccuracy on training set is {}".format(history.history["acc"][-1]))
    print("\nAccuracy on test set is {}".format(results[1]))<jupyter_output><empty_output><jupyter_text>3.1 Starting with a basic model<jupyter_code>model = Sequential()

model.add(Dense(units=25, activation='relu', input_dim=train_set_x_flatten.shape[1]))
model.add(Dense(units=30, activation='relu'))
model.add(Dense(units=20, activation='relu'))
model.add(Dense(units=10, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
learning_rate = 0.0001
opt = Adam(lr=learning_rate)
model.compile(optimizer=opt,
              loss='binary_crossentropy',
              metrics=['accuracy'])
h = evaluate_this_model(model, 5)<jupyter_output>Epoch 1/5
7/7 [==============================] - 0s 11ms/step - loss: 0.7134 - accuracy: 0.3349
Epoch 2/5
7/7 [==============================] - 0s 7ms/step - loss: 0.7010 - accuracy: 0.4833
Epoch 3/5
7/7 [==============================] - 0s 6ms/step - loss: 0.6848 - accuracy: 0.6507
Epoch 4/5
7/7 [==============================] - 0s 6ms/step - loss: 0.6743 - accuracy: 0.6555
Epoch 5/5
7/7 [==============================] - 0s 6ms/step - loss: 0.6585 - accuracy: 0.6555
2/2 [==============================] - 0s 0s/step - loss: 0.8587 - accuracy: 0.3400<jupyter_text>3.2 Improving the basic Model___Objective:___ Design and tune a feed forward deep learning model to achieve at least ___75% accuracy___ on this dataset.<jupyter_code># Run this cell first
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
model = Sequential()

model.add(Dense(units=1, activation='sigmoid'))
learning_rate = ?
opt = Adam(lr=learning_rate)
model.compile(optimizer=opt,
              loss='binary_crossentropy',
              metrics=['accuracy'])
h = evaluate_this_model(model, ?)<jupyter_output>Epoch 1/300
209/209 [==============================] - 4s 17ms/step - loss: 0.8415 - acc: 0.4880
Epoch 2/300
209/209 [==============================] - 0s 899us/step - loss: 0.7900 - acc: 0.5455
Epoch 3/300
209/209 [==============================] - 0s 913us/step - loss: 0.7505 - acc: 0.5215
Epoch 4/300
209/209 [==============================] - 0s 886us/step - loss: 0.7135 - acc: 0.5550
Epoch 5/300
209/209 [==============================] - 0s 852us/step - loss: 0.7697 - acc: 0.5359
Epoch 6/300
209/209 [==============================] - 0s 877us/step - loss: 0.6886 - acc: 0.6172
Epoch 7/300
209/209 [==============================] - 0s 861us/step - loss: 0.6786 - acc: 0.6124
Epoch 8/300
209/209 [==============================] - 0s 858us/step - loss: 0.6730 - acc: 0.6220
Epoch 9/300
209/209 [==============================] - 0s 913us/step - loss: 0.6272 - acc: 0.6459
Epoch 10/300
209/209 [==============================] - 0s 846us/step - loss: 0.6569 - acc: 0.6316
Epoch 11/300
209/209[...]
{'fraction_non_alphanumeric': 0.17263880938752146, 'fraction_numerical': 0.05529479107040641, 'mean_word_length': 5.24892703862661, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '590918', 'license': 'MIT', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3046, 'n_words': 848, 'path': 'CNN for Cat Classification using Keras.ipynb', 'repo_name': 'AbdullahKhan47/Deep-Learning-with-Python', 'size': '8735'}