Библиотека confusion_matrix () выдает ValueError - PullRequest
1 голос
/ 30 мая 2020

При попытке получить матрицу путаницы для Con vNet постоянно появляется одна и та же ошибка.

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
from keras.preprocessing import image
from sklearn.metrics import classification_report, confusion_matrix
img_width, img_height = 150, 150

train_data_dir = "train"
validation_data_dir = "test"
nb_train_samples = 2000
nb_validation_samples = 400
epochs = 50
batch_size = 40 #16

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

train_datagen = ImageDataGenerator(
    rescale = 1. / 255,
    shear_range = 0.2,
    zoom_range= 0.2,
    horizontal_flip= True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = test_datagen.flow_from_directory(
    train_data_dir,
    target_size= (img_width, img_height),
    batch_size= batch_size,
    class_mode= 'binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size= (img_width, img_height),
    batch_size= batch_size,
    class_mode= 'binary')`

Применение слоев CNN ...

model.compile(loss= 'binary_crossentropy',
    optimizer= 'rmsprop',
    metrics= ['accuracy'] )

`model.fit_generator(
    train_generator,
    steps_per_epoch= nb_train_samples // batch_size,
    epochs= epochs,
    validation_data= validation_generator,
    validation_steps= nb_validation_samples // batch_size)

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes, y_pred))`

Ошибка, упомянутая ниже, но не знаю, как ее исправить

ValueError: обнаружены входные переменные с несовместимым количеством выборок: [400, 440]

1 Ответ

1 голос
/ 02 июня 2020

Я могу воссоздать вашу ошибку, используя набор данных Dogs_Vs_Cats. Если у меня 2000 образцов в каталоге поездов и 400 образцов в каталоге проверки.

Пожалуйста, измените model.predict_generator с

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size+1)

на

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size)

решит это ValueError: Found input variables with inconsistent numbers of samples: [400, 440]

Полный код см. Ниже

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
from keras.preprocessing import image
from sklearn.metrics import classification_report, confusion_matrix

from google.colab import drive
drive.mount('/content/drive')

train_data_dir  = '/content/drive/My Drive/Dogs_Vs_Cats/train'
validation_data_dir = '/content/drive/My Drive/Dogs_Vs_Cats/validation'

img_width, img_height = 150, 150

nb_train_samples = 2000
nb_validation_samples = 400
epochs = 10
batch_size = 40 #16

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

train_datagen = ImageDataGenerator(
    rescale = 1. / 255,
    shear_range = 0.2,
    zoom_range= 0.2,
    horizontal_flip= True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = test_datagen.flow_from_directory(
    train_data_dir,
    target_size= (img_width, img_height),
    batch_size= batch_size,
    class_mode= 'binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size= (img_width, img_height),
    batch_size= batch_size,
    class_mode= 'binary')

model = Sequential()

model.add(Conv2D(32, (3, 3), strides = (1, 1), input_shape = input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Conv2D(64, (3, 3), strides = (1, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.summary() 


model.compile(loss = 'binary_crossentropy',
            optimizer = 'rmsprop',
            metrics = ['accuracy'])

model.fit_generator(
    train_generator,
    steps_per_epoch= nb_train_samples // batch_size,
    epochs= epochs,
    validation_data= validation_generator,
    validation_steps= nb_validation_samples // batch_size)

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes, y_pred))

Вывод:

Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Found 2000 images belonging to 2 classes.
Found 400 images belonging to 2 classes.
Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_9 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
activation_9 (Activation)    (None, 148, 148, 32)      0         
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 72, 72, 64)        18496     
_________________________________________________________________
activation_10 (Activation)   (None, 72, 72, 64)        0         
_________________________________________________________________
max_pooling2d_10 (MaxPooling (None, 36, 36, 64)        0         
_________________________________________________________________
flatten_5 (Flatten)          (None, 82944)             0         
_________________________________________________________________
dense_9 (Dense)              (None, 64)                5308480   
_________________________________________________________________
dropout_5 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_10 (Dense)             (None, 1)                 65        
=================================================================
Total params: 5,327,937
Trainable params: 5,327,937
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
50/50 [==============================] - 12s 233ms/step - loss: 0.9345 - accuracy: 0.5375 - val_loss: 0.6303 - val_accuracy: 0.5225
Epoch 2/10
50/50 [==============================] - 11s 226ms/step - loss: 0.6745 - accuracy: 0.5965 - val_loss: 0.6094 - val_accuracy: 0.6725
Epoch 3/10
50/50 [==============================] - 11s 223ms/step - loss: 0.6196 - accuracy: 0.6605 - val_loss: 0.5694 - val_accuracy: 0.7150
Epoch 4/10
50/50 [==============================] - 11s 223ms/step - loss: 0.5501 - accuracy: 0.7285 - val_loss: 0.6216 - val_accuracy: 0.7225
Epoch 5/10
50/50 [==============================] - 11s 221ms/step - loss: 0.4794 - accuracy: 0.7790 - val_loss: 0.6268 - val_accuracy: 0.6025
Epoch 6/10
50/50 [==============================] - 11s 226ms/step - loss: 0.4038 - accuracy: 0.8195 - val_loss: 0.4842 - val_accuracy: 0.6975
Epoch 7/10
50/50 [==============================] - 11s 222ms/step - loss: 0.3207 - accuracy: 0.8595 - val_loss: 0.5600 - val_accuracy: 0.7325
Epoch 8/10
50/50 [==============================] - 13s 257ms/step - loss: 0.2574 - accuracy: 0.8920 - val_loss: 0.9705 - val_accuracy: 0.7525
Epoch 9/10
50/50 [==============================] - 13s 252ms/step - loss: 0.2049 - accuracy: 0.9235 - val_loss: 0.7311 - val_accuracy: 0.7475
Epoch 10/10
50/50 [==============================] - 13s 251ms/step - loss: 0.1448 - accuracy: 0.9515 - val_loss: 1.0541 - val_accuracy: 0.7150
Confusion Matrix
[[200   0]
 [200   0]]

Надеюсь, это ответит на ваш вопрос. Если нет, пожалуйста, поделитесь полной трассировкой и кодом для отладки, я рад вам помочь.

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