from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
#Initialising the CNN
classifier = Sequential()
#step_1:Convolution
classifier.add(Convolution2D(32,3,padding='same', input_shape=(64,64,3),activation='relu'))
#Step_2: Pooling
classifier.add(MaxPooling2D(pool_size=(2,2)))
#Adding a second convolutional layer
classifier.add(Convolution2D(32,3,activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
#Adding a third convolutional layer
classifier.add(Convolution2D(64,3,activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
#Step_3: Flatten
classifier.add(Flatten())
#Step_3: Full_Connection
classifier.add(Dense(activation="relu", units=128))
classifier.add(Dense(activation="sigmoid", units=1))
#Step_4: Compiling the CNN
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
#Step_5: Fitting the CNN to the image
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(
r'D:\dataset1\training_set',
target_size=(64, 64),
batch_size=32,
class_mode='binary'
)
validation_generator = test_datagen.flow_from_directory(
r'D:\dataset1\test_set',
target_size=(64, 64),
batch_size=32,
class_mode='binary',
shuffle= False
)
#validation_generator = test_datagen.flow_from_directory(
#r'D:\dataset\single_prediction',
#target_size=(64, 64),
#batch_size=32,
#class_mode='binary')
classifier.fit_generator(
training_set,
steps_per_epoch=24000//32,
epochs=1,
validation_data=validation_generator,
validation_steps=8000)
#Confution Matrix and Classification Report
Y_pred = classifier.predict_generator(validation_generator)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(len(y_pred))
print(len(validation_generator.classes))
print(confusion_matrix(validation_generator.classes, y_pred))
print('Classification Report')
target_names = ['Cracked', 'NotCracked']
print(classification_report(validation_generator.classes, y_pred, target_names=target_names))
Вывод:
Found 24000 images belonging to 2 classes.
Found 8000 images belonging to 2 classes.
24000/24000 [==============================] - 10432s 435ms/step - loss: 0.0155 - accuracy: 0.9952 - val_loss: 0.0107 - val_accuracy: 0.9976
Confusion Matrix
8000
8000
[[4000 0]
[4000 0]]
Classification Report
C:\Users\Dell\Anaconda3\lib\site-packages\sklearn\metrics\classification.py:1437: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
precision recall f1-score support
Cracked 0.50 1.00 0.67 4000
NotCracked 0.00 0.00 0.00 4000
accuracy 0.50 8000
macro avg 0.25 0.50 0.33 8000
weighted avg 0.25 0.50 0.33 8000
Почему у моей матрицы путаницы 0?