Introduction to Deep Learning using Fastai#

What are we going to do?

  • Create a bird vs forest dataset

  • Train DL model with Datablock api

  • Different type of problems with fastai

    • Vision

    • Segmentation

    • Tabular

    • Collaborative Filtering/ Recommendation

    • NLP

Imports#

%load_ext autoreload
%autoreload 2
from fastai.vision.all import *
from fastai.vision.widgets import *
from fastai.text.all import *
from fastai.tabular.all import *
from fastai.collab import *
from aiking.data.external import *

Birds vs Forests#

Construct Dataset#

clstypes = ["Birds", "Forests"]
dest = "BirdsVsForests"
path = construct_image_dataset(clstypes, dest, key=os.getenv("BING_KEY"), loc=None, count=150, engine='bing'); path
Dowloading images in Birds
Finished Dowloading images in Birds
Dowloading images in Forests
Finished Dowloading images in Forests
[Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Forests/e47d84fc-3204-4bd5-bc95-d0c36ebd2405.jpg'), Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Forests/e404b784-9682-412b-bdd7-a5d655763f2f.jpg')]
[]
[]
Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests')
path.ls()
(#2) [Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Forests')]
(path/"Birds").ls()
(#148) [Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/20d125a1-52eb-4d23-839b-ff03376503e9.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/3a0d4567-c508-4504-8f01-55fd7d3fbe12.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/85fe4078-54b1-40d8-8632-5048178ac8d1.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/95f4fa09-f937-4a3c-b123-a76e0097ab9e.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/f9ec6a9a-dfdd-4c47-b508-19f6b10dd74e.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/62671d29-0765-46fd-9c9f-6f164e7cf0da.jpeg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/41871c58-ba0a-4af8-bb47-98a51f25a20d.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/3859af1e-517d-4eec-a297-549e5ab3c5a5.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/2ed9f88b-f1f0-4fcb-a889-9b8a570100ce.jpg'),Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests/Birds/a7865e04-8579-4e0b-b13d-809c1f34fdb7.jpg')...]
path.resolve()
Path('/home/rahul.saraf/rahuketu/programming/AIKING_HOME/data/BirdsVsForests')

Setup DataBlock and Dataloaders#

dls = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y=parent_label,
    item_tfms=[Resize(192,method='squish')]
).dataloaders(path)
dls.show_batch(max_n=6)
../../_images/01_intro2dl_13_0.png
learn = vision_learner(dls, resnet18, metrics=[error_rate, accuracy])
learn.fine_tune(3)
/opt/anaconda/envs/aiking/lib/python3.8/site-packages/torchvision/models/_utils.py:135: UserWarning: Using 'weights' as positional parameter(s) is deprecated since 0.13 and will be removed in 0.15. Please use keyword parameter(s) instead.
  warnings.warn(
/opt/anaconda/envs/aiking/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
epoch train_loss valid_loss error_rate accuracy time
0 1.087279 0.815820 0.310345 0.689655 00:19
epoch train_loss valid_loss error_rate accuracy time
0 0.097448 0.053835 0.000000 1.000000 00:18
1 0.054589 0.000806 0.000000 1.000000 00:20
2 0.036313 0.000120 0.000000 1.000000 00:18

Use trained model#

uploader = widgets.FileUpload(); uploader
img = PILImage.create(uploader.data[0]); img
../../_images/01_intro2dl_17_0.png
is_bird,_, probs = learn.predict(img)
is_bird, probs[0]
('Birds', TensorBase(1.0000))
print(f"Probability it's a bird : {probs[0]:.3f}")
Probability it's a bird : 1.000