I'm trying to create a neural network that will get two or more faces of PersonA and PersonB and ect... and will identify the name of the person for each face.
For example the NN get those pictures:
and the NN will say - 1 is sheldon, 2 is leonard.
So I did the following code -
import numpy as np
import tensorflow as tf
from pathlib import Path
import cv2
from random import shuffle
X = []
Y = []
NAMES = {
}
i = 0
for filename in Path('data').rglob('*.jpg'):
i += 1
img = cv2.imread(str(filename), cv2.IMREAD_UNCHANGED)
resized = cv2.resize(img, (150, 150), interpolation=cv2.INTER_AREA)
X.append(np.asarray(resized))
thisName = str(filename).split("_")[0].split("\\")[1]
if i == 1:
NAMES[thisName] = 0
if thisName in NAMES.keys():
Y.append(np.asarray(NAMES[thisName]))
else:
print(NAMES.values())
NAMES[thisName] = max(NAMES.values()) + 1
Y.append(np.asarray(NAMES[thisName]))
Z = list(zip(X, Y))
shuffle(Z) # WE SHUFFLE X,Y TO PERFORM RANDOM ON THE TEST LEVEL
X, Y = zip(*Z)
X = np.asarray(X)
Y = np.asarray(Y)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dense(7, activation='softmax', name='pred')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# -------------- OUR TENSOR FLOW NEURAL NETWORK MODEL -------------- #
print("fitting")
history = model.fit(X, Y, epochs=1, batch_size=20)
print("testing")
model.evaluate(X, Y)
( the lines that come before the model are just came up to take a picture from the dataset and give her a number that customize to the name of the picture, let's say it was a picture of sheldon than she put as output 0 and as input the image himself ... )
I took this : test + training data and I had tested the model on it after I was fitting it but unfortuenntly instead of get excepted results of 90% I got ONLY 25%.
Am I doing something wrong?
Thanks alot and sorry for my english
EDIT : my data set looks fine except the pictures of penny ( but it should not affect so much )
EDIT 2: After I added some conv2d I got 75% instead of 25%, I still want more but it's Absoulutly Better
this is my new model :
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(256, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(512, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(1024, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(2048, activation='relu'),
tf.keras.layers.Dense(7, activation='softmax', name='pred')
])
The only obvious flaw that I see in your code is that you only ran one epoch. Set epochs
to like 10, 20, or any amount you have enough patience for.
Things that might help: