vaishanthr/Image-Classifier-TensorFlow
1
1{2 "nbformat": 4,3 "nbformat_minor": 0,4 "metadata": {5 "colab": {6 "provenance": [],7 "gpuType": "T4"8 },9 "kernelspec": {10 "name": "python3",11 "display_name": "Python 3"12 },13 "language_info": {14 "name": "python"15 },16 "accelerator": "GPU"17 },18 "cells": [19 {20 "cell_type": "code",21 "execution_count": 9,22 "metadata": {23 "colab": {24 "base_uri": "https://localhost:8080/"25 },26 "id": "OdOgOEqcDzhY",27 "outputId": "a1787cb0-c94a-4145-ef35-bb222f63a373"28 },29 "outputs": [30 {31 "output_type": "stream",32 "name": "stdout",33 "text": [34 "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n",35 "/content/drive/My Drive/My Projects/Image_Classifier_TensorFlow\n"36 ]37 }38 ],39 "source": [40 "# This mounts your Google Drive to the Colab VM.\n",41 "from google.colab import drive\n",42 "drive.mount('/content/drive')\n",43 "\n",44 "%cd /content/drive/My\\ Drive/My\\ Projects/Image_Classifier_TensorFlow"45 ]46 },47 {48 "cell_type": "code",49 "source": [50 "pwd"51 ],52 "metadata": {53 "colab": {54 "base_uri": "https://localhost:8080/",55 "height": 3656 },57 "id": "EuUA1qNaEdGB",58 "outputId": "b9b3ca06-157a-4686-92ab-72c080dddcfb"59 },60 "execution_count": 10,61 "outputs": [62 {63 "output_type": "execute_result",64 "data": {65 "text/plain": [66 "'/content/drive/My Drive/My Projects/Image_Classifier_TensorFlow'"67 ],68 "application/vnd.google.colaboratory.intrinsic+json": {69 "type": "string"70 }71 },72 "metadata": {},73 "execution_count": 1074 }75 ]76 },77 {78 "cell_type": "markdown",79 "source": [80 "# Gradio App"81 ],82 "metadata": {83 "id": "6XXQqgGmErXJ"84 }85 },86 {87 "cell_type": "code",88 "source": [89 "# installations\n",90 "!pip install gradio"91 ],92 "metadata": {93 "id": "wSuhvzbEE8Ql"94 },95 "execution_count": null,96 "outputs": []97 },98 {99 "cell_type": "markdown",100 "source": [101 "## Training"102 ],103 "metadata": {104 "id": "71zplmVlFU9J"105 }106 },107 {108 "cell_type": "code",109 "source": [110 "print(\"Training model...\")\n",111 "# Create an instance of the ImageClassifier\n",112 "classifier = ImageClassifier()\n",113 "\n",114 "# Load the dataset\n",115 "(x_train, y_train), (x_test, y_test) = classifier.load_dataset()\n",116 "\n",117 "# Build and train the model\n",118 "classifier.build_model(x_train)\n",119 "classifier.train_model(x_train, y_train, batch_size=64, epochs=1, validation_split=0.1)\n",120 "\n",121 "# Evaluate the model\n",122 "classifier.evaluate_model(x_test, y_test)\n",123 "\n",124 "# Save the trained model\n",125 "print(\"Saving model ...\")\n",126 "classifier.save_model(\"image_classifier_model.h5\")"127 ],128 "metadata": {129 "colab": {130 "base_uri": "https://localhost:8080/"131 },132 "id": "Q9vKOsnKFRu4",133 "outputId": "93268865-5288-44a3-bc09-6d30620655f8"134 },135 "execution_count": 13,136 "outputs": [137 {138 "output_type": "stream",139 "name": "stdout",140 "text": [141 "Training model...\n",142 "704/704 [==============================] - 187s 263ms/step - loss: 1.5925 - accuracy: 0.4633 - val_loss: 1.3171 - val_accuracy: 0.5372\n",143 "Test loss: 1.3429059982299805\n",144 "Test accuracy: 0.5228999853134155\n",145 "Saving model ...\n"146 ]147 }148 ]149 },150 {151 "cell_type": "code",152 "source": [153 "import gradio as gr\n",154 "import tensorflow as tf\n",155 "from tensorflow import keras\n",156 "from custom_model import ImageClassifier\n",157 "from resnet_model import ResNetClassifier\n",158 "from vgg16_model import VGG16Classifier\n",159 "from inception_v3_model import InceptionV3Classifier\n",160 "from mobilevet_v2 import MobileNetClassifier\n",161 "\n",162 "CLASS_NAMES =['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']\n",163 "\n",164 "# models\n",165 "custom_model = ImageClassifier()\n",166 "custom_model.load_model(\"image_classifier_model.h5\")\n",167 "resnet_model = ResNetClassifier()\n",168 "vgg16_model = VGG16Classifier()\n",169 "inceptionV3_model = InceptionV3Classifier()\n",170 "mobilenet_model = MobileNetClassifier()\n",171 "\n",172 "def make_prediction(image, model_type):\n",173 " if \"CNN (2 layer) - Custom\" == model_type:\n",174 " top_classes, top_probs = custom_model.classify_image(image, top_k=3)\n",175 " return {CLASS_NAMES[cls_id]:str(prob) for cls_id, prob in zip(top_classes, top_probs)}\n",176 " elif \"ResNet50\" == model_type:\n",177 " predictions = resnet_model.classify_image(image)\n",178 " return {class_name:str(prob) for _, class_name, prob in predictions}\n",179 " elif \"VGG16\" == model_type:\n",180 " predictions = vgg16_model.classify_image(image)\n",181 " return {class_name:str(prob) for _, class_name, prob in predictions}\n",182 " elif \"Inception v3\" == model_type:\n",183 " predictions = inceptionV3_model.classify_image(image)\n",184 " return {class_name:str(prob) for _, class_name, prob in predictions}\n",185 " elif \"Mobile Net v2\" == model_type:\n",186 " predictions = mobilenet_model.classify_image(image)\n",187 " return {class_name:str(prob) for _, class_name, prob in predictions}\n",188 " else:\n",189 " return {\"Select a model to classify image\"}\n",190 "\n",191 "def train_model(epochs, batch_size, validation_split):\n",192 "\n",193 " print(\"Training model\")\n",194 "\n",195 " # Create an instance of the ImageClassifier\n",196 " classifier = ImageClassifier()\n",197 "\n",198 " # Load the dataset\n",199 " (x_train, y_train), (x_test, y_test) = classifier.load_dataset()\n",200 "\n",201 " # Build and train the model\n",202 " classifier.build_model(x_train)\n",203 " classifier.train_model(x_train, y_train, batch_size=int(batch_size), epochs=int(epochs), validation_split=float(validation_split))\n",204 "\n",205 " # Evaluate the model\n",206 " classifier.evaluate_model(x_test, y_test)\n",207 "\n",208 " # Save the trained model\n",209 " print(\"Saving model ...\")\n",210 " classifier.save_model(\"image_classifier_model.h5\")\n",211 "\n",212 " custom_model = classifier\n",213 "\n",214 "\n",215 "def update_train_param_display(model_type):\n",216 " if \"CNN (2 layer) - Custom\" == model_type:\n",217 " return [gr.update(visible=True), gr.update(visible=False)]\n",218 " return [gr.update(visible=False), gr.update(visible=True)]\n",219 "\n",220 "if __name__ == \"__main__\":\n",221 " # gradio gui app\n",222 " with gr.Blocks() as my_app:\n",223 " gr.Markdown(\"<h1><center>Image Classification using TensorFlow</center></h1>\")\n",224 " gr.Markdown(\"<h3><center>This model classifies image using different models.</center></h3>\")\n",225 "\n",226 " with gr.Row():\n",227 " with gr.Column(scale=1):\n",228 " img_input = gr.Image()\n",229 " model_type = gr.Dropdown(\n",230 " [\"CNN (2 layer) - Custom\",\n",231 " \"ResNet50\",\n",232 " \"VGG16\",\n",233 " \"Inception v3\",\n",234 " \"Mobile Net v2\"],\n",235 " label=\"Model Type\", value=\"CNN (2 layer) - Custom\",\n",236 " info=\"Select the inference model before running predictions!\")\n",237 "\n",238 " with gr.Column() as train_col:\n",239 " gr.Markdown(\"Train Parameters\")\n",240 " with gr.Row():\n",241 " epochs_inp = gr.Textbox(label=\"Epochs\", value=\"10\")\n",242 " validation_split = gr.Textbox(label=\"Validation Split\", value=\"0.1\")\n",243 "\n",244 " with gr.Row():\n",245 " batch_size = gr.Textbox(label=\"Batch Size\", value=\"64\")\n",246 "\n",247 " with gr.Row():\n",248 " train_btn = gr.Button(value=\"Train\")\n",249 " predict_btn_1 = gr.Button(value=\"Predict\")\n",250 "\n",251 " with gr.Column(visible=False) as no_train_col:\n",252 " predict_btn_2 = gr.Button(value=\"Predict\")\n",253 "\n",254 " with gr.Column(scale=1):\n",255 " output_label = gr.Label()\n",256 "\n",257 " # app logic\n",258 " predict_btn_1.click(make_prediction, inputs=[img_input, model_type], outputs=[output_label])\n",259 " predict_btn_2.click(make_prediction, inputs=[img_input, model_type], outputs=[output_label])\n",260 " model_type.change(update_train_param_display, inputs=model_type, outputs=[train_col, no_train_col])\n",261 " train_btn.click(train_model, inputs=[epochs_inp, batch_size, validation_split], outputs=[])\n",262 "\n",263 "my_app.queue(concurrency_count=5, max_size=20).launch(debug=True)"264 ],265 "metadata": {266 "colab": {267 "base_uri": "https://localhost:8080/",268 "height": 936269 },270 "id": "1N6d3Y0oEozx",271 "outputId": "07cc9273-30a8-4186-f0bf-e14a5aa45216"272 },273 "execution_count": 14,274 "outputs": [275 {276 "output_type": "stream",277 "name": "stdout",278 "text": [279 "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels.h5\n",280 "102967424/102967424 [==============================] - 1s 0us/step\n",281 "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5\n",282 "553467096/553467096 [==============================] - 9s 0us/step\n",283 "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels.h5\n",284 "96112376/96112376 [==============================] - 1s 0us/step\n",285 "Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5\n",286 "14536120/14536120 [==============================] - 0s 0us/step\n",287 "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",288 "\n",289 "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",290 "Running on public URL: https://bc9c4277de0c1cb0c9.gradio.live\n",291 "\n",292 "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"293 ]294 },295 {296 "output_type": "display_data",297 "data": {298 "text/plain": [299 "<IPython.core.display.HTML object>"300 ],301 "text/html": [302 "<div><iframe src=\"https://bc9c4277de0c1cb0c9.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"303 ]304 },305 "metadata": {}306 },307 {308 "output_type": "stream",309 "name": "stdout",310 "text": [311 "1/1 [==============================] - 0s 178ms/step\n",312 "1/1 [==============================] - 1s 1s/step\n",313 "Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json\n",314 "35363/35363 [==============================] - 0s 0us/step\n",315 "1/1 [==============================] - 1s 755ms/step\n",316 "1/1 [==============================] - 2s 2s/step\n",317 "Keyboard interruption in main thread... closing server.\n",318 "Killing tunnel 127.0.0.1:7860 <> https://bc9c4277de0c1cb0c9.gradio.live\n"319 ]320 },321 {322 "output_type": "execute_result",323 "data": {324 "text/plain": []325 },326 "metadata": {},327 "execution_count": 14328 }329 ]330 },331 {332 "cell_type": "code",333 "source": [],334 "metadata": {335 "id": "6p0TTCYYH2XA"336 },337 "execution_count": null,338 "outputs": []339 }340 ]341}