File size: 3,861 Bytes
c8157a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
About this DeepLearning Model:
We will build an front end application to upload the image and get the deeplearning model predicts the name of the object with acccuracy.
Steps for building the Image classification model:
1. Image classification model using pretrained DL model
1.1 Define deeplearning model
2.2 Preprocess the data
3.3 Get prediction
1.1 Define deep learning model
# import required modules
import json
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# import pytorch related modules
import torch
from torchvision import transforms
from torchvision.models import densenet121
# define pretrained DL model
model = densenet121(pretrained=True)
model.eval();
1.2 Preprocess data
# load image using PIL
input_image = Image.open(filename)
# preprocess image according to the pretrained model
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
# create a mini-batch as expected by the model
input_batch = input_tensor.unsqueeze(0)
# pass input batch to the model
with torch.no_grad():
output = model(input_batch)
1.3 Get prediction
pred = torch.nn.functional.softmax(output[0], dim=0).cpu().numpy()
np.argmax(pred)
# download classes on which the model was trained on
!wget https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
# get the prediction accuracy
print(classes[str(np.argmax(pred))][1], round(max(pred)*100, 2))
2. Deploying Image Classification model
1.1 Install required libraries
1.2 Setup DL model using streamlit
1.3 Deploy DL model on AWS/Colab/HF spaces
1.1 Install required libraries
!pip install -q streamlit
!pip install -q pyngrok
1.2 Setup DL model using streamlit
%%writefile app.py
## create streamlit app
# import required libraries and modules
import json
import numpy as np
import matplotlib.pyplot as plt
import torch
from PIL import Image
from torchvision import transforms
from torchvision.models import densenet121
import streamlit as st
# define prediction function
def predict(image):
# load DL model
model = densenet121(pretrained=True)
model.eval()
# load classes
with open('imagenet_class_index.json', 'r') as f:
classes = json.load(f)
# preprocess image
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
# get prediction
with torch.no_grad():
output = model(input_batch)
pred = torch.nn.functional.softmax(output[0], dim=0).cpu().numpy()
# return confidence and label
confidence = round(max(pred)*100, 2)
label = classes[str(np.argmax(pred))][1]
return confidence, label
# define image file uploader
image = st.file_uploader("Upload image here")
# define button for getting prediction
if image is not None and st.button("Get prediction"):
# load image using PIL
input_image = Image.open(image)
# show image
st.image(input_image, use_column_width=True)
# get prediction
confidence, label = predict(input_image)
# print results
"Model is", confidence, "% confident that this image is of a", label
1.3 Deploy DL model
# run streamlit app
!streamlit run app.py &>/dev/null&
# make streamlit app available publicly
from pyngrok import ngrok
public_url = ngrok.connect('8501');
public_url
Model can be deployed on AWS/Colab/Flask/Hugging Spaces
Hugging spaces model
https://huggingface.co/spaces/ArunkumarCH/BirdClassification |