матрица путаницы в модели keras cnn без xtrain xtest ytrain ytest - PullRequest
0 голосов
/ 12 ноября 2018

В настоящее время я пытаюсь внедрить путаницу в код моей модели cnn. Все примеры, которые я смотрел, включают использование x_train, x_test, y_train, y_test, но я не знаю, как это сделать с моим кодом, или смогу ли я сделать это из файла my_model.h5. Надеюсь, вы могли бы помочь мне. Благодаря.

Я оставляю свой код модели здесь:

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()

1 Ответ

0 голосов
/ 27 ноября 2018

Я бы добавил следующий код:

из sklearn.metrics import confusion_matrix

см = confusion_matrix (Y_test, Y_test_predicted)

печать ( '\ п', 'см =', '\ п', см)

для более наглядной матрицы путаницы смотрите документацию по scikit-learn по следующей ссылке:

https://scikit -learn.org / стабильный / модули / полученные / sklearn.metrics.confusion_matrix.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...