Model giving different results usingpredict and evaulate

Viewed 42

I have the following nn:

model_regresion_multicapa  = Sequential()
model_regresion_multicapa.add(Flatten(input_shape=(64,64, 3)))
model_regresion_multicapa.add(Dense(112, input_dim=12288,activation="sigmoid"))
model_regresion_multicapa.add(Dense(1, input_dim=112,activation="sigmoid"))
model_regresion_multicapa.compile(loss="binary_crossentropy",optimizer=Adam(learning_rate=0.001),metrics=["accuracy"])

model_regresion_multicapa.fit(train_generator,validation_data=validation_generator,epochs=13)

Where the generators are defined as follows:

train_generator = train_datagen.flow_from_directory(
    "drive/MyDrive/Red_neuronal/Datos/anuka1200/",
    target_size=(64,64),
    color_mode="rgb",
    shuffle=True,
    subset="training",
    class_mode="binary"
)
validation_generator = train_datagen.flow_from_directory(
    "drive/MyDrive/Red_neuronal/Datos/anuka1200/",
    target_size=(64,64),
    color_mode="rgb",
    shuffle=True,
    class_mode="binary",
    subset="validation"
)


test_generator = test_datagen.flow_from_directory(
    "drive/MyDrive/Red_neuronal/Datos/TEST/",
    target_size=(64,64),
    color_mode="rgb",
    shuffle=True,
    class_mode="binary",
    seed=100
)

After training, the evaluate function gives the following:

model1.evaluate(test_generator)
3/3 [==============================] - 14s 7s/step - loss: 0.2945 - accuracy: 0.8611

[0.2945164442062378, 0.8611111044883728]

However, using the following code:

pred = model1.predict(test_generator)
y_pred = [0 if proba[0] <0.5 else 1 for proba in pred]
predictions = np.array(y_pred,dtype = "float32")
labels = test_generator.labels

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(labels, predictions)
cm
array([[21, 15],
       [19, 17]])

Which obviously, the cm diagonal divided by total observations is not giving 0.8611111044883728. What am I missing?

0 Answers
Related