{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OdOgOEqcDzhY", "outputId": "a1787cb0-c94a-4145-ef35-bb222f63a373" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n", "/content/drive/My Drive/My Projects/Image_Classifier_TensorFlow\n" ] } ], "source": [ "# This mounts your Google Drive to the Colab VM.\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "\n", "%cd /content/drive/My\\ Drive/My\\ Projects/Image_Classifier_TensorFlow" ] }, { "cell_type": "code", "source": [ "pwd" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 36 }, "id": "EuUA1qNaEdGB", "outputId": "b9b3ca06-157a-4686-92ab-72c080dddcfb" }, "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'/content/drive/My Drive/My Projects/Image_Classifier_TensorFlow'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 10 } ] }, { "cell_type": "markdown", "source": [ "# Gradio App" ], "metadata": { "id": "6XXQqgGmErXJ" } }, { "cell_type": "code", "source": [ "# installations\n", "!pip install gradio" ], "metadata": { "id": "wSuhvzbEE8Ql" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Training" ], "metadata": { "id": "71zplmVlFU9J" } }, { "cell_type": "code", "source": [ "print(\"Training model...\")\n", "# Create an instance of the ImageClassifier\n", "classifier = ImageClassifier()\n", "\n", "# Load the dataset\n", "(x_train, y_train), (x_test, y_test) = classifier.load_dataset()\n", "\n", "# Build and train the model\n", "classifier.build_model(x_train)\n", "classifier.train_model(x_train, y_train, batch_size=64, epochs=1, validation_split=0.1)\n", "\n", "# Evaluate the model\n", "classifier.evaluate_model(x_test, y_test)\n", "\n", "# Save the trained model\n", "print(\"Saving model ...\")\n", "classifier.save_model(\"image_classifier_model.h5\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Q9vKOsnKFRu4", "outputId": "93268865-5288-44a3-bc09-6d30620655f8" }, "execution_count": 13, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Training model...\n", "704/704 [==============================] - 187s 263ms/step - loss: 1.5925 - accuracy: 0.4633 - val_loss: 1.3171 - val_accuracy: 0.5372\n", "Test loss: 1.3429059982299805\n", "Test accuracy: 0.5228999853134155\n", "Saving model ...\n" ] } ] }, { "cell_type": "code", "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "from tensorflow import keras\n", "from custom_model import ImageClassifier\n", "from resnet_model import ResNetClassifier\n", "from vgg16_model import VGG16Classifier\n", "from inception_v3_model import InceptionV3Classifier\n", "from mobilevet_v2 import MobileNetClassifier\n", "\n", "CLASS_NAMES =['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']\n", "\n", "# models\n", "custom_model = ImageClassifier()\n", "custom_model.load_model(\"image_classifier_model.h5\")\n", "resnet_model = ResNetClassifier()\n", "vgg16_model = VGG16Classifier()\n", "inceptionV3_model = InceptionV3Classifier()\n", "mobilenet_model = MobileNetClassifier()\n", "\n", "def make_prediction(image, model_type):\n", " if \"CNN (2 layer) - Custom\" == model_type:\n", " top_classes, top_probs = custom_model.classify_image(image, top_k=3)\n", " return {CLASS_NAMES[cls_id]:str(prob) for cls_id, prob in zip(top_classes, top_probs)}\n", " elif \"ResNet50\" == model_type:\n", " predictions = resnet_model.classify_image(image)\n", " return {class_name:str(prob) for _, class_name, prob in predictions}\n", " elif \"VGG16\" == model_type:\n", " predictions = vgg16_model.classify_image(image)\n", " return {class_name:str(prob) for _, class_name, prob in predictions}\n", " elif \"Inception v3\" == model_type:\n", " predictions = inceptionV3_model.classify_image(image)\n", " return {class_name:str(prob) for _, class_name, prob in predictions}\n", " elif \"Mobile Net v2\" == model_type:\n", " predictions = mobilenet_model.classify_image(image)\n", " return {class_name:str(prob) for _, class_name, prob in predictions}\n", " else:\n", " return {\"Select a model to classify image\"}\n", "\n", "def train_model(epochs, batch_size, validation_split):\n", "\n", " print(\"Training model\")\n", "\n", " # Create an instance of the ImageClassifier\n", " classifier = ImageClassifier()\n", "\n", " # Load the dataset\n", " (x_train, y_train), (x_test, y_test) = classifier.load_dataset()\n", "\n", " # Build and train the model\n", " classifier.build_model(x_train)\n", " classifier.train_model(x_train, y_train, batch_size=int(batch_size), epochs=int(epochs), validation_split=float(validation_split))\n", "\n", " # Evaluate the model\n", " classifier.evaluate_model(x_test, y_test)\n", "\n", " # Save the trained model\n", " print(\"Saving model ...\")\n", " classifier.save_model(\"image_classifier_model.h5\")\n", "\n", " custom_model = classifier\n", "\n", "\n", "def update_train_param_display(model_type):\n", " if \"CNN (2 layer) - Custom\" == model_type:\n", " return [gr.update(visible=True), gr.update(visible=False)]\n", " return [gr.update(visible=False), gr.update(visible=True)]\n", "\n", "if __name__ == \"__main__\":\n", " # gradio gui app\n", " with gr.Blocks() as my_app:\n", " gr.Markdown(\"