I am not 100% sure that this is an error that you guys will care about, but I built a DataBunch using two datasets from a class I built. The issue might be my understanding of datasets, but here is what my dataset class looks like:
class DCMImageDictDataset(LabelDataset):
"Abstract `Dataset` containing images."
def __init__(self, dictionary):
self.patientID = list(dictionary.keys())
#self.imgs = trn_dict
#self.y = np.array(y)
self.images = []
self.bbs = []
self.lbs = []
for i in list(dictionary.values()):
image = i[0][0]
bb = [k for j,k,l in i]
lb = [l for j,k,l in i]
self.images.append(image)
self.bbs.append(bb)
self.lbs.append(lb)
def __getitem__(self,i): return self.patientID[i],(self.images[i], self.bbs[i], self.lbs[i])
def __getattr__(self, name): return f'Invalid attribute: {name}'
def __repr__(self): return f'{len(self.patientID)} total records'
def __len__(self): return len(self.patientID)
@property
def c(self):
unique_list = []
unique_words = 0
for i in self.lbs:
for j in i:
if j not in unique_list:
unique_list.append(j)
unique_words+=1
return unique_words
The biggest things here are that when I feed in a number, it gives me an id, image, bounding box coordinates, and labels for those boxes. (this is for the RSNA Pneumonia challenge).
Once I have my train and validation dataset, I create a DataBunch with this:
trn_ds = DCMImageDictDataset(trn_anno)
val_ds = DCMImageDictDataset(val_anno)
bs = 2
sz = 224
data = DataBunch.create(trn_ds, val_ds, path=PATH, bs=bs, num_workers=0)
When I try to do a learn.fit, I get AttributeError: 'str' object has no attribute 'to'
which I was able to narrow down to it not liking how my dataloader is trying to pull information out. It wants an xb and yb so I am wondering if I need to change something clear back at the top with my DCMImageDictDataset class?
Here is a dump of the error if that is helpful (for xb,yb in progress_bar(data.train_dl, parent=pbar)
):
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-79-021fad3debde> in <module>()
----> 1 learn.fit(a, 1e-5)
~/fastai_v1/fastai/kaggle/Competitions/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
127 callbacks = [cb(self) for cb in self.callback_fns] + listify(callbacks)
128 fit(epochs, self.model, self.loss_fn, opt=self.opt, data=self.data, metrics=self.metrics,
--> 129 callbacks=self.callbacks+callbacks)
130
131 def create_opt(self, lr:Floats, wd:Floats=0.)->None:
~/fastai_v1/fastai/kaggle/Competitions/fastai/basic_train.py in fit(epochs, model, loss_fn, opt, data, callbacks, metrics)
80 except Exception as e:
81 exception = e
---> 82 raise e
83 finally: cb_handler.on_train_end(exception)
84
~/fastai_v1/fastai/kaggle/Competitions/fastai/basic_train.py in fit(epochs, model, loss_fn, opt, data, callbacks, metrics)
64 cb_handler.on_epoch_begin()
65
---> 66 for xb,yb in progress_bar(data.train_dl, parent=pbar):
67 xb, yb = cb_handler.on_batch_begin(xb, yb)
68 loss,_ = loss_batch(model, xb, yb, loss_fn, opt, cb_handler)
~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastprogress/fastprogress.py in __iter__(self)
59 self.update(0)
60 try:
---> 61 for i,o in enumerate(self._gen):
62 yield o
63 if self.auto_update: self.update(i+1)
~/fastai_v1/fastai/kaggle/Competitions/fastai/data.py in proc_batch(self, b)
39 def proc_batch(self,b:Tensor)->Tensor:
40 "Proces batch `b` of `TensorImage`."
---> 41 b = to_device(b, self.device)
42 for f in listify(self.tfms): b = f(b)
43 return b
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in <listcomp>(.0)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in <listcomp>(.0)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
72 device = ifnone(device, default_device)
73 if is_listy(b): return [to_device(o, device) for o in b]
---> 74 return b.to(device)
75
76 def data_collate(batch:ItemsList)->Tensor:
AttributeError: 'str' object has no attribute 'to'
Here is an easier piece of code to recreate the same issue:
for xb, yb in data.train_dl:
print(xb)
Gives this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-78-095290fa3351> in <module>()
----> 1 for xb, yb in data.train_dl:
2 print(xb)
~/fastai_v1/fastai/kaggle/Competitions/fastai/data.py in proc_batch(self, b)
39 def proc_batch(self,b:Tensor)->Tensor:
40 "Proces batch `b` of `TensorImage`."
---> 41 b = to_device(b, self.device)
42 for f in listify(self.tfms): b = f(b)
43 return b
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in <listcomp>(.0)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in <listcomp>(.0)
71 "Ensure `b` is on `device`."
72 device = ifnone(device, default_device)
---> 73 if is_listy(b): return [to_device(o, device) for o in b]
74 return b.to(device)
75
~/fastai_v1/fastai/kaggle/Competitions/fastai/torch_core.py in to_device(b, device)
72 device = ifnone(device, default_device)
73 if is_listy(b): return [to_device(o, device) for o in b]
---> 74 return b.to(device)
75
76 def data_collate(batch:ItemsList)->Tensor:
AttributeError: 'str' object has no attribute 'to'