01: Intro
Setup
Import fastbook and patch IPython for VS Code
from fastbook import *
from IPython.display import clear_output, DisplayHandle
def update_patch(self, obj):
clear_output(wait=True)
self.display(obj)
DisplayHandle.update = update_patch
Set up a path to store the images
data_path = Path('data')/'01'
data_path.mkdir(parents=True,exist_ok=True)
Preview Images
Set up a function to try out different keywords and see how they look
def preview(keyword: string):
path = data_path / f"{keyword}.jpg"
download_url(search_images_ddg(keyword, max_images=1)[0], path)
return Image.open(path).to_thumb(256, 256)
preview("rugby")
100.75% [98304/97571 00:02<00:00]
preview("nfl")
100.88% [245760/243610 00:00<00:00]
preview("soccer")
107.98% [81920/75868 00:00<00:00]
preview("afl")
100.18% [139264/139007 00:00<00:00]
Download Dataset
searches = "rugby","afl","nfl","soccer"
dataset_path = data_path/"datasets"
if not (dataset_path).exists():
for o in searches:
dest = (dataset_path/o)
dest.mkdir(parents=True,exist_ok=True)
results = search_images_ddg(f"{o} game")
download_images(dest, urls=results[:200])
resize_images(dest, max_size=400, dest=dest)
Build Data Block
failed = verify_images(get_image_files(dataset_path))
failed.map(Path.unlink)
(#17) [None,None,None,None,None,None,None,None,None,None...]
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(dataset_path)
dls.show_batch(max_n=6)
Train Model
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(30)
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 1.331276 | 1.282537 | 0.398230 | 00:00 |
1 | 1.073042 | 1.108479 | 0.362832 | 00:00 |
2 | 0.873794 | 0.998766 | 0.371681 | 00:00 |
3 | 0.718180 | 0.987133 | 0.389381 | 00:00 |
4 | 0.583215 | 1.016907 | 0.362832 | 00:00 |
5 | 0.476426 | 1.067516 | 0.345133 | 00:00 |
6 | 0.383498 | 1.137339 | 0.362832 | 00:00 |
7 | 0.314319 | 1.200328 | 0.353982 | 00:00 |
8 | 0.261772 | 1.208668 | 0.336283 | 00:00 |
9 | 0.218976 | 1.175529 | 0.327434 | 00:00 |
10 | 0.184930 | 1.139289 | 0.300885 | 00:00 |
11 | 0.156622 | 1.109112 | 0.292035 | 00:00 |
12 | 0.133540 | 1.112555 | 0.292035 | 00:00 |
13 | 0.114173 | 1.126750 | 0.300885 | 00:00 |
14 | 0.098229 | 1.136930 | 0.300885 | 00:00 |
15 | 0.084903 | 1.140284 | 0.318584 | 00:00 |
16 | 0.074624 | 1.148302 | 0.327434 | 00:00 |
17 | 0.064603 | 1.167305 | 0.318584 | 00:00 |
18 | 0.055911 | 1.179664 | 0.318584 | 00:00 |
19 | 0.048461 | 1.191116 | 0.327434 | 00:00 |
20 | 0.042165 | 1.178158 | 0.336283 | 00:00 |
21 | 0.036902 | 1.185483 | 0.327434 | 00:00 |
22 | 0.032348 | 1.170821 | 0.336283 | 00:00 |
23 | 0.028613 | 1.168687 | 0.336283 | 00:00 |
24 | 0.025399 | 1.167978 | 0.345133 | 00:00 |
25 | 0.022398 | 1.166666 | 0.345133 | 00:00 |
26 | 0.019721 | 1.173368 | 0.345133 | 00:00 |
27 | 0.017580 | 1.174313 | 0.345133 | 00:00 |
28 | 0.015551 | 1.172876 | 0.345133 | 00:00 |
29 | 0.013663 | 1.171674 | 0.345133 | 00:00 |
Test Model
learn.show_results()
sport,_,probs = learn.predict(PILImage.create(data_path/'nfl.jpg'))
print(f"this is {sport}")
print("\n--probabilities--")
for i, o in enumerate(dls.vocab):
print(f"{o}: {probs[i]:.4f}")
this is nfl
--probabilities--
afl: 0.0008
nfl: 0.9891
rugby: 0.0013
soccer: 0.0089