'python : Input 0 of layer "conv2d" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received (None, 200, 200, 3)
i am working in a Deep Learning Project using CNN but i am still a beginner & i have a proplem when trying to use the model to predict from an image file
the error : Input 0 of layer "conv2d" is incompatible with the layer: expected axis -1 of input shape to have value 1, but received (None, 200, 200, 3)
model :
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, Flatten, MaxPool2D, Dense
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_recall_fscore_support
import seaborn as sns
import keras
import splitfolders
splitfolders.ratio(
"input/Data/Data",
output="./dataset",
seed=7,
ratio=(0.90, 0.050, 0.050)
)
train_datagen = ImageDataGenerator(rescale=1 / 255)
valid_datagen = ImageDataGenerator(rescale=1 / 255)
test_datagen = ImageDataGenerator(rescale=1 / 255)
train_dataset = train_datagen.flow_from_directory('./dataset/train',
target_size=(200, 200),
color_mode='grayscale',
class_mode='categorical',
batch_size=100,
)
test_dataset = test_datagen.flow_from_directory('./dataset/test',
target_size=(200, 200),
class_mode='categorical',
color_mode='grayscale',
batch_size=100,
shuffle=False
)
valid_dataset = valid_datagen.flow_from_directory('./dataset/val',
target_size=(200, 200),
class_mode='categorical',
batch_size=100,
color_mode='grayscale',
)
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=train_dataset.image_shape))
model.add(MaxPool2D(2))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPool2D(2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D(2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D(2))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPool2D(2))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPool2D(2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(4, activation='softmax'))
model.summary()
METRICS = [
'accuracy',
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall')
]
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=METRICS)
Info = model.fit(
train_dataset,
validation_data=valid_dataset,
epochs=5,
)
fig, ax = plt.subplots(1, 4, figsize=(20, 3))
ax = ax.ravel()
for i, met in enumerate(['precision', 'recall', 'accuracy', 'loss']):
ax[i].plot(Info.history[met])
ax[i].plot(Info.history['val_' + met])
ax[i].set_title('Model {}'.format(met))
ax[i].set_xlabel('epochs')
ax[i].set_ylabel(met)
ax[i].legend(['train', 'val'])
predictions = model.predict(test_dataset)
diseases_labels = []
for key, value in train_dataset.class_indices.items():
diseases_labels.append(key)
def evaluate(actual, predictions):
pre = []
for i in predictions:
pre.append(np.argmax(i))
accuracy = (pre == actual).sum() / actual.shape[0]
print(f'Accuracy: {accuracy}')
precision, recall, f1_score, _ = precision_recall_fscore_support(actual, pre, average='macro')
print(f'Precision: {precision}')
print(f'Recall: {recall}')
print(f'F1_score: {f1_score}')
fig, ax = plt.subplots(figsize=(20, 20))
conf_mat = confusion_matrix(actual, pre)
sns.heatmap(conf_mat, annot=True, fmt='.0f', cmap="YlGnBu", xticklabels=diseases_labels,
yticklabels=diseases_labels).set_title('Confusion Matrix Heat map')
plt.show()
evaluate(test_dataset.classes, predictions)
model.evaluate(test_dataset)
model.save('model/KidneyDiseasesModel.h5')
model_test :
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
model = load_model('model/KidneyDiseasesModel.h5')
test_image = image.load_img('test.jpg', target_size=(200, 200, 1))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
image to test : test image
Can anyone help me Please ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
