FastAI Demonstration: LeafSnap

See the full explanation behind this demonstration notebook at FastAI quick test / LeafSnap.

In [1]:
from google.colab import drive
drive.mount('/content/gdrive')
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly

Enter your authorization code:
··········
Mounted at /content/gdrive
In [2]:
from pathlib import Path
drive_path = Path('/content/gdrive/My Drive/leafsnap')
base_path = Path('/content/leafsnap-dataset')
In [3]:
!pip install "torch==1.4" "torchvision==0.5.0"
Collecting torch==1.4
  Downloading https://files.pythonhosted.org/packages/24/19/4804aea17cd136f1705a5e98a00618cb8f6ccc375ad8bfa437408e09d058/torch-1.4.0-cp36-cp36m-manylinux1_x86_64.whl (753.4MB)
     |████████████████████████████████| 753.4MB 18kB/s 
Collecting torchvision==0.5.0
  Downloading https://files.pythonhosted.org/packages/7e/90/6141bf41f5655c78e24f40f710fdd4f8a8aff6c8b7c6f0328240f649bdbe/torchvision-0.5.0-cp36-cp36m-manylinux1_x86_64.whl (4.0MB)
     |████████████████████████████████| 4.0MB 21.1MB/s 
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from torchvision==0.5.0) (1.15.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torchvision==0.5.0) (1.18.5)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.5.0) (7.0.0)
Installing collected packages: torch, torchvision
  Found existing installation: torch 1.6.0+cu101
    Uninstalling torch-1.6.0+cu101:
      Successfully uninstalled torch-1.6.0+cu101
  Found existing installation: torchvision 0.7.0+cu101
    Uninstalling torchvision-0.7.0+cu101:
      Successfully uninstalled torchvision-0.7.0+cu101
Successfully installed torch-1.4.0 torchvision-0.5.0
In [4]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
In [5]:
from fastai.vision import *
from fastai.metrics import error_rate
In [19]:
img = open_image(drive_path/'maple.jpg')
img
Output hidden; open in https://colab.research.google.com to view.
In [9]:
model_rn34 = load_learner(drive_path, 'leafsnap-rn34-4e-ft1-ft8.pkl')
In [20]:
pred_class, pred_idx, outputs = model_rn34.predict(img)
pred_class.obj
Out[20]:
'Maple'
In [34]:
def predict_file(file_name):
  img = open_image(drive_path/file_name)
  pred_class, pred_idx, outputs = model_rn34.predict(img)
  img.show(title=f'Prediction: {pred_class.obj}, with {outputs.max()*100:2.0f}% confidence', figsize=(10,10))
In [35]:
predict_file('maple.jpg')
In [36]:
predict_file('spruce.jpg')
In [50]:
predict_file('lilac.jpg')
In [38]:
predict_file('unknown_1.jpg')
In [39]:
predict_file('unknown_2.jpg')
In [40]:
predict_file('unknown_3.jpg')