Lesson 1 official topic

I’m currently going through the is it a bird? notebook on Kaggle, but I ran into an error.

from duckduckgo_search import ddg_images
from fastcore.all import *
​
def search_images(term, max_images=30):
    print(f"Searching for '{term}'")
    return L(ddg_images(term, max_results=max_images)).itemgot('image')
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_17/1717929076.py in <module>
----> 1 from duckduckgo_search import ddg_images
      2 from fastcore.all import *
      3 
      4 def search_images(term, max_images=30):
      5     print(f"Searching for '{term}'")

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/__init__.py in <module>
      8 
      9 # isort: off
---> 10 from .compat import (
     11     ddg,
     12     ddg_answers,

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/compat.py in <module>
      2 import warnings
      3 
----> 4 from .duckduckgo_search import DDGS
      5 
      6 logger = logging.getLogger(__name__)

/opt/conda/lib/python3.7/site-packages/duckduckgo_search/duckduckgo_search.py in <module>
     11 
     12 import requests
---> 13 from requests.exceptions import HTTPError, JSONDecodeError, Timeout
     14 from requests.models import Response
     15 

ImportError: cannot import name 'JSONDecodeError' from 'requests.exceptions' (/opt/conda/lib/python3.7/site-packages/requests/exceptions.py)

It looks like an error from the duckduckgo_search package, but is it because the code is out of date? What else can I use instead? Is there a latest version of the code maybe?

2 Likes

you can try Google Images Search: Google-Images-Search · PyPI

I tried substituting googleimagesearch into the pip install and got a bunch of errors.

Then got this 'Nonetype" object is not subscriptable error when I add this code

gis = GoogleImagesSearch('XXXXXXXXXX', 'XXXXXX')

_search_params = {
    'q': 'birds',
    'num': 1,
}


urls = gis.search(_search_params)
urls[0]

What have I done wrong?

I’m running into the same issue as well

I am getting this error. Can you help me?

This is a “known issue”, at least there is a thread about this.

The following post solved it for me: Duckduckgo search not working - #18 by maitland

Thank you that one was solved. Got a new one though.
download_url(search_images_ddg(‘forest photos’, max_images=1)[0], ‘forest.jpg’, show_progress=False)
Image.open(‘forest.jpg’).to_thumb(256,256)

Missing: Training Diagnostics

When I run learn.fine_tune(3) in the notebooks or in iPython, I don’t get any tables returned with the training log. Can anyone suggest why this is happening and how I could fix it?
20230601-005484

I’ve tried the following:

  • running the cell as-is in the given notebook (no changes)
  • using a rewritten notebook and python file
  • rerouting the stdout to a text file, and there was nothing there

For all, there’s no output. Can anyone suggest why this is happening and how I could fix it?

TIA!

Solved!

Refer to this post: fastprogress progress bar not showing · Issue #13163 · microsoft/vscode-jupyter · GitHub

1 Like

AssertionError: Exception occured in Recorder when calling event after_batch:

For lab1 (Is it a bird?) I’m trying to modify the learner to work in a multiclass setting, i.e. with 3 categories instead of 2.
According to the tutorial in the docs this is straightforward, but I’m having trouble with fine-tuning the model.

When re-training with an optimal learning rate for 3 epochs the following rather complex error message was produced:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_18/568062170.py in <module>
     10 learn2=vision_learner(dls2, resnet18, metrics=[partial(accuracy_multi, thresh=0.5), fbeta_macro, fbeta_sample] )
     11 learn2.lr_find() #find the optimal learning rate
---> 12 learn2.fine_tune(3, 1e-3) #args: n epochs, learning rate. Not in documentation for some reason...ugh

/opt/conda/lib/python3.7/site-packages/fastai/callback/schedule.py in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
    163     "Fine tune with `Learner.freeze` for `freeze_epochs`, then with `Learner.unfreeze` for `epochs`, using discriminative LR."
    164     self.freeze()
--> 165     self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
    166     base_lr /= 2
    167     self.unfreeze()

/opt/conda/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt, start_epoch)
    117     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    118               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 119     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd, start_epoch=start_epoch)
    120 
    121 # %% ../../nbs/14_callback.schedule.ipynb 50

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt, start_epoch)
    262             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    263             self.n_epoch = n_epoch
--> 264             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    265 
    266     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    197 
    198     def _with_events(self, f, event_type, ex, final=noop):
--> 199         try: self(f'before_{event_type}');  f()
    200         except ex: self(f'after_cancel_{event_type}')
    201         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    251         for epoch in range(self.n_epoch):
    252             self.epoch=epoch
--> 253             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    254 
    255     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False, start_epoch=0):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    197 
    198     def _with_events(self, f, event_type, ex, final=noop):
