gradio/guides/10_other-tutorials/image-classification-in-tensorflow.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
4.0 KiB
Markdown
Raw Normal View History

2022-03-01 03:43:51 +08:00
# Image Classification in TensorFlow and Keras
Live website changes (#1578) * fix audio output cache (#804) * fix audio output cache * changes * version update Co-authored-by: Ali Abid <aliabid94@gmail.com> * Website Tracker Slackbot (#797) * added commands to reload script * catch errors with git pull * read new webhook from os variable * correcting bash * bash fixes * formatting * more robust error checking * only sends success if git changes * catching error from script * escaping error text to send with curl * correct text escaping for error message * fix search bug in guides (#809) * Update getting_started.md (#808) * Fix type of server returned by `Launchable` (#810) * `Launchable` returns a FastAPI now * Update .gitignore * Add a missing line to getting started (#816) Former-commit-id: 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 96f203108bf1222fe333a0175687293abdc669d7] Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f * Add a missing line to getting started (#816) Former-commit-id: 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 96f203108bf1222fe333a0175687293abdc669d7]] Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f Former-commit-id: b5112c3f425c0ea961461854efae9c28a73aea01 * Add a missing line to getting started (#816) Former-commit-id: 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 96f203108bf1222fe333a0175687293abdc669d7]]] Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f Former-commit-id: b5112c3f425c0ea961461854efae9c28a73aea01 Former-commit-id: bce6f9c4c5254301eb73e76eb47cddab3e132c24 * Add a missing line to getting started (#816) Former-commit-id: 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 81e271ca22e838e1ee618d48cdb0e904fd233cf3 [formerly 96f203108bf1222fe333a0175687293abdc669d7]]]] Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f Former-commit-id: b5112c3f425c0ea961461854efae9c28a73aea01 Former-commit-id: bce6f9c4c5254301eb73e76eb47cddab3e132c24 Former-commit-id: feba0888e3d488b82a3518343f607517d0836f13 * Add a missing line to getting started (#816) * Clean-History - Remove 51MB file with this commit Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c * Clean-History - Remove 51MB file with this commit Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c Former-commit-id: dd700c33cca3f560621219530444b631b7767392 * Clean-History - Remove 51MB file with this commit Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c Former-commit-id: dd700c33cca3f560621219530444b631b7767392 Former-commit-id: 0d80e6a056abad1c4d1fd6f162eb725e0db5fb4f * Clean-History - Remove 51MB file with this commit Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c Former-commit-id: dd700c33cca3f560621219530444b631b7767392 Former-commit-id: 0d80e6a056abad1c4d1fd6f162eb725e0db5fb4f Former-commit-id: 20523b05194438209cf64cb688008b4599eb847e * changes * changes * Homepage: header image size (#1347) * image size * image in local assets * add dall-e mini banner * undo ui changes * changes * changes * updates * updates * changes * changes * changes * h11 dependency * add npm build-mac * expand demo button to all classes * add demos to docstrings * add anchor tags to headers * add required tag to param table * add consistent styling for headers * skip param beginning with underscore from docs * skip kwargs param from docs * remove types in param docstring * override signature to reflect usage * add supported events * add step-by-step guides * fix guide contribution link * add related spaces * fix img styling on guides * pin quickstart, advanced, and block guides to top * margin fix * autogenerated copy buttons for all codeblocks * changes * documentaiton * format * launch * formatting * style changes * remove backticks * changes * changes Co-authored-by: Ali Abid <aliabid94@gmail.com> Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> Co-authored-by: Julien Chaumond <julien@huggingface.co> Co-authored-by: Ömer Faruk Özdemir <farukozderim@gmail.com> Co-authored-by: Ali <ali.abid@huggingface.co> Co-authored-by: Victor Muštar <victor.mustar@gmail.com> Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2022-07-07 07:22:10 +08:00
Related spaces: https://huggingface.co/spaces/abidlabs/keras-image-classifier
Tags: VISION, MOBILENET, TENSORFLOW
2022-03-01 03:43:51 +08:00
## Introduction
Image classification is a central task in computer vision. Building better classifiers to classify what object is present in a picture is an active area of research, as it has applications stretching from traffic control systems to satellite imaging.
2022-03-01 03:43:51 +08:00
Such models are perfect to use with Gradio's _image_ input component, so in this tutorial we will build a web demo to classify images using Gradio. We will be able to build the whole web application in Python, and it will look like the demo on the bottom of the page.
2022-03-01 03:43:51 +08:00
Let's get started!
### Prerequisites
Make sure you have the `gradio` Python package already [installed](/getting_started). We will be using a pretrained Keras image classification model, so you should also have `tensorflow` installed.
## Step 1 — Setting up the Image Classification Model
First, we will need an image classification model. For this tutorial, we will use a pretrained Mobile Net model, as it is easily downloadable from [Keras](https://keras.io/api/applications/mobilenet/). You can use a different pretrained model or train your own.
2022-03-01 03:43:51 +08:00
```python
import tensorflow as tf
inception_net = tf.keras.applications.MobileNetV2()
```
This line automatically downloads the MobileNet model and weights using the Keras library.
2022-03-01 03:43:51 +08:00
## Step 2 — Defining a `predict` function
Next, we will need to define a function that takes in the _user input_, which in this case is an image, and returns the prediction. The prediction should be returned as a dictionary whose keys are class name and values are confidence probabilities. We will load the class names from this [text file](https://git.io/JJkYN).
2022-03-01 03:43:51 +08:00
In the case of our pretrained model, it will look like this:
```python
import requests
# Download human-readable labels for ImageNet.
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
def classify_image(inp):
inp = inp.reshape((-1, 224, 224, 3))
inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
prediction = inception_net.predict(inp).flatten()
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
return confidences
```
Let's break this down. The function takes one parameter:
2022-03-01 03:43:51 +08:00
- `inp`: the input image as a `numpy` array
2022-03-01 03:43:51 +08:00
Then, the function adds a batch dimension, passes it through the model, and returns:
- `confidences`: the predictions, as a dictionary whose keys are class labels and whose values are confidence probabilities
2022-03-01 03:43:51 +08:00
## Step 3 — Creating a Gradio Interface
Now that we have our predictive function set up, we can create a Gradio Interface around it.
2022-03-01 03:43:51 +08:00
In this case, the input component is a drag-and-drop image component. To create this input, we can use the `"gradio.inputs.Image"` class, which creates the component and handles the preprocessing to convert that to a numpy array. We will instantiate the class with a parameter that automatically preprocesses the input image to be 224 pixels by 224 pixels, which is the size that MobileNet expects.
The output component will be a `"label"`, which displays the top labels in a nice form. Since we don't want to show all 1,000 class labels, we will customize it to show only the top 3 images.
Finally, we'll add one more parameter, the `examples`, which allows us to prepopulate our interfaces with a few predefined examples. The code for Gradio looks like this:
```python
import gradio as gr
gr.Interface(fn=classify_image,
inputs=gr.Image(width=224, height=224),
outputs=gr.Label(num_top_classes=3),
2022-03-01 03:43:51 +08:00
examples=["banana.jpg", "car.jpg"]).launch()
```
This produces the following interface, which you can try right here in your browser (try uploading your own examples!):
<gradio-app space="gradio/keras-image-classifier">
2022-03-01 03:43:51 +08:00
---
2022-03-01 03:43:51 +08:00
And you're done! That's all the code you need to build a web demo for an image classifier. If you'd like to share with others, try setting `share=True` when you `launch()` the Interface!