Commit
·
b1a7756
1
Parent(s):
79ccd3b
Upload .py of the model
Browse files
190055201_matheucvp_matheus_calixto_vaz_pinheiro.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""190055201_matheucvp_Matheus Calixto Vaz Pinheiro.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1oftNwxIgliiduGxHmweISJfFlV4ZHma-
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install --upgrade pip
|
11 |
+
!pip install -Uqq duckduckgo_search
|
12 |
+
!pip install ipywidgets
|
13 |
+
!pip install voila
|
14 |
+
!jupyter serverextension enable --sys-prefix voila
|
15 |
+
|
16 |
+
from duckduckgo_search import ddg_images
|
17 |
+
from fastcore.all import *
|
18 |
+
from fastdownload import download_url
|
19 |
+
from fastai.vision.all import *
|
20 |
+
from time import sleep
|
21 |
+
|
22 |
+
def search_images(term, max_images=200): return L(ddg_images(term, max_results=max_images)).itemgot('image')
|
23 |
+
|
24 |
+
urls = search_images('strawberry', max_images=1)
|
25 |
+
urls[0]
|
26 |
+
|
27 |
+
dest = 'strawberry.jpg'
|
28 |
+
download_url(urls[0], dest, show_progress=False)
|
29 |
+
|
30 |
+
im = Image.open(dest)
|
31 |
+
im.to_thumb(256,256)
|
32 |
+
|
33 |
+
download_url(search_images('spoiled strawberry', max_images=1)[0], 'spoiled_strawberry.jpg', show_progress=False)
|
34 |
+
Image.open('spoiled_strawberry.jpg').to_thumb(256,256)
|
35 |
+
|
36 |
+
searches = 'ripe', 'spoiled'
|
37 |
+
path = Path('ripe_or_spoiled')
|
38 |
+
|
39 |
+
for o in searches:
|
40 |
+
dest = (path/o)
|
41 |
+
dest.mkdir(exist_ok=True, parents=True)
|
42 |
+
download_images(dest, urls=search_images(f'{o} strawberry fruit', 600))
|
43 |
+
sleep(10)
|
44 |
+
resize_images(path/o, max_size=400, dest=path/o)
|
45 |
+
|
46 |
+
failed = verify_images(get_image_files(path))
|
47 |
+
failed.map(Path.unlink)
|
48 |
+
len(failed)
|
49 |
+
|
50 |
+
strawberrys = DataBlock(
|
51 |
+
blocks=(ImageBlock, CategoryBlock),
|
52 |
+
get_items=get_image_files,
|
53 |
+
splitter=RandomSplitter(valid_pct=0.2, seed=42),
|
54 |
+
get_y=parent_label,
|
55 |
+
item_tfms=Resize(264)
|
56 |
+
)
|
57 |
+
|
58 |
+
dls = strawberrys.dataloaders(path)
|
59 |
+
|
60 |
+
dls.valid.show_batch(max_n=4, nrows=1)
|
61 |
+
|
62 |
+
strawberrys = strawberrys.new(item_tfms=Resize(128, ResizeMethod.Squish))
|
63 |
+
dls = strawberrys.dataloaders(path)
|
64 |
+
dls.valid.show_batch(max_n=4, nrows=1)
|
65 |
+
|
66 |
+
strawberrys = strawberrys.new(item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros'))
|
67 |
+
dls = strawberrys.dataloaders(path)
|
68 |
+
dls.valid.show_batch(max_n=4, nrows=1)
|
69 |
+
|
70 |
+
strawberrys = strawberrys.new(item_tfms=RandomResizedCrop(128, min_scale=0.3))
|
71 |
+
dls = strawberrys.dataloaders(path)
|
72 |
+
dls.valid.show_batch(max_n=4, nrows=1, unique=True)
|
73 |
+
|
74 |
+
strawberrys = strawberrys.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))
|
75 |
+
dls = strawberrys.dataloaders(path)
|
76 |
+
dls.valid.show_batch(max_n=8, nrows=2, unique=True)
|
77 |
+
|
78 |
+
strawberrys = strawberrys.new(
|
79 |
+
item_tfms=RandomResizedCrop(224, min_scale=0.5),
|
80 |
+
batch_tfms=aug_transforms())
|
81 |
+
dls = strawberrys.dataloaders(path)
|
82 |
+
|
83 |
+
learn = vision_learner(dls, resnet18, metrics=[error_rate, accuracy])
|
84 |
+
learn.fine_tune(10)
|
85 |
+
|
86 |
+
Learning_interpreter = ClassificationInterpretation.from_learner(learn)
|
87 |
+
|
88 |
+
Learning_interpreter.plot_top_losses(12)
|
89 |
+
|
90 |
+
Learning_interpreter.plot_confusion_matrix()
|
91 |
+
|
92 |
+
from fastai.vision.widgets import *
|
93 |
+
cleaner = ImageClassifierCleaner(learn)
|
94 |
+
cleaner
|
95 |
+
|
96 |
+
for idx in cleaner.delete(): cleaner.fns[idx].unlink()
|
97 |
+
|
98 |
+
for idx, cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)
|
99 |
+
|
100 |
+
is_spoiled_or_ripe,_,probs = learn.predict(PILImage.create('strawberry.jpg'))
|
101 |
+
print(f'this is a: {is_spoiled_or_ripe}.')
|
102 |
+
print(f"Probability it's a ripe: {probs[0]:.4f}")
|
103 |
+
print(f"Probability it's a spoiled: {probs[1]:.4f}")
|
104 |
+
|
105 |
+
learn.export()
|
106 |
+
|
107 |
+
path = Path()
|
108 |
+
path.ls(file_exts='.pkl')
|
109 |
+
|
110 |
+
learn_inf = load_learner(path/'export.pkl')
|
111 |
+
|
112 |
+
learn_inf.predict('strawberry.jpg')
|
113 |
+
|
114 |
+
learn_inf.dls.vocab
|
115 |
+
|
116 |
+
btn_upload = widgets.FileUpload()
|
117 |
+
btn_upload
|
118 |
+
|
119 |
+
btn_upload = SimpleNamespace(data = ['strawberry.jpg'])
|
120 |
+
|
121 |
+
img = PILImage.create(btn_upload.data[-1])
|
122 |
+
|
123 |
+
out_pl = widgets.Output()
|
124 |
+
out_pl.clear_output()
|
125 |
+
with out_pl: display(img.to_thumb(256,256))
|
126 |
+
out_pl
|
127 |
+
|
128 |
+
pred, pred_idx,probs = learn_inf.predict(img)
|
129 |
+
|
130 |
+
lbl_pred = widgets.Label()
|
131 |
+
lbl_pred.value = f'Predition: {pred}; Probability:{probs[pred_idx]:.04f}'
|
132 |
+
lbl_pred
|
133 |
+
|
134 |
+
btn_run = widgets.Button(description='Classify')
|
135 |
+
btn_run
|
136 |
+
|
137 |
+
def on_click_classify(change):
|
138 |
+
img = PILImage.create(btn_upload.data[-1])
|
139 |
+
out_pl.clear_output()
|
140 |
+
with out_pl: display(img.to_thumb(256,256))
|
141 |
+
pred,pred_idx,probs = learn_inf.predict(img)
|
142 |
+
lbl_pred.value = f'Predition: {pred}; Probability:{probs[pred_idx]:.04f}'
|
143 |
+
|
144 |
+
btn_run.on_click(on_click_classify)
|
145 |
+
|
146 |
+
btn_upload = widgets.FileUpload()
|
147 |
+
|
148 |
+
from ipywidgets import vbox
|
149 |
+
|
150 |
+
Vbox([widgets.Label('Select your Strawberry!'), btn_upload, btn_run, out_pl, lbl_pred])
|