{ "cells": [ { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import tensorflow_datasets as tfds\n", "import tensorflow as tf\n", "import tensorflow_hub as hub\n", "import sklearn\n", "import random\n", "from glob import glob\n", "import matplotlib.pyplot as plt\n", "import requests" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TF version: 2.9.2\n", "Hub version: 0.12.0\n", "GPU is available\n" ] } ], "source": [ "print(\"TF version:\", tf.__version__)\n", "print(\"Hub version:\", hub.__version__)\n", "print(\"GPU is\", \"available\" if tf.config.list_physical_devices('GPU') else \"NOT AVAILABLE\")" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading data from https://storage.googleapis.com/keras-applications/efficientnetb7.h5\n", "268326632/268326632 [==============================] - 13s 0us/step\n" ] } ], "source": [ "\n", "inception_net = tf.keras.applications.EfficientNetB7()\n" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "response = requests.get(\"https://git.io/JJkYN\")\n", "labels = response.text.split(\"\\n\")\n", "\n", "def classify_image(inp):\n", " inp = inp.reshape((-1, 600, 600, 3))\n", " inp = tf.keras.applications.efficientnet_v2.preprocess_input(inp)\n", " prediction = inception_net.predict(inp).flatten()\n", " confidences = {labels[i]: float(prediction[i]) for i in range(1000)}\n", " return confidences\n" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "\n", "gr.Interface(fn=classify_image, \n", " inputs=gr.Image(shape=(600, 600)),\n", " outputs=gr.Label(num_top_classes=3),\n", " examples=[\"data/animals/animals/antelope/0a37838e99.jpg\", \"data/animals/animals/starfish/0a63e965c2.jpg\"]).launch(share=True)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.13 ('work')", "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.8.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "59f0528c0641d303038c15eb2f7ee076b3157354b9138799665619ae8b3de89f" } } }, "nbformat": 4, "nbformat_minor": 2 }