<jupyter_start><jupyter_text>Speed comparison between fast.ai and torchvision dataloaders. Using cifar-10 dataset with WideResNet22 as model.<jupyter_code># sources:
#  https://github.com/fastai/fastai/blob/master/courses/dl2/cifar10-dawn.ipynb
#  https://github.com/fastai/imagenet-fast/blob/master/cifar10/cifar10-super-convergence-tuned.ipynb
#  https://github.com/radekosmulski/machine_learning_notebooks/blob/master/cifar10_fastai_dawnbench.ipynb
# my hardware:
#    Intel i7-7700K@4.2GHz x 8(CPU) (actually 4 cores), 32GB of RAM
#    Zotac GeForce 1080Ti 11GB
#    MSI Z170I Gaming Pro motherboard
%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai.conv_learner import *
from fastai.models.cifar10.wideresnet import wrn_22
from datetime import datetime
torch.backends.cudnn.benchmark = True
PATH = Path("data/cifar10_dirs_by_classes/")
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
means,stdevs = (0.4914, 0.48216, 0.44653), (0.24703, 0.24349, 0.26159)
stats = (np.array(means), np.array(stdevs))

bs=512
sz=32
wd=1e-4
lr=1.5
nw=num_cpus()<jupyter_output><empty_output><jupyter_text>ver1: with fast.ai dataloaders<jupyter_code>tfms = tfms_from_stats(stats, sz, aug_tfms=[RandomCrop(sz), RandomFlip()], pad=sz//8)
data = ImageClassifierData.from_paths(PATH, val_name='test', tfms=tfms, bs=bs, num_workers=nw)
m = wrn_22()
learn = ConvLearner.from_model_data(m, data)
learn.crit = nn.CrossEntropyLoss()
learn.metrics = [accuracy]
start_time = datetime.now()
%time learn.fit(lr, 1, wds=wd, cycle_len=2, use_clr_beta=(20,20,0.95,0.85))
end_time = datetime.now()
str(end_time - start_time)
start_time = datetime.now()
%time learn.fit(lr, 1, wds=wd, cycle_len=30, use_clr_beta=(20,20,0.95,0.85))
end_time = datetime.now()
str(end_time - start_time)<jupyter_output><empty_output><jupyter_text>ver2: with torchvision dataloaders<jupyter_code>from torchvision import transforms as tvtf, datasets as tvds
from torch.utils.data import DataLoader as tdl
def get_loaders(bs, num_workers):
    traindir, valdir = str(PATH/'train'), str(PATH/'test')
    tfms = [tvtf.ToTensor(), tvtf.Normalize(means,stdevs)]
    aug_tfms = tvtf.Compose([tvtf.RandomCrop(sz, padding=4), tvtf.RandomHorizontalFlip()] + tfms)

    train_dataset = tvds.ImageFolder(traindir, aug_tfms)
    val_dataset = tvds.ImageFolder(valdir, tvtf.Compose(tfms))
    aug_dataset = tvds.ImageFolder(valdir, aug_tfms)

    train_loader = tdl(train_dataset, batch_size=bs, shuffle=True, num_workers=num_workers, pin_memory=True)
    val_loader = tdl(val_dataset, batch_size=bs, shuffle=False, num_workers=num_workers, pin_memory=True)
    aug_loader = tdl(aug_dataset, batch_size=bs, shuffle=False, num_workers=num_workers, pin_memory=True)

    return train_loader, val_loader, aug_loader

def get_data(bs, num_workers):
    trn_dl, val_dl, aug_dl = get_loaders(bs, num_workers)
    d = ModelData(PATH, trn_dl, val_dl)
    d.aug_dl = aug_dl
    d.sz=sz
    return d

data = get_data(bs, nw)
m = wrn_22()
learn = ConvLearner.from_model_data(m, data)
learn.crit = nn.CrossEntropyLoss()
learn.metrics = [accuracy]
start_time = datetime.now()
%time learn.fit(lr, 1, wds=wd, cycle_len=2, use_clr_beta=(20,20,0.95,0.85))
end_time = datetime.now()
str(end_time - start_time)
start_time = datetime.now()
%time learn.fit(lr, 1, wds=wd, cycle_len=30, use_clr_beta=(20,20,0.95,0.85))
end_time = datetime.now()
str(end_time - start_time)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13828867761452032, 'fraction_numerical': 0.04091040046096226, 'mean_word_length': 6.129363449691992, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '617927', 'license': 'MIT', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1375, 'n_words': 249, 'path': 'cifar-10_dataloader_comparison.ipynb', 'repo_name': 'urmaspitsi/deep-learning', 'size': '3471'}