Pretrained Models in an app

Pretrained Models in an app#

This problem is motivated by the Silicon Valley sketch. Your goal is to build a similar app with a fine tuned model from torchvision deployed through a streamlit.

from IPython.display import YouTubeVideo
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
YouTubeVideo(id = 'tWwCK95X6go')

Today we will use a dataset to determine whether or not an image is a hotdog. Dataset link: link

BEGINNING: To get started, create a basic streamlit app that accepts an image from the user and displays it on the screen.

ls
drive/  sample_data/
test_path = '/content/drive/MyDrive/hotdogs/data/hotdog-nothotdog/test/hotdog/1501.jpg'
import matplotlib.pyplot as plt
plt.imread(test_path)
ndarray (299, 299, 3) 
array([[[ 60,  61,  45],
        [ 47,  47,  37],
        [ 30,  26,  27],
        ...,
        [  4,  16,  12],
        [  0,  12,   8],
        [  1,  13,  11]],

       [[ 84,  87,  76],
        [ 67,  68,  60],
        [ 27,  26,  24],
        ...,
        [  1,  11,  10],
        [  1,  13,  11],
        [  5,  15,  16]],

       [[ 94, 105, 101],
        [ 68,  75,  68],
        [ 33,  35,  24],
        ...,
        [  1,   9,  11],
        [  3,  12,  17],
        [  6,  13,  21]],

       ...,

       [[140, 139, 118],
        [134, 134, 108],
        [139, 143, 108],
        ...,
        [122, 119,  68],
        [122, 118,  71],
        [109, 104,  74]],

       [[131, 133, 112],
        [129, 131, 107],
        [128, 134,  98],
        ...,
        [123, 117,  69],
        [119, 111,  72],
        [107,  96,  76]],

       [[126, 125, 107],
        [129, 131, 107],
        [120, 126,  88],
        ...,
        [121, 113,  66],
        [112, 100,  62],
        [102,  87,  68]]], dtype=uint8)

Pretrained Models#

An example of a pretrained model is that of the ResNet through torchvision.models. Below is an outline of a streamlit app to deploy the model. You should fine tune this model using the hot dog data and deploy a model that allows a user to take a picure with their camera or upload an image and returns a voice stating whether the image is a hot dog or not a hot dog. How would you improve Jinyang’s app to make the rest of the team happy? (HINT: this)

Example: ResNet

import streamlit as st
import numpy as np
from torchvision.models import
import torch



st.header('Hot Dogs!')

uploaded_file = st.file_uploader("Pick a picture")
model = ''

if uploaded_file is not None:
    img = #load the image
    #prepare the image
    #pass through the model
    #make prediction
    #speak!