<filename>Image Classification with Fast.ai.py
# -*- coding: utf-8 -*-
"""Image Classification with Fast.ai

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/18adR92feq-9chhauf4qToOoVKWVCn7Rs

**Initialization**

*Let's import all the necessary packages.*
"""

!curl -s https://course.fast.ai/setup/colab | bash

# Commented out IPython magic to ensure Python compatibility.
# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline

from fastai.vision import *
from fastai.metrics import error_rate

"""**Looking at the Data**"""

help(untar_data)

# ??untar_data # Uncomment it to see the documentation and source code

path = untar_data(URLs.PETS)
path

"""*Let's check the Path*"""

path.ls()

path_anno = path/'annotations'
path_img = path/'images'

fnames = get_image_files(path_img)
fnames[:5]

"""*Let's set random seed to two to ensure the same Validation set every time*"""

np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'

# ??ImageDataBunch

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224,
                                   bs=64).normalize(imagenet_stats)

data.show_batch(rows=3, figsize=(7, 6))

print(data.classes)
len(data.classes), data.c

"""**Training the Data: ResNet**"""

learn = cnn_learner(data, models.resnet34, metrics=error_rate)

learn.model

learn.fit_one_cycle(4)

learn.save('stage-1')

"""**Results and Evaluation**"""

interp = ClassificationInterpretation.from_learner(learn)
losses, idxs = interp.top_losses()
len(data.valid_ds) == len(losses) == len(idxs)

interp.plot_top_losses(9, figsize=(15, 11))

doc(interp.plot_top_losses)

interp.plot_confusion_matrix(figsize=(12, 12), dpi=60)

interp.most_confused(min_val=2)

"""**Un-freezing, Learning rates and Fine Tuning**"""

learn.unfreeze()

learn.fit_one_cycle(1)

learn.load('stage-1')

learn.lr_find()

learn.recorder.plot()

learn.unfreeze()
learn.fit_one_cycle(1, max_lr=slice(1e-6, 1e-4))

"""**Training ResNet:50**"""

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(),
                                   size=299, bs=64//2).normalize(imagenet_stats)

learn = cnn_learner(data, models.resnet50, metrics=error_rate)

learn.lr_find()
learn.recorder.plot()

learn.fit_one_cycle(1)

learn.save('stage-1-50')

learn.unfreeze()
learn.fit_one_cycle(1, max_lr=slice(1e-6, 1e-4))

learn.load('stage-1-50')

interp = ClassificationInterpretation.from_learner(learn)

interp.most_confused(min_val=2)

"""*Hence, This Image Classification Model is 95% accurate.*"""

from google.colab import drive
drive.mount('/content/drive')


<filename>Image Classification with Fast.ai.py
# -*- coding: utf-8 -*-
"""Image Classification with Fast.ai

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/18adR92feq-9chhauf4qToOoVKWVCn7Rs

**Initialization**

*Let's import all the necessary packages.*
"""

!curl -s https://course.fast.ai/setup/colab | bash

# Commented out IPython magic to ensure Python compatibility.
# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline

from fastai.vision import *
from fastai.metrics import error_rate

"""**Looking at the Data**"""

help(untar_data)

# ??untar_data # Uncomment it to see the documentation and source code

path = untar_data(URLs.PETS)
path

"""*Let's check the Path*"""

path.ls()

path_anno = path/'annotations'
path_img = path/'images'

fnames = get_image_files(path_img)
fnames[:5]

"""*Let's set random seed to two to ensure the same Validation set every time*"""

np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'

# ??ImageDataBunch

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224,
                                   bs=64).normalize(imagenet_stats)

data.show_batch(rows=3, figsize=(7, 6))

print(data.classes)
len(data.classes), data.c

"""**Training the Data: ResNet**"""

learn = cnn_learner(data, models.resnet34, metrics=error_rate)

learn.model

learn.fit_one_cycle(4)

learn.save('stage-1')

"""**Results and Evaluation**"""

interp = ClassificationInterpretation.from_learner(learn)
losses, idxs = interp.top_losses()
len(data.valid_ds) == len(losses) == len(idxs)

interp.plot_top_losses(9, figsize=(15, 11))

doc(interp.plot_top_losses)

interp.plot_confusion_matrix(figsize=(12, 12), dpi=60)

interp.most_confused(min_val=2)

"""**Un-freezing, Learning rates and Fine Tuning**"""

learn.unfreeze()

learn.fit_one_cycle(1)

learn.load('stage-1')

learn.lr_find()

learn.recorder.plot()

learn.unfreeze()
learn.fit_one_cycle(1, max_lr=slice(1e-6, 1e-4))

"""**Training ResNet:50**"""

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(),
                                   size=299, bs=64//2).normalize(imagenet_stats)

learn = cnn_learner(data, models.resnet50, metrics=error_rate)

learn.lr_find()
learn.recorder.plot()

learn.fit_one_cycle(1)

learn.save('stage-1-50')

learn.unfreeze()
learn.fit_one_cycle(1, max_lr=slice(1e-6, 1e-4))

learn.load('stage-1-50')

interp = ClassificationInterpretation.from_learner(learn)

interp.most_confused(min_val=2)

"""*Hence, This Image Classification Model is 95% accurate.*"""

from google.colab import drive
drive.mount('/content/drive')


# -*- coding: utf-8 -*- Image Classification with Fast.ai  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/18adR92feq-9chhauf4qToOoVKWVCn7Rs  **Initialization**  *Let's import all the necessary packages.* # Commented out IPython magic to ensure Python compatibility. # %reload_ext autoreload # %autoreload 2 # %matplotlib inline **Looking at the Data** # ??untar_data # Uncomment it to see the documentation and source code *Let's check the Path* *Let's set random seed to two to ensure the same Validation set every time* # ??ImageDataBunch **Training the Data: ResNet** **Results and Evaluation** **Un-freezing, Learning rates and Fine Tuning** **Training ResNet:50** *Hence, This Image Classification Model is 95% accurate.*