confusion matrix in keras cnn model without xtrain xtest ytrain ytest












1














I am currently trying to implement a confusion matrix into my cnn model code. All the examples that I've been watching includes using x_train, x_test, y_train, y_test, but I don't know how to do that on my code, or if I will be able to do it from my_model.h5 file. Hoping you could help me. Thanks.



I leave my model code below here:



classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(256, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(26, activation = 'softmax'))

classifier.compile(
optimizer = optimizers.SGD(lr = 0.01),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])

classifier.summary()

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
'mydata/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

test_set = test_datagen.flow_from_directory(
'mydata/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

model = classifier.fit_generator(
training_set,
steps_per_epoch=int(steps_per_epoch_user),
epochs=int(epochs_user),
validation_data = test_set,
validation_steps = int(validation_steps_user)
)

import h5py
classifier.save('my_model.h5')

print(model.history.keys())

import matplotlib.pyplot as plt

plt.plot(model.history['acc'])
plt.plot(model.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(model.history['loss'])
plt.plot(model.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()









share|improve this question
























  • Does your dataset have the labels?
    – Sandhiya - Intel
    Nov 12 at 4:48










  • They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
    – J. Dav
    Nov 12 at 12:39
















1














I am currently trying to implement a confusion matrix into my cnn model code. All the examples that I've been watching includes using x_train, x_test, y_train, y_test, but I don't know how to do that on my code, or if I will be able to do it from my_model.h5 file. Hoping you could help me. Thanks.



I leave my model code below here:



classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(256, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(26, activation = 'softmax'))

classifier.compile(
optimizer = optimizers.SGD(lr = 0.01),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])

classifier.summary()

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
'mydata/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

test_set = test_datagen.flow_from_directory(
'mydata/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

model = classifier.fit_generator(
training_set,
steps_per_epoch=int(steps_per_epoch_user),
epochs=int(epochs_user),
validation_data = test_set,
validation_steps = int(validation_steps_user)
)

import h5py
classifier.save('my_model.h5')

print(model.history.keys())

import matplotlib.pyplot as plt

plt.plot(model.history['acc'])
plt.plot(model.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(model.history['loss'])
plt.plot(model.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()









share|improve this question
























  • Does your dataset have the labels?
    – Sandhiya - Intel
    Nov 12 at 4:48










  • They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
    – J. Dav
    Nov 12 at 12:39














1












1








1







I am currently trying to implement a confusion matrix into my cnn model code. All the examples that I've been watching includes using x_train, x_test, y_train, y_test, but I don't know how to do that on my code, or if I will be able to do it from my_model.h5 file. Hoping you could help me. Thanks.



I leave my model code below here:



classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(256, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(26, activation = 'softmax'))

classifier.compile(
optimizer = optimizers.SGD(lr = 0.01),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])

classifier.summary()

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
'mydata/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

test_set = test_datagen.flow_from_directory(
'mydata/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

model = classifier.fit_generator(
training_set,
steps_per_epoch=int(steps_per_epoch_user),
epochs=int(epochs_user),
validation_data = test_set,
validation_steps = int(validation_steps_user)
)

import h5py
classifier.save('my_model.h5')

print(model.history.keys())

import matplotlib.pyplot as plt

plt.plot(model.history['acc'])
plt.plot(model.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(model.history['loss'])
plt.plot(model.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()









share|improve this question















I am currently trying to implement a confusion matrix into my cnn model code. All the examples that I've been watching includes using x_train, x_test, y_train, y_test, but I don't know how to do that on my code, or if I will be able to do it from my_model.h5 file. Hoping you could help me. Thanks.



I leave my model code below here:



classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(256, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(26, activation = 'softmax'))

classifier.compile(
optimizer = optimizers.SGD(lr = 0.01),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])

classifier.summary()

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
'mydata/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

test_set = test_datagen.flow_from_directory(
'mydata/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')

model = classifier.fit_generator(
training_set,
steps_per_epoch=int(steps_per_epoch_user),
epochs=int(epochs_user),
validation_data = test_set,
validation_steps = int(validation_steps_user)
)

import h5py
classifier.save('my_model.h5')

print(model.history.keys())

import matplotlib.pyplot as plt

plt.plot(model.history['acc'])
plt.plot(model.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(model.history['loss'])
plt.plot(model.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()






tensorflow keras conv-neural-network confusion-matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:42

























asked Nov 12 at 1:18









J. Dav

113




113












  • Does your dataset have the labels?
    – Sandhiya - Intel
    Nov 12 at 4:48










  • They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
    – J. Dav
    Nov 12 at 12:39


















  • Does your dataset have the labels?
    – Sandhiya - Intel
    Nov 12 at 4:48










  • They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
    – J. Dav
    Nov 12 at 12:39
















Does your dataset have the labels?
– Sandhiya - Intel
Nov 12 at 4:48




Does your dataset have the labels?
– Sandhiya - Intel
Nov 12 at 4:48












They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
– J. Dav
Nov 12 at 12:39




They're just files in labeled folders. For example: mydata/training_set/icecream, mydata/training_set/pizza, mydata/training_set/hotdog and so on.
– J. Dav
Nov 12 at 12:39












1 Answer
1






active

oldest

votes


















0














I would add the following code:



from sklearn.metrics import confusion_matrix



cm = confusion_matrix(Y_test,Y_test_predicted)



print('n','cm=','n',cm)



for more descriptive confusion matrix look at scikit-learn documentation in the following link:



https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254879%2fconfusion-matrix-in-keras-cnn-model-without-xtrain-xtest-ytrain-ytest%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I would add the following code:



    from sklearn.metrics import confusion_matrix



    cm = confusion_matrix(Y_test,Y_test_predicted)



    print('n','cm=','n',cm)



    for more descriptive confusion matrix look at scikit-learn documentation in the following link:



    https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html






    share|improve this answer


























      0














      I would add the following code:



      from sklearn.metrics import confusion_matrix



      cm = confusion_matrix(Y_test,Y_test_predicted)



      print('n','cm=','n',cm)



      for more descriptive confusion matrix look at scikit-learn documentation in the following link:



      https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html






      share|improve this answer
























        0












        0








        0






        I would add the following code:



        from sklearn.metrics import confusion_matrix



        cm = confusion_matrix(Y_test,Y_test_predicted)



        print('n','cm=','n',cm)



        for more descriptive confusion matrix look at scikit-learn documentation in the following link:



        https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html






        share|improve this answer












        I would add the following code:



        from sklearn.metrics import confusion_matrix



        cm = confusion_matrix(Y_test,Y_test_predicted)



        print('n','cm=','n',cm)



        for more descriptive confusion matrix look at scikit-learn documentation in the following link:



        https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 at 22:47









        Faris

        61




        61






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254879%2fconfusion-matrix-in-keras-cnn-model-without-xtrain-xtest-ytrain-ytest%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()