Skip to content
Snippets Groups Projects
04-Keras-cv.ipynb 4.58 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width=\"800px\" src=\"../fidle/img/header.svg\"></img>\n",
    "\n",
    "# <!-- TITLE --> [K3GTSRB4] - Hight level example (Keras-cv)\n",
    "<!-- DESC --> An example of using a pre-trained model with Keras-cv\n",
    "<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
    "\n",
    "## Objectives :\n",
    "  - Using a pre-trained model\n",
    "  \n",
    "## What we're going to do :\n",
    "\n",
    " - Load and use a pre-trained model\n",
    "\n",
    " See : https://keras.io/guides/keras_cv/classification_with_keras_cv/  \n",
    " Imagenet classes can be found at : https://gist.githubusercontent.com/LukeWood/62eebcd5c5c4a4d0e0b7845780f76d55/raw/fde63e5e4c09e2fa0a3436680f436bdcb8325aac/ImagenetClassnames.json\n",
    "\n",
    "## Step 1 - Import and init\n",
    "\n",
    "**ATTENTION :** A specific environment is required for this example (Which may require 6 GB).  \n",
    "This python environment required for this notebook is :\n",
    "```\n",
    "python3 -m venv fidle-kcv\n",
    "pip install --upgrade keras-cv tensorflow torch torchvision torchaudio Matplotlib Jupyterlab\n",
    "pip install --upgrade keras jupyterlab\n",
    "```\n",
    "Note: Tensorflow is not used for interference, and will no longer be required in later versions of Keras 3.\n",
    "\n",
    "### 1.1 - Python stuffs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"KERAS_BACKEND\"] = \"torch\"  # @param [\"tensorflow\", \"jax\", \"torch\"]\n",
    "\n",
    "import json\n",
    "import numpy as np\n",
    "\n",
    "import keras\n",
    "import keras_cv\n",
    "\n",
    "from  modules.ImagenetClassnames import ImagenetClassnames"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2 - Get some images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "images_url=['https://i.imgur.com/2eOWImx.jpeg', 'https://i.imgur.com/YB8sG8R.jpeg', 'https://i.imgur.com/orZEMlv.jpeg']\n",
    "\n",
    "images=[]\n",
    "for img_url in images_url:\n",
    "    \n",
    "    # Get images from urls in ~/.keras cache\n",
    "    img_path = keras.utils.get_file(origin=img_url)\n",
    "\n",
    "    # Get image\n",
    "    img = keras.utils.load_img(img_path, target_size=(256,256))\n",
    "    images.append(img)\n",
    "images=np.array(images)\n",
    "\n",
    "keras_cv.visualization.plot_image_gallery( images, rows=1, cols=3, value_range=(0, 255), show=True, scale=2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3 - Get a nice pretrained classifier (and classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "classifier = keras_cv.models.ImageClassifier.from_preset( \"efficientnetv2_b0_imagenet_classifier\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4 - Try some predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = classifier.predict(images)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5 - Show result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get classes name \n",
    "imc = ImagenetClassnames()\n",
    "\n",
    "for i,img in enumerate(images):\n",
    "    # Get classes id instead classes probabilities\n",
    "    classes_id   = predictions[i].argsort(axis=-1)\n",
    "    # Get classes name instead classes id\n",
    "    classes_name = imc.get(classes_id, top_n=2)\n",
    "    # Plot it\n",
    "    keras_cv.visualization.plot_image_gallery( np.array([img]), rows=1, cols=1, value_range=(0, 255), show=True, scale=2)\n",
    "    print(classes_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "<img width=\"80px\" src=\"../fidle/img/logo-paysage.svg\"></img>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fidle-kcv",
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}