Typical (NN-Based) Classification vs. Zero-Shot, Part 2 - An Example Using FastAI

With my new data-visualization Callback!
classification
ssl
Author

Scott H. Hawley

Published

June 17, 2021

(This blog post implements the concepts illustrated in my previous post, “Typical (Neural-Network-Based) Classification vs. Zero-Shot, Part 1 - The Joy of 3D”. Note that the “3D” part was because of its utility as a teaching tool for gaining intuition; realistic applications usually involve hundreds of “dimensions.”)

Intro

In Part 1, I described how contrastive loss-based “metric learning” methods are similar to and different from traditional “one-hot” neural network classifiers in a few ways: similar in that both types of methods can be viewed as embedding methods (not just metric learning), different in that the latter involve a fixed set of classes, whereas the former rely on a similarity measure.

Confession: I didn’t fully disclose my motivation last time: I simply got sick of not knowing what “contrastive losses” meant in many important paper titles – notably OpenAI’s “CLIP” and Eduardo Fonseca et al’s “Unsupervised Contrastive Learning of Sound Event Representations” – and decided “I need to learn this.” And (as one often finds in the rapidly-advancing and fragmented world of ML) it turned out I already knew what contrastive losses and metric learning were (e.g., I took a 4-course NLP Specialization last year!), just not by those names. So hey, I might actually be qualified to teach on this after all! ;-) Ok, enough with the autobiography.

Here’s an annotated outline of the sections of this post:

  1. Traditional One-Hot Classification Demo, using 3 and 4 classes. We’ll be following Jeremy Howard and Sylvain Guggers’s “bears” example from Chapter 2 of the fastai book. But we’ll focus on concepts and outcomes, saving FastAI details for Section 4
  2. Contrastive Loss Demo using the same data as as Section 1.
  3. Discussion
  4. FastAI Details, including “how I learned to write my own Callback” for progress-tracking and data visualization. Note that these coding details will be present earlier in the post and viewable via the “Display Code” buttons, but the explanations of the code won’t appear until Section 4.

1. Traditional One-Hot Classification Demo: Three (and 4) Types of Bears

We could do the “cats vs. dogs vs. horses” example from the previous Part 1 post, but it turns out that because we’re going to do Transfer Learning using a model pre-trained on ImageNet (which already has categories for cats, dogs, and horses), it’s too easy to even see anything “interesting” happen! I want to be able to show you “dots moving around” as the model trains.

Instead, we’ll copy Jeremy Howard…

Aside: “We’ll copy Jeremy Howard” is something more members the ML community could probably stand to do, or admit they’re doing.

…and do three types of bears: grizzly, brown, and teddy. ;-) And then later we’ll throw in one more “natural kind” of bear. ;-)

Scraping for Images

Using the DuckDuckGo API, let’s grab ~100 images of each kind of bear.

Code
def scrape_for_me(dl_path, labels, search_suffix, erase_dir=True, max_n=100):
    if erase_dir:
        !rm -rf {dl_path}           # clear out last time's downloads
    path = Path(dl_path)
    if not path.exists(): path.mkdir()
    for o in labels:            # scrape images off the web
        search_term = (f'{o} {search_suffix}').strip()
        dest = (path/o)
        dest.mkdir(exist_ok=True)
        print(f"Getting urls for search_term = {search_term}..")
        urls = search_images_ddg(f'{search_term}', max_n=max_n) 
        urls = [ x for x in urls if ("magpies" not in x) and ("charliebears" not in x) ]   # kludge for now to keep download_images from hanging
        print(f"{search_term}: Got {len(urls)} image URLs. Downloading...")
        print("   urls = ",urls)
        download_images(dest, urls=urls, preserve_filename=False)
        print("    Images downloaded.")
        
    fns = get_image_files(path)     # list image filenames
    failed = verify_images(fns)     # check if any are unloadable

    # remove what's unloadable
    failed.map(Path.unlink);
    if failed != []:
        _ = [fns.remove(f) for f in failed]

    # Extra: To avoid Transparency warnings, convert PNG images to RGBA, from https://forums.fast.ai/t/errors-when-training-the-bear-image-classification-model/83422/9
    converted = L()
    for image in fns:
        if '.png' in str(image):
            im = Image.open(image)
            converted.append(image)  # old file name before resaving
            im.convert("RGBA").save(f"{image}2.png")    
    converted.map(Path.unlink); # delete originals
    print(f"After checking images for issues, {len(get_image_files(path))} (total) images remain.")
    return path     # and return a pathlib object pointing to image dir
dl_path = 'scraped_images'  # where we're saving to
labels = 'grizzly','black','teddy'  
search_suffix = 'bear'
path = scrape_for_me(dl_path, labels, search_suffix, max_n=100)
Getting urls for search_term = grizzly bear..

“Only 100(x3) images?! How can you hope to train a sophistical computer vision model with such a small amount of data?” Transfer Learning, my friend.

Load the Data and Take a Look

Here are some examples of the images we grabbed:

Code
def setup_dls(path):
    data = DataBlock(        # define fastai data structure
        blocks=(ImageBlock, CategoryBlock), 
        get_items=get_image_files, 
        splitter=RandomSplitter(valid_pct=0.2, seed=42),
        get_y=parent_label,
        item_tfms=RandomResizedCrop(224, min_scale=0.5),
        batch_tfms=aug_transforms())
    dls = data.dataloaders(path)    # define dataloader(s)
    return dls 

dls = setup_dls(path)
dls.show_batch()                # take a look

Define and Train the Model

…With my special “triangle plot” visualization ( as demonstrated in the Part 1 blog post) called from my VizPreds fastai Callback (described below). (If you run this notebook on Colab, you’ll need to authorize Google Drive when prompted below, in order for the triangle plot image thumbnails to show).

In the plots that follow, mouse over the dots to see our bear images “move” as the model trains! You’ll see one epoch where the “body” network’s weights are frozen, and then four epochs where the full model trains.

Code
learn = cnn_learner(dls, resnet18, metrics=[error_rate])
learn.fine_tune(4, cbs=VizPreds)  # fine_tune does 1 epoch 'frozen', then as many more epochs you specify
Generating (URLS of) thumbnail images...
Drive already mounted at /gdrive; to attempt to forcibly remount, call drive.mount("/gdrive", force_remount=True).
Thumbnails saved to Google Drive in /gdrive/My Drive/scraped_images_thumbs/
Waiting on Google Drive until URLs are ready.

Epoch 0: Plotting 51 (= 51?) points:
Epoch 0: Plotting 51 (= 51?) points:
Epoch 1: Plotting 51 (= 51?) points:
Epoch 2: Plotting 51 (= 51?) points:
Epoch 3: Plotting 51 (= 51?) points:
epoch train_loss valid_loss error_rate time
0 1.788116 0.159182 0.019608 00:13
epoch train_loss valid_loss error_rate time
0 0.327038 0.050930 0.000000 00:13
1 0.231127 0.067547 0.019608 00:13
2 0.170447 0.047806 0.019608 00:13
3 0.134644 0.027070 0.019608 00:13

Depending on which bear images get scraped on which day, even this may be trivially easy. When I ran this above, there was only one error: a tiny brown-colored bear cub that was classified by the model as grizzly but that was labeled (via DuckDuckGo) as a black bear. All the other points correctly “collapsed” very quickly nearly the poles.

If we look at the confusion matrix, we’ll see (for the time I ran this), only the one error mentioned above:

Code
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

And if we plot the top losses, we’ll see (based on mousing-over in the triangle plot above) the images for the points furthest from the poles: first the mislabeled cub, then a grizzly mother and cub, then a teddy bear:

Code
interp.plot_top_losses(6, nrows=1)

When I ran this, the top loss image was actually a small dog! I coulda/shoulda checked that above in the triangle plot!

We’re not going to train this more or clean the dataset, instead let’s just remember that in this case, for 3 classes our error rate was 0.019608 (= 1/51, i.e. 1 error out of 51 images in the validation set), i.e. we were ~98% accurate.

Four Classes

Let’s add one more bear to the mix, the “spectacled bear”, and repeat this process, and visualize in 3D (sadly no image thumbnail mouseovers for 3D plots). First we’ll download and look at what we’ve got..

Code
labels = 'grizzly','black','teddy','spectacled'
scrape_for_me(dl_path, [labels[-1]], search_suffix, erase_dir=False) # download one more class
dls = setup_dls(path)
dls.show_batch()  
spectacled bear: Got 100 image URLs. Downloading...
   urls =  ['http://4.bp.blogspot.com/-6xZLUXHh8Uc/UUxmEy9QZBI/AAAAAAAAXcY/TMXg3WwS3GA/s1600/Spectacled+Bear+2.jpg', 'https://www.coolearth.org/wp-content/uploads/2016/06/Spectacled-Bear-2.jpg', 'http://3.bp.blogspot.com/-Sg8TmR5qh88/UUxmNY4d0HI/AAAAAAAAXcw/dpL2qFHZvp0/s1600/Spectacled+Bear+5.jpg', 'https://i.natgeofe.com/n/804d9cdb-f8da-449a-93b0-aac7a448b7c0/spectacled-bear_thumb.JPG?w=1200', 'https://www.activewild.com/wp-content/uploads/2018/03/spectacled-bears-1024x683.jpg', 'http://www.rainforest-alliance.org/sites/default/files/2016-09/spectacled-bear-header.jpg', 'https://lh6.googleusercontent.com/-Sptgd7fYIqY/TYk4YVRbndI/AAAAAAAAB7E/MYqqcMu-isw/s1600/Spectacled_Bear_Tennoji_2.jpg', 'https://www.vanwageningen.net/i/upload/2014/10/29/20141029132015-bc5e40e5-me.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-1002804-spectacled-bear-face.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear.jpg', 'https://colombiareports.com/wp-content/uploads/2017/07/spectacled_bear.jpg', 'http://tenrandomfacts.com/wp-content/uploads/2016/07/Spectacled-Bear.jpg', 'https://static1.squarespace.com/static/56a1a14b05caa7ee9f26f47d/t/57390f5520c6471cf068c6cc/1463357302812/', 'http://theamazonianassignment.weebly.com/uploads/1/2/1/3/12136910/944137409_orig.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-ecuadorian-andes-mountains-328x562.jpg?c14113', 'http://i1.treknature.com/photos/2959/bear_0111111.jpg', 'http://2.bp.blogspot.com/-vdZv8wGWMdo/UUxmNCvsvhI/AAAAAAAAXco/H5Pzgk5dtOk/s1600/Spectacled+Bear+6.jpg', 'https://media.sciencephoto.com/image/c0175188/800wm/C0175188-Spectacled_Bear.jpg', 'http://www.stlzoo.org/files/1014/0017/1158/ANDEANBEAR9410WINKELMAN2.jpg', 'https://www.tourradar.com/days-to-come/wp-content/uploads/2016/01/Spectacled-bear.jpg', 'https://www.zoochat.com/community/media/spectacled-bear.250657/full?d=1393011952', 'http://2.bp.blogspot.com/-1jh4WfVEyx4/T_A-OoNHGFI/AAAAAAAAMHM/Wm41l9WFfgs/s1600/Spectacled+Bear3.jpg', 'http://www.rainforest-alliance.org/sites/default/files/styles/750w_585h/public/2016-09/spectacled-bear-header.jpg?itok=0V7_KGpe', 'https://www.sustainability-times.com/wp-content/uploads/2019/06/AndeanBear_ZN.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Habitat.jpg', 'http://seethewild.org/wp-content/uploads/2016/05/features.jpeg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/spectacled-bear-portrait-rose-santuci-sofranko.jpg', 'http://kids.sandiegozoo.org/sites/default/files/2017-07/andean-bear-tree.jpg', 'https://southafricatoday.net/wp-content/uploads/2018/07/1532665810_1-1024x1000.jpg', 'https://upload.wikimedia.org/wikipedia/en/c/ce/Spectacled-bear.jpg', 'http://4.bp.blogspot.com/-zHQPWNws7VQ/TwK7vuoleYI/AAAAAAAAEuI/iZJGb0R9CEQ/s1600/spectacled_bear-spectacled_bear.jpg', 'http://www.ecns.cn/hd/2020/05/18/e41ef93a186c40a38726189b8901f6f3.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Baby-Spectacled-Bear.jpg', 'http://www.mammalwatching.com/wp-content/uploads/2018/01/EC-square-fox.jpg', 'http://newyorktrendnyc.com/wp-content/uploads/2013/11/andean-bear.jpg', 'https://home.bt.com/images/spectacled-bears-and-snow-leopards-among-creatures-getting-funding-boost-136427641293802601-180606124211.jpg', 'https://nationalzoo.si.edu/sites/default/files/animals/andeanbear-006.jpg', 'https://www.pitara.com/wordpress/wp-content/uploads/2001/02/spectacled-bear.jpg', 'http://3.bp.blogspot.com/-EGI8NqbPcvo/UUxmUJ6t1qI/AAAAAAAAXdA/fDUqD60Tv00/s1600/Spectacled+Bear.jpg', 'https://lh6.googleusercontent.com/-cc05A4QkDxc/TYk4S3w1qFI/AAAAAAAAB7A/3vb0t85A7GU/s1600/spectacled_bear_03.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-rocks-820x545.jpg?c14113', 'https://i.ytimg.com/vi/VXFIYIQgEkg/maxresdefault.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-71192249-spectacled-bear.jpg', 'https://farm3.staticflickr.com/2913/14137750328_d101997a4c_z.jpg', 'http://www.realworldholidays.co.uk/blog/wp-content/uploads/2015/12/spectacled-bear1.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-2.jpg', 'http://upload.wikimedia.org/wikipedia/commons/6/62/Spectacled_Bear_059.jpg', 'https://media.nbcwashington.com/2019/09/4621594915_9022bfd814_b.jpg?fit=1024%2C702', 'https://philadelphiazoo.org/wp-content/uploads/2019/10/Andean.SpectacledBear_Rosie_3395.jpg', 'http://2.bp.blogspot.com/-4bGjmY9K4pk/T_A-Yq5aNGI/AAAAAAAAMHc/pv9gEJ8O6Uo/s1600/Spectacled+Bear5.jpg', 'https://resize.hswstatic.com/w_907/gif/types-of-spectacled-bear0.jpg', 'http://1.bp.blogspot.com/-xGs4-W-3iSY/TjaZG6_XnOI/AAAAAAAABUE/AkEJ97L1HQc/s1600/156521.jpg', 'https://www.robertharding.com/watermark.php?type=preview&im=RM/RH/HORIZONTAL/1127-12838', 'https://i.pinimg.com/736x/fa/40/33/fa403340878f175322c07b860b9d6426--spectacled-bear-bolivia.jpg', 'http://img.izismile.com/img/img3/20100513/640/cute_spectacled_bear_640_03.jpg', 'https://ourplnt.com/wp-content/uploads/2016/03/Spectacled-bear.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/3956278926_328d1c6a54_o.jpg', 'http://cincinnatizoo.org/system/assets/uploads/2014/02/spectaculed-bear.jpg', 'http://3.bp.blogspot.com/-brSuqjGOIG0/URGtpFtyo_I/AAAAAAAAARg/vLE13RVoCsk/s1600/spectacled-bear.jpg', 'https://c2.staticflickr.com/2/1501/24144684635_73b9e79d88_b.jpg', 'https://critter.science/wp-content/uploads/2019/05/sb1.jpg', 'https://srcnaut.com/cdn-cgi/image/f=auto,fit=crop,g=0.5x0.5,w=2000,h=1125,q=90,d=1/upload/32/02/df/shutterstock-1188627676.jpg', 'http://seethewild.org/wp-content/uploads/2016/05/dreamstime_15373008-1.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Spectacled_Bear_-_Houston_Zoo.jpg/1200px-Spectacled_Bear_-_Houston_Zoo.jpg', 'https://www.zoochat.com/community/media/spectacled-bear-at-chester-15-07-12.192796/full?d=1342391273', 'https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1440,f_auto/MA_00155387_i4leyu.jpg', 'http://2.bp.blogspot.com/-PQGmVUDZAhs/TwK5urRX1LI/AAAAAAAAEtw/s569QHCcx8Y/s1600/spectacled+bear.jpg', 'https://bw-1651cf0d2f737d7adeab84d339dbabd3-gallery.s3.amazonaws.com/images/image_2865567/db2b44df10ba2a567a8e5cc34ddfeed0_original.JPG', 'http://etc.usf.edu/clippix/pix/spectacled-bear-with-piece-of-food-in-its-mouth_medium.jpg', 'https://www.activewild.com/wp-content/uploads/2018/03/spectacled-bear-standing-768x1024.jpg', 'http://2.bp.blogspot.com/-1mdQ66udYmw/T_A-UjaJk5I/AAAAAAAAMHU/uJw0kPcz1vc/s1600/Spectacled+Bear4.jpg', 'https://www.worldatlas.com/upload/43/04/5c/shutterstock-92656222-min.jpg', 'https://www.si.edu/sites/default/files/newsdesk/photos/andean-bear.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-ogimage.jpg', 'https://images.freeimages.com/images/large-previews/e0f/spectacled-bear-1561333.jpg', 'https://kentattractions.co.uk/wp-content/uploads/2016/09/Spectacled-Bear.jpg', 'http://gringosabroad.com/wp-content/uploads/2013/11/Male-Andean-Bear-face.jpg', 'https://rangerplanet.com/wp-content/uploads/2019/10/spectacled-bear.jpg', 'https://www.wisebirding.co.uk/wp-content/uploads/Spectacled-Bear.jpg', 'https://i.ytimg.com/vi/2TmnhBSyQoo/maxresdefault.jpg', 'https://vistapointe.net/images/spectacled-bear-5.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-tree-820x513.jpg?d8bc0c', 'https://standfirst-whitleyaward-production.imgix.net/content/uploads/1996/12/18134457/Tremarctos_ornatus_portrait.jpg?auto=compress,enhance,format&crop=faces,entropy,edges&fit=crop&fm=pjpg&w=1866&h=1400', 'https://c1.staticflickr.com/7/6037/6304684676_8fe4be5717_b.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/5566818433_9c994ec063_o.jpg', 'https://www.bwallpaperhd.com/wp-content/uploads/2018/12/SpectacledBear.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-eating-fruit.jpg', 'https://images.fineartamerica.com/images-medium-large-5/andean-bear-spectacled-bear-houston-zoo-tessa-fairey.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-598140-spectacled-bear.jpg', 'http://magarticles.magzter.com/articles/8803/216564/58e38ecbbeeb4/Conservation-Insight-Andean-Bear.jpg', 'http://www.rainforest-alliance.org/sites/default/files/styles/large/public/2016-09/spectacled-bear.jpg?itok=BXWidYtz', 'https://bw-1651cf0d2f737d7adeab84d339dbabd3-gallery.s3.amazonaws.com/images/image_2865571/a3195154ccdac7c9efcbed5727462346_original.JPG', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-in-the-Wild.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/6862037661_30664cf53f_o.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Eating.jpg', 'http://2.bp.blogspot.com/-3UzekS1270M/TiAwko0VrQI/AAAAAAAAAKI/rOXYR8LXLHM/s1600/spectacled_bear_3.jpg', 'https://i.pinimg.com/736x/d2/99/fa/d299fa76c3d1a5c0e017514b086ddd9c--spectacled-bear-teddy-bears.jpg', 'https://c2.staticflickr.com/4/3685/8852797884_8f3a84d84c_b_d.jpg', 'http://www.kidzone.ws/lw/bears/images/spectacled-bear.jpg', 'https://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Images.jpg']
    Images downloaded.
After checking images for issues, 348 (total) images remain.

Then we’ll train the model:

Code
learn = cnn_learner(dls, resnet18, metrics=[error_rate])
learn.fine_tune(4, cbs=VizPreds)  # fine_tune does 1 epoch 'frozen', then as many more epochs you specify
epoch train_loss valid_loss error_rate time
0 1.685202 0.791340 0.275362 00:16
Epoch 0: Plotting 69 (= 69?) points:
Epoch 0: Plotting 69 (= 69?) points:
Epoch 1: Plotting 69 (= 69?) points:
Epoch 2: Plotting 69 (= 69?) points:
Epoch 3: Plotting 69 (= 69?) points:
epoch train_loss valid_loss error_rate time
0 0.474520 0.377549 0.130435 00:15
1 0.337095 0.232552 0.086957 00:16
2 0.252857 0.182564 0.072464 00:15
3 0.207571 0.131986 0.057971 00:16

…So we see that spectacled bears, being black-colored, can get mixed up with black bears sometimes.

Code
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

The biggest losses are usually when the spectacled bears’ “spectacles” are not fully in view (e.g. in a side shot), in which case either its black coat makes it seem like a black bear, or its shagginess & size make it seem like a grizzly:

Code
interp.plot_top_losses(6, nrows=1)

(Another time I ran this, the top loss was for an image of a fox that was mislabeled as ‘spectacled’! Clearly just scraping from DuckDuckGo is not fool-proof. LOL)

Note that we had to retrain the model in order to handle the spectacled bear – there was no “slot” for it in the original 3-class model. We’re now going to switch to a contrastive loss model that uses a Siamese Network, which…we’ll see how it does on the spectacled bear after we train it on only the first 3 types of bear.

I’m going to pause writing and come back in the morning, so that I can title the next section…

2. Contrastive Loss Demo: Same Bears, Different Day

As we described in blog post Part 1, contrastive loss systems achieve metric learning by employing a Siamese Network.

Lesson 2 of the FastAI book already has a very effective Siamese Network demo. It does not use contrastive loss, and yet it will (see Appenix below) outperform my contrastive loss version (in terms of accuracy, by about 5 to 6%). So why would we want contrastive losses? Well, because they are useful for learning “semantically meaningful” embeddings, something the aforementioned Siamese Network lesson did not set as a goal.

The FastAI lesson uses the Oxford PETS dataset which has a different format than our bear-images dataset, so the “good news” is I got an opportuny to learn more about writing my own fastai dataloader by altering Jeremy’s instructions in the Lesson.

Writing My First DataLoader(s)

…Actually it turns out that we can copy all we need from the fastai tutorial except the parts where we grab the image files and the label_func() (below) that gives us class names based on the file pathnames. What we need is a routine that grabs pairs of images at a time, and labels* them as either “similar” (denoted by a label value of 1) or not (denoted by a value of 0). .

Terminology: By “labels” I refer exclusively to “ground truth” or “target” values, which I may use interchangeably (esp. “targets”) supplied by the dataset/user – we are doing Supervised Learning. Model outputs will be referred to as “predictions”.

So, adapting the FastAI Siamese Networks Tutorial to a slightly different (and slightly simpler) data file structure,… it turns out we won’t be writing a DataBlock, but rather writing a Transform. (I don’t yet understand why TBH.)

Before we do that, we’re going to define a custom input object called SiameseImage that our dataloader/Transform can operate on, and that we can “show” in the notebook – it pays to be able to look at your data! ;-)

Let’s review our list of (3-bears) files, and we’ll leave out the spectacled bears for now

labels = 'grizzly','black','teddy'
path = scrape_for_me(dl_path, labels, search_suffix, max_n=300)
#labels = ['cat','dog','horse']
#path = scrape_for_me(dl_path, labels, '', max_n=300)
subdirs, path, files = labels, Path(dl_path), []
for s in subdirs: files += get_image_files(path/s)

