Data Visualization Cheatsheet 1702209209
Data Visualization Cheatsheet 1702209209
1. Load Data
2. Scaling data
3. Define Keras Model
4. Compile Keras Model
5. Fit Keras Model
6. Evaluate Keras Model
7. Make Predictions
8. flowchart for our NN model
1. Load Data
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 1 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
In [ ]: import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import os
import cv2
from tqdm import tqdm
import tensorflow as tf
from tensorflow import keras
from keras.utils import plot_model
make two categories one for dogs and take label 0 and another for cats with
label 1
convert the image to array as it is gray images* resize all the images to assure
that all in the same size
firstly we will see the images and try things then apply to all
In [ ]: DATADIR = 'PetImages'
CATEGORIES = ["Dog", "Cat"]
for category in CATEGORIES: # do dogs and cats
path = os.path.join(DATADIR,category) # create path to dogs and cats
x=0
for img in os.listdir(path): # iterate over each image per dogs and cats
x+=1
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array, cmap='gray') # graph it
# plt.show() # display!
if x==10 :
break
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 2 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
In [ ]: print(img_array)
print(img_array.shape)
[[ 72 65 60 ... 81 83 92]
[ 66 58 54 ... 78 78 87]
[ 62 54 50 ... 78 76 84]
...
[125 113 114 ... 95 101 105]
[126 114 117 ... 100 107 113]
[128 118 122 ... 105 113 120]]
(500, 448)
Let's try different sizes for images to decide which one will be better.
In [ ]: #IMG_SIZE = 5
#IMG_SIZE = 10
IMG_SIZE = 200
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 3 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
Apply all the processing with the decided size for all images
In [ ]: all_data = []
def create_all_data():
for category in CATEGORIES: # do dogs and cats
create_all_data()
print(len(all_data))
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 4 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
Data Randomization
we will randomize our training data, to prevent the model from learning patterns
based on the order of the data. This is crucial to ensure that the model
generalizes well to unseen data.
In [ ]: import random
random.shuffle(all_data)
We will divide our data into training and testing sets for the purpose of
building our model.
In [ ]: training_data = all_data[:20000]
test_data = all_data[20000:]
In [ ]: X_train = []
y_train = []
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 5 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
X_test = []
y_test = []
# The -1 means it will automatically infer the size based on the size of the or
X_train = np.array(X_train).reshape(-1, IMG_SIZE, IMG_SIZE)
X_test= np.array(X_test).reshape(-1, IMG_SIZE, IMG_SIZE)
If you want to save the data to use later --> use pickle
In [ ]: import pickle
pickle_out = open("X_train.pickle","wb")
pickle.dump(X_train, pickle_out)
pickle_out.close()
pickle_out = open("y_train.pickle","wb")
pickle.dump(y_train, pickle_out)
pickle_out.close()
pickle_in = open("X_train.pickle","rb")
X_train = pickle.load(pickle_in)
pickle_in = open("y_train.pickle","rb")
y_train = pickle.load(pickle_in)
2. Scaling data
We will build this model without scaling
Notes :
The first layer should be equal the dimensions of the compressed images as the
array of each image is the input of the NN
As it is classification (binary; cats & dogs), output layers should use softmax as
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 6 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
activation function
In [ ]: model = keras.Sequential([
keras.layers.Flatten(input_shape=(IMG_SIZE,IMG_SIZE)),
keras.layers.Dense(256, activation=tf.nn.relu),
keras.layers.Dense(256, activation=tf.nn.relu),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(2, activation=tf.nn.softmax)
])
Epoch 1/10
625/625 [==============================] - 5s 8ms/step - loss: 120.2522 -
accuracy: 0.5030
Epoch 2/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6954 - ac
curacy: 0.4925
Epoch 3/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6937 - ac
curacy: 0.5010
Epoch 4/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6930 - ac
curacy: 0.4940
Epoch 5/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6930 - ac
curacy: 0.4938
Epoch 6/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6935 - ac
curacy: 0.4951
Epoch 7/10
625/625 [==============================] - 5s 9ms/step - loss: 0.6932 - ac
curacy: 0.4981
Epoch 8/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6932 - ac
curacy: 0.4931
Epoch 9/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6935 - ac
curacy: 0.4958
Epoch 10/10
625/625 [==============================] - 5s 8ms/step - loss: 0.6932 - ac
curacy: 0.4922
Out[ ]: <keras.callbacks.History at 0x284a33150>
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 7 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
7. Make Predictions
In [ ]: predictions = model.predict(X_test)
Let's see some cases and compare! you try any image number here as
you want
model_prediction = np.argmax(predictions[image_no])
if model_prediction == 1:
print("The model predict the image as Dog")
elif model_prediction == 0:
print("The model predict the image as Cat")
print("The real image as follows ")
plt.imshow(X_test[image_no], cmap='gray');
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 8 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
Our Comments :
The accuracy of the model is very low and can not be accepted as
generalized model in practical life.
The main reason for that --> we are using the DNN with image related
example, while the CNN is the better option of such kind of examples, so in
another tutorial we will try the CNN and see what is the difference
It is very clear when we tried prediction for different images that the model
can not recognize the black cats as cats but see them as dogs.
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…map/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 9 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
Out[ ]:
Case Study No 2 : To observe the impact of increasing the number of hidden layers
on the model's accuracy.
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…ap/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 10 of 11
Keras_CaseStudy_no5 2023/11/22 17:11
Case Study No 4 : Utilizing Keras to construct a DNN for a regression model allows
for the observation of early stopping in action.
Refrences
file:///Users/marzouk/Desktop/marzouk/github_repos/MachineLearni…ap/1_ML_Toolkit/keras/DNN_case_studies/Keras_CaseStudy_no5.html Page 11 of 11