Lesson 2 official topic

Hey @Akindele I believe the new keyword is reassigning a new image transformation to the set of images you’re preprocessing. I have yet to test this with my own data but if you look at the new method and the _merge_tfms method in fastAI’s Github repo it looks like it’s passing the values you pass into the new method back int othe item_tfms parameter.

image

Hi @benkarr,
I am a new student of this course, and I am having problems with the ImageClassifierCleaner.

I tried to run the delete and change cell, for just one selection of black bear from the train set, as you told to… if I am not wrong.
However I am not sure… why do I get this error.

Is there any way to solve it…

If anyone else has solved the problem, kindly let me know.
Thanks in advance.

The error says that there is already an image with that file name. You are moving images from one folder to another with the cleaner (Change image category.). If you already ran this cell or notebook before the images you changed category earlier are still in that new folder.

Thank you for the reply, I think I got it where I was mistaken.
I was actually changing the labels for the images, which were already correct. I think that is why I got the error that the file exists already.

So, with image cleaner, I believe we only need to change those files which seem to be incorrect with the label or needs to be deleted. Rest <> label can stay.

N Once we are done cleaning the data I think we need to re-run the data loaders and train the model.

Also, I believe we need to run the cells after each pair of category and train/valid set.

for idx in cleaner.delete(): cleaner.fns[idx].unlink()
for idx,cat in cleaner.change():shutil.move(str(cleaner.fns[idx]), path/cat)
1 Like

Anybody encountered this error while running this in the course:
pred,pred_idx,probs = learn_inf.predict(img)
?

Thanks!


AttributeError Traceback (most recent call last)
in
----> 1 pred,pred_idx,probs = learn_inf.predict(img)

26 frames
/usr/local/lib/python3.8/dist-packages/fastai/learner.py in predict(self, item, rm_type_tfms, with_input)
319 def predict(self, item, rm_type_tfms=None, with_input=False):
320 dl = self.dls.test_dl([item], rm_type_tfms=rm_type_tfms, num_workers=0)
→ 321 inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
322 i = getattr(self.dls, ‘n_inp’, -1)
323 inp = (inp,) if i==1 else tuplify(inp)

/usr/local/lib/python3.8/dist-packages/PIL/Image.py in open(fp, mode, formats)
2982 exclusive_fp = True
2983
→ 2984 prefix = fp.read(16)
2985
2986 preinit()

/usr/local/lib/python3.8/dist-packages/PIL/Image.py in getattr(self, name)
544 )
545 return self._category
→ 546 raise AttributeError(name)
547
548 @property

AttributeError: read

1 Like

Instead of creating a PIL, you can just pass the filename. PILImages are no longer supported in learn.predict() in v2.7.11:

2 Likes

Thank you so much, it worked!!

A bit off topic, but maybe some will find this interesting: I made an account on landing.ai and uploaded the bear images to see how their training went. The results are equal (although their training took way longer):

They have built a very nice interface on top of their tools, making it even more accessible for non-techies.

Might be a cool project to turn fast.ai as well into a SaaS. I know quite a bit about building a SaaS platform, but I’m just a newbie in AI (it’s lesson two after all :slight_smile:). If anyone is interested to explore this, please let me know.

1 Like

Is it just me? All examples on Home | Jeremy Howard (tinypets) are broken.

Did something change with the hf API?

Why is all the output in the book has two outputs? first output is always epoch0, and the second output depends on how many epochs you trained, if you train 1, the second output will just be one line about epoch0. if you train 5, the second output will be five lines from epoch0 to epock4. I’m really confused, isn’t there supposed to be just one output?

Thanks.

I assume you are talking about the output when using fine_tune? It is showing the output in two parts, initial frozen epochs followed by the unfrozen epochs.

Fine tune with Learner.freeze for freeze_epochs , then with Learner.unfreeze for epochs , using discriminative LR.

docs: learner.fine_tune

More on Unfreezing and Transfer Learning in the fastai book CH05_pet_breeds

tip: when working through the notebooks, you can use ?? to find out more about a function. e.g learn.fine_tune?? or can also use doc(learn.fine_tune).

Thanks Allen. I got it now.

Gordon

I am following the book and lecture 2 fastai (2022) on the grizzly bear chapter…all the cells are working except when I put the code and attempt to run:
dls = bears.dataloaders(path)

I get this error message:

TypeError Traceback (most recent call last)
/tmp/ipykernel_27/3866548988.py in
----> 1 dls = bears.dataloaders(path)

/opt/conda/lib/python3.7/site-packages/fastai/data/block.py in dataloaders(self, source, path, verbose, **kwargs)
153 **kwargs
154 ) → DataLoaders:
→ 155 dsets = self.datasets(source, verbose=verbose)
156 kwargs = {**self.dls_kwargs, **kwargs, ‘verbose’: verbose}
157 return dsets.dataloaders(path=path, after_item=self.item_tfms, after_batch=self.batch_tfms, **kwargs)

