AttributeError: device with fast.ai learner

Hi, I am building a cnn learner using resnet18 with the following code:

from fastai.vision import *

data = (ImageList.from_df(train_df, path='/kaggle/working/')
   .split_by_rand_pct(0.2)
   .label_from_df(label_delim=','))

learner = cnn_learner(data, models.resnet18, metrics=[accuracy])

I get the following error:

AttributeError                            Traceback (most recent call last)

in
----> 1 learner = cnn_learner(data, models.resnet18, metrics=[accuracy])

/opt/conda/lib/python3.6/site-packages/fastai/vision/learner.py in cnn_learner(data, base_arch, cut, pretrained, lin_ftrs, ps, custom_head, split_on, bn_final, init, concat_pool, **kwargs)
97 model = create_cnn_model(base_arch, data.c, cut, pretrained, lin_ftrs, ps=ps, custom_head=custom_head,
98 bn_final=bn_final, concat_pool=concat_pool)
—> 99 learn = Learner(data, model, **kwargs)
100 learn.split(split_on or meta[‘split’])
101 if pretrained: learn.freeze()

in init(self, data, model, opt_func, loss_func, metrics, true_wd, bn_wd, wd, train_bn, path, model_dir, callback_fns, callbacks, layer_groups, add_time, silent)

/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in post_init(self)
163 “Setup path,metrics, callbacks and ensure model directory exists.”
164 self.path = Path(ifnone(self.path, self.data.path))
–> 165 self.model = self.model.to(self.data.device)
166 self.loss_func = self.loss_func or self.data.loss_func
167 self.metrics=listify(self.metrics)

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in getattr(self, k)
468
469 def getattr(self, k):
–> 470 ft = getattr(self.train, k)
471 if not isinstance(ft, Callable): return ft
472 fv = getattr(self.valid, k)

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in getattr(self, k)
641 res = getattr(y, k, None)
642 if res is not None: return res
–> 643 raise AttributeError(k)
644
645 def setstate(self,data:Any): self.dict.update(data)

AttributeError: device

1 Like

I had a same error, I am not sure why I ran into it and not much info is available.

By looking at the error and source code I tried this, before the call to cnn_learner
setattr(data, 'device', 'cuda')

It could be 'cpu' instead of cuda for someone.

2 Likes