Skip to main content

Utility functions for the fast ai mooc

Project description

duckgoose

Utility scripts for the online fast.ai course. There are two main parts: one to download and organise arbitrary image classes, and one to highlight what parts of an image is activating the decision for a classification.

  1. Utility for Lesson 1 experimentation with external image classes. The script:
  • Downloads images from google images for specific classes
  • Sanity checks that images can be opened and have three channels
  • Organises the images into separate folders (train/valid/test + classes) as expected by the fast.ai library
  1. Utility for creating Class Activation Maps for both classifications.

Prerequisites

  • chromedriver is required. On ubuntu/debian: sudo apt-get chromium-chromedriver

Installation

pip install duckgoose

Usage

Fetching, sanity checking and organising images

from duckgoose import fetchImagesAndPrepForClassification

# dictionary structure `class_name => search term`
image_classes = { 'ducks' : 'ducks -rubber' , 'geese' : 'geese' }
download_path = '/home/myuser/data/downloaded_from_google'
output_path = '/home/myuser/data/ducksgeese/'
number_of_images = 100

fetchImagesAndPrepForClassification(image_classes, download_path, output_path, number_of_images)

Create Class Activation Maps (CAM)

Note: This was implemented for fastai v2 part 1. Here is a full example of creating a class activation maps for ducks and geese using fast ai.

from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *

from duckgoose.cam import calculateAndChartHeatZoneFor

PATH = "data/ducksgeese/"
sz=224
arch = resnet34
bs = 64

m = arch(True)
m = nn.Sequential(*children(m)[:-2], 
                  nn.Conv2d(512, 2, 3, padding=1), 
                  nn.AdaptiveAvgPool2d(1),
                  Flatten(), 
                  nn.LogSoftmax())

tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs)
learn = ConvLearner.from_model_data(m, data)

learn.freeze_to(-4)

_, val_tfms = tfms_from_model(learn.model, sz)

learn.fit(0.01, 2)

calculateAndChartHeatZoneFor('./data/ducksgeese/test/ducks/ducks_427.jpg', val_tfms, learn)

Duck and goose heatmap

License

The MIT License (MIT)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

duckgoose-0.1.8.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file duckgoose-0.1.8.tar.gz.

File metadata

  • Download URL: duckgoose-0.1.8.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for duckgoose-0.1.8.tar.gz
Algorithm Hash digest
SHA256 b767cb7d06c07febe6104956cb38eaa2bb447876d624010f3f99fd0cfc45cb91
MD5 0f7577bb152ff4b318fb2596af565fb1
BLAKE2b-256 802256e3dee6d7c3c78b420f3d28b1ee66eefb3e8a531275d8a77ae66ac4a95c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page