Spaces:
Sleeping
Sleeping
Commit
•
208c581
1
Parent(s):
e3f3c63
Update app.py (#1)
Browse files- Update app.py (0171e240f90330af7f0c3d9549a2f7b0db4b59f1)
app.py
CHANGED
@@ -6,139 +6,13 @@ Automatically generated by Colaboratory.
|
|
6 |
Original file is located at
|
7 |
https://colab.research.google.com/drive/1L-z1dtcO8Co-TSZyGVYHNUAX3wvUSswD
|
8 |
"""
|
9 |
-
|
10 |
-
#hide
|
11 |
-
|
12 |
-
'''
|
13 |
-
!pip install fastbook
|
14 |
-
!pip install gradio
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
import fastbook
|
19 |
-
fastbook.setup_book()
|
20 |
-
'''
|
21 |
-
|
22 |
#hide
|
23 |
from fastbook import *
|
24 |
from fastai.vision.widgets import *
|
25 |
from fastai.vision.utils import *
|
26 |
|
27 |
-
ims = search_images_ddg('satellite imagery')
|
28 |
-
len(ims)
|
29 |
-
|
30 |
-
#Image.open(dest)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
def get_failed_images(path):
|
35 |
-
fns = get_image_files(path)
|
36 |
-
failed = verify_images(fns)
|
37 |
-
failed.map(Path.unlink)
|
38 |
-
|
39 |
-
def download_single_type_image(image_of):
|
40 |
-
if not isinstance(image_of, str):
|
41 |
-
return "Please input a string as a file path"
|
42 |
-
for count in range(0, len(ims) - 1):
|
43 |
-
dest = 'images/' + image_of + str(count) + '.jpg'
|
44 |
-
try:
|
45 |
-
download_url(ims[count], dest)
|
46 |
-
#print('this is the file name' + str(dest))
|
47 |
-
except:
|
48 |
-
print("The file at count " + str(count))
|
49 |
-
|
50 |
-
def create_data_block():
|
51 |
-
data = DataBlock(
|
52 |
-
blocks=(ImageBlock, CategoryBlock),
|
53 |
-
get_items=get_image_files,
|
54 |
-
splitter=RandomSplitter(valid_pct=0.2, seed=42),
|
55 |
-
get_y=parent_label,
|
56 |
-
item_tfms=Resize(128)
|
57 |
-
)
|
58 |
-
return data
|
59 |
-
'''
|
60 |
-
def show_batch(path, data, n_items=9, n_rows=3):
|
61 |
-
dls = data.dataloaders('/content/' + path)
|
62 |
-
dls.valid.show_batch(max_n=n_items, nrows=n_rows)
|
63 |
-
'''
|
64 |
-
|
65 |
-
def train_model(path, data, num_epochs):
|
66 |
-
data = data.new(
|
67 |
-
item_tfms=RandomResizedCrop(224, min_scale=0.5),
|
68 |
-
batch_tfms=aug_transforms())
|
69 |
-
dls = data.dataloaders(path)
|
70 |
-
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
71 |
-
#the cbs=ShowGraphCallback should graph the loss
|
72 |
-
learn.fine_tune(num_epochs, cbs=ShowGraphCallback())
|
73 |
-
return learn
|
74 |
-
|
75 |
-
def download_multi_type_image(file_path, categories):
|
76 |
-
if not isinstance(file_path, str):
|
77 |
-
return "Please input a string as a file path"
|
78 |
-
if not isinstance(categories, list):
|
79 |
-
return "Please input a list of strings a categories"
|
80 |
-
|
81 |
-
#urban_types = 'city','town','village'
|
82 |
-
path = Path(file_path)
|
83 |
-
|
84 |
-
if not path.exists():
|
85 |
-
path.mkdir()
|
86 |
-
"""
|
87 |
-
for o in categories:
|
88 |
-
dest = (path/o)
|
89 |
-
dest.mkdir(exist_ok=True)
|
90 |
-
results = search_images_ddg(f'{o}')
|
91 |
-
download_images(dest, urls=results)
|
92 |
-
"""
|
93 |
-
for o in categories:
|
94 |
-
dest = (path/o)
|
95 |
-
print(dest)
|
96 |
-
dest.mkdir(exist_ok=True)
|
97 |
-
results = search_images_ddg(f'{o} ')
|
98 |
-
print(results)
|
99 |
-
download_images(dest, urls=results)
|
100 |
-
print('an image is downlaoding')
|
101 |
-
|
102 |
-
def show_confusion_matrix(learn):
|
103 |
-
interp = ClassificationInterpretation.from_learner(learn)
|
104 |
-
interp.plot_confusion_matrix()
|
105 |
-
|
106 |
-
interp.plot_top_losses(5, nrows=3)
|
107 |
-
|
108 |
-
def clean_data(learn):
|
109 |
-
cleaner = ImageClassifierCleaner(learn)
|
110 |
-
return cleaner
|
111 |
-
|
112 |
-
directory_name = 'satellite imagery'
|
113 |
-
classes = ['satellite imagery tokyo', 'satellite imagery New York City', 'satellite imagery baltimore']
|
114 |
-
|
115 |
-
#dest = 'images/city.jpg'
|
116 |
-
#download_url(ims[0], dest)
|
117 |
-
|
118 |
-
#im = Image.open(dest)
|
119 |
-
#im.to_thumb(1000,1000)
|
120 |
-
|
121 |
-
#step 1 download the data the I want to build a classifier for
|
122 |
-
# the first parameter is the name of the directory that the data will be stored in
|
123 |
-
# the second paramteter is a list of all the of the classes the the image classifier will be trained on
|
124 |
-
download_multi_type_image(directory_name,classes)
|
125 |
-
|
126 |
-
data = create_data_block()
|
127 |
-
|
128 |
-
get_image_files(directory_name)
|
129 |
-
|
130 |
-
a = get_failed_images(directory_name)
|
131 |
-
|
132 |
-
#show_batch(directory_name, data)
|
133 |
-
|
134 |
-
learn = train_model(directory_name, data, 10)
|
135 |
-
|
136 |
-
learn
|
137 |
-
|
138 |
-
show_confusion_matrix(learn)
|
139 |
-
|
140 |
-
get_failed_images(directory_name)
|
141 |
|
|
|
142 |
labels = learn.dls.vocab
|
143 |
def predict(img):
|
144 |
img = PILImage.create(img)
|
@@ -148,42 +22,3 @@ def predict(img):
|
|
148 |
import gradio as gr
|
149 |
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=False)
|
150 |
|
151 |
-
#dls = data.dataloaders('/content/satellite imagery')
|
152 |
-
#dls
|
153 |
-
#dls.valid.show_batch(max_n=10, nrows=4)
|
154 |
-
|
155 |
-
"""
|
156 |
-
data = adatart.new(item_tfms=Resize(128, ResizeMethod.Squish))
|
157 |
-
dls = data.dataloaders(path)
|
158 |
-
dls.valid.show_batch(max_n=10, nrows=2)
|
159 |
-
|
160 |
-
dls.valid.show_batch(max_n=16, nrows=4)
|
161 |
-
|
162 |
-
art = data.new(item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros'))
|
163 |
-
dls = art.dataloaders(path)
|
164 |
-
dls.valid.show_batch(max_n=4, nrows=1)
|
165 |
-
|
166 |
-
art = art.new(item_tfms=RandomResizedCrop(128, min_scale=0.3))
|
167 |
-
dls = art.dataloaders(path)
|
168 |
-
dls.train.show_batch(max_n=16, nrows=4, unique=True)
|
169 |
-
|
170 |
-
art = art.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))
|
171 |
-
dls = art.dataloaders(path)
|
172 |
-
dls.train.show_batch(max_n=8, nrows=2, unique=True)
|
173 |
-
|
174 |
-
art = art.new(
|
175 |
-
item_tfms=RandomResizedCrop(224, min_scale=0.5),
|
176 |
-
batch_tfms=aug_transforms())
|
177 |
-
dls = art.dataloaders(path)
|
178 |
-
|
179 |
-
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
180 |
-
learn.fine_tune(4)
|
181 |
-
|
182 |
-
interp = ClassificationInterpretation.from_learner(learn)
|
183 |
-
interp.plot_confusion_matrix()
|
184 |
-
|
185 |
-
interp.plot_top_losses(5, nrows=3)
|
186 |
-
|
187 |
-
cleaner = ImageClassifierCleaner(learn)
|
188 |
-
cleaner
|
189 |
-
"""
|
|
|
6 |
Original file is located at
|
7 |
https://colab.research.google.com/drive/1L-z1dtcO8Co-TSZyGVYHNUAX3wvUSswD
|
8 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
#hide
|
10 |
from fastbook import *
|
11 |
from fastai.vision.widgets import *
|
12 |
from fastai.vision.utils import *
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
learn = load_learner('model.pkl')
|
16 |
labels = learn.dls.vocab
|
17 |
def predict(img):
|
18 |
img = PILImage.create(img)
|
|
|
22 |
import gradio as gr
|
23 |
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=False)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|