Spaces:
Sleeping
Sleeping
| bear_type = 'grizzly', 'black', 'teddy' | |
| path = Path('bears') | |
| if not path.exists(): | |
| # checks if the path already exists. | |
| path.mkdir() | |
| #When you call the mkdir() method on the path object, it creates a new directory with the name specified by path | |
| for o in bear_type: | |
| dest = (path/o) | |
| # makes a dest varibale that represents a path object for something in o, for example a "bear/grizzly" path | |
| dest.mkdir(exist_ok=True) | |
| # uses the mkdir method on the dest variable | |
| results = search_images_ddg(f'{o} bears') | |
| # goes and searches for urls with grizzly bears (or any thing in bear_type) | |
| download_images(dest, urls=results) | |
| # tells the computer where to save the images and what to download | |
| fns = get_image_files(path) | |
| fns | |
| failed = verify_images(fns) | |
| failed | |
| failed.map(Path.unlink) | |
| bears = DataBlock( | |
| # This specifies the types of the input and output data. | |
| blocks=(ImageBlock, CategoryBlock), | |
| # tells the datablock what files to get | |
| get_items=get_image_files, | |
| # selects 20% of the files for validation and seed insures that its the same 20% | |
| splitter=RandomSplitter(valid_pct=0.2, seed=42), | |
| # lparent_label is a function provided by fastai that simply gets the name of the folder a file is in. Because we put each of our bear images into folders based on the type of bear, this is going to give us the labels that we need. | |
| get_y=parent_label, | |
| # resizes each image | |
| item_tfms=Resize(128)) | |
| dls = bears.dataloaders(path) | |
| dls.valid.show_batch(max_n=4, nrows=1) | |
| bears = bears.new( | |
| item_tfms=RandomResizedCrop(224, min_scale=0.5), | |
| batch_tfms=aug_transforms()) | |
| dls = bears.dataloaders(path) | |
| learn = vision_learner(dls, resnet18, metrics=error_rate) | |
| learn.fine_tune(6) | |