#cats_dogs.py
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import shutil
import random
import os
import glob
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
import numpy as np
#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)
"""
print(os.path.exists('C://Users//bob//keras//data'))
for c in random.sample(glob.glob('C://Users//bob//keras//data//cats//cat*'), 500):
shutil.move(c, 'C://Users//bob//keras//data//cats//train')
"""
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)\
.flow_from_directory(directory='C://Users//bob//keras//data//train', target_size=(224, 224),
classes=['cat', 'dog'], batch_size=32)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)\
.flow_from_directory(directory='C://Users//bob//keras//data//valid', target_size=(224, 224),
classes=['cat', 'dog'], batch_size=32)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)\
.flow_from_directory(directory='C://Users//bob//keras//data//test', target_size=(224, 224),
classes=['cat', 'dog'], batch_size=32, shuffle=False)
imgs, labels = next(train_batches)
def plotImages(image_arr):
fig, axes = plt.subplots(1, 10, figsize=(20, 20))
axes = axes.flatten()
for img, ax in zip(image_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
#plotImages(imgs)
#print(labels)
"""
model = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)),
MaxPool2D(pool_size=(2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same'),
MaxPool2D(pool_size=(2, 2), strides=2),
Flatten(),
Dense(units=2, activation='softmax'),
])
model.summary()
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=train_batches, validation_data=valid_batches, epochs=10, verbose=2)
model.save('models/cnn1.h5')
"""
model = load_model('models/cnn1.h5')
test_imgs, test_labels = next(test_batches)
#plotImages(test_imgs)
#print(test_labels)
#print( test_batches.classes)
predictions = model.predict(x=test_batches, verbose=0)
cm = confusion_matrix(y_true=test_batches.classes, y_pred=np.argmax(predictions, axis=-1))
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
cm_plot_labels = ['cat', 'dog']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')
-------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 224, 224, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 112, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 112, 112, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 56, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 200704) 0
_________________________________________________________________
dense (Dense) (None, 2) 401410
=================================================================
Total params: 420,802
Trainable params: 420,802
Non-trainable params: 0
_________________________________________________________________
2021-01-03 17:23:46.121493: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (register
ed 2)
Epoch 1/10
2021-01-03 17:23:46.477062: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-01-03 17:23:46.671395: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-01-03 17:23:46.739105: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-01-03 17:23:47.099393: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0
2021-01-03 17:23:47.130438: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0
32/32 - 4s - loss: 16.4275 - accuracy: 0.5320 - val_loss: 8.6544 - val_accuracy: 0.5350
Epoch 2/10
32/32 - 2s - loss: 3.8345 - accuracy: 0.7070 - val_loss: 6.7644 - val_accuracy: 0.5600
Epoch 3/10
32/32 - 2s - loss: 2.4791 - accuracy: 0.7600 - val_loss: 8.6995 - val_accuracy: 0.5550
Epoch 4/10
32/32 - 2s - loss: 2.8763 - accuracy: 0.7820 - val_loss: 9.8628 - val_accuracy: 0.5700
Epoch 5/10
32/32 - 2s - loss: 1.4679 - accuracy: 0.8400 - val_loss: 5.2418 - val_accuracy: 0.6150
Epoch 6/10
32/32 - 2s - loss: 0.1877 - accuracy: 0.9590 - val_loss: 5.0586 - val_accuracy: 0.5900
Epoch 7/10
32/32 - 2s - loss: 0.4189 - accuracy: 0.9310 - val_loss: 4.5768 - val_accuracy: 0.6000
Epoch 8/10
32/32 - 2s - loss: 0.0749 - accuracy: 0.9810 - val_loss: 4.7797 - val_accuracy: 0.6000
Epoch 9/10
32/32 - 2s - loss: 0.0169 - accuracy: 0.9950 - val_loss: 4.7663 - val_accuracy: 0.6200
Epoch 10/10
32/32 - 2s - loss: 0.0120 - accuracy: 0.9960 - val_loss: 4.5424 - val_accuracy: 0.6050
#test_batch.classes, 50 cats + 50 dogs
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
#Confusion matrix, without normalization - 60% accuracy
[[30 20]
[19 31]]
reference:
Keras Conv2D class (filters , kernel_size , strides , padding )
default filters
No comments:
Post a Comment