File size: 4,245 Bytes
b1a7756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""190055201_matheucvp_Matheus Calixto Vaz Pinheiro.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1oftNwxIgliiduGxHmweISJfFlV4ZHma-
"""

!pip install --upgrade pip
!pip install -Uqq duckduckgo_search
!pip install ipywidgets
!pip install voila
!jupyter serverextension enable --sys-prefix voila

from duckduckgo_search import ddg_images
from fastcore.all import *
from fastdownload import download_url
from fastai.vision.all import *
from time import sleep

def search_images(term, max_images=200): return L(ddg_images(term, max_results=max_images)).itemgot('image')

urls = search_images('strawberry', max_images=1)
urls[0]

dest = 'strawberry.jpg'
download_url(urls[0], dest, show_progress=False)

im = Image.open(dest)
im.to_thumb(256,256)

download_url(search_images('spoiled strawberry', max_images=1)[0], 'spoiled_strawberry.jpg', show_progress=False)
Image.open('spoiled_strawberry.jpg').to_thumb(256,256)

searches = 'ripe', 'spoiled'
path = Path('ripe_or_spoiled')

for o in searches:
  dest = (path/o)
  dest.mkdir(exist_ok=True, parents=True)
  download_images(dest, urls=search_images(f'{o} strawberry fruit', 600))
  sleep(10)
  resize_images(path/o, max_size=400, dest=path/o)

failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
len(failed)

strawberrys = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y=parent_label,
    item_tfms=Resize(264)
)

dls = strawberrys.dataloaders(path)

dls.valid.show_batch(max_n=4, nrows=1)

strawberrys = strawberrys.new(item_tfms=Resize(128, ResizeMethod.Squish))
dls = strawberrys.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)

strawberrys = strawberrys.new(item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros'))
dls = strawberrys.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)

strawberrys = strawberrys.new(item_tfms=RandomResizedCrop(128, min_scale=0.3))
dls = strawberrys.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1, unique=True)

strawberrys = strawberrys.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))
dls = strawberrys.dataloaders(path)
dls.valid.show_batch(max_n=8, nrows=2, unique=True)

strawberrys = strawberrys.new(
    item_tfms=RandomResizedCrop(224, min_scale=0.5),
    batch_tfms=aug_transforms())
dls = strawberrys.dataloaders(path)

learn = vision_learner(dls, resnet18, metrics=[error_rate, accuracy])
learn.fine_tune(10)

Learning_interpreter = ClassificationInterpretation.from_learner(learn)

Learning_interpreter.plot_top_losses(12)

Learning_interpreter.plot_confusion_matrix()

from fastai.vision.widgets import *
cleaner = ImageClassifierCleaner(learn)
cleaner

for idx in cleaner.delete(): cleaner.fns[idx].unlink()

for idx, cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)

is_spoiled_or_ripe,_,probs = learn.predict(PILImage.create('strawberry.jpg'))
print(f'this is a: {is_spoiled_or_ripe}.')
print(f"Probability it's a ripe: {probs[0]:.4f}")
print(f"Probability it's a spoiled: {probs[1]:.4f}")

learn.export()

path = Path()
path.ls(file_exts='.pkl')

learn_inf = load_learner(path/'export.pkl')

learn_inf.predict('strawberry.jpg')

learn_inf.dls.vocab

btn_upload = widgets.FileUpload()
btn_upload

btn_upload = SimpleNamespace(data = ['strawberry.jpg'])

img = PILImage.create(btn_upload.data[-1])

out_pl = widgets.Output()
out_pl.clear_output()
with out_pl: display(img.to_thumb(256,256))
out_pl

pred, pred_idx,probs = learn_inf.predict(img)

lbl_pred = widgets.Label()
lbl_pred.value = f'Predition: {pred}; Probability:{probs[pred_idx]:.04f}'
lbl_pred

btn_run = widgets.Button(description='Classify')
btn_run

def on_click_classify(change):
  img = PILImage.create(btn_upload.data[-1])
  out_pl.clear_output()
  with out_pl: display(img.to_thumb(256,256))
  pred,pred_idx,probs = learn_inf.predict(img)
  lbl_pred.value = f'Predition: {pred}; Probability:{probs[pred_idx]:.04f}'

btn_run.on_click(on_click_classify)

btn_upload = widgets.FileUpload()

from ipywidgets import vbox

Vbox([widgets.Label('Select your Strawberry!'), btn_upload, btn_run, out_pl, lbl_pred])