AttributeError: device with learner

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

from import *

data = (ImageList.from_df(train_df, path='/kaggle/working/')

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

I get the following error:

AttributeError                            Traceback (most recent call last)

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

/opt/conda/lib/python3.6/site-packages/fastai/vision/ 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/ in post_init(self)
163 “Setup path,metrics, callbacks and ensure model directory exists.”
164 self.path = Path(ifnone(self.path,
–> 165 self.model =
166 self.loss_func = self.loss_func or
167 self.metrics=listify(self.metrics)

/opt/conda/lib/python3.6/site-packages/fastai/ in getattr(self, k)
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/ in getattr(self, k)
641 res = getattr(y, k, None)
642 if res is not None: return res
–> 643 raise AttributeError(k)
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.