<jupyter_start><jupyter_text>Gradio Food Vision demo[Gradio](https://gradio.app/) helps demo your machine learning models with a few lines of code.Let's test it out using a small food recognition model. Similar to the one trained in notebook 03 of the [Zero to Mastery TensorFlow for Deep Learning course](https://dbourke.link/ZTMTFcourse).To start we'll install Gradio.<jupyter_code>!pip install gradio<jupyter_output>Collecting gradio
  Downloading gradio-2.2.6-py3-none-any.whl (2.1 MB)
[K     |████████████████████████████████| 2.1 MB 8.4 MB/s 
[?25hRequirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from gradio) (2.23.0)
Collecting Flask-Cors>=3.0.8
  Downloading Flask_Cors-3.0.10-py2.py3-none-any.whl (14 kB)
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from gradio) (1.4.1)
Requirement already satisfied: Flask>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from gradio) (1.1.4)
Requirement already satisfied: pillow in /usr/local/lib/python3.7/dist-packages (from gradio) (7.1.2)
Collecting paramiko
  Downloading paramiko-2.7.2-py2.py3-none-any.whl (206 kB)
[K     |████████████████████████████████| 206 kB 74.9 MB/s 
[?25hCollecting ffmpy
  Downloading ffmpy-0.3.0.tar.gz (4.8 kB)
Collecting analytics-python
  Downloading analytics_python-1.4.0-py2.py3-none-any.whl (15 kB)
Collecting Flask-Login
  Downloading Flask_Login-0.5.0-py2[...]<jupyter_text>Basic usage<jupyter_code>import gradio as gr

def greet(name):
  return "Hello " + name + "!!"

iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()<jupyter_output>Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`
This share link will expire in 24 hours. If you need a permanent link, visit: https://gradio.app/introducing-hosted (NEW!)
Running on External URL: https://45561.gradio.app
Interface loading below...<jupyter_text>Food Vision Demo<jupyter_code># Download data
!wget https://storage.googleapis.com/ztm_tf_course/food_vision/10_food_classes_10_percent.zip
!unzip -q 10_food_classes_10_percent.zip
train_dir = "10_food_classes_10_percent/train/"
test_dir = "10_food_classes_10_percent/test/"
# Setup data inputs
import tensorflow as tf
IMG_SIZE = (224, 224)
train_data = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
                                                                 label_mode="categorical",
                                                                 image_size=IMG_SIZE)

test_data = tf.keras.preprocessing.image_dataset_from_directory(test_dir,
                                                                label_mode="categorical",
                                                                image_size=IMG_SIZE)

# Get class names
class_names = train_data.class_names

class_names<jupyter_output>Found 750 files belonging to 10 classes.
Found 2500 files belonging to 10 classes.<jupyter_text>Train model<jupyter_code>import tensorflow as tf
from tensorflow.keras import layers

# Setup the input shape to our model
input_shape = (224, 224, 3)

# Create a frozen base model
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
base_model.trainable = False

# Create input and output layers
inputs = layers.Input(shape=input_shape, name="input_layer") # create input layer
x = base_model(inputs, training=False) # pass augmented images to base model but keep it in inference mode, so batchnorm layers don't get updated: https://keras.io/guides/transfer_learning/#build-a-model 
x = layers.GlobalAveragePooling2D(name="global_average_pooling_layer")(x)
outputs = layers.Dense(10, activation="softmax", name="output_layer")(x)
model = tf.keras.Model(inputs, outputs)

# Compile
model.compile(loss="categorical_crossentropy",
              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), # use Adam optimizer with base learning rate
              metrics=["accuracy"])

# Fit the model saving checkpoints every epoch
model.fit(train_data,
          epochs=10,
          validation_data=test_data,
          validation_steps=int(0.25 * len(test_data)))
# Create function to pass to Gradio interface
def food_vision(img):
  img = tf.image.resize(img, size=(224, 224))
  img = tf.expand_dims(img, axis=0) # make image tensor right size for model
  pred = model.predict(img)[0]
  output_dict = dict(zip(class_names, map(float, pred)))
  return output_dict
import gradio as gr

gr.Interface(fn=food_vision,
             inputs=gr.inputs.Image(),
             outputs=gr.outputs.Label(num_top_classes=5)).launch(debug=True) # debug=True = show errors in notebook<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup