ArunkumarCH commited on
Commit
c8157a2
·
1 Parent(s): f6f5b2e

Create README.md

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