/opt/conda/lib/python3.7/site-packages/fastai/data/block.py in datasets(self, source, verbose)
145 splits = (self.splitter or RandomSplitter())(items)
146 pv(f"{len(splits)} datasets of sizes {‘,’.join([str(len(s)) for s in splits])}", verbose)
→ 147 return Datasets(items, tfms=self._combine_type_tfms(), splits=splits, dl_type=self.dl_type, n_inp=self.n_inp, verbose=verbose)
148
149 def dataloaders(self,

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in init(self, items, tfms, tls, n_inp, dl_type, **kwargs)
452 ):
453 super().init(dl_type=dl_type)
→ 454 self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
455 self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
456

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in (.0)
452 ):
453 super().init(dl_type=dl_type)
→ 454 self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
455 self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
456

/opt/conda/lib/python3.7/site-packages/fastcore/foundation.py in call(cls, x, *args, **kwargs)
96 def call(cls, x=None, *args, **kwargs):
97 if not args and not kwargs and x is not None and isinstance(x,cls): return x
—> 98 return super().call(x, *args, **kwargs)
99
100 # %% …/nbs/02_foundation.ipynb 46

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in init(self, items, tfms, use_list, do_setup, split_idx, train_setup, splits, types, verbose, dl_type)
366 if do_setup:
367 pv(f"Setting up {self.tfms}", verbose)
→ 368 self.setup(train_setup=train_setup)
369
370 def _new(self, items, split_idx=None, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastai/data/core.py in setup(self, train_setup)
395 x = f(x)
396 self.types.append(type(x))
→ 397 types = L(t if is_listy(t) else [t] for t in self.types).concat().unique()
398 self.pretty_types = ‘\n’.join([f’ - {t}’ for t in types])
399

TypeError: ‘NoneType’ object is not iterable

It looks like you either haven’t specified what path is, or there is no data file where path is pointing.

Sharing a quick “bay area birds classifier” prototype: https://jingyun.xyz/bay-area-birds

(well, it only classifies the 10 bay area birds that I’ve personally seen…)

I am so impressed with these free prototyping tools! I’ve been out of datascience for ~3 years, and I feel like all this used to take so much work to set up. It was so much work to collect and clean data, so much work to load in models, so much work to deploy. Makes me feel old…

Amazing that this can now be built in a day. A true prototype experience.

1 Like

Hi jingyun,

Thanks for the response. I simply took the code from the book:

bear_types = ‘grizzly’,‘black’,‘teddy’
path = Path(‘bears’)

if not path.exists():
path.mkdir()
for o in bear_types:
dest = (path/o)
dest.mkdir(exist_ok=True)
results = search_images_ddg(key, f’{o} bear’)
download_images(dest, urls=results.attrgot(‘contentUrl’))

bears = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=Resize(128))

dls = bears.dataloaders(path)

At this point, I got the error messages but as you can see, I defined the path. Prior to this, I downloaded from ddg

ims = search_images_ddg(‘grizzly bear’)
len(ims)

dest = ‘images/grizzly.jpg’
download_url(ims[0], dest)

In addition,
when I run the code:

fns = get_image_files(path)
fns

It outputs [0] files

before I got there, I used:
search_images_ddg

and ims outputs 200 images when I call
len(ims)

when I run
ims

I get:
[‘http://www.kristitrimmer.com/wp-content/uploads/2014/06/6-4-14-Grizzly-Bear-Nose.jpg’,‘http://2.bp.blogspot.com/-e6e4YOB-K9I/UQEz1wt72TI/AAAAAAAAC0s/YZUgPnIwTks/s640/Grizzly+bear.jpg’,‘http://www.bearsoftheworld.net/images/bears/grizzly_bear04.jpg’,‘http://www.famouscutouts.com/images/detailed/0/675-Bear.jpg’,‘http://endangeredanimalsiy.yolasite.com/resources/grizzly_bear_1a.jpg’,‘http://4.bp.blogspot.com/-ocppOkj9hTc/UNMpyLQ-XCI/AAAAAAAAADU/SrOUp6pvTSA/s1600/grizzly+bear.jpg’,‘http://survival-mastery.com/wp-content/uploads/2015/06/Grizzly-Bear-1-680x485.jpg’,‘http://wallsdesk.com/wp-content/uploads/2017/01/Grizzly-Bear-Download.jpg’,‘http://2.bp.blogspot.com/-yZMfP4l4ArQ/T74-7rfY_rI/AAAAAAAADf8/2fVC197XAtQ/s1600/Grizzly+Bear7.jpg’,'http://mediad.publicbroadcasting.net/p/kufm/files/styles/x_large/public/201809/grizzly-bear-02_Istock.jpg’…]

So everything check out until I get to
fns = get_image_files(path)
fns

and get (#0)

Thank you Tanishq for the incredibly thought you put into putting these educational resources!

I am having issue with bing search. i signed up for cognitive services and use subscription key. but still get a “permission error”. Did anyone else have the same issue? Thanks!

I suggest following along with the recent part 1 course. It combines the chapters of the book with updated materials & methods. And particularly the ‘is it a bird?’ example which uses an updated alternative to Bing to download images. (You won’t need to worry about trying to get Bing working)