%matplotlib inline
%reload_ext autoreload
%autoreload
import jovian
import torch
torch.cuda.set_device(0)
import fastai
from fastai.vision import models, untar_data
from fastai.datasets import URLs
data_path = untar_data(URLs.CIFAR)
data_path.ls()
[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')]
(data_path/'train').ls()
[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')]
(data_path/'test').ls()
[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')]
from fastai.vision import ImageList, get_transforms
from fastai.vision import *
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))
data
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
data.train_dl
data.valid_dl
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>)
data.show_batch()
data.classes
['airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck']
data.c
10
from fastai.vision import cnn_learner, models, Learner
from fastai.metrics import accuracy
learn = cnn_learner(data, models.resnet50, metrics=[accuracy])
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
learn.fit_one_cycle(5, max_lr=1e-03)
from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b3')
Loaded pretrained weights for efficientnet-b3
model._fc
Linear(in_features=1536, out_features=1000, bias=True)
from torch.nn import Linear
model._fc = Linear(1536, 10)
nn.init.kaiming_normal_(model._fc.weight);
model = model.to(torch.device("cuda"))
import torch.optim as optim
opt_func = partial(optim.Adam, betas=(0.9,0.99), eps=1e-6)
en_learn = (Learner(data, model,metrics=[accuracy,top_k_accuracy]))
en_learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
en_learn.recorder.plot()
en_learn.fit_one_cycle(5, max_lr=2e-03)