Create DataLoader from Pytorch Dataset


I’m trying to create a Dataloader for an Image Classification task from the Cifar10 Pytorch dataset:

import torchvision.datasets as datasets
from import *

train_dataset = datasets.CIFAR10(root = 'datasets/',train = True ,transform = transforms.ToTensor(), download=True)
train_loader = DataLoader(train_dataset, bs = 64, shuffle=True)

but when I try to train the model:

learn = vision_learner(train_loader , resnet34, metrics=error_rate, n_out = 10, loss_func=CrossEntropyLossFlat())`

I’m getting the following error:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4280\ in 
----> 1 learn = vision_learner(train_loader, resnet34, metrics=error_rate, n_out = 10, loss_func=CrossEntropyLossFlat())
      2 learn.fine_tune(1)

c:\Users\Guillermo\Anaconda3\envs\nst\lib\site-packages\fastai\vision\ in vision_learner(dls, arch, normalize, n_out, pretrained, loss_func, opt_func, lr, splitter, cbs, metrics, path, model_dir, wd, wd_bn_bias, train_bn, moms, cut, init, custom_head, concat_pool, pool, lin_ftrs, ps, first_bn, bn_final, lin_first, y_range, **kwargs)
    225         if normalize: _timm_norm(dls, cfg, pretrained, n_in)
    226     else:
--> 227         if normalize: _add_norm(dls, meta, pretrained, n_in)
    228         model = create_vision_model(arch, n_out, pretrained=pretrained, **model_args)

c:\Users\Guillermo\Anaconda3\envs\nst\lib\site-packages\fastai\vision\ in _add_norm(dls, meta, pretrained, n_in)
    194     if stats is None: return
    195     if n_in != len(stats[0]): return
--> 196     if not dls.after_batch.fs.filter(risinstance(Normalize)):
    197         dls.add_tfms([Normalize.from_stats(*stats)],'after_batch')

AttributeError: 'function' object has no attribute 'fs'

I’ve read the following post with a similar problem, but it does not work:

How would be the right way to load this cifar10 pytorch dataset into a dataloader to fit a learner?

Thank you!

You need to set add_norm=False iirc when doing vision_learner

Thank you @muellerzr, but there is no add_norm. I tried with normalize = False, but there is still an error:

TypeError                                 Traceback (most recent call last)
<ipython-input-20-1fd205003e23> in <module>
      1 learn = vision_learner(train_loader , resnet34, metrics=error_rate, n_out = 10, loss_func=CrossEntropyLossFlat(), normalize=False)
----> 2 learn.fine_tune(2)

18 frames
/usr/local/lib/python3.9/dist-packages/fastprogress/ in __init__(self, gen, total, display, leave, parent, master, comment)
     17     def __init__(self, gen, total=None, display=True, leave=True, parent=None, master=None, comment=''):
     18         self.gen,self.parent,self.master,self.comment = gen,parent,master,comment
---> 19 = None if total=='noinfer' else len(gen) if total is None else total
     20         self.last_v = 0
     21         if parent is None: self.leave,self.display = leave,display

TypeError: Exception occured in `ProgressCallback` when calling event `before_train`:
	object of type 'bool' has no len()

This is because your dataloader needs to have a length