-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
65 lines (54 loc) · 1.72 KB
/
train_model.py
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
#from time import sleep
from duckduckgo_search import DDGS
from fastcore.all import L, Path
from fastdownload import download_url
from fastai.vision.all import (
#Image,
download_images,
resize_images,
verify_images,
get_image_files,
DataBlock,
ImageBlock,
CategoryBlock,
RandomSplitter,
parent_label,
Resize,
vision_learner,
resnet18,
error_rate,
PILImage
)
def search_images(term, max_images=100):
'Search images with search term "term"'
print(f"Searching for '{term}'")
with DDGS() as ddgs:
return L(ddgs.images(term, max_results=max_images)).itemgot('image')
searches = 'crimson rosella', 'cockatoo', 'australian magpie'
path = Path('australian_birds')
for o in searches:
dest = path/o
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f'{o} photo'))
#sleep(10) # Pause between searches to avoid over-loading server
#download_images(dest, urls=search_images(f'{o} sun photo'))
#sleep(10)
#download_images(dest, urls=search_images(f'{o} shade photo'))
#sleep(10)
resize_images(path/o, max_size=400, dest=path/o)
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
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(path, bs=32)
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
learn.export()
urls = search_images('cockatoo', max_images=1)
DEST = 'cockatoo.jpg'
download_url(urls[0], DEST, show_progress=False)
bird_prediction, _, probs = learn.predict(PILImage.create('cockatoo.jpg'))