Skip to content

saket-118/CAR-LICENSE-PLATE-DETECTION

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install lxml\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:30:42.861411Z",
     "iopub.status.busy": "2023-04-04T14:30:42.860808Z",
     "iopub.status.idle": "2023-04-04T14:30:43.087706Z",
     "shell.execute_reply": "2023-04-04T14:30:43.086417Z",
     "shell.execute_reply.started": "2023-04-04T14:30:42.861377Z"
    }
   },
   "outputs": [],
   "source": [
    "import random, os, cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import visualkeras,glob\n",
    "import seaborn as sns\n",
    "\n",
    "from tensorflow.nn import relu,sigmoid,softmax\n",
    "from tensorflow.keras.utils import plot_model,load_img,to_categorical\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.layers import MaxPool2D,Conv2D,Dense,Flatten,BatchNormalization\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.callbacks import Callback\n",
    "\n",
    "from sklearn.utils import shuffle\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:02.574353Z",
     "iopub.status.busy": "2023-04-04T14:31:02.573332Z",
     "iopub.status.idle": "2023-04-04T14:31:02.578082Z",
     "shell.execute_reply": "2023-04-04T14:31:02.577191Z",
     "shell.execute_reply.started": "2023-04-04T14:31:02.574312Z"
    }
   },
   "outputs": [],
   "source": [
    "mypath_img=\"images\"\n",
    "mypath_annotation=\"annotations\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:05.014207Z",
     "iopub.status.busy": "2023-04-04T14:31:05.013246Z",
     "iopub.status.idle": "2023-04-04T14:31:13.706980Z",
     "shell.execute_reply": "2023-04-04T14:31:13.705879Z",
     "shell.execute_reply.started": "2023-04-04T14:31:05.014167Z"
    }
   },
   "outputs": [],
   "source": [
    "images=[]\n",
    "list_img=[mypath_img+\"/\"+f for f in os.listdir(mypath_img)]\n",
    "list_img=sorted(list_img)\n",
    "list_img\n",
    "for i in list_img:\n",
    "    input_img=cv2.imread(i)\n",
    "    input_img=cv2.resize(input_img,(224,224))\n",
    "    images.append(np.array(input_img))\n",
    "x=images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:13.711104Z",
     "iopub.status.busy": "2023-04-04T14:31:13.710794Z",
     "iopub.status.idle": "2023-04-04T14:31:13.716378Z",
     "shell.execute_reply": "2023-04-04T14:31:13.715338Z",
     "shell.execute_reply.started": "2023-04-04T14:31:13.711075Z"
    }
   },
   "outputs": [],
   "source": [
    "size=224"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:13.719507Z",
     "iopub.status.busy": "2023-04-04T14:31:13.717759Z",
     "iopub.status.idle": "2023-04-04T14:31:13.775298Z",
     "shell.execute_reply": "2023-04-04T14:31:13.774381Z",
     "shell.execute_reply.started": "2023-04-04T14:31:13.719468Z"
    }
   },
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "def resizeannotation(f):\n",
    "    tree=etree.parse(f)\n",
    "    for dim in tree.xpath(\"size\"):\n",
    "        width=int(dim.xpath(\"width\")[0].text)\n",
    "        height=int(dim.xpath(\"height\")[0].text)\n",
    "    for dim in tree.xpath(\"object/bndbox\"):\n",
    "        xmin=int(dim.xpath(\"xmin\")[0].text)/(width/size)\n",
    "        ymin=int(dim.xpath(\"ymin\")[0].text)/(height/size)\n",
    "        xmax=int(dim.xpath(\"xmax\")[0].text)/(width/size)\n",
    "        ymax=int(dim.xpath(\"ymax\")[0].text)/(height/size)\n",
    "    return [int(xmax),int(ymax),int(xmin),int(ymin)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read XML Files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:13.777793Z",
     "iopub.status.busy": "2023-04-04T14:31:13.777384Z",
     "iopub.status.idle": "2023-04-04T14:31:15.772261Z",
     "shell.execute_reply": "2023-04-04T14:31:15.771223Z",
     "shell.execute_reply.started": "2023-04-04T14:31:13.777765Z"
    }
   },
   "outputs": [],
   "source": [
    "y=[]\n",
    "list_dir=[mypath_annotation+\"/\"+f for f in os.listdir(mypath_annotation)]\n",
    "for i in sorted(list_dir):\n",
    "    y.append(resizeannotation(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:15.776137Z",
     "iopub.status.busy": "2023-04-04T14:31:15.775849Z",
     "iopub.status.idle": "2023-04-04T14:31:15.785093Z",
     "shell.execute_reply": "2023-04-04T14:31:15.783938Z",
     "shell.execute_reply.started": "2023-04-04T14:31:15.776109Z"
    }
   },
   "outputs": [],
   "source": [
    "resizeannotation(\"annotations/Cars147.xml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:15.787226Z",
     "iopub.status.busy": "2023-04-04T14:31:15.786657Z",
     "iopub.status.idle": "2023-04-04T14:31:15.794970Z",
     "shell.execute_reply": "2023-04-04T14:31:15.793776Z",
     "shell.execute_reply.started": "2023-04-04T14:31:15.787183Z"
    }
   },
   "outputs": [],
   "source": [
    "y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:15.797241Z",
     "iopub.status.busy": "2023-04-04T14:31:15.796667Z",
     "iopub.status.idle": "2023-04-04T14:31:15.828269Z",
     "shell.execute_reply": "2023-04-04T14:31:15.827086Z",
     "shell.execute_reply.started": "2023-04-04T14:31:15.797204Z"
    }
   },
   "outputs": [],
   "source": [
    "np.array(x).shape,np.array(y).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:15.832098Z",
     "iopub.status.busy": "2023-04-04T14:31:15.831646Z",
     "iopub.status.idle": "2023-04-04T14:31:16.668620Z",
     "shell.execute_reply": "2023-04-04T14:31:16.667578Z",
     "shell.execute_reply.started": "2023-04-04T14:31:15.832060Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(10,20))\n",
    "for i in range(0,15) :\n",
    "    plt.subplot(10,5,i+1)\n",
    "    plt.axis('off')\n",
    "    plt.imshow(x[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:16.669985Z",
     "iopub.status.busy": "2023-04-04T14:31:16.669656Z",
     "iopub.status.idle": "2023-04-04T14:31:16.795204Z",
     "shell.execute_reply": "2023-04-04T14:31:16.794180Z",
     "shell.execute_reply.started": "2023-04-04T14:31:16.669953Z"
    }
   },
   "outputs": [],
   "source": [
    "fig=plt.figure(figsize=(10,7))\n",
    "fig.add_subplot(2,2,1)\n",
    "image=cv2.rectangle(x[0],(y[0][0],y[0][1]),(y[0][2],y[0][3]),(0, 0, 255))\n",
    "plt.imshow(image)\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:16.800761Z",
     "iopub.status.busy": "2023-04-04T14:31:16.799968Z",
     "iopub.status.idle": "2023-04-04T14:31:16.839798Z",
     "shell.execute_reply": "2023-04-04T14:31:16.838342Z",
     "shell.execute_reply.started": "2023-04-04T14:31:16.800721Z"
    }
   },
   "outputs": [],
   "source": [
    "x=np.array(x)\n",
    "y=np.array(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:16.842609Z",
     "iopub.status.busy": "2023-04-04T14:31:16.841875Z",
     "iopub.status.idle": "2023-04-04T14:31:17.015026Z",
     "shell.execute_reply": "2023-04-04T14:31:17.013972Z",
     "shell.execute_reply.started": "2023-04-04T14:31:16.842568Z"
    }
   },
   "outputs": [],
   "source": [
    "x=x/255\n",
    "y=y/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:17.017255Z",
     "iopub.status.busy": "2023-04-04T14:31:17.016861Z",
     "iopub.status.idle": "2023-04-04T14:31:17.620874Z",
     "shell.execute_reply": "2023-04-04T14:31:17.619720Z",
     "shell.execute_reply.started": "2023-04-04T14:31:17.017212Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)\n",
    "x_train,x_val,y_train,y_val=train_test_split(x_train,y_train,test_size=0.1,random_state=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:31:17.622861Z",
     "iopub.status.busy": "2023-04-04T14:31:17.622493Z",
     "iopub.status.idle": "2023-04-04T14:31:24.898074Z",
     "shell.execute_reply": "2023-04-04T14:31:24.896977Z",
     "shell.execute_reply.started": "2023-04-04T14:31:17.622821Z"
    }
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing import image\n",
    "from tensorflow.keras.applications.vgg16 import VGG16\n",
    "from tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D,Dropout, MaxPool2D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:06.040729Z",
     "iopub.status.busy": "2023-04-04T14:32:06.039744Z",
     "iopub.status.idle": "2023-04-04T14:32:06.069238Z",
     "shell.execute_reply": "2023-04-04T14:32:06.067184Z",
     "shell.execute_reply.started": "2023-04-04T14:32:06.040677Z"
    }
   },
   "outputs": [],
   "source": [
    "model=Sequential()\n",
    "model.add(Conv2D(filters=16,kernel_size=(5,5),input_shape=(224,224,3),padding='same', activation = 'relu'))\n",
    "model.add(MaxPool2D(pool_size=(2,2)))\n",
    "model.add(Conv2D(32,(5,5),padding='same',activation='relu'))\n",
    "model.add(MaxPool2D(pool_size=(2,2)))\n",
    "model.add(Conv2D(64,(3,3),activation='relu'))\n",
    "model.add(MaxPool2D(pool_size=(2,2)))\n",
    "model.add(Flatten())\n",
    "model.add(Dense(512,activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Dense(256,activation='relu'))\n",
    "model.add(Dense(128,activation='relu'))\n",
    "model.add(Dense(4,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:06.797255Z",
     "iopub.status.busy": "2023-04-04T14:32:06.796902Z",
     "iopub.status.idle": "2023-04-04T14:32:06.821347Z",
     "shell.execute_reply": "2023-04-04T14:32:06.820106Z",
     "shell.execute_reply.started": "2023-04-04T14:32:06.797223Z"
    }
   },
   "outputs": [],
   "source": [
    "visualkeras.layered_view(model,legend=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:21.199850Z",
     "iopub.status.busy": "2023-04-04T14:32:21.199153Z",
     "iopub.status.idle": "2023-04-04T14:32:21.224777Z",
     "shell.execute_reply": "2023-04-04T14:32:21.222447Z",
     "shell.execute_reply.started": "2023-04-04T14:32:21.199809Z"
    }
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:21.491601Z",
     "iopub.status.busy": "2023-04-04T14:32:21.490764Z",
     "iopub.status.idle": "2023-04-04T14:32:21.497890Z",
     "shell.execute_reply": "2023-04-04T14:32:21.496816Z",
     "shell.execute_reply.started": "2023-04-04T14:32:21.491550Z"
    }
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.callbacks import Callback\n",
    "class myCallback(Callback):\n",
    "    def on_epoch_end(self,epoch,logs={}):\n",
    "        if logs.get('accuracy') is not None and logs.get('accuracy')>=0.980:\n",
    "            print(\"\\nReached 99.9% accuracy so cancelling training!\")\n",
    "            self.model.stop_training = True\n",
    "callback=myCallback()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:21.745831Z",
     "iopub.status.busy": "2023-04-04T14:32:21.744999Z",
     "iopub.status.idle": "2023-04-04T14:32:21.770603Z",
     "shell.execute_reply": "2023-04-04T14:32:21.768746Z",
     "shell.execute_reply.started": "2023-04-04T14:32:21.745798Z"
    }
   },
   "outputs": [],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:21.964068Z",
     "iopub.status.busy": "2023-04-04T14:32:21.963499Z",
     "iopub.status.idle": "2023-04-04T14:32:21.986385Z",
     "shell.execute_reply": "2023-04-04T14:32:21.984361Z",
     "shell.execute_reply.started": "2023-04-04T14:32:21.964037Z"
    }
   },
   "outputs": [],
   "source": [
    "hist=model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=10,batch_size=32,verbose=1,callbacks=callback)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:22.146616Z",
     "iopub.status.busy": "2023-04-04T14:32:22.146265Z",
     "iopub.status.idle": "2023-04-04T14:32:22.175688Z",
     "shell.execute_reply": "2023-04-04T14:32:22.174386Z",
     "shell.execute_reply.started": "2023-04-04T14:32:22.146586Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"accuracy\"],label=\"Train Accuracy\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_accuracy\"],label=\"Validation Accuracy\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Accuracy\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:22.346234Z",
     "iopub.status.busy": "2023-04-04T14:32:22.345911Z",
     "iopub.status.idle": "2023-04-04T14:32:22.375643Z",
     "shell.execute_reply": "2023-04-04T14:32:22.374379Z",
     "shell.execute_reply.started": "2023-04-04T14:32:22.346206Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"loss\"],label=\"Train Loss\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_loss\"],label=\"Validation Loss\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Loss\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:22.555147Z",
     "iopub.status.busy": "2023-04-04T14:32:22.554517Z",
     "iopub.status.idle": "2023-04-04T14:32:22.579139Z",
     "shell.execute_reply": "2023-04-04T14:32:22.577395Z",
     "shell.execute_reply.started": "2023-04-04T14:32:22.555114Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Score : 56.32%\n"
     ]
    }
   ],
   "source": [
    "scores=model.evaluate(x_test,y_test,verbose=0)\n",
    "print(\"Score : %.2f%%\" % (scores[1]*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:22.755167Z",
     "iopub.status.busy": "2023-04-04T14:32:22.754871Z",
     "iopub.status.idle": "2023-04-04T14:32:22.777581Z",
     "shell.execute_reply": "2023-04-04T14:32:22.774969Z",
     "shell.execute_reply.started": "2023-04-04T14:32:22.755140Z"
    }
   },
   "outputs": [],
   "source": [
    "test_loss,test_accuracy=model.evaluate(x_test,y_test,steps=int(100),verbose=0)\n",
    "print(\"Test results \\n Loss:\",test_loss,'\\n Accuracy',test_accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:22.934545Z",
     "iopub.status.busy": "2023-04-04T14:32:22.933566Z",
     "iopub.status.idle": "2023-04-04T14:32:22.954456Z",
     "shell.execute_reply": "2023-04-04T14:32:22.953038Z",
     "shell.execute_reply.started": "2023-04-04T14:32:22.934503Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pred=model.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:23.139285Z",
     "iopub.status.busy": "2023-04-04T14:32:23.138735Z",
     "iopub.status.idle": "2023-04-04T14:32:23.237426Z",
     "shell.execute_reply": "2023-04-04T14:32:23.233871Z",
     "shell.execute_reply.started": "2023-04-04T14:32:23.139253Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(20,40))\n",
    "for i in range(0,43):\n",
    "    plt.subplot(10,5,i+1)\n",
    "    plt.axis('off')\n",
    "    ny=y_pred[i]*255\n",
    "    image=cv2.rectangle(x_test[i],(int(ny[0]),int(ny[1])),(int(ny[2]),int(ny[3])),(0,255,0))\n",
    "    plt.imshow(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# VGG Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:32:23.578916Z",
     "iopub.status.busy": "2023-04-04T14:32:23.578193Z",
     "iopub.status.idle": "2023-04-04T14:32:23.605472Z",
     "shell.execute_reply": "2023-04-04T14:32:23.603917Z",
     "shell.execute_reply.started": "2023-04-04T14:32:23.578879Z"
    }
   },
   "outputs": [],
   "source": [
    "model=Sequential()\n",
    "model.add(VGG16(weights=\"imagenet\",include_top=False,input_shape=(224,224,3)))\n",
    "model.add(Flatten())\n",
    "model.add(Dense(128,activation=\"relu\"))\n",
    "model.add(Dense(128,activation=\"relu\"))\n",
    "model.add(Dense(64,activation=\"relu\"))\n",
    "model.add(Dense(4,activation=\"sigmoid\"))\n",
    "model.layers[-6].trainable = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:20:27.730338Z",
     "iopub.status.busy": "2023-04-04T14:20:27.729959Z",
     "iopub.status.idle": "2023-04-04T14:20:27.744277Z",
     "shell.execute_reply": "2023-04-04T14:20:27.743105Z",
     "shell.execute_reply.started": "2023-04-04T14:20:27.730306Z"
    }
   },
   "outputs": [],
   "source": [
    "visualkeras.layered_view(model,legend=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:20:28.048582Z",
     "iopub.status.busy": "2023-04-04T14:20:28.047653Z",
     "iopub.status.idle": "2023-04-04T14:20:28.061352Z",
     "shell.execute_reply": "2023-04-04T14:20:28.0604Z",
     "shell.execute_reply.started": "2023-04-04T14:20:28.048534Z"
    }
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:20:28.273558Z",
     "iopub.status.busy": "2023-04-04T14:20:28.272205Z",
     "iopub.status.idle": "2023-04-04T14:20:28.280409Z",
     "shell.execute_reply": "2023-04-04T14:20:28.279177Z",
     "shell.execute_reply.started": "2023-04-04T14:20:28.273512Z"
    }
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.callbacks import Callback\n",
    "class myCallback(Callback):\n",
    "    def on_epoch_end(self,epoch,logs={}):\n",
    "        if logs.get('accuracy') is not None and logs.get('accuracy')>=0.980:\n",
    "            print(\"\\nReached 99.9% accuracy so cancelling training!\")\n",
    "            self.model.stop_training = True\n",
    "callback=myCallback()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:20:28.412236Z",
     "iopub.status.busy": "2023-04-04T14:20:28.411852Z",
     "iopub.status.idle": "2023-04-04T14:20:28.436015Z",
     "shell.execute_reply": "2023-04-04T14:20:28.435188Z",
     "shell.execute_reply.started": "2023-04-04T14:20:28.412205Z"
    }
   },
   "outputs": [],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:20:28.656033Z",
     "iopub.status.busy": "2023-04-04T14:20:28.654961Z",
     "iopub.status.idle": "2023-04-04T14:21:17.315353Z",
     "shell.execute_reply": "2023-04-04T14:21:17.31432Z",
     "shell.execute_reply.started": "2023-04-04T14:20:28.655983Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "hist=model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=10,batch_size=32,verbose=1,callbacks=callback)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:17.317986Z",
     "iopub.status.busy": "2023-04-04T14:21:17.31761Z",
     "iopub.status.idle": "2023-04-04T14:21:52.247762Z",
     "shell.execute_reply": "2023-04-04T14:21:52.246094Z",
     "shell.execute_reply.started": "2023-04-04T14:21:17.317947Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"accuracy\"],label=\"Train Accuracy\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_accuracy\"],label=\"Validation Accuracy\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Accuracy\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:52.249939Z",
     "iopub.status.busy": "2023-04-04T14:21:52.249563Z",
     "iopub.status.idle": "2023-04-04T14:21:52.47383Z",
     "shell.execute_reply": "2023-04-04T14:21:52.472808Z",
     "shell.execute_reply.started": "2023-04-04T14:21:52.249899Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"loss\"],label=\"Train Loss\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_loss\"],label=\"Validation Loss\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Loss\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:52.477197Z",
     "iopub.status.busy": "2023-04-04T14:21:52.476342Z",
     "iopub.status.idle": "2023-04-04T14:21:52.972313Z",
     "shell.execute_reply": "2023-04-04T14:21:52.971202Z",
     "shell.execute_reply.started": "2023-04-04T14:21:52.477157Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Score : 71.26%\n"
     ]
    }
   ],
   "source": [
    "scores=model.evaluate(x_test,y_test,verbose=0)\n",
    "print(\"Score : %.2f%%\" % (scores[1]*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:52.974406Z",
     "iopub.status.busy": "2023-04-04T14:21:52.973854Z",
     "iopub.status.idle": "2023-04-04T14:21:53.775665Z",
     "shell.execute_reply": "2023-04-04T14:21:53.774524Z",
     "shell.execute_reply.started": "2023-04-04T14:21:52.974364Z"
    }
   },
   "outputs": [],
   "source": [
    "test_loss,test_accuracy=model.evaluate(x_test,y_test,steps=int(100),verbose=0)\n",
    "print(\"Test results \\n Loss:\",test_loss,'\\n Accuracy',test_accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:53.777878Z",
     "iopub.status.busy": "2023-04-04T14:21:53.777056Z",
     "iopub.status.idle": "2023-04-04T14:21:54.278162Z",
     "shell.execute_reply": "2023-04-04T14:21:54.277165Z",
     "shell.execute_reply.started": "2023-04-04T14:21:53.777839Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pred=model.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:21:54.280407Z",
     "iopub.status.busy": "2023-04-04T14:21:54.279996Z",
     "iopub.status.idle": "2023-04-04T14:21:57.987711Z",
     "shell.execute_reply": "2023-04-04T14:21:57.986352Z",
     "shell.execute_reply.started": "2023-04-04T14:21:54.280368Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(20,40))\n",
    "for i in range(0,43):\n",
    "    plt.subplot(10,5,i+1)\n",
    "    plt.axis('off')\n",
    "    ny=y_pred[i]*255\n",
    "    image=cv2.rectangle(x_test[i],(int(ny[0]),int(ny[1])),(int(ny[2]),int(ny[3])),(0,255,0))\n",
    "    plt.imshow(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ResNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:09.12205Z",
     "iopub.status.busy": "2023-04-04T14:18:09.121154Z",
     "iopub.status.idle": "2023-04-04T14:18:12.077134Z",
     "shell.execute_reply": "2023-04-04T14:18:12.076071Z",
     "shell.execute_reply.started": "2023-04-04T14:18:09.121993Z"
    }
   },
   "outputs": [],
   "source": [
    "from keras.applications import VGG16\n",
    "from keras.layers import Concatenate, Dense, Input\n",
    "from keras.models import Model\n",
    "from tensorflow.keras.applications.resnet50 import ResNet50\n",
    "\n",
    "model=Sequential()\n",
    "model.add(ResNet50(weights=\"imagenet\",include_top=False,input_shape=(224,224,3)))\n",
    "model.add(Flatten())\n",
    "model.add(Dense(128,activation=\"relu\"))\n",
    "model.add(Dense(128,activation=\"relu\"))\n",
    "model.add(Dense(64,activation=\"relu\"))\n",
    "model.add(Dense(4,activation=\"sigmoid\"))\n",
    "model.layers[-6].trainable = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:12.07878Z",
     "iopub.status.busy": "2023-04-04T14:18:12.07843Z",
     "iopub.status.idle": "2023-04-04T14:18:12.126051Z",
     "shell.execute_reply": "2023-04-04T14:18:12.125138Z",
     "shell.execute_reply.started": "2023-04-04T14:18:12.078745Z"
    }
   },
   "outputs": [],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:12.128348Z",
     "iopub.status.busy": "2023-04-04T14:18:12.127631Z",
     "iopub.status.idle": "2023-04-04T14:18:12.150793Z",
     "shell.execute_reply": "2023-04-04T14:18:12.149588Z",
     "shell.execute_reply.started": "2023-04-04T14:18:12.128307Z"
    }
   },
   "outputs": [],
   "source": [
    "visualkeras.layered_view(model,legend=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:12.153596Z",
     "iopub.status.busy": "2023-04-04T14:18:12.152741Z",
     "iopub.status.idle": "2023-04-04T14:18:12.186522Z",
     "shell.execute_reply": "2023-04-04T14:18:12.185008Z",
     "shell.execute_reply.started": "2023-04-04T14:18:12.15355Z"
    }
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:12.191204Z",
     "iopub.status.busy": "2023-04-04T14:18:12.190192Z",
     "iopub.status.idle": "2023-04-04T14:18:41.343342Z",
     "shell.execute_reply": "2023-04-04T14:18:41.342189Z",
     "shell.execute_reply.started": "2023-04-04T14:18:12.19116Z"
    }
   },
   "outputs": [],
   "source": [
    "hist=model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=30,batch_size=32,verbose=1,callbacks=callback)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:41.347254Z",
     "iopub.status.busy": "2023-04-04T14:18:41.346954Z",
     "iopub.status.idle": "2023-04-04T14:18:41.571658Z",
     "shell.execute_reply": "2023-04-04T14:18:41.570642Z",
     "shell.execute_reply.started": "2023-04-04T14:18:41.347225Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"accuracy\"],label=\"Train Accuracy\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_accuracy\"],label=\"Validation Accuracy\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Accuracy\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:41.573904Z",
     "iopub.status.busy": "2023-04-04T14:18:41.573245Z",
     "iopub.status.idle": "2023-04-04T14:18:41.784305Z",
     "shell.execute_reply": "2023-04-04T14:18:41.783204Z",
     "shell.execute_reply.started": "2023-04-04T14:18:41.573863Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.plot(hist.history[\"loss\"],label=\"Train Loss\",color=\"black\")\n",
    "plt.plot(hist.history[\"val_loss\"],label=\"Validation Loss\",color=\"mediumvioletred\",linestyle=\"dashed\",markeredgecolor=\"purple\",markeredgewidth=2)\n",
    "plt.title(\"Model Loss\",color=\"darkred\",size=13)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:41.786161Z",
     "iopub.status.busy": "2023-04-04T14:18:41.785812Z",
     "iopub.status.idle": "2023-04-04T14:18:42.135005Z",
     "shell.execute_reply": "2023-04-04T14:18:42.133939Z",
     "shell.execute_reply.started": "2023-04-04T14:18:41.786124Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Score : 50.57%\n"
     ]
    }
   ],
   "source": [
    "scores=model.evaluate(x_test,y_test,verbose=0)\n",
    "print(\"Score : %.2f%%\" % (scores[1]*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:42.137953Z",
     "iopub.status.busy": "2023-04-04T14:18:42.137311Z",
     "iopub.status.idle": "2023-04-04T14:18:43.582309Z",
     "shell.execute_reply": "2023-04-04T14:18:43.581303Z",
     "shell.execute_reply.started": "2023-04-04T14:18:42.137913Z"
    }
   },
   "outputs": [],
   "source": [
    "test_loss,test_accuracy=model.evaluate(x_test,y_test,steps=int(100),verbose=0)\n",
    "print(\"Test results \\n Loss:\",test_loss,'\\n Accuracy',test_accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:43.584403Z",
     "iopub.status.busy": "2023-04-04T14:18:43.58393Z",
     "iopub.status.idle": "2023-04-04T14:18:44.835018Z",
     "shell.execute_reply": "2023-04-04T14:18:44.833984Z",
     "shell.execute_reply.started": "2023-04-04T14:18:43.584364Z"
    }
   },
   "outputs": [],
   "source": [
    "y_pred=model.predict(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-04-04T14:18:44.837585Z",
     "iopub.status.busy": "2023-04-04T14:18:44.83718Z",
     "iopub.status.idle": "2023-04-04T14:18:48.976196Z",
     "shell.execute_reply": "2023-04-04T14:18:48.973358Z",
     "shell.execute_reply.started": "2023-04-04T14:18:44.837543Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure(figsize=(20,40))\n",
    "for i in range(0,43):\n",
    "    plt.subplot(10,5,i+1)\n",
    "    plt.axis('off')\n",
    "    ny=y_pred[i]*255\n",
    "    image=cv2.rectangle(x_test[i],(int(ny[0]),int(ny[1])),(int(ny[2]),int(ny[3])),(0,255,0))\n",
    "    plt.imshow(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

About

No description, website, or topics provided.

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published