print(f'files has {len(files)} elements.')
print(f"files[0] = {files[0]}, files[-1] = {files[-1]}")
grizzly bear: Got 280 image URLs. Downloading...
   urls =  ['https://i0.wp.com/www.commonsenseevaluation.com/wp-content/uploads/2013/08/Bear.jpg', 'https://blog.humanesociety.org/wp-content/uploads/2019/04/BEAR_GRIZZLY_A45T3555_468435.jpg', 'https://www.conservationnw.org/wp-content/uploads/2017/06/Grizzly-bear-family-Copyright-iStock.com_PhotosbyAndy-4.2015-e1507229404830.jpg', 'https://images.fineartamerica.com/images-medium-large/grizzly-bear-portrait-jim-guy.jpg', 'https://www.mtpr.org/sites/kufm/files/styles/x_large/public/201809/grizzly-bear-02_Istock.jpg', 'https://www.rmef.org/wp-content/uploads/2019/04/grizzly.jpg', 'https://images.dailyhive.com/20170509095127/bear-148-parks-canada-amar-atwal.jpeg', 'https://api.time.com/wp-content/uploads/2015/08/grizzly-bear.jpg?quality=85&w=1200&h=628&crop=1', 'https://a57.foxnews.com/a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2018/09/640/320/1862/1048/Grizzly-iStock.jpg?ve=1&tl=1?ve=1&tl=1', 'https://media.spokesman.com/photos/2014/09/15/jj-Grizzly_MT.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/grizzly-bear-in-snow-tendrel-images.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Desktop-Wallpaper-.jpg', 'https://i.cbc.ca/1.4132188.1495755019!/fileImage/httpImage/image.jpg_gen/derivatives/original_780/grizzly-bear.jpg', 'http://elelur.com/data_images/mammals/grizzly-bear/grizzly-bear-08.jpg', 'https://images.fineartamerica.com/images-medium-large-5/grizzly-bear-cub-phyllis-taylor.jpg', 'https://www.mtpr.org/sites/kufm/files/styles/x_large/public/201812/Grizzly-bear-family_LuCaAr-iStock.jpg', 'https://images.fineartamerica.com/images-medium-large/grizzly-bear-yukon-robert-postma.jpg', 'https://www.unilad.co.uk/wp-content/uploads/2020/02/close-up-photography-of-grizzly-bear-1068554.jpg', 'https://reidparkzoo.org/wp-content/uploads/2013/09/grzbear2812a.jpg', 'https://outsider.com/wp-content/uploads/2020/06/Grizzly-Bear-3.png', 'http://i.huffpost.com/gen/1227495/images/o-GRIZZLY-BEAR-facebook.jpg', 'https://i.ytimg.com/vi/Tue__Hlazh4/maxresdefault.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Wallpapers-and-Backgrounds.jpg', 'https://get.wallhere.com/photo/animals-wildlife-bears-fur-wilderness-Grizzly-Bears-Grizzly-bear-brown-bear-bear-fauna-mammal-1920x1200-px-snout-carnivoran-organism-american-black-bear-terrestrial-animal-771353.jpg', 'http://traveltalesoflife.com/wp-content/uploads/Kicking-Horse-Resort-Grizzly-Close-Up.jpg', 'http://www.hickerphoto.com/images/1024/-56752.jpg', 'https://images.dailyhive.com/20161104114500/Grizzly-bears-TrophyLush.jpg', 'https://mtsgreenway.org/wp-content/uploads/2019/08/September-Grizzly-Large.png', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/1-grizzly-bear-art-wildlife-fine-art.jpg', 'https://www.livewaterjacksonhole.com/wp-content/uploads/2020/06/Grizzly-399-Grand-Teton-National-Park.jpg', 'https://i0.wp.com/rangerrick.org/wp-content/uploads/2018/04/RRMAR13_6-11Grizzlies.jpg?fit=1156%2C650&ssl=1', 'http://grizzlybearwatching.com/wp-content/uploads/2017/03/BlackBear1-1500x998.jpg', 'http://mediad.publicbroadcasting.net/p/kufm/files/styles/medium/public/201410/grizzlybear.jpg', 'https://www.vmcdn.ca/f/files/rmotoday/images/grizzly122_kalra.jpg;w=960', 'http://4.bp.blogspot.com/-iRDehywX024/UNMpj9s5kVI/AAAAAAAAADI/9sxChqYMV2c/s1600/Grizzly-Bear-Yellowstone.jpg', 'https://2.bp.blogspot.com/-d0DjXibvH8w/UQN3Xn4QWDI/AAAAAAAAFb0/2nUaH5eoapg/s1600/Grizzly+Bear-2013-0pic-0.jpg', 'https://www.gannett-cdn.com/presto/2020/07/21/PGRF/83612915-6ab2-4f0d-aeca-7088f219ecec-AP20191697142470.jpg?crop=2999,1687,x0,y0&width=2999&height=1687&format=pjpg&auto=webp', 'http://assets.nydailynews.com/polopoly_fs/1.3269663.1498159924!/img/httpImage/image.jpg_gen/derivatives/article_750/654079900.jpg', 'https://d.ibtimes.co.uk/en/full/1621655/grizzly-bears.jpg', 'https://www.idahoconservation.org/wp-content/uploads/2020/06/Grizzly-Bear-photo-by-Jim-Mellen-1.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Ursus_arctos_californicus%2C_Santa_Barbara%2C_Natural_History_Museum.jpg/1200px-Ursus_arctos_californicus%2C_Santa_Barbara%2C_Natural_History_Museum.jpg', 'https://patch.com/img/cdn20/users/22906546/20180731/095817/styles/raw/public/processed_images/image006_1-1533085927-8474.jpg', 'https://wallup.net/wp-content/uploads/2017/11/17/265280-bears-Grizzly_bear.jpg', 'https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/2/2019/06/GettyImages-525103104.jpg', 'https://www.tsln.com/wp-content/uploads/2018/10/bears-tsln-101318-3-1240x826.jpg', 'http://www.bearsmart.com/wp-content/uploads/2015/01/why-bears3-730x330.jpg', 'https://cdn.rbth.com/980x-/all/2016/10/07/grizzly_gettyimages-10188130_b.jpg', 'https://wildlife.org/wp-content/uploads/2015/08/Grizzly-Bear-Credit-Washington-State-University-620x264.jpg', 'http://agenda21news.com/wp-content/uploads/2014/12/Grizzly-bear.gif', 'https://bloximages.chicago2.vip.townnews.com/codyenterprise.com/content/tncms/assets/v3/editorial/c/75/c75e6ae4-5ab0-11e7-9823-3b1d2aaec452/595173d52fa4e.image.jpg?resize=1200%2C885', 'https://cdn11.bigcommerce.com/s-db59p/images/stencil/1280x1280/products/21092/42704/oversized_12162019_01__97480.1577021805.jpg?c=2', 'http://1.bp.blogspot.com/_VgXaDXiFvX4/TOLc_oEzGcI/AAAAAAAAA9c/Q7qtQ7R1ju8/s1600/grizzly-bear-1.jpg', 'https://devdenverzoo.com/wp-content/uploads/2018/09/Grizzly-Bear_03.jpg', 'https://miro.medium.com/max/1758/1*cBBVLzODaagrL7V2eNSjSQ.jpeg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/grizzly-bear-jerry-fornarotto.jpg', 'https://www.wildnatureimages.com/images/xl/170628-145-Grizzly-Bear-Sow-with-Cubs.jpg', 'http://www.telegraph.co.uk/content/dam/news/2016/06/21/99682788_Two_grizzly_bears_at_the_Woodland_Park_Zoo_eat_salmon_after_the_fish_was_tossed_to_them_by-xlarge_trans_NvBQzQNjv4BqDfnT11g5XZty1Nobnb9oJsN7dAtWJUhNbBLTzbEHUEY.jpg', 'https://bloximages.chicago2.vip.townnews.com/trib.com/content/tncms/assets/v3/editorial/c/af/caf539ee-9c28-5fda-819a-3aeaa1f029bc/582cffad235a4.image.jpg?resize=1200%2C800', 'https://wildlife.org/wp-content/uploads/2016/08/grizzly1.jpg', 'https://cdn.thefencepost.com/wp-content/uploads/sites/12/2017/06/grizzly-RFP-070317-1240x826.jpg', 'http://static.independent.co.uk/s3fs-public/styles/story_large/public/thumbnails/image/2015/10/06/09/Grizzly.jpg', 'http://mediad.publicbroadcasting.net/p/kufm/files/styles/x_large/public/201603/grizzly-bear_Nathan-Rupert-CC-BY-NC-ND_0.jpg', 'https://www.cbbulletin.com/wp-content/uploads/2020/04/griz.jpg', 'https://img.thedailybeast.com/image/upload/v1492108548/articles/2016/10/15/won-t-you-please-adopt-a-grizzly-bear-cub/161014-Andrews-Grizzly-cubs-tease__p9gwbc.jpg', 'https://onekindplanet.org/wp-content/uploads/2017/09/grizzlybear-edit.jpg', 'http://www.akronzoo.org/Data/Sites/1/media/GalleryImages/105/WebImages/grizzly-bear-hero-1.jpg', 'http://wallup.net/wp-content/uploads/2016/01/139369-bears-nature-animals-river-baby_animals-Grizzly_Bears-Grizzly_bear.jpg', 'https://weneedfun.com/wp-content/uploads/Grizzly-Bear-4.jpg', 'https://1.bp.blogspot.com/-l-Y6zD_KF6o/UQN3jPgigAI/AAAAAAAAFcU/HOKJWQmNjo4/s1600/Grizzly+Bear-2013-0pic-05.jpg', 'https://devdenverzoo.com/wp-content/uploads/2018/09/Grizzly-Bear_02.jpg', 'https://www.conservationnw.org/wp-content/uploads/2017/10/grizzly-bears-1280x950.jpg', 'https://s1.it.atcdn.net/wp-content/uploads/2013/07/Grizzly-bear.jpg', 'https://outsider.com/wp-content/uploads/2020/09/grizzly-bear-ferociously-protects-kill-while-tourists-watch-yellowstone-national-park-1170x702.jpg', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/wyoming-grizzly-bear-og_0.jpg', 'https://www.wildernesscommittee.org/sites/default/files/2018-05/grizzly_bear.jpg', 'http://www.wyofile.com/wp-content/uploads/2017/11/grizzly-pic-e1509641980482.jpg', 'http://averageoutdoorsman.com/wp-content/uploads/2013/02/grizzly-bear.jpg', 'https://www.publicdomainpictures.net/pictures/90000/velka/grizzly-bear-portrait.jpg', 'https://www.vancouverobserver.com/sites/vancouverobserver.com/files/images/article/body/grouse-grizzly_n3d3306-web.jpg', 'http://www.wild-facts.com/wp-content/uploads/2012/04/Grizzlybear55.jpg', 'http://kgmi-am.sagacom.com/wp-content/blogs.dir/70/files/2014/08/grizzly-bear.jpg', 'http://www.shetzers.com/wp-content/uploads/2017/06/W-Blonde-Bear-Cub-and-Mother.jpg', 'https://i2.wp.com/jakesnatureblog.com/wp-content/uploads/2016/06/baby-brown-bear.jpg', 'https://d.newsweek.com/en/full/400233/rtr4kseg.jpg', 'https://wallup.net/wp-content/uploads/2018/10/09/125286-grizzly-bear-forest.jpg', 'https://www.columbiatribune.com/storyimage/MO/20171003/NEWS/171009653/AR/0/AR-171009653.jpg', 'https://images.wallpapersden.com/image/download/bear-grizzly-bear-eyes_amhrZ5SZmpqtpaSklGZuZ2WtZmVtZQ.jpg', 'https://wallpapertag.com/wallpaper/full/a/1/1/437141-grizzly-bear-backgrounds-1920x1080-for-lockscreen.jpg', 'https://bloximages.chicago2.vip.townnews.com/idahostatejournal.com/content/tncms/assets/v3/editorial/6/45/64588e0b-55e7-5585-bc61-819ae3630cff/59b721e2eb964.image.jpg', 'https://devdenverzoo.com/wp-content/uploads/2018/09/Grizzly-Bear_05.jpg', 'https://www.washingtonpost.com/resizer/i6X-FE4FqlbaGDzh9CanT4YU99Y=/1440x0/smart/arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/2C55XVXHAE5FTHUGC726432CYU.jpg', 'http://www.100hdwallpapers.com/wallpapers/3840x2160/two_grizzly_bears_in_water_4k-hd_wallpapers.jpg', 'https://www.uniquelynorthwest.com/wp-content/uploads/2014/12/grizzly-bear-alaska-incentive.jpg', 'https://www.conservationnw.org/wp-content/uploads/2020/03/JSW_6993-scaled.jpg', 'https://www.conservationnw.org/wp-content/uploads/2017/10/Grizzly-bear-in-mountain-meadow.-Photo-copyright-Jason-Verschoor.-iStockphoto.com_-e1507583569416.jpg', 'http://cdn.roaring.earth/wp-content/uploads/2016/03/Scratching-head-main-text.jpg', 'https://content.presspage.com/uploads/1979/1920_190416-oped-nielsen-grizzly-alex-taylor-banner-534230.jpg?10000', 'https://outsider.com/wp-content/uploads/2020/07/Grizzly-Bear.png', 'https://static01.nyt.com/images/2020/07/10/multimedia/10xp-bears-pix/10xp-bears-pix-videoSixteenByNineJumbo1600.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/grizzli-bear-cub-lyl-dil-creations.jpg', 'https://cowboystatedaily.com/wp-content/uploads/2020/05/bear-photo-not-cat-scaled.jpg', 'https://2.bp.blogspot.com/-p-QKKk3T8O0/UQN3a5YSALI/AAAAAAAAFcE/iTlNg3jee9A/s1600/Grizzly+Bear-2013-0pic-03.jpg', 'https://3.bp.blogspot.com/-_kQl-36lVL4/ThYEDrnAGBI/AAAAAAAABdM/sQkA6n28LJY/s1600/Grizzly+7+WA.jpg', 'https://fotos.perfil.com/2019/12/02/trim/1140/641/grizly-bear-811359.jpg', 'https://wildlifeimages.org/wp-content/uploads/2016/09/DSC1704.jpg', 'https://lakechelannow.com/wp-content/uploads/2019/08/Grizzly_Bear-Face-800x600.jpg', 'https://www.realtree.com/sites/default/files/content/images/open-graph/2020/cdenniswdonohue-shutterstock-griz.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-4K.jpg', 'http://2.bp.blogspot.com/-u2jNl3VZLpU/T5LoHWgdunI/AAAAAAAAFPo/DdDtaVJGTco/s1600/grizzly-bear_grizzly-bear.jpg', 'https://life.hawkeoptics.com/wp-content/uploads/2019/07/Keith-Crowley-Grizzly-Bears-03.jpg', 'https://img.theculturetrip.com/1440x807/wp-content/uploads/2018/01/carnivore-nature-wild-alaska-wildlife-grizzly-bear-861962.jpg', 'https://www.skinnymoose.com/outdoorsmorgasbord/files/2014/03/Grizzly-Bear-3.jpg', 'https://blog.humanesociety.org/wp-content/uploads/2020/07/grizzly-bear-iStock-647126690_369827.jpg', 'https://ewscripps.brightspotcdn.com/dims4/default/083faf0/2147483647/strip/true/crop/806x453+0+21/resize/1280x720!/quality/90/?url=https:%2F%2Fewscripps.brightspotcdn.com%2F00%2F7f%2F2489255d4d9ab8fc447f4b57ed66%2Fgrizzly-bear.jpg', 'https://www.lintelligencer.com/wp-content/uploads/2020/09/Alaska-Grizzly-kills-hunter-in-attack-at-national-park-Report.jpg', 'https://the-biggest.net/wp-content/uploads/2019/07/how-tall-was-the-biggest-grizzly-bear.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-for-desktop-background.jpg', 'https://www.shetzers.com/wp-content/uploads/2017/06/R-Grizzly-bear-cub-standing-tall-704x880.jpg', 'https://4.bp.blogspot.com/-7YmqPws-fF4/WhI9SBZv5dI/AAAAAAAAKik/KR02M342yKYwifxV3MgJQJYu1vhydOhFQCLcBGAs/s1600/IMG_3113.JPG', 'https://resize.hswstatic.com/w_1024/gif/grizzly-center.jpg', 'https://c.wallhere.com/photos/80/58/brown_bear_face_beautiful_background-629567.jpg!d', 'https://buckrail.com/wp-content/uploads/2018/05/Grizzly-bears-are-a-popular-attraction-for-visitors.-Jackson-Hole-EcoTour-Adventures.jpg', 'https://gephardtdaily.com/wp-content/uploads/2015/08/11754872_1109393535742666_5103459483633495575_o.jpg', 'http://www.outdoorhub.com/wp-content/uploads/sites/2/2015/08/outdoorhub-yellowstone-grizzly-bear-kills-hiker-in-apparent-predatory-attack-2015-08-12_15-18-35.jpg', 'https://www.whistler.ca/sites/default/files/styles/crop_min_663/public/2019/Apr/page/main-images/25900/grizzly.jpg?itok=NGH8XPEe', 'https://d.ibtimes.co.uk/en/full/1462246/grizzly-bear.jpg', 'https://cdn.rbth.com/all/2016/10/07/grizzly_gettyimages-dv1620005_b.jpg', 'https://www.rmef.org/wp-content/uploads/2020/05/wyoming-grizzly-bear.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/standing-grizzly-bear-cub-in-the-evening-light-daryl-l-hunter.jpg', 'https://www.wintersexpress.com/files/2019/05/animal-bear-cute-162340.jpg', 'https://weneedfun.com/wp-content/uploads/Grizzly-Bear-Picture-4.jpg', 'https://intercontinentalcry.org/wp-content/uploads/2019/08/grizzly-bear1.jpg', 'http://calgaryguardian.com/wp-content/uploads/2017/05/142_1289.jpg', 'https://cdn.abcotvs.com/dip/images/492803_012715-ss-grizzly-bears-img.jpg?w=1600', 'http://www.publicdomainpictures.net/pictures/90000/velka/grizzly-bear-profile.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-HD-Desktop.jpg', 'https://townsquare.media/site/101/files/2020/05/GrizzRS.jpg?w=1200', 'https://wallpapertag.com/wallpaper/full/e/b/8/437151-grizzly-bear-backgrounds-2560x1600-for-tablet.jpg', 'https://images.fineartamerica.com/images-medium-large-5/usa-alaska-grizzly-bear-cub-sits-brenda-tharp.jpg', 'https://external-preview.redd.it/bccjV-0WX40IgFdJ1FOy7_IE9nLPXR0wloCap7hXTSA.jpg?auto=webp&s=a17fd3832e57032a5b44af94010a189e4e906ea6', 'https://defenders.org/sites/default/files/styles/meta_image/public/2019-04/grizzly_bear_sow_jim_peaco_nps_header.jpg?itok=vTdBffpn', 'https://outsider.com/wp-content/uploads/2020/08/grizzly-bear-hiker-glacier-national-park-video.png', 'https://i.cbc.ca/1.4174820.1506608673!/fileImage/httpImage/image.jpg_gen/derivatives/16x9_1180/grizzly-glares-at-viewer.jpg', 'https://ourplnt.com/wp-content/uploads/2020/12/grizzly-bear.jpg', 'https://spca.bc.ca/wp-content/uploads/WS2017_92846_Stephen-Lustig_Grizzly-Bear.jpg', 'https://braincharm.com/wp-content/uploads/2019/08/cropped-Grizzly-Bear-Bozeman-Grizzly-Montana-Bear-3483886.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Wallpapers-HD.jpg', 'https://cdn.britannica.com/19/186719-050-887A6F2C/Grizzly-bear-Rocky-Mountains-Wyoming.jpg', 'http://www.pbs.org/wnet/nature/files/2018/07/Bear133.jpg', 'http://1.bp.blogspot.com/-CoOIzMhLQUI/UHzVx6IxyVI/AAAAAAAAA-c/RHMhnAXXniU/s1600/Grizzly+Bear+Sleepy.jpg', 'http://www.photos-public-domain.com/wp-content/uploads/2012/03/grizzly-bear.jpg', 'http://img.huffingtonpost.com/asset/1910_1000/58c0a5fe1d000027007cd5c0.jpg', 'http://blog.theclymb.com/wp-content/uploads/2014/09/angry-grizzly-bear.jpg', 'http://s3.amazonaws.com/Photo_of_the_Day/system/uploads/photo/image/12388/sized_Grizzly_Bear_Cubs.jpg', 'https://www.unilad.co.uk/wp-content/uploads/2015/08/UNILAD-mom-grizzly8.jpg', 'https://www.hoglezoo.org/wp-content/themes/hoglezoo_new/images/animal_finder/491283162GrizzlyBear.jpg', 'http://dawnsbrain.com/wp-content/uploads/2014/11/grizzly-bear-6.jpg', 'https://4.bp.blogspot.com/-2_RuGHXGm0Y/UQW3vwH37JI/AAAAAAAAEzc/Mt1seFmFrog/s1600/Grizzly-Bear-.jpg', 'https://i.ytimg.com/vi/zzJbkmOVgMI/maxresdefault.jpg', 'https://cdn.abcotvs.com/dip/images/1407687_062916-wtvd-file-grizzly-bear-img.jpg?w=1280&r=16:9', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Background-.jpg', 'https://devdenverzoo.com/wp-content/uploads/2018/09/Grizzly-Bear_04.jpg', 'http://www.montanaoutdoor.com/wp-content/uploads/2018/02/Grizzly-King-FEAT.jpg', 'https://outsider.com/wp-content/uploads/2020/06/Grizzly-Bear-Unsplash.png', 'http://www.wallpapergeeks.com/wp-content/uploads/2014/03/Grizzly-Bear-Near-McNeil-River-Alaska.jpg', 'https://outsider.com/wp-content/uploads/2020/09/two-grizzly-bears-brutally-fight-over-fish-in-alaska-1170x702.png', 'https://lh3.googleusercontent.com/proxy/XiCQkPJB2tAag16S3wO0yNJnMQXTpTzEOZw-eWQKr6nOaJhlqeJzHrDhPowuHfLQREfxRPAN9kjW-6KrwU8HUeBesZsH-nLKkYV2lr5NE5sXcRy5xwMqv9cbYYYNpEJnhJs=w1200-h630-p-k-no-nu', 'https://cottagelife.com/wp-content/uploads/2020/07/shutterstock_546562111.jpg', 'https://images.fineartamerica.com/images-medium-large-5/grizzly-bear-6-thomas-woolworth.jpg', 'https://redtri.com/wp-content/uploads/2014/07/grizzly-bear-2.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/grizzly-bear-twenty-two-north-photography.jpg', 'https://a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2019/06/931/524/iStock-grizzly-bear-and-cubs.jpg?ve=1&tl=1', 'http://www.alaskaphotoworld.com/alaska365/wp-content/uploads/2013/06/Grizzly-Bear-Yukon-Alaska-c-Laurent-Dick-Wild-Alaska-Travel(pp_w1060_h657).jpg', 'http://mediad.publicbroadcasting.net/p/kufm/files/styles/x_large/public/201712/grizzly-bear-snow_pd.jpg', 'https://i.ytimg.com/vi/jR_U9AoAtJE/maxresdefault.jpg', 'http://www.marketwire.com/library/MwGo/2017/3/7/11G132324/Images/3_Bears_Approach_by_Jim_Lawrence-4f4d16b51311853286570ff0d3022a81.jpg', 'http://2.bp.blogspot.com/-e6e4YOB-K9I/UQEz1wt72TI/AAAAAAAAC0s/YZUgPnIwTks/s640/Grizzly+bear.jpg', 'https://media.spokesman.com/photos/2018/08/18/Grizzly_Bears_Montana.JPG.jpg', 'https://s3-assets.eastidahonews.com/wp-content/uploads/2015/10/03030033/0ad8593723.jpg', 'https://www.mtpr.org/sites/kufm/files/styles/x_large/public/201512/grizzly-bear_02-PD.jpg', 'https://www.indianz.com/News/2017/01/12/grizzlybearyellowstoneitingchiang.jpg', 'https://www.alloutdoor.com/wp-content/uploads/2019/08/Grizzly_Bear_Ursus_arctos_ssp..jpg', 'https://www.getwest.ca/site/assets/files/1049/uchuck-grizzly-bear.jpg', 'http://zooidaho.org/wp-content/uploads/2016/02/shonibear.jpg', 'http://www.cs.columbia.edu/~sedwards/photos/kyle200604/20060412-7108%20Grizzly%20bear.jpg', 'https://cdn.suwalls.com/wallpapers/animals/grizzly-bear-46852-1920x1200.jpg', 'http://wildlifewaystation.org/images/content/animals/Bear-Grizzly-2.JPG', 'http://2.bp.blogspot.com/-NjMTuklENdE/UHzVv_8dIxI/AAAAAAAAA-U/tNBsQDn8kFI/s1600/Grizzly+Bear+Pic.jpg', 'https://live.staticflickr.com/8078/8277276763_b4d8cff511_b.jpg', 'http://images.fineartamerica.com/images-medium-large/1-grizzly-bear-yukon-robert-postma.jpg', 'https://www.yourhikeguide.com/wp-content/uploads/scandinavian_wildlife_park_26-26995.jpg', 'http://bloximages.newyork1.vip.townnews.com/stltoday.com/content/tncms/assets/v3/editorial/b/99/b99156d5-0ad8-5b5c-a98a-d805c9a5d6bb/57eaaca236063.image.jpg?resize=1200%2C1369', 'http://www.tweedsmuirparklodge.com/assets/Uploads/_resampled/ScaleWidthWzEyMDBd/Grizzly-bear-with-fish-at-Tweedsmuir.jpg', 'https://1471793142.rsc.cdn77.org/data/images/full/27318/palm-beach-zoo-debuts-grizzly-bear-cubs.jpg', 'https://media.salon.com/2014/03/Grizzly-Bear.jpg', 'https://d3d0lqu00lnqvz.cloudfront.net/media/media/897b2e5d-6d4c-40fa-bbe8-6829455747e2.jpg', 'https://www.trbimg.com/img-5966ab20/turbine/ct-yellowstone-grizzly-scarface-killed-20170712', 'https://www.wyomingpublicmedia.org/sites/wpr/files/styles/x_large/public/202005/grizzly_and_cubs.png', 'https://cmkt-image-prd.freetls.fastly.net/0.1.0/ps/5129718/910/1365/m2/fpnw/wm1/mrkxj2x8a849nf6stvtxuofhydmjnf7slsysexqsuhadz3xaxi6ze1soz8huzzsg-.jpg?1538388129&s=48c555641b232ab7c64b9c76b97a67bc', 'http://www.my-photo-blog.com/wp-content/uploads/2017/07/Grizzly-Bear-Nursing.jpg', 'https://i0.wp.com/pgdailynews.ca/wp-content/uploads/2017/12/Grizzly.jpg', 'https://www.naturalworldsafaris.com/~/media/images/blogs/2018/01/grizzly-bear-hunting-ban/mmr_2015-08-06_7055.jpg', 'http://i.huffpost.com/gen/1747973/images/o-GRIZZLY-BEAR-RAINFOREST-facebook.jpg', 'https://bloximages.chicago2.vip.townnews.com/trib.com/content/tncms/assets/v3/editorial/b/6c/b6c62d25-f84e-51b0-95ae-223fcd943312/57cc8795a69a1.image.jpg?resize=1200%2C800', 'https://www.readersdigest.ca/wp-content/uploads/2018/09/grizzly-bear-cub.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/grizzly-bear-judy-vincent.jpg', 'http://2.bp.blogspot.com/-9JAlJayP2XU/UQN3Wffpa8I/AAAAAAAAFbs/eMP5BcuLetc/w1200-h630-p-k-nu/Grizzly+Bear-2013-0pic-02.jpg', 'https://townsquare.media/site/99/files/2018/12/Bear.jpg?w=1200&h=0&zc=1&s=0&a=t&q=89', 'http://www.hickerphoto.com/images/1024/alert-grizzly-bear-56745.jpg', 'https://s3.amazonaws.com/images.gearjunkie.com/uploads/2015/07/Grizzly-Bear.jpg', 'https://wildlifefriendly.org/wp-content/uploads/2015/09/Grizzly.jpg', 'https://www.thefencepost.com/wp-content/uploads/sites/12/2018/08/grizzlies-tsln-081118-1240x828.jpg', 'https://www.expeditionsalaska.com/wp-content/uploads/2017/04/16-brown-bear-photos-2377.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Download.jpg', 'http://4.bp.blogspot.com/-QmwOWiR3ydM/USOj4DBjZSI/AAAAAAAAFBM/bvX4-MmH-tY/s1600/Grizzly_Bear_Info_Images+08.jpg', 'https://www.scitechnow.org/wp-content/uploads/2015/05/Ep27_Grizzly-Bears.jpg', 'https://whatthingsweigh.com/wp-content/uploads/grizzly_bear.jpg', 'https://www.huntingillustrated.com/wp-content/uploads/2014/03/Depositphotos_93855856_xl-2015-1-scaled.jpg', 'https://www.wildnatureimages.com/images/xl/080914-113-Grizzly-Bear.jpg', 'http://media1.s-nbcnews.com/j/newscms/2017_07/1905761/170217-grizzly-alaska-mn-1130_e938eae58cdfe33a8c5bd9af07356dd9.nbcnews-ux-2880-1000.jpg', 'https://i.ytimg.com/vi/-klfQbvz0Nw/maxresdefault.jpg', 'https://s1.ibtimes.com/sites/www.ibtimes.com/files/styles/embed/public/2017/11/20/grizzly-bear.jpg', 'https://www.canadatravelspecialists.com/media/3362/bear-catching-salmon.jpeg', 'https://www.legendsofamerica.com/wp-content/uploads/2018/07/Grizzlybear-269x300.png', 'https://farm8.staticflickr.com/7026/6843888709_b6136360c8_z.jpg', 'https://i.cbc.ca/1.4775731.1533917302!/fileImage/httpImage/image.jpg_gen/derivatives/original_780/eyes-of-the-grizzly.jpg', 'http://mediad.publicbroadcasting.net/p/kufm/files/styles/x_large/public/201606/grizzly_PD.jpg', 'https://i1.wp.com/backcountryjourneys.com/wp-content/uploads/2015/12/bigstock-Grizzly-Bear-9532331.jpg', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/washington-grizzly-bear-og.jpg', 'https://www.expeditionsalaska.com/wp-content/uploads/2016/09/09_SEP3502.jpg', 'https://www.shared.com/content/images/2017/08/Ordelheide_20160803-_D5A7701_bearfamily.jpg', 'https://detroitzoo.org/wp-content/uploads/2015/08/Grizzly-Boys.jpg', 'https://filson-life.imgix.net/2020/03/grizzly-bear-reintroduction-1.jpg?fit=scale&fm=pjpg&h=573&ixlib=php-1.2.1&w=860&wpsize=entry-main', 'https://cdn0.wideopenspaces.com/wp-content/uploads/2020/04/whatdogrizzlybearseat4-scaled.jpg', 'https://s3-assets.eastidahonews.com/wp-content/uploads/2019/05/06175149/grizzly-bear-860x571.jpg', 'https://cdn.s3-media.wbal.com/Media/2017/03/22/ac89e9aa-1e3f-4f88-bef3-aeba490d0059/original.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/GrizzlyBearJeanBeaufort.jpg/1200px-GrizzlyBearJeanBeaufort.jpg', 'https://gray-kmvt-prod.cdn.arcpublishing.com/resizer/XDqt0Wh9pw0QyVfKY5V7d77a44o=/1200x675/smart/cloudfront-us-east-1.images.arcpublishing.com/gray/XDDLIRAFIRMF7KDSS56OJSWEIQ.jpg', 'https://miro.medium.com/max/9504/1*1cfuPs8clxIj8f_CDH4sYw.jpeg', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/Idaho-grizzly-bear-hunt-approved-og.jpg', 'https://bloximages.chicago2.vip.townnews.com/ravallirepublic.com/content/tncms/assets/v3/editorial/9/05/90555037-3848-5130-a254-aca92323e46f/5c87f956c8b30.image.jpg?resize=1200%2C755', 'https://media.spokesman.com/photos/2017/11/30/Yellowstone_Grizzlies.JPG.jpg', 'https://www.wpxi.com/resizer/e8abHzJalKpN_CfzbIS07FZBOig=/1200x628/d1hfln2sfez66z.cloudfront.net/09-24-2020/t_82d50577f56b4e2fa00191d8f33c12fe_name_Moose_hunter_mauled__killed_by_grizzly_bear_in_Alaska_Poster.jpg', 'https://bloximages.chicago2.vip.townnews.com/helenair.com/content/tncms/assets/v3/editorial/6/45/645660ec-59b9-58a0-a7d7-213e342af246/5a02181673916.image.jpg', 'https://i2.wp.com/nypost.com/wp-content/uploads/sites/2/2020/09/grizzly-bear-89.jpg?quality=90&strip=all&ssl=1', 'https://www.grandviewoutdoors.com/uploads/images/_facebook/Grizzly.jpg', 'https://i1.wp.com/nypost.com/wp-content/uploads/sites/2/2020/05/grizzly-bear-57.jpg?quality=90&strip=all&ssl=1', 'https://images.fineartamerica.com/images-medium-large-5/grizzly-bear-with-yearling-cub-matthias-breiter.jpg', 'https://davidsuzuki.org/wp-content/uploads/2017/04/grizzly-bears-alaska-wildlife-sanctuary-whittier.jpg', 'https://www.campbellriver.travel/media/2020/02/bear-ears1.jpg', 'https://3.bp.blogspot.com/-ERmaouEbCOw/ThYPiIH9ACI/AAAAAAAAB4E/AzbPHNqSMxE/s1600/Feeling_Grizzly-1600x1200.jpg', 'http://www.famouscutouts.com/images/detailed/0/675-Bear.jpg', 'https://cdn.apartmenttherapy.info/image/upload/f_auto,q_auto:eco,c_fill,g_auto,w_1500/at/news-culture/2019-11/grizzly-bear', 'http://1.bp.blogspot.com/-wzfvrOdkAao/USOjE-6mdDI/AAAAAAAAFAg/GRopRcG-p1k/s1600/Grizzly_Bear_Info_Images+01.jpg', 'http://static.independent.co.uk/s3fs-public/thumbnails/image/2016/06/30/08/yellowstone-grizzly-bear-.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/5004711979_d7bbce11c4_o.jpg', 'https://www.tsln.com/wp-content/uploads/2018/10/bears-tsln-101318-1-1240x823.jpg', 'https://i1.wp.com/www.animalprotectionparty.ca/wp-content/uploads/2017/10/grizzly-bear-report.jpg?fit=1072%2C712&ssl=1', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/grizzly-bear-og_0.jpg', 'http://i.huffpost.com/gen/1390652/images/o-GRIZZLY-BEAR-ROAR-facebook.jpg', 'https://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-HD.jpg', 'https://thenarwhal.ca/wp-content/uploads/2018/04/BC-grizzly-bear.jpg', 'https://www.vmcdn.ca/f/files/via/import/2018/07/08143100_grizzly-bear.jpg;w=960', 'https://wyo4news.com/wp-content/uploads/2020/07/grizzly-bear.jpg', 'https://cdn.shopify.com/s/files/1/1520/0394/products/Grizzly_bear.jpg?v=1541529305', 'https://bozone.com/site/wp-content/uploads/2019/03/GRIZZLY.jpeg', 'http://www.news1130.com/wp-content/blogs.dir/sites/9/2015/09/17/iStock_000010656016_Double-e1442507974878.jpg', 'http://static-21.sinclairstoryline.com/resources/media/6078265b-815a-4172-a17f-67f94d69e53c-large16x9_ImportedfromLakana.jpg?1514431909725', 'https://4.bp.blogspot.com/-HNxOANqzIwQ/Un_rDbUxCGI/AAAAAAAABUc/CpX9FRsAdLA/s1600/Grizzly_Bear_2.jpg', 'https://cdn4.creativecirclemedia.com/powell/original/20190926-083657-Griz.jpg', 'https://tetonvalleylodge.com/wp-content/uploads/2015/04/grizzly-bear-idaho.jpg', 'https://idahobusinessreview.com/files/2018/08/grizzly-bear-scaled.jpg', 'https://cdn0.wideopenspaces.com/wp-content/uploads/2017/08/grizzly.jpg', 'https://holidayarchitects.co.uk/canada-holidays/wp-content/uploads/sites/7/Grizzly-bear-3-CRWW.jpg', 'https://imgc.allpostersimages.com/img/print/u-g-PZLADY0.jpg?w=338&h=450', 'http://1.bp.blogspot.com/-422rK0Kje1E/UQW3yELFQeI/AAAAAAAAEzs/Rcg4Gniz4rU/s1600/Grizzly-Bear-2.jpg', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/Grizzly-bear-protections-west-OG.jpg', 'https://cdnph.upi.com/sv/ph/og/upi/9261403133345/2014/1/f6484c26a086306575f2eabf184d2d11/v1.5/Conservationists-push-to-triple-Americas-grizzly-bear-population.jpg', 'https://www.the-sun.com/wp-content/uploads/sites/6/2020/09/NINTCHDBPICT000512282491.jpg?strip=all&quality=100&w=1200&h=800&crop=1', 'https://www.wyofile.com/wp-content/uploads/2013/09/blondie-the-griz2_Jackie-Skaggs_June-2011.jpg']
    Images downloaded.
black bear: Got 278 image URLs. Downloading...
   urls =  ['https://scx2.b-cdn.net/gfx/news/hires/2019/blackbear.jpg', 'https://www.seacoastonline.com/storyimage/SO/20190730/NEWS/190739992/AR/0/AR-190739992.jpg', 'https://www.theintell.com/storyimage/PA/20190610/NEWS/190619908/AR/0/AR-190619908.jpg', 'https://www.enr.gov.nt.ca/sites/enr/files/black_bear_banner.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/02/american-black-bear-2.jpg', 'https://www.gannett-cdn.com/-mm-/870435c5930644f2716846ecf8ba7354bdf4dead/c=0-414-862-901/local/-/media/2018/07/09/Westchester/Westchester/636667658911415885-Armonk-bear-3.jpg?width=3200&height=1680&fit=crop', 'https://wallup.net/wp-content/uploads/2019/09/719345-baribal-black-bear-bear-muzzle-eyes-predator.jpg', 'https://www.visitmt.com/binaries/content/gallery/MTOT/responsive/hero-f/dec-2014/blackbear_subjectmatterlandinghero.jpg', 'https://d3i3l3kraiqpym.cloudfront.net/wp-content/uploads/2016/05/26232920/12079543_847437148710024_685610861999970941_n.jpg', 'http://www.adirondackexplorer.org/wp-content/uploads/2016/12/17-Black-bear-Raquette.jpg', 'https://static1.pointsinfocus.com/2017/12/black-bears-of-revillagigedo-island-ketchikan-alaska/Fat-Bear.jpg', 'https://outsider.com/wp-content/uploads/2020/10/black-bear-encounter-how-to-prevent-survive-attack.jpg', 'https://nypost.com/wp-content/uploads/sites/2/2014/10/black-bear-cub.jpg?quality=90&strip=all&w=1200', 'http://www.stgeorgeutah.com/wp-content/uploads/2015/11/lynn_11-22-2011_black_bear_1.jpg', 'http://bearsmartdurango.org/wp-content/uploads/2013/06/100_0642.jpg', 'https://alaskashoreexcursions.com/media/ecom/prodxl/Baby-Black-Bear.JPG', 'https://www.fascinationwildlife.com/wp-content/uploads/2018/11/TON6447_DxO.jpg', 'http://www.canadianrockies.net/wp-content/uploads/2012/03/blb0389_bigblackbear.jpg', 'https://i.cbc.ca/1.3267259.1444652249!/fileImage/httpImage/image.jpg_gen/derivatives/16x9_620/black-bear.jpg', 'https://binderparkzoo.org/wp-content/uploads/2017/10/black-bear-5-1024x683.jpg', 'http://www.camelcitydispatch.com/wp-content/uploads/2013/05/black-bear-stting-face-on.jpg', 'https://www.huntingnewfoundlandlabrador.com/-/media/marquees/hunting/hunting-species/black-bear/black-bear-main-header.jpg?mh=960&mw=1280&hash=935A53A530E3F07AFB9DC4FF03D08C8B103FD601', 'https://www.gofingerlakes.org/wp-content/uploads/Ursus_americanus_Simon-Pierre-Barrette.CC-BY-SA-004-1.jpg', 'http://elelur.com/data_images/mammals/asian-black-bear/asian-black-bear-04.jpg', 'https://www.cullmantribune.com/wp-content/uploads/2019/08/Black-bear-mother-and-cubs-1024x835.jpg', 'http://3.bp.blogspot.com/_1N1pZVb1_C4/TUCDSYH2XuI/AAAAAAAABU4/N-8QVjyjpiA/s1600/bear_black.jpg', 'http://naturalbridgezoo.com/wp-content/uploads/2017/05/BB1-1.jpg', 'https://live.staticflickr.com/3223/2762479597_07c4b2f908_b.jpg', 'http://grizzlybearwatching.com/wp-content/uploads/2017/03/BlackBear1-1500x998.jpg', 'https://en.reepark.dk/media/1613/_n011753.jpg?width=1024', 'https://render.fineartamerica.com/images/rendered/default/print/8.000/7.000/break/images/artworkimages/medium/2/bear-stare-jerry-lofaro.jpg', 'http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1522685410/black-bear-BEAR418.jpg?itok=qmoXuFRx', 'https://exploreoakridge.com/wp-content/uploads/2019/10/American-Black-Bear-photo-by-Chris-Norcott.jpg', 'https://cottagelife.com/wp-content/uploads/2017/07/shutterstock_503744119.jpg', 'https://i0.wp.com/imagineourflorida.org/wp-content/uploads/2016/09/Florida_Black_Bear_square_Kon_Studio.jpg?fit=800%2C800&ssl=1', 'https://2.bp.blogspot.com/-bMCoR7-d0SU/TmstJPh8sOI/AAAAAAAAAPo/4J5H-Mm2qEM/s1600/black-bear-wallpaper-11-755660.jpg', 'http://infotel.ca/news/medialibrary/image/orig-mediaitemid22167-3862.jpg', 'http://www.sott.net/image/s13/261520/full/Black_bear.jpg', 'https://downeast.com/wp-content/uploads/2014/02/Bear4.jpg', 'https://visitadirondacks.com/sites/default/files/images/adirondack-black-bear.jpg', 'https://www.vpr.org/sites/vpr/files/styles/x_large/public/202007/black-bear-fish-wildlife-dept-Tom-Rogers-2013.png', 'http://www.mountaincountrycabinrentals.com/wp-content/uploads/2014/10/Closeup-of-a-black-bear.jpg', 'https://nypost.com/wp-content/uploads/sites/2/2020/08/black-bear-81.jpg?quality=90&strip=all&w=1200', 'https://bear.org/wp-content/uploads/2008/02/20130607_Bow-copy.jpg', 'https://st.focusedcollection.com/14026668/i/1800/focused_181901682-stock-photo-american-black-bear-ursus-americanus.jpg', 'https://www.gulflive.com/resizer/XxMMGfEvOQQAVOEKfieknpQOIv8=/1280x0/smart/advancelocal-adapter-image-uploads.s3.amazonaws.com/image.gulflive.com/home/gulf-media/width2048/img/news_impact/photo/bears-threatened-fbdfb579fa67c166.jpg', 'https://wildlife.utah.gov/news_photos/11-18-19_black-bear-in-grass.jpg', 'https://www.piecebypiece.ca/wp-content/uploads/2018/05/165-Black-Bear-Cub.jpg', 'https://www.hdwallpapers.in/download/black_bear_hd_animals-HD.jpg', 'https://www.greatswamp.org/wp-content/uploads/2016/01/black-bear.jpg', 'http://getwallpapers.com/wallpaper/full/a/8/4/1452654-wallpaper-black-bear-pictures-2560x1600-for-4k.jpg', 'https://beartrackoutfitters.com/wp-content/uploads/2018/09/bear.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/02/american-black-bear-smell.jpg', 'http://upload.wikimedia.org/wikipedia/commons/1/18/Black_Bear_7.jpg', 'http://snowbrains.com/wp-content/uploads/2015/08/American-Black-Bear-jpg.jpg', 'https://vignette.wikia.nocookie.net/parody/images/5/5a/11775722235_19bb44c6e7_b.jpg/revision/latest?cb=20161008190514', 'https://www.wildlifecenter.org/sites/default/files/patient_images/DSCN2925.JPG', 'https://media-cdn.wehco.com/img/photos/2020/12/11/blackbearnoppers06cmykoriginal1483610356_t1070_h5b279ad6a60bb8d6261090d0367921ea9d3e0cc8.jpg', 'https://www.gannett-cdn.com/-mm-/ba9293e2ff57f90c5ea896bbb7fb36126f38dbc9/c=0-134-3468-2093/local/-/media/2016/06/28/CNYGroup/Elmira/636027114617396897-black-bear.jpg?width=3200&height=1680&fit=crop', 'https://bearexpert.com/wp-content/uploads/2019/10/american-black-bear.jpg', 'https://cdn2.outdoorphotographer.com/gallery/7522/leahed_100307_589944_290c530a74_28f7de1b95-leahed-dsc_9774.jpg', 'https://onncg8dr7k-flywheel.netdna-ssl.com/wp-content/uploads/2020/10/GettyImages-1195788516.jpg', 'https://www.gannett-cdn.com/-mm-/cd26c8713e884a8483a3eb4efd7e9b47f526e46d/c=0-264-4620-2863/local/-/media/Bridgewater/2015/01/05/B9315749966Z.1_20150105153333_000_GUS9JJHMR.1-0.jpg?width=3200&height=1680&fit=crop', 'https://www.mlive.com/resizer/Klh1maBtQ2BXs9MJ79W4mVfcL8k=/1280x0/smart/advancelocal-adapter-image-uploads.s3.amazonaws.com/image.mlive.com/home/mlive-media/width2048/img/outdoors_impact/photo/black-bears-new-exhibit-at-binder-park-zoo-07-a88a34fbe0690fea.jpg', 'https://texnat.tamu.edu/files/2018/07/IMG_1573.jpg', 'https://photos1.blogger.com/blogger/5712/2802/1600/DSC01502.jpg', 'https://bloximages.newyork1.vip.townnews.com/pilotonline.com/content/tncms/assets/v3/editorial/e/d5/ed52c12b-4e68-5024-a886-f0fc147bf94e/563d906095138.image.jpg', 'https://img1-azrcdn.newser.com/image/1255962-12-20190904134100.jpeg', 'https://www.gravel.org/wp-content/uploads/2015/10/Black-Bear.jpg', 'https://3.bp.blogspot.com/-nFt10gfJrzM/UDKIjeW21LI/AAAAAAAADfA/uomt-SjA-Sc/s1600/IMG_5635.jpg', 'https://www.crozetgazette.com/wp-content/uploads/2018/06/iStock-153562011.jpg', 'https://images.thestar.com/-S-RKsYxNaEcCX0LRaqhNeLpMAs=/1200x932/smart/filters:cb(1565190981014)/https://www.thestar.com/content/dam/thestar/vancouver/2019/08/06/child-injured-by-bear-bite-at-greater-vancouver-zoo-officials-say/blackbear.jpg', 'https://socalguidedhunts.com/wp-content/uploads/2013/09/guided-black-bear-hunts.jpg', 'https://seniors101.ca/wp-content/uploads/2015/08/Black-bear-cub.jpg', 'https://www.koamnewsnow.com/content/uploads/2020/05/pixabay-black-bear.jpg', 'https://www.gannett-cdn.com/-mm-/40756a948d5a0edcc7ce6d0e16b133540995c9c8/c=0-138-1997-1266/local/-/media/2017/03/06/JacksonMS/JacksonMS/636243918786987260-0307bear01.jpg?width=3200&height=1680&fit=crop', 'https://www.wallpaperflare.com/static/805/363/779/bear-black-bear-forest-black-wallpaper.jpg', 'https://img.huffingtonpost.com/asset/5d71a56a2500007a1205d0b4.jpeg?cache=i3bilfyydx&ops=1778_1000', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/one-big-black-bear-john-rowe.jpg', 'http://i.huffpost.com/gen/1105509/images/o-MARYLAND-BLACK-BEAR-facebook.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/02/black-bear.jpg', 'https://blog.humanesociety.org/wp-content/uploads/2016/06/BLACK-BEAR-ISTOCK_000046186676_288801-1220x813.jpg', 'https://aldf.org/wp-content/uploads/2018/05/blackbear-121274561-16x9.jpg', 'https://www.timberon.org/wp-content/uploads/Young_black_bear.jpg', 'https://www.outsideonline.com/sites/default/files/styles/img_600x600/public/2019/06/28/black-bear-grazes_s.jpg?itok=ZaqoTZKg', 'https://a-z-animals.com/media/Asiatic-black-bear.jpg', 'https://www.worldlandtrust.org/wp-content/uploads/2019/07/youung-black-bear.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/bear-family-timothy-flanigan.jpg', 'https://nhpbs.org/wild/images/blackbearusfw2.jpg', 'https://a57.foxnews.com/a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2018/09/640/320/1862/1048/Black20Bear20iStock.jpg?ve=1&tl=1?ve=1&tl=1', 'https://www.gannett-cdn.com/presto/2019/05/08/PASH/9be97cd2-c85d-4eb6-a503-68b17b29ce94-NC_Bear_study_NO27_resting_after_work_up.jpg?crop=639,359,x0,y23&width=3200&height=1680&fit=bounds', 'https://abdnews.org/wp-content/uploads/2017/04/Authorities-Rescue-Black-Bear-Cubs-After-Mother-Hit-By-Truck.jpg', 'http://blog.healthywildlife.ca/wp-content/uploads/2016/07/black-bear-hg-1-of-1.jpg', 'https://www.vmcdn.ca/f/files/via/import/2019/10/01132841_black-bear.jpg;w=700;h=347;mode=crop', 'https://advancelocal-adapter-image-uploads.s3.amazonaws.com/image.pennlive.com/home/penn-media/width2048/img/pa-sportsman/photo/23770296-standard.jpg', 'http://wildlife.org/wp-content/uploads/2015/01/Florida-Blackbear-via-Flickr-credit-Rich-Turner-940.jpg', 'http://snowbrains.com/wp-content/uploads/2015/08/connie-lemperle-black-bear-web.jpg', 'https://detroit.cbslocal.com/wp-content/uploads/sites/15909782/2015/05/black-bear-forest.jpg?w=1500', 'https://www.gannett-cdn.com/-mm-/4f6c5cb86bdd5d6da988b998c98d5233eb4312d0/c=0-250-4921-3030/local/-/media/2017/12/30/Tallahassee/Tallahassee/636502285489982021-Black-Bears.jpg?width=3200&height=1680&fit=crop', 'https://www.poconorecord.com/storyimage/PR/20161114/NEWS/161119826/AR/0/AR-161119826.jpg', 'https://floridablackbearscenicbyway.org/wp-content/uploads/2020/02/2411420-1920x1080-DesktopNexus.com_.jpg', 'http://www.wildernesscollege.com/images/blkbr-face.jpg', 'https://www.trbimg.com/img-557b8359/turbine/ct-indiana-black-bear-20150612', 'http://wiseaboutbears.org/wp-content/uploads/2014/04/img034.jpg', 'https://img.thrfun.com/img/214/282/bears_tx3.jpg', 'http://www.sciencebuzz.org/sites/default/files/images/Black_bear.jpg', 'http://i.huffpost.com/gen/1669786/images/o-BLACK-BEAR-CANADA-facebook.jpg', 'https://images.wallpaperscraft.com/image/bear_black_bear_forest_113045_800x1200.jpg', 'https://storage.googleapis.com/afs-prod/media/media:c1a9aa6b7050465d83977305953495aa/3000.jpeg', 'https://www.wildnatureimages.com/images/xl/140729-016-Black-Bear-at-Anan-Wildlife-Observatory.jpg', 'https://brevardzoo.org/wp-content/uploads/2019/05/bearsidebar.jpg', 'https://canadaintherough.com/wp-content/uploads/2020/06/blackbear.jpg', 'https://i1.wp.com/albertawild.com/wp-content/uploads/2017/02/bear-1698.jpg?fit=1698%2C1132', 'http://mediad.publicbroadcasting.net/p/upr/files/styles/x_large/public/201612/black-bear.jpg', 'http://www.encyclopediaofalabama.org/images/m-9033.jpg', 'http://bloximages.newyork1.vip.townnews.com/pilotonline.com/content/tncms/assets/v3/editorial/8/47/84743c51-975b-555f-9f30-1234790e97a8/563d905fe2c27.image.jpg?resize=1200%2C852', 'http://bearlegend.com/wp-content/uploads/2012/04/2641024724_d12e4afcb9_o-361.jpg', 'https://images.dailyhive.com/20190916163711/North-Shore-Black-Bear-Society.jpg', 'https://s29027.pcdn.co/wp-content/uploads/2019/08/Black-Bear-mating-Tekiela-TEK1811.jpg', 'https://someinterestingfacts.net/wp-content/uploads/2016/07/American-black-bear.jpg', 'https://media.gettyimages.com/photos/black-bear-standing-in-rocky-stream-british-columbia-canada-picture-id974154560', 'http://www.toledoblade.com/image/2013/01/21/black-bear.JPG', 'https://www.thehuntingpage.com/wp-content/uploads/sites/2/2014/01/o-BLACK-BEAR-facebook.jpg', 'https://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1572373216/black-bear-tennessee-BEARLODGE1019.jpg?itok=jO4eurBD', 'http://4.bp.blogspot.com/-YS1MBBAHGPs/TjkTZIS8WGI/AAAAAAAABi0/alssUhDH3Pk/s1600/Baby+black+bears+3.jpg', 'https://www.transylvaniatimes.com/home/cms_data/dfault/photos/stories/id/9/1/41091/s_top', 'https://www.wsls.com/resizer/fmqqOp8dNrEIIDxQOlsBSRip5KY=/1600x1333/smart/filters:format(jpeg):strip_exif(true):strip_icc(true):no_upscale(true):quality(65)/arc-anglerfish-arc2-prod-gmg.s3.amazonaws.com/public/3VGIKDGQDNAY5I4NO7KP4ZX5N4.jpg', 'https://mediad.publicbroadcasting.net/p/wvtf/files/styles/x_large/public/201607/AP_608334627084.jpg', 'https://gothunts.com/wp-content/uploads/2019/06/Black-Bear-scaled.jpeg', 'http://www.visitmysmokies.com/wp-content/uploads/2013/08/Black-Bear-Cub.jpg', 'https://cdn.audleytravel.com/-/-/79/160033025091139069040022125202060038124167252088.jpg', 'http://2.bp.blogspot.com/-oL5wK6KYI7M/UBji1ikDXXI/AAAAAAAAAMY/jmXUpXVGet4/s1600/Black-Bear-05.jpg', 'https://www.montanaoutdoor.com/wp-content/uploads/2018/05/bear6.jpg', 'http://2.bp.blogspot.com/_1N1pZVb1_C4/TUCDn6EEngI/AAAAAAAABVA/h6BmG9TLADA/s1600/Black+Bear+159087.jpg', 'https://www.oregonlive.com/resizer/nSifR1unuKEMPswzQtebZRI_03g=/600x0/arc-anglerfish-arc2-prod-advancelocal.s3.amazonaws.com/public/6GF72P3X25EXVLEKGSGVXUJRKI.png', 'http://someinterestingfacts.net/wp-content/uploads/2016/07/Black-bear.jpg', 'https://www.remotelands.com/travelogues/app/uploads/2019/07/AsiaWire-BearReturn-2.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/7-maine-black-bear-sharon-fiedler.jpg', 'https://d.ibtimes.co.uk/en/full/1611947/black-bear.jpg', 'https://animalhype.com/wp-content/uploads/2019/12/How-Fast-Do-Black-Bears-Run-1024x682.jpg', 'http://uffmannatureimages.net/wp-content/uploads/2012/07/20120519-_Q2C6428Black-Bears-save-for-web-cs6.jpg', 'https://www.gannett-cdn.com/presto/2019/01/02/PASH/bff6ff36-67d2-44be-bdda-0d5e29cda4e1-Black_bear_Ken_Taylor.jpg?crop=1796,1007,x0,y0&width=3200&height=1680&fit=bounds', 'https://wildlife.org/wp-content/uploads/2015/07/Black-Bear-Reintroduction-Image-1-600.jpg', 'https://imgc.allpostersimages.com/img/print/posters/barrett-hedges-a-large-black-bear-ursus-americanus-moves-quickly-over-the-rocks_a-L-13725186-4990831.jpg', 'http://www.wildernessrealty.com/wp-content/uploads/2015/02/Maine-Black-Bear.jpg', 'http://cdn0.wideopenspaces.com/wp-content/uploads/2014/10/bigstock-Black-bear-cub-62558696.jpg', 'http://archery360.com/wp-content/uploads/2014/06/black-bear-cub.jpg', 'https://www.wideopenspaces.com/wp-content/uploads/2020/09/wisconsinblackbear.jpg', 'https://cdn.extra.ie/wp-content/uploads/2018/05/15124559/Black-bear-1.jpg', 'https://2.bp.blogspot.com/-oMj7lKf-Lb8/UMJTf68A6SI/AAAAAAAABfw/L91dawds-Nk/s1600/black-bear-photos-2012+03.jpg', 'https://i.cbc.ca/1.4130464.1495668985!/fileImage/httpImage/image.jpg_gen/derivatives/16x9_780/black-bear.jpg', 'http://content.wzzm13.com/photo/2015/10/09/635799990906985114-ThinkstockPhotos-178500326_40932_ver1.0.jpg', 'http://www.my-photo-blog.com/wp-content/uploads/2014/07/Black-Bear-Cub.jpg', 'https://www.massaudubon.org/var/ezdemo_site/storage/images/media/departments/lww/mammals/images/black-bear/black-bear-standing-up-next-to-tree-c-karen-karlberg-500vert/196541-4-eng-US/black-bear-standing-up-next-to-tree-c-karen-karlberg-500vert_blockitemmedium.jpg', 'https://www.grandviewoutdoors.com/uploads/images/BlackBearUSFWS_181004_111434.jpg', 'https://mediad.publicbroadcasting.net/p/wusf/files/styles/x_large/public/201504/black_bear_closeup_gary_langley.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/6-american-black-bear-cub-david-kenny.jpg', 'https://www.coniferousforest.com/wp-content/uploads/2016/08/American-Black-Bear.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/10/asiatic-black-bear-5.jpg', 'https://www.gannett-cdn.com/presto/2019/12/30/PKNS/e7f38dd1-a441-4cc3-a48a-1df9445344de-KNS-Zoo_Knoxville_Bears_BP_3.JPG?width=540&height=&fit=bounds&auto=webp', 'https://miro.medium.com/max/6014/1*sGT7ccEjG6KF0-UjdsJ3nQ.jpeg', 'https://wildlife.org/wp-content/uploads/2017/03/blackbear.png', 'https://bearexpert.com/wp-content/uploads/2020/08/american-black-bear-6.jpg', 'http://cdn0.wideopenspaces.com/wp-content/uploads/2015/12/BlackBearMass.jpg', 'http://dnr.maryland.gov/wildlife/PublishingImages/Maryland-Bear-Stamp-Winning-Artwork-19-20.jpg', 'http://www.thestar.com/content/dam/thestar/news/gta/2015/06/01/black-bear-cornered-in-newmarket-backyard/bear-file.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/2015-08-07_Black_Bear_near_Stewart%2C_BC_1090.jpg/1280px-2015-08-07_Black_Bear_near_Stewart%2C_BC_1090.jpg', 'http://www.adirondackalmanack.com/wp-content/uploads/2017/05/blackbear.jpg', 'https://www.coniferousforest.com/wp-content/uploads/2016/08/American-Black-Bear-Cubs.jpg', 'http://strangesounds.org/wp-content/uploads/2014/08/bear-cubs-photo.jpg', 'https://bear.org/wp-content/uploads/2017/10/Serious-Bear.jpg', 'https://thelaurelofasheville.com/wp-content/uploads/2016/06/Carolina_Nature_Photographers_Association-Black_Bear-6.jpg', 'https://yukon.ca/sites/yukon.ca/files/env/env-imgs/env-black-bear_0.jpg', 'https://www.victoriabuzz.com/wp-content/uploads/2019/08/black-bear-unsplash.jpg', 'https://media.spokesman.com/photos/2017/07/04/BLACK_BEAR.JPG.jpg', 'http://yesofcorsa.com/wp-content/uploads/2018/10/American-Black-Bear-Best-Wallpaper-1024x753.jpg', 'https://wallup.net/wp-content/uploads/2019/09/300546-minnesota-black-bear.jpg', 'https://cdn1.theweek.co.uk/sites/theweek/files/2016/10/161026_black_bear.jpg', 'https://bloximages.newyork1.vip.townnews.com/swnewsmedia.com/content/tncms/assets/v3/editorial/7/1a/71a9b7ca-1825-5fdd-9534-5a7c30ac10c8/5d444ec6ac2d5.image.jpg', 'https://quincy-network.s3.ca-central-1.amazonaws.com/wp-content/uploads/sites/37/2020/06/MGN_1280x960_70623P00-TYOFD-860x645.jpg', 'http://www.karelianbeardog.us/photos/outdoor/blackbears/Black%20bear.jpg', 'https://brevardzoo.org/wp-content/uploads/2019/05/bearmain.gif', 'http://justfunfacts.com/wp-content/uploads/2017/10/asiatic-black-bear-2.jpg', 'http://ovlc.org/wp-content/uploads/2014/07/black_bear.jpg', 'https://cdn0.wideopenspaces.com/wp-content/uploads/2017/02/black-bear-feature.jpg', 'https://nhpbs.org/wild/images/blackbearusfw1.jpg', 'http://uffmannatureimages.net/wp-content/uploads/2012/07/20120518-KQ2C5610Black_Bears-Edit-692x1024.jpg', 'https://www.raincoast.org/wp-content/uploads/2019/12/black-bear-diane-bb-v1.1.jpg', 'https://gohunt-assets-us-west-2.s3.amazonaws.com/media/Large-Montana-black-bear.jpg', 'http://www.hww.ca/kaboom/images/Mammals/Black-Bear/Black-Bear-David-Cracknell.jpg', 'http://2.bp.blogspot.com/-yc7X_uO7iMM/UA7U_q5_NtI/AAAAAAAAAqI/72EaqVglzPM/s1600/black-bear.jpg', 'https://d.newsweek.com/en/full/1092319/black-bear.jpg', 'https://www.grandviewoutdoors.com/uploads/images/_facebook/Black-Bear-Sweepstakes-beauty.jpg', 'http://3.bp.blogspot.com/-vwLa2VwfXg4/Ul6IwR1sIJI/AAAAAAAABYw/INnD5gQH-dM/s1600/Black-Bear.jpg', 'http://wyrz.org/wp-content/uploads/2016/07/black_bear.jpg', 'https://www.gannett-cdn.com/-mm-/98e9eb0055148d8550b3a7d264f19fd6462e6062/c=0-213-4288-2636/local/-/media/2017/03/22/CarolinaGroup/Asheville/636257813777562324-Black-bear-Getty-images.jpg?width=3200&height=1680&fit=crop', 'https://floridawildlifefederation.org/wp-content/uploads/2020/04/black-bear-scaled-e1598566846480.jpg', 'https://www.stgeorgeutah.com/wp-content/uploads/2015/01/lynn_11-22-2011_black_bear_6-1.jpg', 'https://bento.cdn.pbs.org/hostedbento-prod/gallery/20170713_222850_315402black-bear-01-gettyimages-155467721.jpg.1280x720_q85.jpg', 'https://dehayf5mhw1h7.cloudfront.net/wp-content/uploads/sites/301/2020/06/26101300/bear-illinois-dnr.jpg', 'http://2.bp.blogspot.com/-MKgsmEatcvk/UKuf-CiWdFI/AAAAAAAACQU/VPvHckGI7DA/s1600/Black_bear_large.jpg', 'https://kanati.com/wp-content/uploads/black-bear-life-size-mount_poseBB109-feature.png', 'https://compote.slate.com/images/0a308e2c-48e2-476a-ad72-61c20aa92709.jpg', 'https://external-preview.redd.it/ieT9RTLmwvN4hfQBYix4109CLYniR3fSUAeT6jg6kO0.jpg?auto=webp&s=4f2ec1a52f7c1f5b16238503377981645c54ae27', 'http://wallpapersdsc.net/wp-content/uploads/2017/10/Black-Bear-Background.jpg', 'https://www.gannett-cdn.com/-mm-/2b06f05d8e31b4112ec2ecbca6e9ef0db0cf4d0b/c=0-108-2125-1309/local/-/media/2016/05/18/Wilmington/Wilmington/635991906230694281-black-bear.jpg?width=3200&height=1680&fit=crop', 'https://i.ytimg.com/vi/-2cj5pPe778/maxresdefault.jpg', 'https://www.outdoorrevival.com/wp-content/uploads/2017/04/american-black-bear.jpg', 'https://www.pennlive.com/resizer/zKZdn6cmyoUATjOlNY4O_OEWRTI=/1280x0/smart/advancelocal-adapter-image-uploads.s3.amazonaws.com/image.pennlive.com/home/penn-media/width2048/img/pa-sportsman/photo/black-bear-8789aeebd5c67247.jpg', 'http://www.hww.ca/kaboom/images/Mammals/Black-Bear/Black-Bear-Elliott-Fotherby.jpg', 'http://www.hdnicewallpapers.com/Walls/Big/Bear/Animal_Black_Bear_Wallpaper.jpg', 'https://biologydictionary.net/wp-content/uploads/2020/09/shutterstock_1452356795-1.jpg', 'http://theazaleainnbb.com/wp-content/uploads/2018/07/black-bear.jpg', 'https://wallpapercave.com/wp/wp3102474.jpg', 'https://www.simplemost.com/wp-content/uploads/2019/06/AdobeStock_122783246.jpeg', 'https://onekindplanet.org/wp-content/uploads/2017/09/blackbear.jpg', 'https://images.fineartamerica.com/images-medium-large-5/baby-black-bear-jack-nevitt.jpg', 'http://www.wildlifecenter.org/sites/default/files/patient_images/Black_Bear/DSCN6285.JPG', 'https://audubonnatureinstitute.org/images/600x450/zoo/black-bear-600.jpg', 'https://bear.org/wp-content/uploads/2008/01/Bear-by-lake-looking.jpg', 'https://cottagelife.com/wp-content/uploads/2019/02/Cottage-Life-Black-Bear-cub.jpg', 'https://images.axios.com/WACso7e7dbhjup_i7iVfclq3BdI=/0x322:4288x2734/1920x1080/2018/05/22/1527002317479.jpg', 'https://northdakotawildlifefederation.org/wp-content/uploads/2020/02/black-bear.jpg', 'https://a57.foxnews.com/a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2018/11/640/320/1862/1048/louisiana-black-bear.jpg?ve=1&tl=1?ve=1&tl=1', 'https://www.montanaoutdoor.com/wp-content/uploads/2015/05/bears_desktop_2700x1809_hd-wallpaper-832781.jpg', 'https://www.stgeorgeutah.com/wp-content/uploads/2017/01/blackbear.jpg', 'https://fthmb.tqn.com/OUfFPaVmyC8KJnjYhPqr-aq-Sy4=/4896x3264/filters:fill(auto,1)/tired-black-bear-cub-537071213-59bda526845b340011591e7f.jpg', 'https://cdn2.outdoorphotographer.com/gallery/7522/thegagglephotog@gmail.com_102543_647246_d57a3bd609_1c886941c4-thegagglephotoggmail-com-baby-black-bear-standing-fb.jpg', 'http://images.dailyhive.com/20160826144034/black-bear-e1475078273412.jpg', 'http://animalia.bio/uploads/animals/photos/full/1.25x1/YCezTGiM824aJwMLQgqJ.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/10/asiatic-black-bear-eating.jpg', 'https://images.fineartamerica.com/images-medium-large-5/standing-bear-timothy-flanigan.jpg', 'https://www.trbimg.com/img-58eeaeb6/turbine/os-black-bear-dead-sanford-criminal-20170412', 'https://winchestersun.com/wp-content/uploads/sites/44/2020/06/A1-062320-Black-bear-e1593005720644.jpg', 'https://i0.wp.com/taiwan-scene.com/wp-content/uploads/2019/06/taiwan-hualien-formosan-black-bear.jpg?fit=870%2C654&ssl=1', 'https://www.gannett-cdn.com/presto/2018/11/04/PGRF/7c692a2a-f1d1-4f07-8181-6bfc90c42b2c-AP18305732083625.jpg?crop=1999,1144,x0,y211&width=3200&height=1680&fit=bounds', 'http://i.huffpost.com/gen/1190583/thumbs/o-BLACK-BEAR-facebook.jpg', 'http://1.bp.blogspot.com/-Jc2_QqcDTz4/UBjix7v9rjI/AAAAAAAAAMA/Hr67qTiw_I4/s1600/Black-Bear-02.jpg', 'http://mediad.publicbroadcasting.net/p/nhpr/files/styles/x_large/public/201303/April26girlapril30.jpg', 'http://mediad.publicbroadcasting.net/p/nhpr/files/201303/April25onlog.jpg', 'http://www.exploreclarion.com/wp-content/uploads/2018/11/black-bear.png', 'https://external-preview.redd.it/ny2anVEkjB6eTawYfAN6KcZ7TUyH69buX9Vhm56cds8.jpg?auto=webp&s=500e3a52f7e797eff002fcd91f0f3cf62ff1f401', 'https://img.huffingtonpost.com/asset/594a9e981700001f001021dc.jpeg?ops=1910_1000', 'https://site-547756.mozfiles.com/files/547756/medium/Black_Bear3.jpg', 'https://images.fineartamerica.com/images-medium-large/pa-black-bear-portrait-bill-maile-.jpg', 'https://www.oceanlight.com/stock-photo/ursus-americanus-black-bear-image-18790-351363.jpg', 'https://www.gannett-cdn.com/-mm-/a5076e7a43a0cec6129489319d0fb728e2cd1814/c=0-264-5184-3193/local/-/media/2017/02/15/DetroitFreePress/DetroitFreePress/636227474186999380-GettyImages-497031868.jpg?width=3200&height=1680&fit=crop', 'https://3.bp.blogspot.com/-T62hFwmwBSg/TmstHyDCjWI/AAAAAAAAAAg/hZSRZIqaEpE/s1600/black-bear-backgrounds-4-751651.jpg', 'https://media.news4jax.com/photo/2015/11/06/Black-bear-blurb-jpg_308110_ver1.0_1280_720.jpg', 'https://downeast.com/wp-content/uploads/2014/02/Bear9.jpg', 'http://www.minnesotaseasons.com/Mammals/Large/American_black_bear_03.jpg', 'https://animals.net/wp-content/uploads/2018/12/Black-Bear-2.jpg', 'https://i.cbc.ca/1.4656566.1525955305!/cpImage/httpImage/image.jpg_gen/derivatives/16x9_780/alberta-black-bear.jpg', 'http://www.hdnicewallpapers.com/Walls/Big/Bear/Animal_Black_Bear_in_Forest.jpg', 'http://2.bp.blogspot.com/-NU71JkUr_MU/UNois4w5TnI/AAAAAAAABK4/ul6piYtCZUU/s1600/christmas+bear.JPG', 'http://www.trbimg.com/img-52bde9f3/turbine/os-blackbearhabitat9910-jpg-20131227/2000/2000x1182', 'http://elelur.com/data_images/mammals/asian-black-bear/asian-black-bear-05.jpg', 'https://globaljusticeecology.org/wp-content/uploads/black-bear-937037_1920-980x460.png', 'http://s1.ibtimes.com/sites/www.ibtimes.com/files/styles/embed/public/2017/05/16/black-bears-19723071920.jpg', 'http://www.nwtrek.org/wp-content/uploads/2017/12/KBYG-black-bear.jpg', 'https://wildlifefriendly.org/wp-content/uploads/2015/09/Black-bear-Full.jpg', 'https://www.sott.net/image/s13/270500/full/Black_bear_looks_angry_Shutter.jpg', 'http://wiseaboutbears.org/wp-content/uploads/2014/06/bearmaleJune-222012D80_2149-copy.jpg', 'https://www.columbiatribune.com/storyimage/MO/20200519/NEWS/200519240/AR/0/AR-200519240.jpg', 'https://cdn2.bigcommerce.com/server100/6ca92/product_images/uploaded_images/florida-black-bear.jpg?t=1430242234', 'http://explorenorth.com/library/nature/images/brown_black_bear-tutshi_lake-0709.jpg', 'https://cdn.shopify.com/s/files/1/1603/1175/products/American_Black_Bear_Cub_3265_2048x.jpg?v=1523665570', 'http://getwallpapers.com/wallpaper/full/a/0/7/1303653-black-bear-desktop-wallpaper-1920x1080-for-meizu.jpg', 'https://farm6.staticflickr.com/5588/15080045718_78c2561e6e_b.jpg', 'https://www.nps.gov/shen/learn/nature/images/BlackBear_6.jpg?maxwidth=1200&autorotate=false', 'https://www.gannett-cdn.com/-mm-/a5076e7a43a0cec6129489319d0fb728e2cd1814/c=0-264-5184-3193/local/-/media/2017/06/23/PAGroup/YorkDailyRecord/636338199651309082-GettyImages-482466374.jpg?width=3200&height=1680&fit=crop', 'http://www.reportingday.com/wp-content/uploads/2018/07/Black-Bear-Cute-Pictures-1.png', 'https://upload.wikimedia.org/wikipedia/commons/9/9c/%22Cinnamon%22_Black_Bear.jpg', 'https://media.timesfreepress.com/img/photos/2014/11/09/111014b04_black_bears_t800_h8b150168072b2d9c018ca481200aa3e621674f4c.JPG', 'https://www.realtree.com/sites/default/files/styles/site_large/public/content/inserts/642px-blackbearatwoburnsafaripark.jpg', 'http://adwimages.co.uk/Blog/wp-content/uploads/2016/11/adult-black-bear.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/black-bear-cub-in-a-tree-ursula-salzmann.jpg', 'https://cdn.vox-cdn.com/thumbor/SNyMBO6T0qwheM_sxxaWIKDDPAc=/0x0:5000x3313/1200x800/filters:focal(1435x1342:2235x2142)/cdn.vox-cdn.com/uploads/chorus_image/image/57942213/GettyImages_187133529.0.jpg']
    Images downloaded.
teddy bear: Got 283 image URLs. Downloading...
   urls =  ['https://i2-prod.birminghammail.co.uk/incoming/article17583074.ece/ALTERNATES/s1227b/0_IFP_BEM_160120bears011JPG.jpg', 'http://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-14271861817446_1200x.jpg?v=1584804420', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/970/6782/Life_size_72in_black_teddy_bear_Juju_Cuddles_is_Tall_dark_and_adorable__95688.1463592717.jpg?c=2', 'https://www.sugardelites.com/assets/images/teddybearlarge.jpg', 'http://www.photos-public-domain.com/wp-content/uploads/2010/12/white_teddy_bear.jpg', 'http://wallsdesk.com/wp-content/uploads/2016/11/Lonely-Teddy-Bear.jpg', 'http://www.fitzulas.com/Merchant4c/graphics/00000001/2014/gund-4040161-xl.jpg', 'https://backdropscanada.ca/wp-content/uploads/2015/01/p-8282-Teddy_Bear_Blue_TL_800302__02186.jpg', 'https://cdn0.rubylane.com/shops/chippewalakeantiques/863.1L.jpg', 'https://cdn.shopify.com/s/files/1/2975/6858/products/tan-big-bear-9_1200x1200.jpg?v=1524446749', 'https://cdn.notonthehighstreet.com/system/product_images/images/001/708/113/original_standing-teddy-bears.jpg', 'https://n2.sdlcdn.com/imgs/i/6/4/X-Atom-Teddy-Bears-Couple-SDL372766290-2-658f9.jpeg', 'https://www.sippycupmom.com/wp-content/uploads/2017/09/NTBD-Exclusive-Bear.jpg', 'http://theawesomedaily.com/wp-content/uploads/2017/10/teddy-bear-images-10-2.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Photos.png', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/883/8957/48in_green_teddy_bear_Big_smile_and_soft_cuddly_bear__93790.1493921224.jpg?c=2', 'https://hips.hearstapps.com/vader-prod.s3.amazonaws.com/1543338622-costco-giant-teddy-bear-1543338603.jpg?crop=1xw:1xh;center,top', 'http://icecreamoffpaperplates.com/wp-content/uploads/2016/08/teddy-bears.jpg', 'http://cdn.shopify.com/s/files/1/2610/9472/products/BL1985_BIRKIN_TEDDY_BEAR_01_1024x1024.jpg?v=1554885535', 'https://d3n8a8pro7vhmx.cloudfront.net/accessalliance/pages/454/meta_images/original/Cute_but_Lonely_Teddy_Bear_Sitting_on_Grass.jpg?1591840883', 'https://www.super99.in/media/catalog/product/cache/1/image/1800x/040ec09b1e35df139433887a97daa66f/0/1/01_22.jpg', 'https://static1.momsimage.com/wordpress/wp-content/uploads/2020/03/2020-03-30_110127.jpg', 'https://www.stuffedwithplushtoys.com/assets/full/27-1704436.jpg?20200129144114', 'https://www.celebrationgiftware.com.au/media/catalog/product/cache/cb58f11d039ee9c021f50f3360077761/t/s/tshirtbear-yellow-birthdetails.jpg', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-8100-14243358867558_1024x1024.jpg?v=1588363171', 'https://cdn.notonthehighstreet.com/fs/34/8c/3995-961a-4318-86e4-a5ee78006bf4/original_personalised-teddy-bear-gift.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG114.png', 'http://edicollector.com/wp-content/uploads/2017/10/Krampus-Teddy-Bear-Edicollector-02.jpg', 'https://img.pngio.com/teddy-bear-png-baby-teddy-bear-png-1456_1456.png', 'https://cdn1.tedsby.com/tb/large/storage/1/3/0/13062/cute-handmade-teddy-bear-miniature-ooak-artist.jpg', 'https://cdn0.rubylane.com/_pod/item/1068141/0001459/1910s-Antique-Steiff-Tiny-Teddy-Bear-pic-1-2048-50-f.jpg', 'https://cdn0.rubylane.com/_pod/item/1494839/RL446/Large-Ideal-Antique-Teddy-Bear-full-2o-2048-14-r-ffffff-9f9b7e.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG104.png', 'https://startknitting.org/wp-content/uploads/2018/03/Teddy-Bear-Free-Knitting-Pattern-f.jpg', 'https://cdn.notonthehighstreet.com/fs/7a/fe/5bda-9f46-403c-9d21-c798fa5de5ed/original_personalised-siblings-teddy-bears.jpg', 'https://cdn1.tedsby.com/tb/large/storage/1/3/9/139462/cute-handmade-teddy-bear-oliver.jpg', 'http://www.listofdownload.com/wp-content/uploads/2017/02/red-roses-cute-teddy-bear-picture-background-wallpaper.jpg', 'https://i5.walmartimages.com/asr/5d5541cd-d9cc-4b47-b7de-ebc107b74d82_1.9f14f307142cfd730852e875760b90bc.jpeg', 'https://townsquare.media/site/43/files/2020/08/Teddy-Bear.jpg?w=1200&h=0&zc=1&s=0&a=t&q=89', 'https://alqurumresort.com/img/vermont-teddy-bear-valentines-day.jpg', 'https://api.time.com/wp-content/uploads/2016/12/teddy-bear.jpg', 'https://www.kentishembroidery.co.uk/133/mm01-cuddly-mumbles-bracken-teddy-bear-989.jpg', 'http://www.fitzulas.com/Merchant4c/graphics/00000001/2012/gund-015418-xl.jpg', 'http://oglasi.ovh/images/2020/02/15/84/pomeranian-boo-white-teddy-bear_4.jpg', 'https://perpetualpreschool.com/wp-content/uploads/2017/12/32221695_ml.jpg', 'https://cdn.shopify.com/s/files/1/0270/2981/products/Bear_Plush_Pink_Color_with_Bow_25_cm_1024x1024.jpg?v=1527556065', 'https://www.myflowergift.com/media/catalog/product/cache/1/image/1200x1200/9df78eab33525d08d6e5fb8d27136e95/s/n/snow_white..3_feet_height_white_teddy_bear_4000.jpg', 'http://www.photos-public-domain.com/wp-content/uploads/2011/01/teddy-bear-wearing-mardi-gras-beads.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Wallpaper.jpg', 'https://3.bp.blogspot.com/-MF361EGT_e4/TrwxX4LsSMI/AAAAAAAAEP8/xKbu4DUVjEI/s1600/little+bear.JPG', 'https://www.petlandracine.com/wp-content/uploads/2019/05/1386238_800.jpg', 'https://2.bp.blogspot.com/-v-DbNHEhI0c/UkK6mc0ImdI/AAAAAAAAAB8/4rdaS87Bxqo/s1600/Giant+Teddy+Halloween+2013+Photo+Blog+4.jpg', 'https://cdn.shopify.com/s/files/1/0798/6397/products/eddie_cover_no_logo.jpg?v=1535766533', 'https://www.kidswholesaleclothing.co.uk/4599-thickbox_default/soft-toys-for-personalisation-teddy-bear-grey.jpg', 'https://cdn.thisiswhyimbroke.com/images/ebonics-romantic-bear.jpg', 'https://www.worldofbears.com/acatalog/Oxford%2010in_WEB.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG5.png', 'https://cdn.notonthehighstreet.com/system/product_images/images/001/941/993/original_baby-teddy-bear-dress-up-costume.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG14.png', 'https://www.zookini.com.au/media/catalog/product/cache/8/image/5beb446e055b53f6b95ea24807ed418d/w/a/waffle-cute-teddy-bear-beige-01.jpg', 'https://teddy.ph/wp-content/uploads/2019/08/feature-30.jpg', 'http://www.sippycupmom.com/wp-content/uploads/2017/09/Teddy-Bear.jpg', 'https://cdn.jewishboston.com/uploads/2019/01/iStock-932242472.jpg', 'https://newcastlebeach.org/images/teddy-bear-with-heart-4.jpg', 'https://1.bp.blogspot.com/_GSA-YfDFMXc/TDhlS3NvdcI/AAAAAAAAAb8/m0-7WTaPif8/s1600/DSC_1528.JPG', 'https://teddybears.co.uk/wp-content/uploads/2019/02/uk-2019-2736x3648.jpg', 'https://cdn.thewhoot.com/wp-content/uploads/2018/06/Knitted-Teddy-Bear-Patterns-1.jpg', 'https://cdn0.rubylane.com/shops/1147637/bk727.1L.jpg', 'https://cdn0.rubylane.com/_pod/item/737200/266/Vintage-Steiff-Original-Teddy-Bear-1950x7827s-full-3o-2048-eea9d521-f.jpg', 'https://cdn-r.fishpond.com/0037/184/777/237402416/6.jpeg', 'https://cdn.notonthehighstreet.com/fs/0e/9b/9f39-5053-4edd-a7b8-33a6780fb100/original_personalised-1st-christmas-teddy-bear.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG24.png', 'https://www.teddybearaustralia.com.au/wp-content/uploads/2018/09/FullSizeRender-6.jpg', 'https://worldofbears.com/acatalog/Steiff-022098.jpg', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/883/8961/4ft_green_teddy_bear_Big_smile_and_soft_cuddly_bear__85725.1493921227.jpg?c=2', 'https://thegadgetflow.com/wp-content/uploads/2013/01/Personalised-Teddy-Bear-In-A-Gift-Tin-4.jpg', 'https://www.steiffteddybears.co.uk/img/products/027710.png', 'https://image.made-in-china.com/2f0j00wqhfEUTWOVbo/Factary-Price-Giant-Teddy-Bear-Big-Plush-Teddy-Bear-Soft-Toys-for-Girls.jpg', 'https://cdn0.rubylane.com/_pod/item/1491561/Peacockx20Bear/Peacock-Stores-Teddy-Bear-foot-label-pic-1A-2048:10.10-27-f.jpg', 'https://n3.sdlcdn.com/imgs/g/u/e/Ultra-Blue-Teddy-Bear-12-SDL607416536-2-76098.jpeg', 'https://1.bp.blogspot.com/-ChPUEPH4ssg/T-nXRUqH6zI/AAAAAAAAAWY/7HEyWdQvV_E/s1600/teddy+bear+picnic+(7).JPG', 'https://s2.r29static.com/bin/entry/a85/0,0,2000,1050/x,80/1655673/image.jpg', 'https://app.skufetch.com/images.tmp/Gemmy_8_5_ft_Animate_294606_109_0_res.jpg', 'https://www.stuffedwithplushtoys.com/assets/full/u4040131.jpg', 'https://www.bloomsonly.com/media/catalog/product/cache/2/image/1200x1200/9df78eab33525d08d6e5fb8d27136e95/m/e/meng-meng-cute-brown-teddy-bear-head-tilt-twisting-holiday-gifts-velvet-ribbon-light-brown-bear.jpg', 'https://www.desicomments.com/dc3/02/215657/215657.jpg', 'https://www.teddybearacademy.net/wp-content/uploads/2014/08/Marco-full.jpg', 'https://st4.depositphotos.com/4431055/22853/i/950/depositphotos_228530988-stock-photo-cute-teddy-bears-hugging-back.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG45.png', 'http://images.antiquesatlas.com/dealer-stock-images/sladesantiques/Vintage_Small_9_Steiff_Mohair__as527a528z.jpg', 'https://www.worldofbears.com/acatalog/LG01.jpg', 'https://teddybears.co.uk/wp-content/uploads/2017/08/075797-2736x3648.jpg', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/882/8984/6ft_Green_lucky_teddy_Life_Size_bear_cuddly_Giant_and_biggest_cuddles_bear__64679.1493929106.jpg?c=2', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG11.png', 'http://www.pavlovspuppets.com/p/teddy3.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Pictures.jpg', 'https://www.taylorherring.com/wp-content/uploads/2015/04/GiantTeddyBears-6.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Wallpapers.jpg', 'https://primopromo.com.au/assets/Uploads/Teddy-Bear+Dark-Green-v3.jpg', 'https://cdn0.rubylane.com/shops/700632/I-1277.1L.jpg', 'https://cdn1.bigcommerce.com/server1900/dee9d/images/stencil/700x900/products/970/6782/Life_size_72in_black_teddy_bear_Juju_Cuddles_is_Tall_dark_and_adorable__95688.1463592717.jpg?c=2', 'https://n1.sdlcdn.com/imgs/i/7/9/Teddy-bear-SDL324941391-1-f3e93.png', 'https://1.bp.blogspot.com/-eTFJzsbT2Pg/T3qHL_rPBMI/AAAAAAAABsA/5J8eXBe2d9A/s1600/Teddy+Bear+Wallpapers.jpg', 'http://geekologie.com/2014/11/10/teddy-bear-dog-2.jpg', 'https://plazarealestate.com.au/wp-content/uploads/sites/131/2016/07/Teddy-Bears.jpg', 'https://cdn0.rubylane.com/_pod/item/1491561/1920sx20Germanx20bear/lovely-German-1920s-teddy-bear--full-2-2048-93.jpg', 'https://cdn.geekwire.com/wp-content/uploads/2017/11/Pasted-image-at-2017_11_15-02_22-PM.png', 'https://twinsmom.com/wp-content/uploads/2020/04/GUND-Slumbers-Teddy-Bear-Stuffed-Animal-Plush-Brown-1722.jpg', 'https://teddybears.co.uk/wp-content/uploads/2019/02/1903-2736x3648.jpg', 'https://dayz.gamepedia.com/media/dayz.gamepedia.com/e/e5/TeddyBearBrown.png', 'https://www.melijoe.com/images/142519/open_graph.jpg', 'http://3.bp.blogspot.com/-oU6bGNvicT0/UjH80jmsEfI/AAAAAAAAFTA/H6gDxpq_rE4/s1600/teddy_bear_wallpapers+%2814%29.jpg', 'https://i.ebayimg.com/images/i/221655718748-0-1/s-l1000.jpg', 'https://www.worldofbears.com/acatalog/steiff-ean-001765.jpg', 'https://winkgo.com/wp-content/uploads/2016/09/Proud-Grandpa-Buys-Giant-Teddy-Bear-His-Granddaughter-05.jpg', 'https://s-i.huffpost.com/gen/1995262/images/o-TEDDY-BEAR-COOKIES-facebook.jpg', 'https://n3.sdlcdn.com/imgs/i/h/h/Mable-Cuddle-Big-Teddy-Bear-SDL312942984-1-7984b.jpeg', 'https://i5.walmartimages.com/asr/dfe8f0fc-3ada-4f4d-b641-4e86af319c30_1.2c587767c675128916b101e1563dc5b2.jpeg', 'https://n4.sdlcdn.com/imgs/a/0/r/Toysaa-Brown-Jumbo-Teddy-SDL441635333-1-f7f59.jpg', 'https://www.worldofbears.com/acatalog/steiff-ean-012037.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Background.jpg', 'https://www.tteengift.com/wp-content/uploads/2014/12/Small-Classic-Teddy-Bear-G01.jpg', 'https://www.dhresource.com/0x0s/f2-albu-g3-M01-AF-81-rBVaHFoewiyATnxuAAPEbt5vzFw787.jpg/63-160-cm-white-giant-teddy-bear-big-huge.jpg', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-105-14241810382950_1200x.jpg?v=1584732813', 'https://www.jarrold.co.uk/userdata/root/images/products/steiff/teddy-bear-replica-1909.jpg', 'https://usercontent2.hubstatic.com/14656219_f1024.jpg', 'https://www.worldofbears.com/acatalog/steiff-113673.jpg', 'https://4.bp.blogspot.com/_OTdJvruSaiI/Swtbm1GvbWI/AAAAAAAAFdo/WXyAoA_1Qco/s1600/IMG_3609.JPG', 'http://cdn.shopify.com/s/files/1/0748/9103/products/steiff_10_1920_brass_000713-37_grande.jpeg?v=1426675827', 'https://giftteens.com/wp-content/uploads/2018/01/PPST0063.jpg', 'https://theawesomedaily.com/wp-content/uploads/2017/10/teddy-bear-images-17-1.jpg', 'http://www.worldofbears.com/acatalog/gund-4056332.jpg', 'http://4.bp.blogspot.com/_ED0zYuiiwBg/TBke2UCnvHI/AAAAAAAACJU/AfQyXaz-NJI/s1600/aetna+bear.jpg', 'https://www.buildabear.co.uk/on/demandware.static/-/Sites-buildabear-master/default/dw82e7e236/25239x.jpg', 'https://www.lifejackets.com/wp-content/uploads/2020/03/229-EBbearblueribboneverych-1.png', 'http://www.funandcash.com/ebay/teddy-white-small-X-1-A.jpg', 'https://3.bp.blogspot.com/_ED0zYuiiwBg/TLNvSWhZTCI/AAAAAAAACrM/1zXtB07mYlY/s1600/teddy+bears+halloween+closeup+2.JPG', 'https://jooinn.com/images/teddy-bear-48.jpg', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-106-14242280636518_1200x.jpg?v=1597087632', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-8101-14241651163238_1200x.jpg?v=1584559861', 'https://www.carouselshop.co.uk/media/catalog/product/cache/9/image/2000x/040ec09b1e35df139433887a97daa66f/B/F/BFFC8662B837D2AA8409CA13393604F6.jpg', 'https://www.worldofbears.com/acatalog/Steiff-237010.jpg', 'https://cdn3.volusion.com/9nxdj.fchy5/v/vspfiles/photos/BB-1486-2.jpg?v-cache=1520605016', 'https://cdn.shopify.com/s/files/1/3045/2256/products/GRID-1_76173971-e87f-42ab-bebd-e5f0db3fe386_600x900.png?v=1594136153', 'https://www.grabhub.co.uk/wp-content/uploads/2020/06/Big-Cuddly-Teddy-Bear-4.jpg', 'https://cdn.shopify.com/s/files/1/1417/0972/products/4056786-high-res_1024x1024.jpg?v=1548786811', 'https://lovemydoodles.com/wp-content/uploads/2020/04/IMG_0006-18.jpg', 'https://i5.walmartimages.com/asr/ab097d42-d89e-4db0-a7df-6d87b08de9ab_1.0e68fae386f2fafcb61bfbd7fc12828e.jpeg', 'https://www.worldofbears.com/acatalog/steiff-000867.jpg', 'http://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-110-14235158642790_1200x.jpg?v=1584033067', 'https://www.braillehouse.org.au/wp-content/uploads/2018/12/Hand-knitted-teddy-bear-3.jpg', 'https://3.bp.blogspot.com/-vmp7XBf08eM/Tca-_DqLdeI/AAAAAAAACGQ/cJa3P2VUpT4/s1600/%252362+Teddy+bears+picnic.JPG', 'https://image.made-in-china.com/2f0j00NdUtRvuYOLbp/2017-New-Design-Scary-Teddy-Bear-Zombie-Bear-Undead-Bear-Bloody-Bear-Horrible-Teddy-Bear.jpg', 'https://i5.walmartimages.com/asr/aa216a37-5af0-447c-a7eb-515b94be2bc7.c9ca3e8b8902b7cac68d5a93587a5598.jpeg', 'https://teddybears.co.uk/wp-content/uploads/2015/09/petsy-28cm.jpg', 'http://www.iheartteacups.com/wp-content/uploads/2017/05/IMG_3852.jpg', 'https://cdn.notonthehighstreet.com/fs/93/ed/2016-9d7d-4c2d-bc12-e84f66c2e4d5/original_valentine-s-day-bertie-bear.jpg', 'https://4.bp.blogspot.com/-003EDE5jTOw/UDSwjEWC7oI/AAAAAAAADuQ/lA_x_5ECQAo/s1600/teddy-bear-14-hqwallpapers4u.co.cc.jpg', 'https://3.bp.blogspot.com/_ED0zYuiiwBg/TTeCTVngmNI/AAAAAAAADF0/b6ySiPXTVlA/s1600/sleep+eyed+bear.JPG', 'http://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-108-14241933426790_1200x.jpg?v=1588417411', 'http://www.fitzulas.com/Merchant4c/graphics/00000001/2016/4056250.jpg', 'https://www.sayitwithbears.co.uk/wp-content/uploads/2016/07/large-personalised-bonnie-honey-scaled.jpg', 'https://webimg.secondhandapp.com/1.1/5e30189ab33a15626007900b', 'https://www.buildabear.com/on/demandware.static/-/Sites-buildabear-master/default/dwa68008ec/26614x.jpg', 'http://www.worldofbears.com/acatalog/mxmas-2017-1.jpg', 'https://hdwallpaperfun.com/wp-content/uploads/2015/10/Cute-Teddy-Bear-Wallpaper.jpg', 'https://teddybears.co.uk/wp-content/uploads/2015/05/MT-new-2017-cropped.jpg', 'https://cdn0.rubylane.com/shops/1539575/0025.1L.jpg', 'http://cdn.shopify.com/s/files/1/2429/7549/products/Edgar-_4_634bce55-3448-4579-adaf-349cc2dfe069_1200x1200.jpg?v=1557254763', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-14271863685222_1024x1024.jpg?v=1586794398', 'https://2.bp.blogspot.com/_ED0zYuiiwBg/S_FJV5v3f4I/AAAAAAAAB5o/7GGX-bleb1Y/s1600/bears.JPG', 'https://cdn11.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/59/9448/Huge_Blue_Teddy_Bear_Happy_Cuddles_38in__38354.1495128960.jpg?c=2', 'http://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-103-14241683964006_1200x.jpg?v=1585177055', 'https://n1.sdlcdn.com/imgs/g/x/j/ToyHub-3-Feet-Super-Soft-SDL448496021-1-7dfe5.jpeg', 'https://cdn-image.realsimple.com/sites/default/files/styles/portrait_435x518/public/1510689111/giant-teddy-bear.jpg?itok=JRUEDZtP', 'https://www.buildabear.com/dw/image/v2/BBNG_PRD/on/demandware.static/-/Sites-buildabear-master/default/dw78d308a2/28167x.jpg?sw=600&sh=600&sm=fit&q=70', 'https://www.pcsoutdoors.com/images/products/detail/BeaverBearLgFront.jpg', 'https://www.buildabear.co.uk/on/demandware.static/-/Sites-buildabear-master/default/dwdac83d3c/24663x.jpg', 'http://allpicts.in/wp-content/uploads/2015/10/Cute-Teddy-bear-with-Pink-roses.jpg', 'https://www.thetoyshoppe.com/images/productslarge/57457d.jpg', 'https://www.stuffedwithplushtoys.com/assets/full/17223-RD.jpg?20201103131623', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG97.png', 'https://teddybears.co.uk/wp-content/uploads/2017/08/113413_113420_113437_web-2736x3648.jpg', 'https://4.bp.blogspot.com/-fGtPzuETOho/T3qG6e61h9I/AAAAAAAABro/z8CWfoVG3bQ/s1600/Teddy+Bear+Wallpapers+2.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG94.png', 'http://cdn.shopify.com/s/files/1/0704/5013/products/teddy-bears-gund-maxie-bear-beige-24-1_1024x1024.jpg?v=1544388291', 'http://c.shld.net/rpx/i/s/i/spin/image/spin_prod_1099814412??hei=64&wid=64&qlt=50', 'https://cdn0.rubylane.com/shops/1754162/118.1L.jpg', 'http://www.photos-public-domain.com/wp-content/uploads/2010/12/cream_colored_teddy_bear.jpg', 'https://cdn0.rubylane.com/_pod/item/737200/266/Vintage-Steiff-Original-Teddy-Bear-1950x7827s-full-1o-2048-fe3a394d-f.png', 'https://cdn0.rubylane.com/_pod/item/1491561/Peacockx20Bear/Peacock-Stores-Teddy-Bear-foot-label-full-3o-2048-94-r-ffffff-d8cbbb.jpg', 'https://cdn0.rubylane.com/shops/your-favorite-doll/156.1L.jpg', 'https://954puppies.com/wp-content/uploads/2019/07/DSC03401-1.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG7.png', 'https://www.pets4homes.co.uk/images/classifieds/2018/08/16/2028360/large/stunning-toy-cavapoo-teddy-bear-puppies-pra-clear-5b9d3f067fc36.jpg', 'https://n1.sdlcdn.com/imgs/f/g/5/Lovely-Multicolour-Small-Teddy-Bear-SDL963778079-1-36406.jpg', 'https://www.buildabear.com/on/demandware.static/-/Sites-buildabear-master/default/dwbebeadb4/24925x.jpg', 'http://cdn.shopify.com/s/files/1/2679/2566/articles/19424368_161403274403867_6843080688077087305_n_1200x1200.jpg?v=1563408993', 'https://www.luxurypuppies2u.com/wp-content/uploads/2020/01/bibi-teddy-bear-dog-2.jpg', 'https://cdn.notonthehighstreet.com/fs/d7/0c/8e2c-5db7-4ecf-8fec-9dd401de329a/original_personalised-new-baby-teddy-bear.jpg', 'https://www.worldofbears.com/acatalog/steiff-403286.jpg', 'https://www.worldofbears.com/acatalog/steiff-026812a.jpg', 'https://www.pioupiou-et-merveilles.fr/3237-large_default/giant-teddy-bear-gaston-beige-100cm.jpg', 'https://d3h6k4kfl8m9p0.cloudfront.net/stories/qjGrIHo8t0ha-j8tgE3tdQ.jpeg', 'https://rare.us/wp-content/uploads/sites/3/2017/07/teddy-bears.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG90.png', 'http://1.bp.blogspot.com/_ED0zYuiiwBg/TCoGLyKhVNI/AAAAAAAACMU/Sk2m6kJtwQ0/s1600/odd+bear+seated.jpg', 'https://www.henrymayo.com/images/content-images/Teddy-Bear.jpg', 'https://www.onlinedelivery.in/images/thumbnails/1202/1165/detailed/29/8522.jpg', 'https://usercontent2.hubstatic.com/14720461_f1024.jpg', 'https://www.buildabear.com/on/demandware.static/-/Sites-buildabear-master/default/dw2b81d8a7/28502x.jpg', 'https://cdn.shopify.com/s/files/1/2622/2548/products/wholesale-plush-in-a-rush-112-14235212972134_1200x.jpg?v=1584034090', 'https://www.hicodenver.com/wp-content/uploads/2020/05/Teddy-Bear-with-Heart.png', 'http://1.bp.blogspot.com/-z6yWAofGr14/T3FujYmUuTI/AAAAAAAABZ8/LY-pQFofo0o/s1600/Christmas+Teddy+Bear+Wallpapers+4.jpg', 'https://www.worldofbears.com/acatalog/steiff-113666.jpg', 'http://traditionsjewishgifts.com/media/brown-hanukkah-plush-teddy-bear-RLTYTEDDY2.jpg', 'https://www.school-bears.co.uk/wp-content/uploads/2018/01/personalised-teddy-bear-anne.jpg', 'https://www.worldofbears.com/acatalog/ean-034978.jpg', 'https://sc01.alicdn.com/kf/HTB17fixJ1uSBuNjy1Xcq6AYjFXaJ.jpg', 'https://api.time.com/wp-content/uploads/2019/12/Teddy-Bear.jpg?quality=85&w=1200&h=628&crop=1', 'http://weneedfun.com/wp-content/uploads/2015/10/Cute-Teddy-Bear-Wallpaper-4.jpg', 'https://1.bp.blogspot.com/-gdpjJWS07gg/UkK6oVmYZXI/AAAAAAAAACY/RZu_k0a2hl4/s1600/Giant+Teddy+Halloween+2013+photo+blog+1.jpg', 'https://pixfeeds.com/images/baby-care/baby-gifts/1280-622057492-white-teddy-bear.jpg', 'https://purepng.com/public/uploads/large/two-teddy-bear-in-basket-qyo.png', 'https://3.bp.blogspot.com/_ED0zYuiiwBg/S_FJynlFrhI/AAAAAAAAB5w/ioeinkP5Ri0/s1600/bears+profile.JPG', 'https://cdn11.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/143/1173/Sweetie-Pie-Big-Love-hazelnut-brown-teddy-bear-30in__97755.1542992358.jpg?c=2', 'https://d3h6k4kfl8m9p0.cloudfront.net/stories/B4LWqbQh48c0ultSGdJraA.jpeg', 'https://cdn.notonthehighstreet.com/system/product_images/images/001/942/001/original_baby-teddy-bear-dress-up-costume.jpg', 'http://cdn.shopify.com/s/files/1/0748/9103/products/jellycat_18_bumbly_bears_grande.jpg?v=1530708337', 'https://www.taylorherring.com/wp-content/uploads/2015/04/GiantTeddyBears-5-781x1020.jpg', 'http://1.bp.blogspot.com/-4Z91ISiPs94/UYzX1S-xB9I/AAAAAAAAE7c/MGsL0bQ2EpY/s1600/Coco-L-Cuddles-white-teddy-bear-with-necklace-26in__23715.1327379838.1280.1280.jpg', 'https://theeducationhub.org.nz/wp-content/uploads/2020/03/Teddy-bear-hunt-scaled.jpg', 'https://cdn.suwalls.com/wallpapers/photography/happy-teddy-bear-family-under-the-snowy-pine-tree-51847-1920x1080.jpg', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/875/6201/Adorable_Brown_Bears_Set_Of_three_Gift_Huge_Family_Giant_Teddy__55363.1436899340.jpg?c=2', 'https://teddybearsandfriends.com/wp-content/uploads/2013/12/Teddy-Bear-Miniature-US-Special-1984-97-98-1_.jpg', 'https://usercontent2.hubstatic.com/11954973_f260.jpg', 'https://wallsdesk.com/wp-content/uploads/2016/11/Teddy-Bear-Wallpapers-HD.jpg', 'http://www.fitzulas.com/Merchant4c/graphics/00000001/2017/173902.jpg', 'https://4.bp.blogspot.com/-m23FOuiaNfM/Vlcb1HUm1-I/AAAAAAAABi4/NAAxnfHZ7kY/s1600/IMG_2519.JPG', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/1280x1280/products/354/2910/Sweetie-Tubs-mocha-brown-teddy-bear-65in_3__58929.1323818431.jpg?c=2', 'https://oneofakindantiques.com/r.php?itemNumber=5261&imageNumber=1&w=1500&type=regular', 'https://teddybears.co.uk/wp-content/uploads/2017/09/113321.jpg', 'https://i.ebayimg.com/00/s/MTAyNFg3Njg=/z/pnMAAOSw7eld87sL/$_86.JPG', 'https://www.megelles.com/assets/full/MEG-frankie.jpg?20191212083854', 'https://www.fromme.co.zw/uploads/2020/08/Teddy-FM_.jpg', 'http://2.bp.blogspot.com/-uBj8qN2NvdM/T3FurKV2-WI/AAAAAAAABaE/04CNHLuEczw/s1600/Christmas+Teddy+Bear+Wallpapers.jpg', 'https://bestdolltoys.com/wp-content/uploads/2020/03/51YOoVe9H1L.jpg', 'https://cdn.thewhoot.com/wp-content/uploads/2018/06/Knitted-Teddy-Bear-Patterns--550x896.jpg', 'https://cdn0.rubylane.com/_pod/item/1130386/Bear1/Adorable-antique-Jointed-Mohair-Teddy-Bear-full-4o-2048-a9115339-r-ffffff-f68787.jpg', 'https://i.ebayimg.com/00/s/MTAyNFg4OTk=/z/pwYAAOSw4VRc6XRa/$_86.JPG', 'http://2.bp.blogspot.com/_ED0zYuiiwBg/TTd9rrMJswI/AAAAAAAADFc/i3w3l43MeGg/s1600/aetna.JPG', 'https://s32071.pcdn.co/wp-content/uploads/ep0195_1.jpg', 'https://craftsmumship.com/wp-content/uploads/2013/04/Kina-dolls-size-knitted-cardigan.jpg', 'https://www.professorshouse.com/wp-content/uploads/2019/08/teddy-bear-names.jpg', 'http://mylitter.com/wp-content/uploads/2019/02/bear.jpg', 'https://petvblog.com/wp-content/uploads/2020/06/teddy-bear-puppies-1024x683.jpg', 'https://www.worldofbears.com/acatalog/steiff-ean-000997.jpg', 'https://3.bp.blogspot.com/_ED0zYuiiwBg/TLNuVSd7t_I/AAAAAAAACrE/AXkCJv9QF9Y/s1600/teddy+bears+halloween.JPG', 'http://cdn.shopify.com/s/files/1/2975/6858/products/large-tan-bear_1200x1200.jpg?v=1524446719', 'http://www.familytree.com/wp-content/uploads/2017/09/National-Teddy-Bear-Day-Find-more-genealogy-blogs-at-FamilyTree.com_.jpg', 'https://s3.amazonaws.com/hoover.church.of.christ/wp-content/uploads/2020/08/teddy-bear-sick-in-the-hospital.jpg', 'https://www.stuffedwithplushtoys.com/assets/full/9750-30.jpg?20180212090824', 'https://www.worldofbears.com/acatalog/steiff-ean-111556.jpg', 'https://cdn8.bigcommerce.com/s-dee9d/images/stencil/700x900/products/883/8961/4ft_green_teddy_bear_Big_smile_and_soft_cuddly_bear__85725.1493921227.jpg?c=2', 'https://www.carouselshop.co.uk/media/catalog/product/cache/9/image/2000x/040ec09b1e35df139433887a97daa66f/E/E/EE49104AE5D9AFA3C9C246D2F4F81F73.jpg', 'https://www.elizabeths.co.uk/upload/mt/eliz126/products/lg_null-teddy-bears---grey.jpg', 'https://www.sayitwithbears.co.uk/wp-content/uploads/2017/05/SN0778-925x1024.jpg', 'https://www.grabhub.co.uk/wp-content/uploads/2020/07/Peek-a-Boo-Teddy-Bear-1.jpg', 'https://images.wallpaperscraft.com/image/soft_toy_teddy_bear_bear_117110_938x1668.jpg', 'https://www.snappysnaps.co.uk/media/catalog/product/cache/62/image/650x650/9df78eab33525d08d6e5fb8d27136e95/s/n/snappy_teddy_bear.png', 'https://petclassifieds.com/wp-content/uploads/2020/10/20201003_153234.jpg', 'https://images.wallpaperscraft.com/image/toy_teddy_bear_teddy_31381_800x1420.jpg', 'https://www.curiousmondo.com/images/2017/11/02/teddy-bear-making-04.jpg', 'https://www.worldofbears.com/acatalog/LCG01.jpg', 'https://image.made-in-china.com/2f0j00iOetbZrDhvzU/Mini-Baby-Teddy-Bear-Keychain-with-Shirt.jpg', 'https://pngimg.com/uploads/teddy_bear/teddy_bear_PNG106.png', 'https://www.thunderbay.ca/en/recreation/resources/Thunder-Bay_recreation_interior_0032_Events---teddy-bears-picnic.jpg', 'http://1.bp.blogspot.com/-NosZBJngTnA/Tp_rt11Z4PI/AAAAAAAAAJg/GNVaaoWmesg/s1600/Teddy+bear+detail1.JPG', 'https://www.carouselshop.co.uk/media/catalog/product/cache/9/image/2000x/040ec09b1e35df139433887a97daa66f/D/1/D10F255704F7CE7FBEB25963EFF90A74.jpg', 'https://www.scarymommy.com/wp-content/uploads/2017/11/screen-shot-2017-11-13-at-8-39-00-am.png', 'https://3.bp.blogspot.com/-JB9Xu_dEIxA/UkLLT_b-TXI/AAAAAAAAADM/2UnCl1TvU8g/s1600/Giant+Teddy+Fall+2013+photo+blog+3.jpg', 'https://s34468.pcdn.co/wp-content/uploads/2019/09/DSC07821-scaled.jpg', 'https://cdn0.rubylane.com/shops/738907/T722.1L.jpg?63']
    Images downloaded.
After checking images for issues, 714 (total) images remain.
files has 714 elements.
files[0] = scraped_images/grizzly/00000143.jpg, files[-1] = scraped_images/teddy/00000109.png2.png

And a routine that will parse filename to get the class label, and another that will list all the files in a certain class

def label_func(file_path):
    return file_path.parents[0].name  # just get the name of the parent directory for the file

print(f"label_func yields {set([label_func(fp) for fp in files])}")  # sanity check: do we get all the labels we expect?

lbl2files = {l: [f for f in files if label_func(f) == l] for l in labels}
label_func yields {'teddy', 'grizzly', 'black'}
class SiameseImage(fastuple):
    def show(self, ctx=None, **kwargs): 
        if len(self) > 2:
            img1,img2,similarity = self
        else:
            img1,img2 = self
            similarity = 'Undetermined'
        if not isinstance(img1, Tensor):
            if img2.size != img1.size: img2 = img2.resize(img1.size)
            t1,t2 = tensor(img1),tensor(img2)
            t1,t2 = t1.permute(2,0,1),t2.permute(2,0,1)
        else: t1,t2 = img1,img2
        line = t1.new_zeros(t1.shape[0], t1.shape[1], 10)
        return show_image(torch.cat([t1,line,t2], dim=2), title=similarity, ctx=ctx, **kwargs)
class SiameseTransform(Transform):
    def __init__(self, files, splits):
        self.splbl2files = [{l: [f for f in files[splits[i]] if label_func(f) == l] for l in labels}
                          for i in range(2)]
        self.valid = {f: self._draw(f,1) for f in files[splits[1]]}
    def encodes(self, f):
        f2,same = self.valid.get(f, self._draw(f,0))
        img1,img2 = PILImage.create(f),PILImage.create(f2)
        return SiameseImage(img1, img2, int(same))
    
    def _draw(self, f, split=0):
        same = random.random() < 0.4
        cls = label_func(f)
        if not same: cls = random.choice(L(l for l in labels if l != cls)) 
        return random.choice(self.splbl2files[split][cls]),same

splits = RandomSplitter()(files)
tfm = SiameseTransform(files, splits)
tls = TfmdLists(files, tfm, splits=splits)
dls = tls.dataloaders(after_item=[Resize(224), ToTensor], 
                      after_batch=[IntToFloatTensor, Normalize.from_stats(*imagenet_stats)])

valids = [v[0] for k,v in tfm.valid.items()]      
assert not [v for v in valids if v in files[splits[0]]]

Defining the Contrastive Loss Model

We’re going to, again, copy the fastai Siamese Networks tutorial but change up the model a bit: we’re going to output two “embedding vectors”, one for each input image.

I found it way easier to interface with the rest of fastai if I just “packaged” these two vectors into one big long vector, and then later just ‘cut it in half’ whenever I needed one vector or the other.

Initially we’re going to use an embedding dimension of 3 so we can visualize it. This isn’t actually going to be ideal for achieving high accuracy, so afterward we’ll re-do it with the dimension increased.

class SiameseNet(nn.Module):
    def __init__(self, 
        arch=resnet34, 
        lin_ftrs= [256, 128], 
        emb_sz=128,  # dimensionality of embeddings: 128 works pretty well. Sadly 3 is too small in my experience
        ps=0.5, 
        bn_final=False):
        super(SiameseNet, self).__init__()
        store_attr()
        self.body = create_body(self.arch, cut=-2) 
        self.head = create_head(num_features_model(self.body), self.emb_sz, self.lin_ftrs, self.ps, self.bn_final)
        self.cnn = nn.Sequential(self.body, self.head)
                                  
    def forward(self, x1, x2):  # note that this expects two inputs not a single pair-like "SiameseImage" 
        output1 = self.cnn(x1)
        output2 = self.cnn(x2)
        return torch.cat((output1,output2),dim=-1)  
        
    def get_embedding(self, x): # operates on just one image instead of 2
        return self.cnn(x)


def split_out(out):  # this will split the model output down the middle, into the set of two embeddings
    return torch.split(out, int(out.shape[-1]/2), dim=-1)

device = torch.device('cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu')
model = SiameseNet().to(device)
apply_init(model.head, nn.init.kaiming_normal_)


CL_DISTANCE_FUNC = F.pairwise_distance  # btw, cosine similarity did not work well for me -SH
CL_MARGIN = 4.   # distance beyond which we stop pushing dissimilar items apart. 4 seems to work as well as any other value I tried.

class ContrastiveLoss(nn.Module):
    """Takes embeddings of two samples and a target label == 1 if samples are from the same class and label == 0 otherwise"""
    def __init__(self, margin=CL_MARGIN):  # big margin for training means "always keep pushing dissimilar points away"
        super(ContrastiveLoss, self).__init__()
        self.margin = margin
    def forward(self, embs, target):
        emb1, emb2 = split_out(embs)
        dist = CL_DISTANCE_FUNC(emb1, emb2)
        pdist, ndist = dist*target, dist*(1-target)
        loss = 0.5* ((pdist**2) + (F.relu(self.margin-ndist)**2))  # relu serves as the "hinge"
        return loss.mean()
    
    
def siamese_splitter(m):
    return L(m.body, m.head).map(params)
Downloading: "https://download.pytorch.org/models/resnet34-333f7ec4.pth" to /root/.cache/torch/hub/checkpoints/resnet34-333f7ec4.pth

We’ll define the following custom way of measuring accuracy, called sim_acc

# custom accuracy metric 
def sim(p, delta=None): 
    "where outputs are within delta of each other, they are similar (1), otherwise not (0)" 
    if delta is None: delta = CL_MARGIN * 0.45  # a bit less than half the margin seems to work ok
    p0, p1 = split_out(p)
    return (CL_DISTANCE_FUNC(p0,p1) <= delta)*1

def sim_acc(pred,targ, delta=None):
    "similarity accuracy"
    return ( sim(pred, delta=delta) == targ ).float().mean()

Define the model, freeze it, and run the LRFinder:

learn = Learner(dls, model, loss_func=ContrastiveLoss(), splitter=siamese_splitter, metrics=sim_acc)
learn.freeze() 
learn.lr_find()   # for some reason lr_find takes FOREVER.
SuggestedLRs(valley=tensor(0.0132))

learn.fit_one_cycle(5, 0.02)   # below, note that sim_acc=0.5 is roughly the same as "random guessing"
epoch train_loss valid_loss sim_acc time
0 182.274185 35.945770 0.654930 00:55
1 127.189186 14.088330 0.654930 00:55
2 91.577187 6.649684 0.654930 00:57
3 69.468987 4.976199 0.669014 00:55
4 55.596302 4.554039 0.683099 00:56
learn.unfreeze()
learn.fit_one_cycle(4, slice(1e-5, 1e-3))
epoch train_loss valid_loss sim_acc time
0 15.782226 4.337954 0.690141 00:56
1 15.417539 4.133821 0.725352 00:57
2 14.990416 4.219871 0.753521 01:00
3 14.579235 4.243847 0.767606 00:59

So, the accuracy is not as good as our regular “traditional”/“vanilla” classifier, but it’s still…well…actually…. I’ve run this many times in the course of preparing the post, and I’ve seen it score as high as 95% and as low as 75%. …Hmm. Well, moving on for now.

Now we’ll add a show_results method:

myacc_val = 0  # for diagnostically messing with delta
@typedispatch
def show_results(x:SiameseImage, y, samples, outs, ctxs=None, max_n=6, nrows=None, ncols=3, figsize=None, **kwargs):
    global myacc_val
    sims = sim(y[2], delta=None if 'delta' not in kwargs else kwargs['delta'])
    acc = (sims == x[2]).float().mean().item()   # compute accuracy for diagnostic purposes
    if 'show_acc' in kwargs and kwargs['show_acc']: print("accuracy = ",acc)
    myacc_val = acc
    if 'just_acc' in kwargs: return

    if figsize is None: figsize = (ncols*6, max_n//ncols * 3)
    if ctxs is None: ctxs = get_grid(min(x[0].shape[0], max_n), nrows=None, ncols=ncols, figsize=figsize)
    for i,ctx in enumerate(ctxs):
        pred, targ = sims[i].item(), x[2][i].item()
        title = f'Actual: {["Not similar","Similar"][targ]} \n Prediction: {["Not similar","Similar"][pred]}'
        if pred != targ: title = '***'+title.upper()+'***' # uppercase when we get it wrong
        SiameseImage(x[0][i], x[1][i], title).show(ctx=ctx)
learn.show_results(max_n=12, delta=2, show_acc=True) #  make max_n large & vary delta to tweak performance
accuracy =  0.78125

Now Let’s Visualize the Embeddings

@patch
def siamembed(self:Learner, item, rm_type_tfms=None, with_input=False):
    return self.predict(item, rm_type_tfms=None, with_input=False)[0]
import plotly.graph_objects as go

nclasses = len(labels)
def plot_clusters(labels,dim=3):
    fig = go.Figure()
    colors = ['red','green','blue','orange']+['black']*max(nclasses-4,0)
    for i,l in enumerate(labels):      
        cluster = np.zeros((len(lbl2files[l]),dim))
        for j,f in enumerate(lbl2files[l]):         # TODO: this is insanely slow; should use batches instead
            img = PILImage.create(f)
            cluster[j] = learn.siamembed(SiameseImage(img, img))[0:3].detach().numpy() # TODO: for now just grab the first 3 coorrdinates, not even PCA
        fig.add_trace( go.Scatter3d(x=cluster[:,0], y=cluster[:,1], z=cluster[:,2], hovertext=labels[i], name=labels[i], \
            mode='markers', marker=dict(size=5, opacity=0.6, color=colors[i])))
    fig.show()
    
plot_clusters(labels)

^TODO: that is the first 3 of 128 dimensions. So it’ll look more “overlappy” than it really is.

And Now: Do the Zero-Shot!

Give it the spectacled bears, which this model has never seen before!

labels = ['grizzly','black','teddy','spectacled']
scrape_for_me(dl_path, [labels[-1]], search_suffix, erase_dir=False, max_n=300)

subdirs, path, files = labels, Path(dl_path), []
for s in subdirs: files += get_image_files(path/s)

print(f'{len(files)} files ready to load.')
print(f"files[0] = {files[0]}, files[-1] = {files[-1]}")

print(f"label_func yields {set([label_func(fp) for fp in files])}")  # sanity check: do we get all the labels we expect?

lbl2files = {l: [f for f in files if label_func(f) == l] for l in labels}

plot_clusters(labels)
spectacled bear: Got 262 image URLs. Downloading...
   urls =  ['https://s1.ibtimes.com/sites/www.ibtimes.com/files/styles/embed/public/2017/06/28/andean-bear-14218511920.jpg', 'http://4.bp.blogspot.com/-6xZLUXHh8Uc/UUxmEy9QZBI/AAAAAAAAXcY/TMXg3WwS3GA/s1600/Spectacled+Bear+2.jpg', 'https://www.coolearth.org/wp-content/uploads/2016/06/Spectacled-Bear-2.jpg', 'https://s.hdnux.com/photos/01/07/31/01/18714719/5/920x920.jpg', 'https://www.bioexpedition.com/wp-content/uploads/2014/10/Spectacled_Bear_Dave-Pape.jpg', 'http://www.bearsoftheworld.net/images/bears/spectacled_bear_02.jpg', 'https://live.staticflickr.com/7431/9349596216_623a25b335_b.jpg', 'https://wallpapercave.com/wp/wp2676931.jpg', 'http://3.bp.blogspot.com/-Sg8TmR5qh88/UUxmNY4d0HI/AAAAAAAAXcw/dpL2qFHZvp0/s1600/Spectacled+Bear+5.jpg', 'https://wanderlord.com/wp-content/uploads/2017/03/Spectacled-Bear.jpg', 'https://i.natgeofe.com/n/804d9cdb-f8da-449a-93b0-aac7a448b7c0/spectacled-bear_thumb.JPG?w=1200', 'https://img00.deviantart.net/46a0/i/2011/237/1/8/spectacled_bear_by_skarkdahn-d47sr16.jpg', 'https://www.animalphotos.me/mamm1/bear-spec_files/spectacle2.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-5.jpg', 'https://www.activewild.com/wp-content/uploads/2018/03/spectacled-bears-1024x683.jpg', 'https://a-z-animals.com/media/animals/images/original/spectacled_bear4.jpg', 'http://www.rainforest-alliance.org/sites/default/files/2016-09/spectacled-bear-header.jpg', 'https://lh6.googleusercontent.com/-Sptgd7fYIqY/TYk4YVRbndI/AAAAAAAAB7E/MYqqcMu-isw/s1600/Spectacled_Bear_Tennoji_2.jpg', 'https://pre00.deviantart.net/e486/th/pre/i/2012/023/6/5/spectacled_bear_by_shimmichan-d4nd6wh.jpg', 'https://www.vanwageningen.net/i/upload/2014/10/29/20141029132015-bc5e40e5-me.jpg', 'https://live.staticflickr.com/6124/6014296334_89fcc4133a_b.jpg', 'https://farm4.staticflickr.com/3606/3292437597_527cab83ef_z.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-1002804-spectacled-bear-face.jpg', 'https://focusingonwildlife.com/news/wp-content/uploads/003_MTF_0221_spectacled.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear.jpg', 'https://res.cloudinary.com/dk-find-out/image/upload/q_70,c_pad,w_1200,h_630,f_auto/MA_00155387_i4leyu.jpg', 'https://colombiareports.com/wp-content/uploads/2017/07/spectacled_bear.jpg', 'https://i.ytimg.com/vi/wWgdrMOH8d4/maxresdefault.jpg', 'http://1.bp.blogspot.com/-qAm-_Q_k5I0/TfA4AvZt_1I/AAAAAAAABNM/Ov-m-olxu_I/s640/Spectacled-Bear.jpg', 'https://1.bp.blogspot.com/-hq_blqxeAVk/XsFZuPiS06I/AAAAAAAADps/lVr1KiOLbLEfjVO34nufzWmMdo8DJXsOQCPcBGAYYCw/w1200-h630-p-k-no-nu/Spectacled_Bear.jpg', 'http://tenrandomfacts.com/wp-content/uploads/2016/07/Spectacled-Bear.jpg', 'https://live.staticflickr.com/8393/8696456854_49f079714b_b.jpg', 'https://happygringo.com/wp-content/uploads/2020/02/portada-1.jpg', 'https://static1.squarespace.com/static/56a1a14b05caa7ee9f26f47d/t/57390f5520c6471cf068c6cc/1463357302812/', 'https://www.zoochat.com/community/media/spectacled-bears.112852/full?d=1282939786', 'http://3.bp.blogspot.com/-EGI8NqbPcvo/UUxmUJ6t1qI/AAAAAAAAXdA/fDUqD60Tv00/w1200-h630-p-k-no-nu/Spectacled+Bear.jpg', 'https://www.zoochat.com/community/media/spectacled-bear.109154/full?d=1281223819', 'https://nationalzoo.si.edu/sites/default/files/styles/1400x700_scale_and_crop/public/animals/andean-bear-007.jpg?itok=MJoAKHeV', 'https://colombiareports.com/wp-content/uploads/2017/07/andean-bear-2-2017-01-31.jpg', 'https://img.izismile.com/img/img3/20100513/640/cute_spectacled_bear_640_11.jpg', 'https://mma.prnewswire.com/media/1030258/The_Wild_Animal_Sanctuary_Rescued_Spectacled_Bear.jpg?p=facebook', 'https://www.zoochat.com/community/media/spectacled-bear.108090/full?d=1280694438', 'https://natureandculture.org/wp-content/uploads/2019/03/Cloud-Forest-Andean-Bear-768x432.jpg', 'http://featuredcreature.com/wp-content/uploads/2012/10/6a010535647bf3970b0134830b6483970c-800wi2.jpg', 'https://lh6.googleusercontent.com/-Sptgd7fYIqY/TYk4YVRbndI/AAAAAAAAB7E/MYqqcMu-isw/w1200-h630-p-k-no-nu/Spectacled_Bear_Tennoji_2.jpg', 'https://live.staticflickr.com/2900/14021204319_c4d111529b_b.jpg', 'http://theamazonianassignment.weebly.com/uploads/1/2/1/3/12136910/944137409_orig.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-ecuadorian-andes-mountains-328x562.jpg?c14113', 'http://norwichsciencefestival.co.uk/wp-content/uploads/sites/4/SavingSpectacledBear1200x500_1200x500_acf_cropped.jpg', 'https://www.parkgrandhydepark.co.uk/blog/wp-content/uploads/2017/05/shutterstock_492598162.jpg', 'https://www.apex-expeditions.com/wp-content/uploads/2015/04/04-Spectacled-Bear-Papallacta-%C2%A9-Jonathan-Rossouw.jpg', 'https://66.media.tumblr.com/9decce76250961499fe7054e55a907b2/tumblr_omltex2dq01qg5f3ho1_1280.jpg', 'http://i1.treknature.com/photos/2959/bear_0111111.jpg', 'https://nationalzoo.si.edu/sites/default/files/animals/andean-bear-001.jpg', 'https://live.staticflickr.com/4103/4831613245_311f43d099_b.jpg', 'https://imgs.mongabay.com/wp-content/uploads/sites/20/2018/06/15124317/MTF_0409_golden_spectacled_bear_%C2%A9michael_tweddle.jpg', 'https://img.izismile.com/img/img3/20100513/640/cute_spectacled_bear_640_13.jpg', 'http://farm4.staticflickr.com/3419/3774261161_577e09be2c_z.jpg?zz=1', 'http://2.bp.blogspot.com/-vdZv8wGWMdo/UUxmNCvsvhI/AAAAAAAAXco/H5Pzgk5dtOk/s1600/Spectacled+Bear+6.jpg', 'https://www.zoochat.com/community/media/spectacled-bear.342492/full?d=1479072515', 'https://www.si.edu/sites/default/files/newsdesk/photos/20100422-040mm.jpg', 'http://1.bp.blogspot.com/-G-M1STXREJI/UUxmFpUBSoI/AAAAAAAAXcg/qvb2WX5SW0s/s1600/Spectacled+Bear+3.jpg', 'https://www.wisebirding.co.uk/wp-content/uploads/Spectacled-Bear2.jpg', 'https://www.sustainability-times.com/wp-content/uploads/thumbs/AndeanBear_ZN-38o7iv74jqj9grl16xtssg.jpg', 'https://media.sciencephoto.com/image/c0175188/800wm/C0175188-Spectacled_Bear.jpg', 'https://wallpapercave.com/wp/wp2676880.jpg', 'http://www.stlzoo.org/files/1014/0017/1158/ANDEANBEAR9410WINKELMAN2.jpg', 'https://2.bp.blogspot.com/-KBaTMMdVHoY/T_A-HQlM8mI/AAAAAAAAMHE/cBonrzV6sfY/s1600/Spectacled+Bear2.jpg', 'https://www.theanimalfiles.com/images/spectacled_bear_1.jpg', 'https://www.mindenpictures.com/cache/pcache2/00542362.jpg', 'https://www.tourradar.com/days-to-come/wp-content/uploads/2016/01/Spectacled-bear.jpg', 'https://www.zoochat.com/community/media/spectacled-bear.250657/full?d=1393011952', 'https://images.fineartamerica.com/images-medium-large-5/2-spectacled-bear-in-andean-foothills-peru-tui-de-roy.jpg', 'https://images.robertharding.com/preview/RM/RH/HORIZONTAL/1127-16827.jpg', 'https://newyork.cbslocal.com/wp-content/uploads/sites/14578484/2017/05/julie-larsen-maher_1638_andean-bear-and-cub_qz_05-01-17.jpg?w=1024&h=0&crop=1', 'http://2.bp.blogspot.com/-1jh4WfVEyx4/T_A-OoNHGFI/AAAAAAAAMHM/Wm41l9WFfgs/s1600/Spectacled+Bear3.jpg', 'http://blog.nationalgeographic.org/wp-content/uploads/2009/02/Spectacled-Bear-picture-2.jpg', 'http://2.bp.blogspot.com/-und_0cRk_O8/URGtY1vnHGI/AAAAAAAAARQ/9aAIlFO437s/s1600/spectacled-bear3.jpg', 'http://www.rainforest-alliance.org/sites/default/files/styles/750w_585h/public/2016-09/spectacled-bear-header.jpg?itok=0V7_KGpe', 'https://wtop.com/wp-content/uploads/2015/03/16619693699_5bd5ac78d4_b.jpg', 'https://i.pinimg.com/originals/90/88/80/908880871027303cfe43c19ea4655e75.jpg', 'https://www.sustainability-times.com/wp-content/uploads/2019/06/AndeanBear_ZN.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Habitat.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-sitting-820x377.jpg?d8bc0c', 'http://seethewild.org/wp-content/uploads/2016/05/features.jpeg', 'https://i.pinimg.com/originals/24/eb/4e/24eb4e758b8bf55ef1c8a0d10e3d6b4a.jpg', 'https://www.discoveranimals.co.uk/wp-content/uploads/2014/09/spectacled_bear.jpg', 'https://www.shropshirestar.com/resizer/GqPX0-puTDFAn5Ff_V9fBEpM2Jk=/1000x0/filters:quality(100)/arc-anglerfish-arc2-prod-shropshirestar-mna.s3.amazonaws.com/public/4PL5ZCNSKVBPVK6UPFUAPHLMUI.jpg', 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Spectacled_Bear.jpg/436px-Spectacled_Bear.jpg', 'https://d.ibtimes.co.uk/en/full/1613674/spectacled-bear.jpg?w=400', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/1/spectacled-bear-portrait-rose-santuci-sofranko.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-cubs.jpg', 'http://1.bp.blogspot.com/-Srm8EQe_xjU/TwK6I75A_jI/AAAAAAAAEt8/jQqGqztNuls/s1600/spectacled-bear-spectacled-bear.jpg', 'https://st2.depositphotos.com/4380587/11651/i/950/depositphotos_116510914-stock-photo-picture-of-a-spectacled-bear.jpg', 'http://kids.sandiegozoo.org/sites/default/files/2017-07/andean-bear-tree.jpg', 'https://southafricatoday.net/wp-content/uploads/2018/07/1532665810_1-1024x1000.jpg', 'https://wallpapercave.com/wp/wp2676970.jpg', 'https://upload.wikimedia.org/wikipedia/en/c/ce/Spectacled-bear.jpg', 'https://i.pinimg.com/originals/0b/24/7b/0b247b1295ab238a6a9873bfc9465d45.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/6093558068_e2c0675823_o.jpg', 'http://4.bp.blogspot.com/-zHQPWNws7VQ/TwK7vuoleYI/AAAAAAAAEuI/iZJGb0R9CEQ/s1600/spectacled_bear-spectacled_bear.jpg', 'http://www.ecns.cn/hd/2020/05/18/e41ef93a186c40a38726189b8901f6f3.jpg', 'https://lh3.googleusercontent.com/-TijTJ1kPu7U/TYk4Z-b-flI/AAAAAAAAB7M/X8njDXrfBAk/s1600/spectacled-beard.jpg', 'https://vignette.wikia.nocookie.net/vsbattles/images/0/0c/Spectacled_bear_vector_by_phoenixtdm_dcek9fs-fullview.png/revision/latest?cb=20190617023302', 'https://4.bp.blogspot.com/-xKDgjZh9q_Y/Ux2oRtCuyXI/AAAAAAAAMsM/PEgp-j44bS8/s1600/Spectacled-Bear.jpg', 'https://www.naturalhistoryonthenet.com/wp-content/uploads/2017/01/Spectacled-Bear.jpg', 'https://www.saczoo.org/wp-content/uploads/2017/01/Spectacled-Bears.jpg', 'https://images6.alphacoders.com/406/406817.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Baby-Spectacled-Bear.jpg', 'http://www.mammalwatching.com/wp-content/uploads/2018/01/EC-square-fox.jpg', 'https://www.natureplprints.com/p/729/lucas-bustamante/spectacled-andean-bear-tremarctos-ornatus-maquipucuna-15316013_73045.jpg', 'http://4.bp.blogspot.com/-VxZ-1v7UTjY/UEAGEWmRNTI/AAAAAAAAk5w/iLr_Y0Pnrtg/s1600/120813+Spectacled+Bear+small.jpg', 'https://www.bearsinmind.org/Uploaded_files/Content/brilbeer.a3cb36.JPG', 'http://newyorktrendnyc.com/wp-content/uploads/2013/11/andean-bear.jpg', 'https://i.pinimg.com/originals/5b/88/ab/5b88ab3ff6b9d74aea2e1d94e521bc72.jpg', 'http://www.zooborns.com/.a/6a010535647bf3970b0192aac4e2da970d-500wi', 'https://home.bt.com/images/spectacled-bears-and-snow-leopards-among-creatures-getting-funding-boost-136427641293802601-180606124211.jpg', 'http://2qn4774cz98e17p2h549htbt.wpengine.netdna-cdn.com/wp-content/uploads/2013/11/Male-Andean-Bear-face.jpg', 'https://nationalzoo.si.edu/sites/default/files/animals/andeanbear-006.jpg', 'https://live.staticflickr.com/250/515823267_281f36649e_b.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/5409184198_ccc4b2c5a4_o.jpg', 'https://www.worldatlas.com/r/w728/upload/73/02/b4/shutterstock-118635484-min.jpg', 'https://4.bp.blogspot.com/-8Na0W3PKG-U/UpRE_rP_gXI/AAAAAAAABZI/rU-WV9XxIIE/s1600/Spectacled-Bear-gaert.JPG', 'https://www.pitara.com/wordpress/wp-content/uploads/2001/02/spectacled-bear.jpg', 'http://3.bp.blogspot.com/-EGI8NqbPcvo/UUxmUJ6t1qI/AAAAAAAAXdA/fDUqD60Tv00/s1600/Spectacled+Bear.jpg', 'https://www.si.edu/sites/default/files/newsdesk/photos/bernardo-021mm.jpg', 'https://lh6.googleusercontent.com/-cc05A4QkDxc/TYk4S3w1qFI/AAAAAAAAB7A/3vb0t85A7GU/s1600/spectacled_bear_03.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-rocks-820x545.jpg?c14113', 'https://philadelphiazoo.160over90.io/wp-content/uploads/2019/10/Andean.SpectacledBear_Joaquin_0665.jpg', 'https://i.ytimg.com/vi/VXFIYIQgEkg/maxresdefault.jpg', 'https://a4.pbase.com/o6/34/564334/1/70568852.6wuZLzi7.20061119spectacledbear03ccomp.jpg', 'http://www.animalphotos.me/mamm1/bear-spec_files/spectacle1.jpg', 'https://ca-times.brightspotcdn.com/dims4/default/0a24096/2147483647/strip/true/crop/2000x3000+0+0/resize/840x1260!/quality/90/?url=https:%2F%2Fcalifornia-times-brightspot.s3.amazonaws.com%2Faa%2F74%2F245459fb41edb020b50a502e2a3a%2Ft16-0554-017-lg.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-71192249-spectacled-bear.jpg', 'https://farm3.staticflickr.com/2913/14137750328_d101997a4c_z.jpg', 'http://www.realworldholidays.co.uk/blog/wp-content/uploads/2015/12/spectacled-bear1.jpg', 'http://www.facts-about.info/wp-content/uploads/2014/12/Andean-Bear-at-Reid-Park-Zoo.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-2.jpg', 'https://1.bp.blogspot.com/-Tou2SRVCRqw/UpRFIXCbzQI/AAAAAAAABZ8/CTtSSU6GhZI/s1600/Spectacled-Bear.jpg', 'https://img.izismile.com/img/img3/20100513/640/cute_spectacled_bear_640_01.jpg', 'http://www.facts-about.info/wp-content/uploads/2014/12/Andean-Bear1.jpg', 'http://upload.wikimedia.org/wikipedia/commons/6/62/Spectacled_Bear_059.jpg', 'https://media.nbcwashington.com/2019/09/4621594915_9022bfd814_b.jpg?fit=1024%2C702', 'https://philadelphiazoo.org/wp-content/uploads/2019/10/Andean.SpectacledBear_Rosie_3395.jpg', 'https://mma.prnewswire.com/media/1030258/The_Wild_Animal_Sanctuary_Rescued_Spectacled_Bear.jpg?p=publish&w=950', 'https://c1.staticflickr.com/5/4149/4956021357_d3aa0407c1_z.jpg', 'http://images.fineartamerica.com/images-medium-large/spectacled-bear-tremarctos-ornatus-cub-pete-oxford.jpg', 'https://i.pinimg.com/736x/b9/ab/6f/b9ab6f27f50527d8ddd610f0f02e88cb--spectacled-bear-spirit-animal.jpg', 'http://2.bp.blogspot.com/-4bGjmY9K4pk/T_A-Yq5aNGI/AAAAAAAAMHc/pv9gEJ8O6Uo/s1600/Spectacled+Bear5.jpg', 'https://vg5b2ejdwb-flywheel.netdna-ssl.com/wp-content/uploads/2015/06/7oboa0_tu_u.jpg', 'https://resize.hswstatic.com/w_907/gif/types-of-spectacled-bear0.jpg', 'http://1.bp.blogspot.com/-xGs4-W-3iSY/TjaZG6_XnOI/AAAAAAAABUE/AkEJ97L1HQc/s1600/156521.jpg', 'https://www.robertharding.com/watermark.php?type=preview&im=RM/RH/HORIZONTAL/1127-12838', 'https://www.zooportraits.com/wp-content/uploads/2018/05/Spectacled-Bear-Tremarctos-Ornatus.jpg', 'https://i.pinimg.com/736x/fa/40/33/fa403340878f175322c07b860b9d6426--spectacled-bear-bolivia.jpg', 'http://img.izismile.com/img/img3/20100513/640/cute_spectacled_bear_640_03.jpg', 'https://ourplnt.com/wp-content/uploads/2016/03/Spectacled-bear.jpg', 'https://i.pinimg.com/originals/ec/0c/96/ec0c969cd38a1a701a2c1e2a87a96561.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/3956278926_328d1c6a54_o.jpg', 'https://www.rainbowtours.co.uk/upload-files/product-excursions-gallery/EXC-PER-024_1_spectacled-bear.jpg', 'https://2.bp.blogspot.com/-6-AcWMkgynE/VEMIW1x0vtI/AAAAAAAAAEU/xUsNNkFj6rg/s1600/oso.jpg', 'http://cincinnatizoo.org/system/assets/uploads/2014/02/spectaculed-bear.jpg', 'http://3.bp.blogspot.com/-brSuqjGOIG0/URGtpFtyo_I/AAAAAAAAARg/vLE13RVoCsk/s1600/spectacled-bear.jpg', 'https://c2.staticflickr.com/2/1501/24144684635_73b9e79d88_b.jpg', 'https://critter.science/wp-content/uploads/2019/05/sb1.jpg', 'https://reidparkzoo.org/wp-content/uploads/2016/05/andeanbear9768z.jpg', 'https://srcnaut.com/cdn-cgi/image/f=auto,fit=crop,g=0.5x0.5,w=2000,h=1125,q=90,d=1/upload/32/02/df/shutterstock-1188627676.jpg', 'http://i1.chesterchronicle.co.uk/incoming/article8089938.ece/ALTERNATES/s615b/JS50546090.jpg', 'https://conservationnation.org/wp-content/uploads/2020/03/andean-bear-hero.jpg', 'http://seethewild.org/wp-content/uploads/2016/05/dreamstime_15373008-1.jpg', 'https://i1.wp.com/www.bearz.org/en/wp-content/uploads/2015/06/604014_10200989864724976_224585035_n1.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-30167654-spectacled-bear-with-bamboo.jpg', 'https://www.textbooktravel.com/wp-content/uploads/2020/08/Spectacled-Bear-Cub.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Spectacled_Bear_-_Houston_Zoo.jpg/1200px-Spectacled_Bear_-_Houston_Zoo.jpg', 'https://1.bp.blogspot.com/-cloWtyc4dnw/T-5pMXHLd7I/AAAAAAAACog/ebO0-a0x89M/s1600/SpectacledBear.jpg', 'https://www.zoochat.com/community/media/spectacled-bear-at-chester-15-07-12.192796/full?d=1342391273', 'https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1440,f_auto/MA_00155387_i4leyu.jpg', 'http://2.bp.blogspot.com/-PQGmVUDZAhs/TwK5urRX1LI/AAAAAAAAEtw/s569QHCcx8Y/s1600/spectacled+bear.jpg', 'https://bw-1651cf0d2f737d7adeab84d339dbabd3-gallery.s3.amazonaws.com/images/image_2865567/db2b44df10ba2a567a8e5cc34ddfeed0_original.JPG', 'http://etc.usf.edu/clippix/pix/spectacled-bear-with-piece-of-food-in-its-mouth_medium.jpg', 'https://www.activewild.com/wp-content/uploads/2018/03/spectacled-bear-standing-768x1024.jpg', 'https://www.biolib.cz/IMG/GAL/BIG/28527.jpg', 'http://2.bp.blogspot.com/-1mdQ66udYmw/T_A-UjaJk5I/AAAAAAAAMHU/uJw0kPcz1vc/s1600/Spectacled+Bear4.jpg', 'https://www.apex-expeditions.com/wp-content/uploads/2015/04/02-Spectacled-Bear-Chaparri-JR-5.jpg', 'https://www.factzoo.com/sites/all/img/mammals/bears/spectacled-bear-face.jpg', 'http://images.fineartamerica.com/images-medium-large/andean-or-spectacled-bear-tremarctos-philippe-henry.jpg', 'https://i.pinimg.com/736x/ad/b9/c5/adb9c533cfd5c8f292ce21cabae6f2f9--spectacled-bear-baby-animals.jpg', 'https://4.bp.blogspot.com/-6TV6R9HEc-o/T_A98vwyTEI/AAAAAAAAMG8/ffIXmwgDoXw/s1600/Spectacled+Bear.jpg', 'http://cuencahighlife.com/wp-content/uploads/2015/03/chl-bear2.jpg', 'https://cms.prod.nypr.digital/images/27490/fill-1200x650/', 'https://www.worldatlas.com/upload/43/04/5c/shutterstock-92656222-min.jpg', 'https://c1.staticflickr.com/4/3444/3293260140_4257d5b231.jpg', 'https://www.animalfactsencyclopedia.com/images/bear-spectacledcloseinset.jpg', 'https://i.pinimg.com/originals/9b/34/7e/9b347e3e5fb5b58d954ce84aeccdcc42.jpg', 'https://wallpapercave.com/wp/wp2676943.jpg', 'https://www.si.edu/sites/default/files/newsdesk/photos/andean-bear.jpg', 'https://ecosphere.plus/wp-content/uploads/2018/02/Spectacled-bear_LAM0695-Heinz-Plenge-2-1080x540.jpg', 'https://i2-prod.chesterchronicle.co.uk/incoming/article8089934.ece/ALTERNATES/s1200/JS50546081.jpg', 'https://dancantravel.azurewebsites.net/wp-content/uploads/2017/09/DSC09006-2.jpg', 'https://pre00.deviantart.net/fc19/th/pre/i/2015/204/a/b/an_11dv___spectacled_bear_by_szallonntaidalma-d92d0pi.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-4.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-3.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-ogimage.jpg', 'https://images.freeimages.com/images/large-previews/e0f/spectacled-bear-1561333.jpg', 'https://kentattractions.co.uk/wp-content/uploads/2016/09/Spectacled-Bear.jpg', 'https://i.pinimg.com/originals/7c/48/b1/7c48b17f45853a48890e0a32f02aa875.jpg', 'http://gringosabroad.com/wp-content/uploads/2013/11/Male-Andean-Bear-face.jpg', 'https://rangerplanet.com/wp-content/uploads/2019/10/spectacled-bear.jpg', 'https://2.bp.blogspot.com/-1jh4WfVEyx4/T_A-OoNHGFI/AAAAAAAAMHM/Wm41l9WFfgs/s400/Spectacled+Bear3.jpg', 'http://1.bp.blogspot.com/-qM3UoT_SQRk/TiAwjcnmqiI/AAAAAAAAAKE/mQAIjK3XPRg/s1600/spectacled_bear_2.jpg', 'https://www.wisebirding.co.uk/wp-content/uploads/Spectacled-Bear.jpg', 'http://www.factzoo.com/sites/all/img/mammals/bears/spectacled-bear-south-american.jpg', 'https://s3-eu-west-1.amazonaws.com/images.noahsarkzoofarm.co.uk/uploads/tinymce/rasu.jpg', 'https://i.ytimg.com/vi/2TmnhBSyQoo/maxresdefault.jpg', 'https://reidparkzoo.org/wp-content/uploads/2016/05/andeanbear9646z.jpg', 'https://vistapointe.net/images/spectacled-bear-5.jpg', 'https://www.aboutanimals.com/images/spectacled-bear-tree-820x513.jpg?d8bc0c', 'https://standfirst-whitleyaward-production.imgix.net/content/uploads/1996/12/18134457/Tremarctos_ornatus_portrait.jpg?auto=compress,enhance,format&crop=faces,entropy,edges&fit=crop&fm=pjpg&w=1866&h=1400', 'https://c1.staticflickr.com/7/6037/6304684676_8fe4be5717_b.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/5566818433_9c994ec063_o.jpg', 'https://www.bwallpaperhd.com/wp-content/uploads/2018/12/SpectacledBear.jpg', 'https://cms.prod.nypr.digital/images/27460/fill-661x496/', 'https://i.ytimg.com/vi/lLcCBkNDQxs/maxresdefault.jpg', 'http://justfunfacts.com/wp-content/uploads/2017/11/spectacled-bear-eating-fruit.jpg', 'http://img07.deviantart.net/9fc0/i/2010/201/7/6/spectacled_bear_by_citronvert79.jpg', 'https://critterfacts.com/wp-content/uploads/2019/02/s_158118350.jpg', 'https://images.fineartamerica.com/images-medium-large-5/andean-bear-spectacled-bear-houston-zoo-tessa-fairey.jpg', 'https://images.fineartamerica.com/images/artworkimages/mediumlarge/2/spectacled-bear-david-stasiak.jpg', 'https://media.buzzle.com/media/images-en/photos/mammals/bears/1200-598140-spectacled-bear.jpg', 'http://magarticles.magzter.com/articles/8803/216564/58e38ecbbeeb4/Conservation-Insight-Andean-Bear.jpg', 'https://1.bp.blogspot.com/-DAyv4HJRE4E/XjRg-JFM_SI/AAAAAAAAAPU/NUHh82dMHZYSbMEQR3WEJOUXhO08GI4pACLcBGAsYHQ/s1600/baby-animal-photo-05032016-image-056.jpg', 'http://www.rainforest-alliance.org/sites/default/files/styles/large/public/2016-09/spectacled-bear.jpg?itok=BXWidYtz', 'http://www.parkgrandhydepark.co.uk/blog/wp-content/uploads/2017/05/shutterstock_550667422.jpg', 'https://nationalzoo.si.edu/sites/default/files/animals/andeanbear-005.jpg', 'https://bw-1651cf0d2f737d7adeab84d339dbabd3-gallery.s3.amazonaws.com/images/image_2865571/a3195154ccdac7c9efcbed5727462346_original.JPG', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-in-the-Wild.jpg', 'http://bearlegend.com/wp-content/uploads/2012/04/6862037661_30664cf53f_o.jpg', 'https://www.bwallpaperhd.com/wp-content/uploads/2018/12/SpectacledBear-1024x576.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Eating.jpg', 'http://2.bp.blogspot.com/-3UzekS1270M/TiAwko0VrQI/AAAAAAAAAKI/rOXYR8LXLHM/s1600/spectacled_bear_3.jpg', 'http://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bears-300x225.jpg', 'https://animalia.us-east-1.linodeobjects.com/animals/photos/full/1x1/determined.jpg', 'http://cdn4.awwnews.com/images/2015/03/044_3-Spectacled-Bear-Cubs.jpg', 'http://www.peru-breathtaking-trips.com/imgs/Spectacled%20bear.jpg', 'http://lh4.ggpht.com/-2YrERpe5ZeU/UOUBP6Zr8ZI/AAAAAAAAASQ/7oSIuID4iSs/%25255BUNSET%25255D.jpg', 'https://i.pinimg.com/736x/d2/99/fa/d299fa76c3d1a5c0e017514b086ddd9c--spectacled-bear-teddy-bears.jpg', 'https://c2.staticflickr.com/4/3685/8852797884_8f3a84d84c_b_d.jpg', 'https://www.si.edu/sites/default/files/newsdesk/photos/andean_bear_cubs2.jpg', 'https://critterfacts.com/wp-content/uploads/2019/02/s_1083570302_.jpg', 'https://www.worldlandtrust.org/wp-content/uploads/2017/08/RS26943_Ramiro-Mendoza-scr.jpg', 'http://www.kidzone.ws/lw/bears/images/spectacled-bear.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Oso_andino_Porcon.jpg/1200px-Oso_andino_Porcon.jpg', 'https://www.bioexpedition.com/wp-content/uploads/2012/05/Spectacled-Bear.jpg', 'https://www.earlham.ac.uk/sites/default/files/images/articles/Saving-Spectacled-Bear-Deepest-Darkest-Colombia/Saving-Spectacled-Bear-Deepest-Darkest-Colombia-Andean-770.jpg', 'https://featuredcreature.com/wp-content/uploads/2012/10/6a010535647bf3970b0134830b6313970c-800wi2.jpg', 'https://www.apex-expeditions.com/wp-content/uploads/2015/04/01-Spectacled-Bear-Chaparri-JR-2.jpg', 'http://2.bp.blogspot.com/-uKXzB18yA0M/UDBPCtHRm6I/AAAAAAAABrg/cceW2WF1lwQ/w1200-h630-p-k-no-nu/Spectacled-Bear-1.jpg', 'https://www.animalspot.net/wp-content/uploads/2015/12/Spectacled-Bear-Images.jpg', 'https://ptes.org/wp-content/uploads/2018/12/Andean-bear_credit-Tashkin-Meza-Andean-Bear-1000.jpg', 'http://www.telesurtv.net/export/sites/telesur/img/news/2015/11/27/spectacled_bear_2.jpg_1167511010.jpg', 'https://www.worldlandtrust.org/wp-content/uploads/2017/07/RS4817_Spectacled-Bear-2007-Fundacion-Ecominga-Candelaria-Reserve-c-Lou-Jost-1.jpg', 'http://etc.usf.edu/clippix/pix/spectacled-bear_medium.jpg']
    Images downloaded.
After checking images for issues, 958 (total) images remain.
958 files ready to load.
files[0] = scraped_images/grizzly/00000143.jpg, files[-1] = scraped_images/spectacled/00000003.jpg
label_func yields {'teddy', 'grizzly', 'black', 'spectacled'}

TODO: Woops! Forgot to score how the model did on the spectacled bears! Will add that soon. ;-) Watch this space.

3. Discussion

From this example, it looks like the contrastive loss method is worse than traditional classification in a couple ways: it requires more data (or else the model tends to overfit), it runs slower, and it’s less accurate. (And we completely left out the challenge of training CL models which typically need to have their positive and negative examples “mined” to find “hard” cases so that the model “learns” more per iteration.) Why would anyone ever want to use it?

Well, we already mentioned the use case of “produces semantically meaningful embeddings” that can then be used for other downstream tasks. For example, word embeddings like Word2Vec or GloVe can be used for lots of NLP tasks. Jeremy Howard in the FastAI course shows how to embeddings of tabular data and how that can work better and be more meaningful than one-hot encoded tabular data.

Furthermore, regarding accuracy: OpenAI’s CLIP became SOTA on ImageNet in early 2020! They used a TON of pre-training data, and ended up classifying images via contrastive losses better than any other method!

Are contrastive loss methods right for you and your use case, and your dataset? I don’t know. Hopefully after this, you at least have some clue of how to implement such a thing, and how it compares and constrasts with the sort of vanilla classification most people who work with neural networks are used to.

4. FastAI Details: How I Wrote My First Callback

I wrote a Callback to use my visualization routines. It’s generic enough that you could easily pass in some other visualization routine – in fact, mine is generic enough that I must assume someone else (LOL, as in Jeremy) has probably already written one at least as good, but this one is mine!

The basic structure of the callback is very simple. In the following I’ve removed all the URL-bookkeeping so you can see the structure:

class VizPreds_Structure(Callback):
    """Structure of my Progress-like callback: call the bokeh triangle plot (or some other routine) 
    with each batch of training. Scroll down to next code block for 'full' VizPreds routine."""
    order = ProgressCallback.order+1    # run it after the main fastai ProgressCallback
    def __init__(self,
        method='auto',   # method is a plotting function, must have a .do_plot(preds,targs) and not mind unexpected kwargs. Or 'auto'
        activation=F.softmax, # PyTorch activation for preds. For trad. classification, 'preds' are logits & need softmax. or None
    ):
        store_attr()    # one of my favorite fastai routines ever, sets all the "self." vars

    def before_fit(self):       # set up stuff 
        if self.method=='auto': # added the 'auto' thing so it choose which default method to use based on num. of dimensions
            self.method = TrianglePlot2D_Bokeh if len(self.learn.dls.vocab) <= 3 else TrianglePlot3D_Plotly

    def before_epoch(self):           # remove our accumulated info over batches
        if not self.learn.training:   # only operated on validation datasets
            self.learn.viz_preds, self.learn.viz_targs = None, None # stuff that's gonna get plotted

    def after_batch(self, **kwargs):  # make predictions after each val batch and accumulate them to plot at the end
        if not self.learn.training:   # only operated on validation datasets
            with torch.no_grad():
                preds = self.activation(self.learn.pred, dim=1) if (self.activation is not None) else preds 
                preds, targs = [x.detach().cpu().numpy().copy() for x in [preds, y]] # Uh...I probly don't need "copy" do I?
                self.learn.viz_preds = preds if self.learn.viz_preds is None else np.vstack((self.learn.viz_preds, preds))
                self.learn.viz_targs = targs if self.learn.viz_targs is None else np.hstack((self.learn.viz_targs, targs))

    def after_epoch(self):          
        if not self.learn.training:
            print(f"Epoch {self.learn.epoch}: Plotting {len(self.learn.viz_targs)} (= {len(self.learn.dls.valid.items)}?) points:")
            # call whatever the plotting method is and make the plot
            self.method(self.learn.viz_preds, self.learn.viz_targs, labels=self.dls.vocab,
                comment=f'Epoch {self.learn.epoch}', thumb_height=self.thumb_height).do_plot()

That’s the idea. If you only cared about plotting the very last batch of predictions, you could put all that code currently in after batch into the start of after_epoch – which is how I had it initially until I started wondering, “Hey where’s the rest of my validation data?!”

It’s important that this viz routine gets registered as a callback for the fit, not the Learner. If you make a callback for the Learner, it will be executing lots of times when you don’t want it to, such if/when you call the LRFinder. Don’t do that.

In order to implement the mouseover-image-thumbnail preview feature that my mrspuff routine TrianglePlot2D_Bokeh provides, there’s a bunch of extra bookkeeping needed to generate and then hang on to the thumbnail URLs. If we include all that, then the full Callback is below.

class VizPreds(Callback):
    """Progress-like callback: call the bokeh triangle plot (or some other routine) 
    with each batch of training"""
    order = ProgressCallback.order+1    # run it after the main fastai ProgressCallback
    def __init__(self,
        method='auto',   # method is a plotting function, must have a .do_plot(preds,targs) and not mind unexpected kwargs. Or 'auto'
        activation=F.softmax, # PyTorch activation for preds. For trad. classification, 'preds' are logits & need softmax. or None
        gen_urls=True,   # generate thumbnail urls (if they don't exist) (2D_Bokeh only)
        thumb_height=80, # lets you vary the height of image thumbnails (2D_Bokeh only)
        force_new_urls=False, # if False, keeps whatever thumbnail urls are in learn.dls (2D_Bokeh only)
    ):
        store_attr()
        self.we_made_the_thumbs = False  # so that we don't force-gen new urls a 2nd time after un-freezing

    def before_fit(self):   # set up the plotting method and generate thumbnail URLs if applicable
        if self.method=='auto':
            self.method = TrianglePlot2D_Bokeh if len(self.learn.dls.vocab) <= 3 else TrianglePlot3D_Plotly

        dv = self.learn.dls.valid       # dv is just a shorthand for "dls.valid"
        if self.gen_urls and self.method==TrianglePlot2D_Bokeh and (('url_dict' not in dir(dv)) or self.force_new_urls) and not self.we_made_the_thumbs:
            self.we_made_the_thumbs = True  # as in "I made the donuts", https://www.youtube.com/watch?v=IYRurPB4WA0
            #url_dict maps filenames to urls.  if you're on regular-Jupyter instead of Colab, you can use filenames as urls 
            self.learn.dls.valid.url_dict = dict(zip(dv.items, get_thumb_urls(dv.items, size=(self.thumb_height,self.thumb_height))))

    def before_epoch(self):
        if not self.learn.training:
            self.learn.viz_preds, self.learn.viz_targs = None, None

    def after_batch(self, **kwargs): # make predictions after each val batch and accumulate them to plot at the end
        if not self.learn.training:
            with torch.no_grad():
                preds = self.activation(self.learn.pred, dim=1) if (self.activation is not None) else preds 
                preds, targs = [x.detach().cpu().numpy().copy() for x in [preds, y]] # remove copy()?
                self.learn.viz_preds = preds if self.learn.viz_preds is None else np.vstack((self.learn.viz_preds, preds))
                self.learn.viz_targs = targs if self.learn.viz_targs is None else np.hstack((self.learn.viz_targs, targs))

    def after_epoch(self):  # now that we have all our pred/targ batches and urls ready (if applicable), plot!
        if not self.learn.training:
            print(f"Epoch {self.learn.epoch}: Plotting {len(self.learn.viz_targs)} (= {len(self.learn.dls.valid.items)}?) points:")
            urls = None
            if self.method==TrianglePlot2D_Bokeh:
                dv = self.learn.dls.valid       # dv is just a shorthand for "dls.valid"
                urls = [dv.url_dict[f] for f in dv.items] if 'url_dict' in dir(dv) else dv.items # image urls or filenames
            self.method(self.learn.viz_preds, self.learn.viz_targs, urls=urls, labels=self.dls.vocab,
                comment=f'Epoch {self.learn.epoch}', thumb_height=self.thumb_height).do_plot()

TrianglePlot2D_Bokeh is my mrspuff routine that allows for image-thumbnail previews, and these require that we generate and hang on to URLs that point to the images. (If you were to cut out all the URL-related stuff, this callback is really trivial.)

On regular-Jupyter (on your local machine, or on Paperspace Gradient, or Binder, etc.) the Jupyter server lets us just use filenames and it converts them all to URLs automatically for us! But on Colab, we need to generate the “sharing URLs”, and then remember those. So that’s what a lot of the extra verbiage in this callback is for: it’s about managing the image thumbnail URLs (if we’re plotting in 2D, i.e. 3 classes where we’re in the plane of the “triangle” that forms).

If there are in 4 or more dimensions, then we automatically trigger the Plotly-based plotter, which will automatically PCA-down to a 3D projection – so for 4 classes, we get a 3D tetrahedron. And there are no thumnail URLs because sadly Plotly doesn’t support those.

Acknowledgements:

Special thanks to Zach Mueller for assistance in interfacing with fastai!