--> 199         try: self(f'before_{event_type}');  f()
    200         except ex: self(f'after_cancel_{event_type}')
    201         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    246     def _do_epoch(self):
    247         self._do_epoch_train()
--> 248         self._do_epoch_validate()
    249 
    250     def _do_fit(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
    242         if dl is None: dl = self.dls[ds_idx]
    243         self.dl = dl
--> 244         with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
    245 
    246     def _do_epoch(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    197 
    198     def _with_events(self, f, event_type, ex, final=noop):
--> 199         try: self(f'before_{event_type}');  f()
    200         except ex: self(f'after_cancel_{event_type}')
    201         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    203     def all_batches(self):
    204         self.n_iter = len(self.dl)
--> 205         for o in enumerate(self.dl): self.one_batch(*o)
    206 
    207     def _backward(self): self.loss_grad.backward()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    233         b = self._set_device(b)
    234         self._split(b)
--> 235         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    236 
    237     def _do_epoch_train(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    199         try: self(f'before_{event_type}');  f()
    200         except ex: self(f'after_cancel_{event_type}')
--> 201         self(f'after_{event_type}');  final()
    202 
    203     def all_batches(self):

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in __call__(self, event_name)
    170 
    171     def ordered_cbs(self, event): return [cb for cb in self.cbs.sorted('order') if hasattr(cb, event)]
--> 172     def __call__(self, event_name): L(event_name).map(self._call_one)
    173 
    174     def _call_one(self, event_name):

/opt/conda/lib/python3.7/site-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
    154     def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
    155 
--> 156     def map(self, f, *args, **kwargs): return self._new(map_ex(self, f, *args, gen=False, **kwargs))
    157     def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
    158     def argfirst(self, f, negate=False):

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
    838     res = map(g, iterable)
    839     if gen: return res
--> 840     return list(res)
    841 
    842 # %% ../nbs/01_basics.ipynb 336

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in __call__(self, *args, **kwargs)
    823             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    824         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 825         return self.func(*fargs, **kwargs)
    826 
    827 # %% ../nbs/01_basics.ipynb 326

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _call_one(self, event_name)
    174     def _call_one(self, event_name):
    175         if not hasattr(event, event_name): raise Exception(f'missing {event_name}')
--> 176         for cb in self.cbs.sorted('order'): cb(event_name)
    177 
    178     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     60             try: res = getcallable(self, event_name)()
     61             except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
---> 62             except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)
     63         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     64         return res

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     58         res = None
     59         if self.run and _run:
---> 60             try: res = getcallable(self, event_name)()
     61             except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
     62             except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in after_batch(self)
    558         if len(self.yb) == 0: return
    559         mets = self._train_mets if self.training else self._valid_mets
--> 560         for met in mets: met.accumulate(self.learn)
    561         if not self.training: return
    562         self.lrs.append(self.opt.hypers[-1]['lr'])

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in accumulate(self, learn)
    480     def accumulate(self, learn):
    481         bs = find_bs(learn.yb)
--> 482         self.total += learn.to_detach(self.func(learn.pred, *learn.yb))*bs
    483         self.count += bs
    484     @property

/opt/conda/lib/python3.7/site-packages/fastai/metrics.py in accuracy_multi(inp, targ, thresh, sigmoid)
    199 def accuracy_multi(inp, targ, thresh=0.5, sigmoid=True):
    200     "Compute accuracy when `inp` and `targ` are the same size."
--> 201     inp,targ = flatten_check(inp,targ)
    202     if sigmoid: inp = inp.sigmoid()
    203     return ((inp>thresh)==targ.bool()).float().mean()

/opt/conda/lib/python3.7/site-packages/fastai/torch_core.py in flatten_check(inp, targ)
    785     "Check that `inp` and `targ` have the same number of elements and flatten them."
    786     inp,targ = TensorBase(inp.contiguous()).view(-1),TensorBase(targ.contiguous()).view(-1)
--> 787     test_eq(len(inp), len(targ))
    788     return inp,targ
    789 

/opt/conda/lib/python3.7/site-packages/fastcore/test.py in test_eq(a, b)
     35 def test_eq(a,b):
     36     "`test` that `a==b`"
---> 37     test(a,b,equals, cname='==')
     38 
     39 # %% ../nbs/00_test.ipynb 25

/opt/conda/lib/python3.7/site-packages/fastcore/test.py in test(a, b, cmp, cname)
     25     "`assert` that `cmp(a,b)`; display inputs and `cname or cmp.__name__` if it fails"
     26     if cname is None: cname=cmp.__name__
---> 27     assert cmp(a,b),f"{cname}:\n{a}\n{b}"
     28 
     29 # %% ../nbs/00_test.ipynb 16

AssertionError: Exception occured in `Recorder` when calling event `after_batch`:
	==:
96
32

Here is my code:

#establish which DL model to use
#here we need to change the perforamnce metric as unidim error rate won't work
#brie_pop=BrierScoreMulti(thresh=0.4) did not work due to non-mutual exclusivity of strictly proper scoring rule?
fbeta_macro=FBetaMulti(beta=1.1, thresh=0.5, average='macro')
fbeta_macro.name='FBeta (macro)'
fbeta_sample=FBetaMulti(beta=1.1, thresh=0.5, average='samples')
fbeta_macro.name='FBeta (samples)'

#multiclass modification
learn2=vision_learner(dls2, resnet18, metrics=[partial(accuracy_multi, thresh=0.5), fbeta_macro, fbeta_sample] )
learn2.lr_find() #find the optimal learning rate
learn2.fine_tune(3, 1e-3) #args: n epochs, learning rate. Not in documentation for some reason...ugh

Has anyone else experienced a similar error, or possesses the API expertise to find the root cause of this issue?
Thanks for your time!

I’m not very familiar with this, but I’ll give it a go…

You report your error using three cutom metrics.

metrics=[partial(accuracy_multi, thresh=0.5), 
                                fbeta_macro, fbeta_sample] )

First thing to help would be try each one separately to narrow down the issue.

You don’t say if you already had a minimal metric implementation working.

The minimal metric example is:

def custom_accuracy(preds, targets, threshold=0.5):
    preds2 = (preds > threshold).float()
    accuracy = (preds2 == targets).float().mean()
    return accuracy

learn = vision_learner(dls, resnet34, metrics=[custom_accuracy])

I notice the FBetaMulti function definition does not have parameters preds, targets, so something might clash there.

FBetaMulti (beta, thresh=0.5, sigmoid=True, labels=None, pos_label=1,
             average='macro', sample_weight=None)

I’m not familiar with the FBetaMulti function, but its documentation says its for “multi-label classification problems”, which is different from “multi-class classification” using a single label.

IIUC…

  • Multi-class classification: Each instance belongs to exactly one class from a set of mutually exclusive classes.
  • Multi-label classification: Each instance can have multiple labels, and the classes are not mutually exclusive.

Pay attention to the warning under Single-label classification.

So an approach to solve this would be:

  1. Drop back to a single custom metric
  2. Get the basic example working
  3. Wrap your basic example around your calls to FBeta, so you can sprinkle debug-prints to show type and content of data in/out between “basic example” and “Fbeta”.
1 Like

Thanks ben, your suggestion worked well - the basic implementation got the job done and I’ll play around with more complicated metrics for a little while before moving on to the next lesson. Appreciate your clear exposition of the problem

Take care

1 Like

The lesson 1 cats and dogs “running your first notebook” model example screenshot shows an error rate under 0.10. (https://github.com/fastai/fastbook/blob/master/01_intro.ipynb)

But when I try to run it locally, the error rate is always 0.331529. Seems like it should at least change from epoch to epoch? And it should eventually get lower?

In the “Limitations Inherent To Machine Learning” section, one of the points mentioned is that ML models “This learning approach only creates predictions, not recommended actions.”

What kind of actions did he have in mind? And how are they different from predictions (which can theoretically inform actions)?

I’m not sure where the error is in my training, but I tried to change bird/forest for seaweed/car and I am getting odd results. While it is correctly detecting seaweed or a car, the probability is really off. For my baseline seaweed.jpg for example, it is saying ‘0.00000’. What am I overlooking?
Also what is returned in probs[0] vs probs[1]?

.

Can you print out the full output from learn.predict and see what that shows? My guess is that “seaweed” is actually the second class (with the first being “car”) and so the probs value corresponding to that would be probs[1]. probs is the predicted value for each class, with probs[0] being the prediction for the first class, and probs[1] being the prediction of the second class. Since this is a classification problem, probs is the probability of each class that the model predicts for the image.

Also, the second value returned by learn.predict returns the index of the class with the highest probability, so printing that out you can see if it’s 0 or 1 and compare that with the probs tensor.

You are correct that it could be reversed but how do i determine which class is where? I looked at the previous example and I replaced forest with car and bird with seaweed respectively, but in that example, it also used probs[0] to confirm bird.jpg. Here is my screenshot.

this is the bird code. In this, it expects bird class to be in probs[0]


is the original instructions incorrect?

This is a common confusion since the example is extra-simplified and ignores the middle parameter of the three parameters returned by learn.predict().
Try…

pred, ndx, probs = learn.predict(...)
print(f"This is a {pred} with probability {probs[ndx]}.")
3 Likes

ah that helps. So basically the function comes back with a prediction (pred) of two things it understands (bird, forest) and then tells you where in the tensor (probs) you can see the probability via ndx as the key?

2 Likes