{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "3DR-eO17geWu" }, "source": [ "## Image Recognition Model With CNN" ] }, { "cell_type": "markdown", "metadata": { "id": "EMefrVPCg-60" }, "source": [ "### Importing the libraries" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "w3mybNcNuyRf" }, "outputs": [], "source": [ "import tensorflow as tf\n", "from keras.preprocessing.image import ImageDataGenerator" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 38 }, "id": "Gk81TsV3vNYv", "outputId": "ac51f686-a76e-4be8-ae56-5db116913a8b" }, "outputs": [ { "data": { "text/plain": [ "'2.15.0'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.__version__" ] }, { "cell_type": "markdown", "metadata": { "id": "oxQxCBWyoGPE" }, "source": [ "## Part 1 - Data Preprocessing" ] }, { "cell_type": "markdown", "metadata": { "id": "MvE-heJNo3GG" }, "source": [ "### Preprocessing the Training set" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "IZPgIbSyzcZz" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 8000 images belonging to 2 classes.\n" ] } ], "source": [ "train_datagen = ImageDataGenerator(rescale = 1./255,\n", " shear_range = 0.2,\n", " zoom_range = 0.2,\n", " horizontal_flip = True)\n", "training_set = train_datagen.flow_from_directory('dataset/training_set',\n", " target_size = (64, 64),\n", " batch_size = 32,\n", " class_mode = 'binary')" ] }, { "cell_type": "markdown", "metadata": { "id": "mrCMmGw9pHys" }, "source": [ "### Preprocessing the Test set" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "qTwLxo10zn-P" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 2000 images belonging to 2 classes.\n" ] } ], "source": [ "test_datagen = ImageDataGenerator(rescale = 1./255)\n", "test_set = test_datagen.flow_from_directory('dataset/test_set',\n", " target_size = (64, 64),\n", " batch_size = 32,\n", " class_mode = 'binary')" ] }, { "cell_type": "markdown", "metadata": { "id": "af8O4l90gk7B" }, "source": [ "## Part 2 - Building the CNN" ] }, { "cell_type": "markdown", "metadata": { "id": "ces1gXY2lmoX" }, "source": [ "### Initialising the CNN" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "OywsP_gTz8VA" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From C:\\Users\\manik\\anaconda3\\Lib\\site-packages\\keras\\src\\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", "\n" ] } ], "source": [ "cnn=tf.keras.models.Sequential()" ] }, { "cell_type": "markdown", "metadata": { "id": "u5YJj_XMl5LF" }, "source": [ "### Step 1 - Convolution" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "dkA5f15L0IRA" }, "outputs": [], "source": [ "cnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape=[64,64,3]))" ] }, { "cell_type": "markdown", "metadata": { "id": "tf87FpvxmNOJ" }, "source": [ "### Step 2 - Pooling" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "Khzh28Gj056f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From C:\\Users\\manik\\anaconda3\\Lib\\site-packages\\keras\\src\\layers\\pooling\\max_pooling2d.py:161: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n", "\n" ] } ], "source": [ "cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))" ] }, { "cell_type": "markdown", "metadata": { "id": "xaTOgD8rm4mU" }, "source": [ "### Adding a second convolutional layer" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "G7GvlV8W1zFv" }, "outputs": [], "source": [ "cnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu'))\n", "cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))" ] }, { "cell_type": "markdown", "metadata": { "id": "tmiEuvTunKfk" }, "source": [ "### Step 3 - Flattening" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "CgmlA6xs16ub" }, "outputs": [], "source": [ "cnn.add(tf.keras.layers.Flatten())" ] }, { "cell_type": "markdown", "metadata": { "id": "dAoSECOm203v" }, "source": [ "### Step 4 - Full Connection" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "1DXEM0C-2Cv6" }, "outputs": [], "source": [ "cnn.add(tf.keras.layers.Dense(units=128,activation='relu'))" ] }, { "cell_type": "markdown", "metadata": { "id": "yTldFvbX28Na" }, "source": [ "### Step 5 - Output Layer" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "bM9tOMr02dTk" }, "outputs": [], "source": [ "cnn.add(tf.keras.layers.Dense(units=1,activation='sigmoid'))" ] }, { "cell_type": "markdown", "metadata": { "id": "D6XkI90snSDl" }, "source": [ "## Part 3 - Training the CNN" ] }, { "cell_type": "markdown", "metadata": { "id": "vfrFQACEnc6i" }, "source": [ "### Compiling the CNN" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "QRSs7B252tM8" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From C:\\Users\\manik\\anaconda3\\Lib\\site-packages\\keras\\src\\optimizers\\__init__.py:309: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n", "\n" ] } ], "source": [ "cnn.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])" ] }, { "cell_type": "markdown", "metadata": { "id": "ehS-v3MIpX2h" }, "source": [ "### Training the CNN on the Training set and evaluating it on the Test set" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "ibgjSnzf3Mai" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/25\n", "WARNING:tensorflow:From C:\\Users\\manik\\anaconda3\\Lib\\site-packages\\keras\\src\\utils\\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.\n", "\n", "WARNING:tensorflow:From C:\\Users\\manik\\anaconda3\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n", "\n", "250/250 [==============================] - 75s 298ms/step - loss: 0.6505 - accuracy: 0.6121 - val_loss: 0.5777 - val_accuracy: 0.7080\n", "Epoch 2/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.5849 - accuracy: 0.6865 - val_loss: 0.5398 - val_accuracy: 0.7290\n", "Epoch 3/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.5501 - accuracy: 0.7189 - val_loss: 0.5827 - val_accuracy: 0.6865\n", "Epoch 4/25\n", "250/250 [==============================] - 30s 122ms/step - loss: 0.5212 - accuracy: 0.7369 - val_loss: 0.5147 - val_accuracy: 0.7455\n", "Epoch 5/25\n", "250/250 [==============================] - 30s 119ms/step - loss: 0.4896 - accuracy: 0.7613 - val_loss: 0.5152 - val_accuracy: 0.7550\n", "Epoch 6/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.4721 - accuracy: 0.7770 - val_loss: 0.4776 - val_accuracy: 0.7640\n", "Epoch 7/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.4506 - accuracy: 0.7826 - val_loss: 0.4668 - val_accuracy: 0.7845\n", "Epoch 8/25\n", "250/250 [==============================] - 32s 126ms/step - loss: 0.4380 - accuracy: 0.7868 - val_loss: 0.4637 - val_accuracy: 0.7870\n", "Epoch 9/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.4231 - accuracy: 0.8026 - val_loss: 0.4680 - val_accuracy: 0.7850\n", "Epoch 10/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.4048 - accuracy: 0.8135 - val_loss: 0.5443 - val_accuracy: 0.7470\n", "Epoch 11/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.3917 - accuracy: 0.8242 - val_loss: 0.4698 - val_accuracy: 0.7875\n", "Epoch 12/25\n", "250/250 [==============================] - 37s 148ms/step - loss: 0.3743 - accuracy: 0.8284 - val_loss: 0.4812 - val_accuracy: 0.7940\n", "Epoch 13/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.3720 - accuracy: 0.8317 - val_loss: 0.4309 - val_accuracy: 0.8140\n", "Epoch 14/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.3497 - accuracy: 0.8443 - val_loss: 0.4593 - val_accuracy: 0.7980\n", "Epoch 15/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.3407 - accuracy: 0.8514 - val_loss: 0.4699 - val_accuracy: 0.7925\n", "Epoch 16/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.3249 - accuracy: 0.8593 - val_loss: 0.4712 - val_accuracy: 0.8035\n", "Epoch 17/25\n", "250/250 [==============================] - 30s 122ms/step - loss: 0.3044 - accuracy: 0.8748 - val_loss: 0.4846 - val_accuracy: 0.8060\n", "Epoch 18/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.3082 - accuracy: 0.8608 - val_loss: 0.4750 - val_accuracy: 0.8040\n", "Epoch 19/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.2860 - accuracy: 0.8752 - val_loss: 0.4533 - val_accuracy: 0.8180\n", "Epoch 20/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.2761 - accuracy: 0.8792 - val_loss: 0.5786 - val_accuracy: 0.7975\n", "Epoch 21/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.2634 - accuracy: 0.8861 - val_loss: 0.4989 - val_accuracy: 0.8085\n", "Epoch 22/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.2491 - accuracy: 0.8946 - val_loss: 0.5098 - val_accuracy: 0.8160\n", "Epoch 23/25\n", "250/250 [==============================] - 31s 124ms/step - loss: 0.2385 - accuracy: 0.8981 - val_loss: 0.5296 - val_accuracy: 0.8115\n", "Epoch 24/25\n", "250/250 [==============================] - 30s 120ms/step - loss: 0.2260 - accuracy: 0.9040 - val_loss: 0.5117 - val_accuracy: 0.8105\n", "Epoch 25/25\n", "250/250 [==============================] - 30s 121ms/step - loss: 0.2117 - accuracy: 0.9146 - val_loss: 0.5145 - val_accuracy: 0.8180\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cnn.fit(x=training_set,validation_data=test_set,epochs=25)" ] }, { "cell_type": "markdown", "metadata": { "id": "U3PZasO0006Z" }, "source": [ "## Part 4 - Making a single prediction" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "PQGfPACq3mGS" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 0s 107ms/step\n" ] } ], "source": [ "import numpy as np\n", "from keras.preprocessing import image\n", "test_image=image.load_img('dataset/single_prediction/cat_or_dog_1.jpg',target_size=(64,64))\n", "test_image=image.img_to_array(test_image)\n", "test_image=np.expand_dims(test_image,axis=0)\n", "result=cnn.predict(test_image)\n", "training_set.class_indices\n", "if result[0][0]==1:\n", " prediction='dog'\n", "else:\n", " prediction='cat'" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "xMvlNcFN5yEX" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dog\n" ] } ], "source": [ "print(prediction)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 0s 25ms/step\n" ] } ], "source": [ "import numpy as np\n", "from keras.preprocessing import image\n", "test_image=image.load_img('dataset/single_prediction/cat_or_dog_2.jpg',target_size=(64,64))\n", "test_image=image.img_to_array(test_image)\n", "test_image=np.expand_dims(test_image,axis=0)\n", "result=cnn.predict(test_image)\n", "training_set.class_indices\n", "if result[0][0]==1:\n", " prediction='dog'\n", "else:\n", " prediction='cat'" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat\n" ] } ], "source": [ "print(prediction)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 0s 26ms/step\n" ] } ], "source": [ "import numpy as np\n", "from keras.preprocessing import image\n", "test_image=image.load_img('dataset/single_prediction/cat_or_dog_3.jpg',target_size=(64,64))\n", "test_image=image.img_to_array(test_image)\n", "test_image=np.expand_dims(test_image,axis=0)\n", "result=cnn.predict(test_image)\n", "training_set.class_indices\n", "if result[0][0]==1:\n", " prediction='dog'\n", "else:\n", " prediction='cat'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat\n" ] } ], "source": [ "print(prediction)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/1 [==============================] - 0s 25ms/step\n" ] } ], "source": [ "import numpy as np\n", "from keras.preprocessing import image\n", "test_image=image.load_img('dataset/single_prediction/cat_or_dog_4.jpg',target_size=(64,64))\n", "test_image=image.img_to_array(test_image)\n", "test_image=np.expand_dims(test_image,axis=0)\n", "result=cnn.predict(test_image)\n", "training_set.class_indices\n", "if result[0][0]==1:\n", " prediction='dog'\n", "else:\n", " prediction='cat'" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dog\n" ] } ], "source": [ "print(prediction)" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 1 }