{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Image Classification Project Using CNN in Tensorflow","metadata":{"id":"ht3ou9fM3KSA"}},{"cell_type":"markdown","source":"### import libraries","metadata":{"id":"edGTy2ft3QW8"}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import datasets, layers,models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"id":"Kzci-n0gv5iI","execution":{"iopub.status.busy":"2024-09-11T19:28:30.644925Z","iopub.execute_input":"2024-09-11T19:28:30.645371Z","iopub.status.idle":"2024-09-11T19:28:30.652632Z","shell.execute_reply.started":"2024-09-11T19:28:30.645327Z","shell.execute_reply":"2024-09-11T19:28:30.651396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### download the dataset","metadata":{"id":"lJDLi9Cz3StL"}},{"cell_type":"code","source":"(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()","metadata":{"id":"9IJ6TYVBwQkg","outputId":"9c942881-9b40-4928-fa9c-d6a8f70ae1b6","execution":{"iopub.status.busy":"2024-09-11T19:28:30.655624Z","iopub.execute_input":"2024-09-11T19:28:30.656098Z","iopub.status.idle":"2024-09-11T19:28:31.647407Z","shell.execute_reply.started":"2024-09-11T19:28:30.656045Z","shell.execute_reply":"2024-09-11T19:28:31.646293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.shape","metadata":{"id":"pO3VoRsDwUor","outputId":"f84aa682-80e0-4e5f-fd65-59a445a3b5f7","execution":{"iopub.status.busy":"2024-09-11T19:28:31.649278Z","iopub.execute_input":"2024-09-11T19:28:31.649701Z","iopub.status.idle":"2024-09-11T19:28:31.657631Z","shell.execute_reply.started":"2024-09-11T19:28:31.649658Z","shell.execute_reply":"2024-09-11T19:28:31.656457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.shape","metadata":{"id":"ouNB6aeiwV1y","outputId":"35dbe7f3-6a09-46cc-d38c-c8acb3d249a1","execution":{"iopub.status.busy":"2024-09-11T19:28:31.658924Z","iopub.execute_input":"2024-09-11T19:28:31.659350Z","iopub.status.idle":"2024-09-11T19:28:31.672598Z","shell.execute_reply.started":"2024-09-11T19:28:31.659291Z","shell.execute_reply":"2024-09-11T19:28:31.671449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images.shape","metadata":{"id":"6aM_EeTPwaUZ","outputId":"42261b91-3d89-439f-f2a0-6f154dfee72f","execution":{"iopub.status.busy":"2024-09-11T19:28:31.675609Z","iopub.execute_input":"2024-09-11T19:28:31.676041Z","iopub.status.idle":"2024-09-11T19:28:31.687786Z","shell.execute_reply.started":"2024-09-11T19:28:31.675999Z","shell.execute_reply":"2024-09-11T19:28:31.686696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.shape","metadata":{"id":"4MuR0yhHwa9M","outputId":"bb705eb0-1207-4a49-ee7b-33f6bdbbc26d","execution":{"iopub.status.busy":"2024-09-11T19:28:31.689155Z","iopub.execute_input":"2024-09-11T19:28:31.689561Z","iopub.status.idle":"2024-09-11T19:28:31.700936Z","shell.execute_reply.started":"2024-09-11T19:28:31.689516Z","shell.execute_reply":"2024-09-11T19:28:31.699848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### make normalization to the values of the image to be between [0,1]","metadata":{"id":"onkzCOXB3aXI"}},{"cell_type":"code","source":"train_images = train_images / 255.0\ntest_images = test_images / 255.0","metadata":{"id":"zd-JRVX3yfof","execution":{"iopub.status.busy":"2024-09-11T19:28:31.702461Z","iopub.execute_input":"2024-09-11T19:28:31.702858Z","iopub.status.idle":"2024-09-11T19:28:32.327568Z","shell.execute_reply.started":"2024-09-11T19:28:31.702818Z","shell.execute_reply":"2024-09-11T19:28:32.326027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### convert the numerical classes to be binary with 10 column every column represent separate class","metadata":{"id":"_BmeOFoI3fbx"}},{"cell_type":"code","source":"train_labels = tf.keras.utils.to_categorical(train_labels,10)\ntest_labels = tf.keras.utils.to_categorical(test_labels,10)","metadata":{"id":"G6TeXP0JzENg","execution":{"iopub.status.busy":"2024-09-11T19:28:32.329528Z","iopub.execute_input":"2024-09-11T19:28:32.330022Z","iopub.status.idle":"2024-09-11T19:28:32.340798Z","shell.execute_reply.started":"2024-09-11T19:28:32.329965Z","shell.execute_reply":"2024-09-11T19:28:32.339568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using Simple Architecture","metadata":{"id":"aebCQAXQ3mgE"}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3), padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.2)) \nmodel.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.2)) \nmodel.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0.5)) \nmodel.add(layers.Dense(10, activation='softmax'))","metadata":{"id":"5F6axCg3wemK","outputId":"baae4e41-afd3-4a8e-ab45-08a44dd86d01","execution":{"iopub.status.busy":"2024-09-11T19:28:32.342273Z","iopub.execute_input":"2024-09-11T19:28:32.342732Z","iopub.status.idle":"2024-09-11T19:28:32.482750Z","shell.execute_reply.started":"2024-09-11T19:28:32.342689Z","shell.execute_reply":"2024-09-11T19:28:32.481603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"id":"9LbMsejEyLt4","outputId":"5eab0779-b659-45f1-cd01-08e3a0776e59","execution":{"iopub.status.busy":"2024-09-11T19:28:32.484526Z","iopub.execute_input":"2024-09-11T19:28:32.484921Z","iopub.status.idle":"2024-09-11T19:28:32.517178Z","shell.execute_reply.started":"2024-09-11T19:28:32.484878Z","shell.execute_reply":"2024-09-11T19:28:32.516091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss=tf.keras.losses.CategoricalCrossentropy(),\n    metrics=['accuracy']\n)","metadata":{"id":"9etbvl-GyN6Z","execution":{"iopub.status.busy":"2024-09-11T19:28:32.521248Z","iopub.execute_input":"2024-09-11T19:28:32.521766Z","iopub.status.idle":"2024-09-11T19:28:32.532878Z","shell.execute_reply.started":"2024-09-11T19:28:32.521723Z","shell.execute_reply":"2024-09-11T19:28:32.531740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = ImageDataGenerator(\n    rotation_range=15,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True,\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-11T19:28:32.534423Z","iopub.execute_input":"2024-09-11T19:28:32.534920Z","iopub.status.idle":"2024-09-11T19:28:32.542362Z","shell.execute_reply.started":"2024-09-11T19:28:32.534866Z","shell.execute_reply":"2024-09-11T19:28:32.541217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(data_generator.flow(train_images, train_labels, batch_size=32), epochs=15, validation_data=(test_images, test_labels))","metadata":{"id":"UP2lSwRnzbS8","outputId":"db610398-04a8-4dc0-aa16-81de979880b1","execution":{"iopub.status.busy":"2024-09-11T19:28:32.543807Z","iopub.execute_input":"2024-09-11T19:28:32.544181Z","iopub.status.idle":"2024-09-11T19:47:27.561421Z","shell.execute_reply.started":"2024-09-11T19:28:32.544141Z","shell.execute_reply":"2024-09-11T19:47:27.560351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### plot the validation accuracy and training accuracy versus the epochs","metadata":{"id":"tl_m7PDwc0kM"}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')\n\nplt.show()","metadata":{"id":"rG7wSW3P0FOF","outputId":"1d2d76f8-ac87-4808-c49c-4eabbbfeeaff","execution":{"iopub.status.busy":"2024-09-11T19:47:27.563151Z","iopub.execute_input":"2024-09-11T19:47:27.563861Z","iopub.status.idle":"2024-09-11T19:47:28.097282Z","shell.execute_reply.started":"2024-09-11T19:47:27.563815Z","shell.execute_reply":"2024-09-11T19:47:28.096032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### plot the validation loss and training loss versus epochs","metadata":{"id":"VTAJ0dlSc75x"}},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['val_loss'], label = 'val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(loc='upper right')\n\nplt.show()","metadata":{"id":"4IBxD36l0JIz","outputId":"94b1a717-f0db-4b70-94ad-00647434f7c9","execution":{"iopub.status.busy":"2024-09-11T19:47:28.098931Z","iopub.execute_input":"2024-09-11T19:47:28.099422Z","iopub.status.idle":"2024-09-11T19:47:28.327672Z","shell.execute_reply.started":"2024-09-11T19:47:28.099364Z","shell.execute_reply":"2024-09-11T19:47:28.326521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## After 10 epochs we see the validation loss start increasing and training loss decreasing so it was supposed to stop training after epochs because the model is overfitting the dataset and its validation accuracy will be low ","metadata":{}},{"cell_type":"markdown","source":"## we can increase methods to prevent overfitting like regularization or use early stopping method or ensemble method to improve the generalization of the model ","metadata":{}},{"cell_type":"markdown","source":"# If you see the notebook is useful , dont forget to upvote! ","metadata":{}}]}