#!/usr/bin/env python # coding: utf-8 # #### Gradio Comparing Transfer Learning Models import gradio as gr import tensorflow as tf import numpy as np from PIL import Image import requests # Download human-readable labels for ImageNet. response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") mobile_net = tf.keras.applications.MobileNetV2() inception_net = tf.keras.applications.InceptionV3() # In[2]: def classify_image_with_mobile_net(im): im = Image.fromarray(im.astype('uint8'), 'RGB') im = im.resize((224, 224)) arr = np.array(im).reshape((-1, 224, 224, 3)) arr = tf.keras.applications.mobilenet.preprocess_input(arr) prediction = mobile_net.predict(arr).flatten() return {labels[i]: float(prediction[i]) for i in range(1000)} # In[3]: def classify_image_with_inception_net(im): # Resize the image to im = Image.fromarray(im.astype('uint8'), 'RGB') im = im.resize((299, 299)) arr = np.array(im).reshape((-1, 299, 299, 3)) arr = tf.keras.applications.inception_v3.preprocess_input(arr) prediction = inception_net.predict(arr).flatten() return {labels[i]: float(prediction[i]) for i in range(1000)} # In[4]: imagein = gr.inputs.Image() label = gr.outputs.Label(num_top_classes=3) sample_images = [ ] # In[6]: gr.Interface( fn = classify_image_with_mobile_net, inputs=imagein, outputs=label, title="MobileNet",examples=sample_images).launch()