<jupyter_start><jupyter_text>Recognition Of Objects with Convolutional Neural Network - <NAME> IMPORTING THE REQUIRED<jupyter_code>import tensorflow as tf
from tensorflow import keras
from matplotlib import pyplot as plt
import numpy as np
from tensorflow.python.keras.utils import np_utils<jupyter_output><empty_output><jupyter_text>LOADING AND SPLITTING THE DATA<jupyter_code>(X_train, y_train), (X_test, y_test) = keras.datasets.cifar10.load_data()
print('Training Images: {}'.format(X_train.shape))
print('Testing Images: {}'.format(X_test.shape))
print(X_train[0].shape)

for i in range(332,336):
    plt.subplot(120+ 1 + i)
    img = X_train[i]
    plt.imshow(img)
    plt.show()<jupyter_output><empty_output><jupyter_text>PREPROCESSING THE DATA<jupyter_code>X_train = X_train.reshape(X_train.shape[0], 32, 32, 3)
X_test = X_test.reshape(X_test.shape[0], 32, 32, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')


X_train /= 255
X_test=X_test/255
n_classes = 10


print("Shape before one-hot encoding: ", y_train.shape)
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)
print("Shape after one-hot encoding: ", Y_train.shape)<jupyter_output>Shape before one-hot encoding:  (50000, 1)
Shape after one-hot encoding:  (50000, 10)<jupyter_text>BUILDING THE MODEL<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten



model = Sequential()
#convolutional layers
model.add(Conv2D(50, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu', input_shape=(32, 32, 3)))

model.add(Conv2D(75, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(125, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())

# hidden layer
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(250, activation='relu'))
model.add(Dropout(0.3))
# output layer
model.add(Dense(10, activation='softmax'))

# compiling 
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

# training the model 
model.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test))<jupyter_output>Epoch 1/10
391/391 [==============================] - 199s 504ms/step - loss: 1.5686 - accuracy: 0.4243 - val_loss: 1.1705 - val_accuracy: 0.5846
Epoch 2/10
391/391 [==============================] - 255s 654ms/step - loss: 1.1063 - accuracy: 0.6088 - val_loss: 0.9083 - val_accuracy: 0.6808
Epoch 3/10
391/391 [==============================] - 259s 662ms/step - loss: 0.9184 - accuracy: 0.6758 - val_loss: 0.8062 - val_accuracy: 0.7195
Epoch 4/10
391/391 [==============================] - 261s 667ms/step - loss: 0.7996 - accuracy: 0.7207 - val_loss: 0.7516 - val_accuracy: 0.7421
Epoch 5/10
391/391 [==============================] - 270s 692ms/step - loss: 0.7208 - accuracy: 0.7496 - val_loss: 0.7260 - val_accuracy: 0.7471
Epoch 6/10
391/391 [==============================] - 302s 772ms/step - loss: 0.6457 - accuracy: 0.7745 - val_loss: 0.6910 - val_accuracy: 0.7655
Epoch 7/10
391/391 [==============================] - 305s 781ms/step - loss: 0.5894 - accuracy: 0.7950 - val_loss: 0.6814 -[...]<jupyter_text>PREDICTING<jupyter_code>classes = range(0,10)

names = ['airplane',
        'automobile',
        'bird',
        'cat',
        'deer',
        'dog',
        'frog',
        'horse',
        'ship',
        'truck']

# zip the names and classes to make a dictionary of class_labels
class_labels = dict(zip(classes, names))

# generate batch of 9 images to predict
batch = X_test[500:509]
labels = np.argmax(Y_test[500:509],axis=-1)

# make predictions
predictions = model.predict(batch, verbose = 1)
print (predictions)
for image in predictions:
    print(np.sum(image))
class_result = np.argmax(predictions,axis=-1)
print (class_result)<jupyter_output>[6 6 5 4 9 4 7 9 9]<jupyter_text>FINAL OBJECT DETECTION<jupyter_code>fig, axs = plt.subplots(3, 3, figsize = (19,6))
fig.subplots_adjust(hspace = 1)
axs = axs.flatten()

for i, img in enumerate(batch):
    for key, value in class_labels.items():
        if class_result[i] == key:
            title = 'Prediction: {}\nActual: {}'.format(class_labels[key], class_labels[labels[i]])
            axs[i].set_title(title)
            axs[i].axes.get_xaxis().set_visible(False)
            axs[i].axes.get_yaxis().set_visible(False)
            
    # plot the image
    axs[i].imshow(img)
    
# show the plot
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18693169803341086, 'fraction_numerical': 0.08077817720448298, 'mean_word_length': 4.897755610972569, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '19243', 'license': 'MIT', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1751, 'n_words': 361, 'path': 'Recognition Of Objects with Convolutional Neural Network.ipynb', 'repo_name': 'AshwinPrksh00/Object-Recognition-with-CNN', 'size': '4737'}