Spaces:
Runtime error
Runtime error
Umang Kaushik
commited on
Commit
·
c2b3f16
1
Parent(s):
fe1684a
first and final commit
Browse files- classifier.ipynb +128 -0
- classifier.py +47 -0
- dog_breed_model.ipynb +0 -0
- export.pkl +3 -0
- requirements.txt +6 -0
classifier.ipynb
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "e0bddbd4",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"! pip install -Uqq fastbook\n",
|
11 |
+
"import fastbook"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "code",
|
16 |
+
"execution_count": 2,
|
17 |
+
"id": "1b46b67e",
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"from fastai.vision.all import *\n",
|
22 |
+
"from fastai.vision.widgets import *"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 3,
|
28 |
+
"id": "4a089247",
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"import pathlib\n",
|
33 |
+
"temp = pathlib.PosixPath\n",
|
34 |
+
"pathlib.PosixPath = pathlib.WindowsPath"
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "code",
|
39 |
+
"execution_count": 4,
|
40 |
+
"id": "1f07c129",
|
41 |
+
"metadata": {},
|
42 |
+
"outputs": [],
|
43 |
+
"source": [
|
44 |
+
"path = Path()\n",
|
45 |
+
"learn_inf = load_learner(path/'export.pkl', cpu=True)\n",
|
46 |
+
"btn_upload = widgets.FileUpload()\n",
|
47 |
+
"out_pl = widgets.Output()\n",
|
48 |
+
"lbl_pred = widgets.Label()\n",
|
49 |
+
"btn_run = widgets.Button(description='Classify')"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": 5,
|
55 |
+
"id": "b319ee30",
|
56 |
+
"metadata": {},
|
57 |
+
"outputs": [],
|
58 |
+
"source": [
|
59 |
+
"def on_click_classify(change):\n",
|
60 |
+
" img = PILImage.create(btn_upload.data[-1])\n",
|
61 |
+
" out_pl.clear_output()\n",
|
62 |
+
" with out_pl: display(img.to_thumb(128,128))\n",
|
63 |
+
" pred, pred_idx, probs = learn_inf.predict(img)\n",
|
64 |
+
" lbl_pred.value = f'Prediction: {str(pred)[10:]}; Probability: {probs[pred_idx]:.04f}'\n",
|
65 |
+
" \n",
|
66 |
+
"btn_run.on_click(on_click_classify)"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "code",
|
71 |
+
"execution_count": 6,
|
72 |
+
"id": "114b5a71",
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [
|
75 |
+
{
|
76 |
+
"data": {
|
77 |
+
"application/vnd.jupyter.widget-view+json": {
|
78 |
+
"model_id": "476e154bfede489a9560404bca8fff16",
|
79 |
+
"version_major": 2,
|
80 |
+
"version_minor": 0
|
81 |
+
},
|
82 |
+
"text/plain": [
|
83 |
+
"VBox(children=(Label(value='Select your images'), FileUpload(value={}, description='Upload'), Button(descripti…"
|
84 |
+
]
|
85 |
+
},
|
86 |
+
"metadata": {},
|
87 |
+
"output_type": "display_data"
|
88 |
+
}
|
89 |
+
],
|
90 |
+
"source": [
|
91 |
+
"VBox([widgets.Label('Select your images'),\n",
|
92 |
+
" btn_upload, btn_run, out_pl, lbl_pred])"
|
93 |
+
]
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"execution_count": null,
|
98 |
+
"id": "d640937e",
|
99 |
+
"metadata": {},
|
100 |
+
"outputs": [],
|
101 |
+
"source": []
|
102 |
+
}
|
103 |
+
],
|
104 |
+
"metadata": {
|
105 |
+
"interpreter": {
|
106 |
+
"hash": "d925690840644bcd766647b280b19c370132b1426b7601e4e2f280a77c18a034"
|
107 |
+
},
|
108 |
+
"kernelspec": {
|
109 |
+
"display_name": "Python 3 (ipykernel)",
|
110 |
+
"language": "python",
|
111 |
+
"name": "python3"
|
112 |
+
},
|
113 |
+
"language_info": {
|
114 |
+
"codemirror_mode": {
|
115 |
+
"name": "ipython",
|
116 |
+
"version": 3
|
117 |
+
},
|
118 |
+
"file_extension": ".py",
|
119 |
+
"mimetype": "text/x-python",
|
120 |
+
"name": "python",
|
121 |
+
"nbconvert_exporter": "python",
|
122 |
+
"pygments_lexer": "ipython3",
|
123 |
+
"version": "3.9.7"
|
124 |
+
}
|
125 |
+
},
|
126 |
+
"nbformat": 4,
|
127 |
+
"nbformat_minor": 5
|
128 |
+
}
|
classifier.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from fastai.vision.all import *
|
3 |
+
from PIL import Image
|
4 |
+
import pathlib
|
5 |
+
import urllib.request
|
6 |
+
|
7 |
+
temp = pathlib.PosixPath
|
8 |
+
plt = platform.system()
|
9 |
+
if plt == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
|
10 |
+
|
11 |
+
# MODEL_URL = "https://drive.google.com/uc?export=download&id=1cH5nY1T5oykEcLyWtjA8Wv-Xn8vO20BS"
|
12 |
+
# urllib.request.urlretrieve(MODEL_URL, "model.pkl")
|
13 |
+
|
14 |
+
path = Path()
|
15 |
+
path.ls(file_exts='.pkl')
|
16 |
+
|
17 |
+
learn_inf = load_learner(path/'export.pkl', cpu=True)
|
18 |
+
# out_pl = st.image(load_image(image), width=250)
|
19 |
+
|
20 |
+
|
21 |
+
def load_image(img_file):
|
22 |
+
img = PILImage.create(img_file)
|
23 |
+
return img
|
24 |
+
|
25 |
+
def on_click_classify(image):
|
26 |
+
# load_image(image)
|
27 |
+
out_pl = st.image(load_image(image), width=250)
|
28 |
+
pred, pred_idx, probs = learn_inf.predict(load_image(image))
|
29 |
+
st.write('Prediction: ', str(pred)[10:], '; Probability: ', float(probs[pred_idx]))
|
30 |
+
|
31 |
+
|
32 |
+
st.title('Dog Classifier')
|
33 |
+
st.header('Choose your Dog!!')
|
34 |
+
image = st.file_uploader(label=' ', type=['png', 'jpg'], key='img', help='upload an img of dog')
|
35 |
+
|
36 |
+
# picture = st.camera_input(label='Click your dog!')
|
37 |
+
|
38 |
+
|
39 |
+
# btn_run.on_change(on_click_classify)
|
40 |
+
btn_run = st.button(label='Classify')
|
41 |
+
if btn_run:
|
42 |
+
on_click_classify(image)
|
43 |
+
|
44 |
+
|
45 |
+
st.markdown('#### Created by **Umang Kaushik**')
|
46 |
+
st.markdown('##### **[Github](https://github.com/Umang-10)**')
|
47 |
+
|
dog_breed_model.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
export.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:edf8e8bca20c151ae9b5787c8d1b6c3f3119d7a1c5ef5e1a6a7995c98d55ac60
|
3 |
+
size 88116257
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
-f https://download.pytorch.org/whl/torch_stable.html
|
2 |
+
torch==1.11.0+cpu
|
3 |
+
torchvision==0.12.0+cpu
|
4 |
+
fastai>=2.3.1
|
5 |
+
streamlit
|
6 |
+
pathlib
|