<jupyter_start><jupyter_text>"Keras: CNN - CIFAR-10 Multiple Classification"- title: "Keras CNN: CIFAR-10 Multiple Classification"- toc: true- badges: False- comments: true- author: <NAME>- categories: [keras, cnn, tensorflow, classification, python]<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train.shape
plt.imshow(X_train[4]);<jupyter_output><empty_output><jupyter_text>Normalise Data<jupyter_code>X_train.max(), 
X_train = X_train/255
X_test  = X_test/255<jupyter_output><empty_output><jupyter_text>Label Data<jupyter_code>from tensorflow.keras.utils import to_categorical
y_train.max()
y_train = to_categorical(y_train, 10)
y_test  = to_categorical(y_test , 10)<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Activation, MaxPool2D, Flatten
model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(4,4), activation='relu', input_shape=(32,32,3)))
model.add(MaxPool2D(2,2))
model.add(Conv2D(filters=32, kernel_size=(4,4), activation='relu', input_shape=(32,32,3)))
model.add(MaxPool2D(2,2))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10,  activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop', 
              metrics=['accuracy'])
model.summary()
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=4)
model.fit(X_train, y_train, epochs=20, batch_size=516,
          validation_data=(X_test, y_test),
          callbacks = [early_stop],
          verbose= 1)
losses = pd.DataFrame(model.history.history)
losses[['loss', 'val_loss']].plot()
losses[['accuracy', 'val_accuracy']].plot()
model.metrics_names
print(model.metrics_names)
print(model.evaluate(X_test, y_test, verbose=0))
from sklearn.metrics import classification_report, confusion_matrix
X_test.shape
predictions = model.predict_classes(X_test)
predictions
y_test
y_test = y_test.argmax(axis=1)
y_test
print(classification_report(y_test, predictions))
confusion_matrix(y_test, predictions)
conf_matrix = confusion_matrix(y_test, predictions) * (np.ones((10,10)) - np.eye(10))


import seaborn as sns
plt.figure(figsize=(12,8))
sns.heatmap(conf_matrix, annot=True);<jupyter_output><empty_output><jupyter_text>Predicing a specific image<jupyter_code>new_image = X_test[8]

plt.imshow(new_image);
model.predict_classes(new_image.reshape(1,32,32,3))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13436206247647722, 'fraction_numerical': 0.02898005269100489, 'mean_word_length': 7.772277227722772, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '184777', 'license': 'Apache-2.0', 'n_tokens_mistral': 1026, 'n_tokens_neox': 969, 'n_words': 159, 'path': '_notebooks/2020-10-04-Keras_CNN_CIFAR10 multiclassification.ipynb', 'repo_name': 'Sams101/DataScience', 'size': '2661'}