<jupyter_start><jupyter_text><jupyter_code>!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

from fastbook import *

key = 'XXX'
bear_types = 'grizzly','black','teddy'
path = Path('bears')
if not path.exists():
    path.mkdir()
    for o in bear_types:
        dest = (path/o)
        dest.mkdir(exist_ok=True)
        results = search_images_bing(key, f'{o} bear')
        download_images(dest, urls=results.attrgot('content_url'))
fns = get_image_files(path)
fns
failed = verify_images(fns)
failed
failed.map(Path.unlink);

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

dls = bears.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)
learn = cnn_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(4)
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
learn.export()
path = Path()
path.ls(file_exts='.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13270142180094788, 'fraction_numerical': 0.011374407582938388, 'mean_word_length': 5.478527607361963, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '538557', 'license': 'MIT', 'n_tokens_mistral': 415, 'n_tokens_neox': 403, 'n_words': 65, 'path': 'bear_class.ipynb', 'repo_name': 'phoughton/bear_class', 'size': '1055'}