Hi.
Can anyone help me through this error. I’m actually trying to extract labels from list of file paths using ImageDataBunch.from_name_re
and ImageDataBunch.from_name_func
, but I get the same error every time. I tried to update the pillow library, but it seems to be using python 3.7 where as my conda enviorment is using python 3.6.
First I extract the file paths using the function I created.
Then I search the path and extract all sub-paths into a list. Function to get labels from individual path was defined in fast ai’s documentation, so I used that. The moment I create a ImageDataBunch I get this error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/.local/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode)
2846 try:
-> 2847 fp.seek(0)
2848 except (AttributeError, io.UnsupportedOperation):
AttributeError: 'PosixPath' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-21-415196e37479> in <module>
----> 1 data = ImageDataBunch.from_name_func(path, fn_paths, label_func = get_labels, size = 24)
~/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in from_name_func(cls, path, fnames, label_func, valid_pct, seed, **kwargs)
145 "Create from list of `fnames` in `path` with `label_func`."
146 src = ImageList(fnames, path=path).split_by_rand_pct(valid_pct, seed)
--> 147 return cls.create_from_ll(src.label_from_func(label_func), **kwargs)
148
149 @classmethod
~/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in create_from_ll(cls, lls, bs, val_bs, ds_tfms, num_workers, dl_tfms, device, test, collate_fn, size, no_check, resize_method, mult, padding_mode, mode, tfm_y)
95 "Create an `ImageDataBunch` from `LabelLists` `lls` with potential `ds_tfms`."
96 lls = lls.transform(tfms=ds_tfms, size=size, resize_method=resize_method, mult=mult, padding_mode=padding_mode,
---> 97 mode=mode, tfm_y=tfm_y)
98 if test is not None: lls.add_test_folder(test)
99 return lls.databunch(bs=bs, val_bs=val_bs, dl_tfms=dl_tfms, num_workers=num_workers, collate_fn=collate_fn,
~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in transform(self, tfms, **kwargs)
503 if not tfms: tfms=(None,None)
504 assert is_listy(tfms) and len(tfms) == 2, "Please pass a list of two lists of transforms (train and valid)."
--> 505 self.train.transform(tfms[0], **kwargs)
506 self.valid.transform(tfms[1], **kwargs)
507 if self.test: self.test.transform(tfms[1], **kwargs)
~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in transform(self, tfms, tfm_y, **kwargs)
722 def transform(self, tfms:TfmList, tfm_y:bool=None, **kwargs):
723 "Set the `tfms` and `tfm_y` value to be applied to the inputs and targets."
--> 724 _check_kwargs(self.x, tfms, **kwargs)
725 if tfm_y is None: tfm_y = self.tfm_y
726 tfms_y = None if tfms is None else list(filter(lambda t: getattr(t, 'use_on_y', True), listify(tfms)))
~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
591 if (tfms is None or len(tfms) == 0) and len(kwargs) == 0: return
592 if len(ds.items) >= 1:
--> 593 x = ds[0]
594 try: x.apply_tfms(tfms, **kwargs)
595 except Exception as e:
~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in __getitem__(self, idxs)
118 "returns a single item based if `idxs` is an integer or a new `ItemList` object if `idxs` is a range."
119 idxs = try_int(idxs)
--> 120 if isinstance(idxs, Integral): return self.get(idxs)
121 else: return self.new(self.items[idxs], inner_df=index_row(self.inner_df, idxs))
122
~/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in get(self, i)
269 def get(self, i):
270 fn = super().get(i)
--> 271 res = self.open(fn)
272 self.sizes[i] = res.size
273 return res
~/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in open(self, fn)
265 def open(self, fn):
266 "Open image in `fn`, subclass and overwrite for custom behavior."
--> 267 return open_image(fn, convert_mode=self.convert_mode, after_open=self.after_open)
268
269 def get(self, i):
~/anaconda3/lib/python3.7/site-packages/fastai/vision/image.py in open_image(fn, div, convert_mode, cls, after_open)
396 with warnings.catch_warnings():
397 warnings.simplefilter("ignore", UserWarning) # EXIF warning from TiffPlugin
--> 398 x = PIL.Image.open(fn).convert(convert_mode)
399 if after_open: x = after_open(x)
400 x = pil2tensor(x,np.float32)
~/.local/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode)
2847 fp.seek(0)
2848 except (AttributeError, io.UnsupportedOperation):
-> 2849 fp = io.BytesIO(fp.read())
2850 exclusive_fp = True
2851
AttributeError: 'PosixPath' object has no attribute 'read'