Jovian
⭐️
Sign In
In [1]:
%matplotlib inline
%reload_ext autoreload
%autoreload

In [2]:
import jovian
import torch
In [3]:
torch.cuda.set_device(0)
In [4]:
import fastai
from fastai.vision import models, untar_data
from fastai.datasets import URLs
In [5]:
data_path = untar_data(URLs.CIFAR)
In [6]:
data_path.ls()
Out[6]:
[PosixPath('/home/jupyter/.fastai/data/cifar10/train'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/labels.txt'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/models')]
In [7]:
(data_path/'train').ls()
Out[7]:
[PosixPath('/home/jupyter/.fastai/data/cifar10/train/deer'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/frog'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/automobile'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/airplane'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/bird'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/horse'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/truck'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/ship'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/cat'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/train/dog')]
In [8]:
(data_path/'test').ls()
Out[8]:
[PosixPath('/home/jupyter/.fastai/data/cifar10/test/deer'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/frog'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/automobile'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/airplane'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/bird'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/horse'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/truck'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/ship'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/cat'),
 PosixPath('/home/jupyter/.fastai/data/cifar10/test/dog')]
In [9]:
from fastai.vision import ImageList, get_transforms
from fastai.vision import *

In [10]:
tfms = get_transforms()
bs=64
data = (ImageList.from_folder(data_path)
        .split_by_folder(valid='test')
        .label_from_folder()
        .transform(tfms, size=64)
        .databunch(bs=bs, device=torch.device("cuda"))
        .normalize(imagenet_stats))
In [11]:
data
Out[11]:
ImageDataBunch;

Train: LabelList (50000 items)
x: ImageList
Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64)
y: CategoryList
deer,deer,deer,deer,deer
Path: /home/jupyter/.fastai/data/cifar10;

Valid: LabelList (10000 items)
x: ImageList
Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64),Image (3, 64, 64)
y: CategoryList
deer,deer,deer,deer,deer
Path: /home/jupyter/.fastai/data/cifar10;

Test: None
In [12]:
data.train_dl
data.valid_dl
Out[12]:
DeviceDataLoader(dl=<torch.utils.data.dataloader.DataLoader object at 0x7f20ceae9080>, device=device(type='cuda'), tfms=[functools.partial(<function _normalize_batch at 0x7f20cffe6400>, mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250]), do_x=True, do_y=False)], collate_fn=<function data_collate at 0x7f20e0694d08>)
In [48]:
data.show_batch()
Notebook Image
In [13]:
data.classes
Out[13]:
['airplane',
 'automobile',
 'bird',
 'cat',
 'deer',
 'dog',
 'frog',
 'horse',
 'ship',
 'truck']
In [14]:
data.c
Out[14]:
10

ResNet50

In [20]:
from fastai.vision import cnn_learner, models, Learner
from fastai.metrics import accuracy
In [16]:
learn = cnn_learner(data, models.resnet50, metrics=[accuracy])
In [17]:
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Notebook Image
In [18]:
learn.fit_one_cycle(5, max_lr=1e-03)

EfficientNet

In [15]:
from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b3')
Loaded pretrained weights for efficientnet-b3
In [16]:
model._fc
Out[16]:
Linear(in_features=1536, out_features=1000, bias=True)
In [17]:
from torch.nn import Linear

model._fc = Linear(1536, 10)
nn.init.kaiming_normal_(model._fc.weight);
model = model.to(torch.device("cuda"))
In [18]:
import torch.optim as optim
In [19]:
opt_func = partial(optim.Adam, betas=(0.9,0.99), eps=1e-6)
In [20]:
en_learn = (Learner(data, model,metrics=[accuracy,top_k_accuracy]))
In [21]:
en_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [22]:
en_learn.recorder.plot()
Notebook Image
In [23]:
en_learn.fit_one_cycle(5, max_lr=2e-03)
In [ ]: