Jovian
⭐️
Sign In
In [2]:
from fastai.vision import *
from fastai.metrics import error_rate
In [3]:
!pip install jovian --upgrade
Collecting jovian Downloading https://files.pythonhosted.org/packages/de/a0/b4bc29837a7dd2d561e0291d40e8ebfa77ab1de5c7e9c1e338692ae47450/jovian-0.1.53.tar.gz Requirement already satisfied, skipping upgrade: requests in /opt/conda/lib/python3.6/site-packages (from jovian) (2.21.0) Collecting uuid (from jovian) Downloading https://files.pythonhosted.org/packages/ce/63/f42f5aa951ebf2c8dac81f77a8edcc1c218640a2a35a03b9ff2d4aa64c3d/uuid-1.30.tar.gz Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in /opt/conda/lib/python3.6/site-packages (from requests->jovian) (1.22) Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.6/site-packages (from requests->jovian) (3.0.4) Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /opt/conda/lib/python3.6/site-packages (from requests->jovian) (2019.3.9) Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /opt/conda/lib/python3.6/site-packages (from requests->jovian) (2.6) Building wheels for collected packages: jovian, uuid Building wheel for jovian (setup.py) ... done Stored in directory: /tmp/.cache/pip/wheels/60/73/79/0926f9cc17a6f1686c5b2356b299947bb3f6972f5d160916b2 Building wheel for uuid (setup.py) ... done Stored in directory: /tmp/.cache/pip/wheels/2a/80/9b/015026567c29fdffe31d91edbe7ba1b17728db79194fca1f21 Successfully built jovian uuid Installing collected packages: uuid, jovian Successfully installed jovian-0.1.53 uuid-1.30 You are using pip version 19.0.3, however version 19.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
In [4]:
import jovian
In [5]:
path = untar_data(URLs.PETS); path
Out[5]:
PosixPath('/tmp/.fastai/data/oxford-iiit-pet')
In [6]:
path_anno = path/'annotations'
path_img = path/'images'
In [7]:
fnames = get_image_files(path_img)
fnames[:5]
Out[7]:
[PosixPath('/tmp/.fastai/data/oxford-iiit-pet/images/Ragdoll_158.jpg'),
 PosixPath('/tmp/.fastai/data/oxford-iiit-pet/images/Egyptian_Mau_160.jpg'),
 PosixPath('/tmp/.fastai/data/oxford-iiit-pet/images/staffordshire_bull_terrier_119.jpg'),
 PosixPath('/tmp/.fastai/data/oxford-iiit-pet/images/great_pyrenees_127.jpg'),
 PosixPath('/tmp/.fastai/data/oxford-iiit-pet/images/english_setter_37.jpg')]
In [8]:
np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'
In [10]:
bs=64
In [11]:
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs).normalize(imagenet_stats)
In [12]:
data.show_batch(rows=3, figsize=(7,6))
Notebook Image
In [13]:
print(data.classes)
len(data.classes),data.c
['Abyssinian', 'Bengal', 'Birman', 'Bombay', 'British_Shorthair', 'Egyptian_Mau', 'Maine_Coon', 'Persian', 'Ragdoll', 'Russian_Blue', 'Siamese', 'Sphynx', 'american_bulldog', 'american_pit_bull_terrier', 'basset_hound', 'beagle', 'boxer', 'chihuahua', 'english_cocker_spaniel', 'english_setter', 'german_shorthaired', 'great_pyrenees', 'havanese', 'japanese_chin', 'keeshond', 'leonberger', 'miniature_pinscher', 'newfoundland', 'pomeranian', 'pug', 'saint_bernard', 'samoyed', 'scottish_terrier', 'shiba_inu', 'staffordshire_bull_terrier', 'wheaten_terrier', 'yorkshire_terrier']
Out[13]:
(37, 37)

Training: Resnet34

In [14]:
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
Downloading: "https://download.pytorch.org/models/resnet34-333f7ec4.pth" to /tmp/.torch/models/resnet34-333f7ec4.pth 87306240it [00:00, 101168141.09it/s]
In [22]:
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [23]:
learn.recorder.plot()
Notebook Image
In [24]:
lr = 0.01
In [25]:
params = {
    'batch_size': bs,
    'n_epochs': 4,
    'learning_rate': lr,
    'annealing': 'one_cycle'
}
In [26]:
jovian.log_hyperparams(params)
[jovian] Please enter your API key (from https://jvn.io ): ········ [jovian] Hypermaters logged.
In [27]:
learn.fit_one_cycle(4)
In [28]:
metrics = {
    'train_loss': 0.250892,
    'valid_loss': 0.222714,
    'error_rate': 0.069012,
    'time': '01:24'
}
In [29]:
jovian.log_metrics(metrics)
[jovian] Metrics logged.
In [30]:
jovian.commit()
[jovian] Saving notebook..
[jovian] Creating a new notebook on https://jvn.io [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Recording metrics & hyperparameters.. [jovian] Committed successfully! https://jvn.io/aakashns/d1fb0571727d4cbc9091d10b00c55b35
In [31]:
learn.save('stage-1')

Unfreeze and fine-tune

In [32]:
learn.unfreeze()
In [33]:
params = {
    'batch_size': bs,
    'n_epochs': 1,
    'learning_rate': lr,
    'annealing': 'normal'
}
In [34]:
jovian.log_hyperparams(params)
[jovian] Hypermaters logged.
In [35]:
learn.fit(1, lr)
In [36]:
metrics = {
    'train_loss': 4.031126,
    'valid_loss': 4.994879,
    'error_rate': 0.931664,
    'time': '01:26'
}
In [37]:
jovian.log_metrics(metrics)
[jovian] Metrics logged.
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: