Conv2D ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))

Viewed 300

So I'm trying to make a faceID with Tensorflow using CNN type, i only gave to my data 15 images of my face(is a simple project), and i want it to predict if it is my face or is not my face, like true or false, the problem is that I don't understand what is the error in my code. this is

ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))

and here is my code

# Face ID project, using CNN tensorflow
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.optimizers import Adam 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Activation
from tensorflow.keras import backend as K
import numpy as np
import cv2  
import glob

# Preparing the data and parameters
epochs = 10
lr = 1e-3
batch_size = 64 
img_dims = (96,96,3)

data = []
labels = []

image_files = glob.glob("C:/Users/berna/Desktop/Programming/AI_ML_DL/Projects/FaceID/Data/*")

for img in image_files:
    image = cv2.imread(img)

    image = cv2.resize(image, (img_dims[0], img_dims[1]))
    image = img_to_array(image)

    data.append(image)

    if img == img:
        label = 1
    else:
        label = 0
    
    labels.append([label])
    

# Preproccesing the data (convert arrays)
data = np.array(data, dtype="float32") / 255.0
labels = np.array(labels)

X = data 
y = labels

def build(width, height, depth, classes):
    model = Sequential()
    inputShape = height, width, depth 
    chanDim = -1

    if K.image_data_format() == "channels_first":
        inputShape = depth, height, width 
        chanDim = 1

    
    # Creating the model
    model.add(Conv2D(32, (3,3), padding="same", input_shape=inputShape))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3,3)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, (3,3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))

    model.add(Conv2D(64, (3,3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(128, (3,3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))

    model.add(Conv2D(128, (3,3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    model.add(Dense(classes))
    model.add(Activation("sigmoid"))

    return model

# Build the model call 
model = build(width=img_dims[0], height=img_dims[1], depth=img_dims[2], classes=2)

# compile the model
opt = Adam(lr=lr, decay=lr/epochs)
model.compile(loss="binary_crossentropy",
            optimizer=opt,
            metrics=['accuracy'])

# fitting the model
H = model.fit(X, y, batch_size=batch_size,
                    epochs=epochs, verbose=1)

model.save('faceid.model')
1 Answers

As stated by @Frightera, replacing:

model.add(Dense(classes)) by model.add(Dense(1)) should work.

Your label is an integer but your last layer output a 2d array.

Related