strawberry_ripe_or_spoiled / 190055201_matheucvp_matheus_calixto_vaz_pinheiro.py
matheuscvp's picture
Upload .py of the model
b1a7756
# -*- 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])