RuntimeError from call to fine_tune()

Hi everyone, I am working through the quick start guide and I ran into a problem. I executed this code:

from fastai.vision.all import *

path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

learn = vision_learner(dls, resnet34, metrics=error_rate)

learn.fine_tune(1)

I got this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python310\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Python310\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Python310\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Python310\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Python310\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\XXXX\Documents\Code\fastbook\fastaitest.py", line 14, in <module>
    learn.fine_tune(1)
  File "C:\Python310\lib\site-packages\fastai\callback\schedule.py", line 165, in fine_tune
    self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\callback\schedule.py", line 119, in fit_one_cycle
    self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd, start_epoch=start_epoch)
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 256, in fit
    self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 193, in _with_events
    try: self(f'before_{event_type}');  f()
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 245, in _do_fit
    self._with_events(self._do_epoch, 'epoch', CancelEpochException)
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 193, in _with_events
    try: self(f'before_{event_type}');  f()
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 239, in _do_epoch
    self._do_epoch_train()
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 231, in _do_epoch_train
    self._with_events(self.all_batches, 'train', CancelTrainException)
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 193, in _with_events
    try: self(f'before_{event_type}');  f()
  File "C:\Python310\lib\site-packages\fastai\learner.py", line 199, in all_batches
    for o in enumerate(self.dl): self.one_batch(*o)
  File "C:\Python310\lib\site-packages\fastai\data\load.py", line 127, in __iter__
    for b in _loaders[self.fake_l.num_workers==0](self.fake_l):
  File "C:\Python310\lib\site-packages\torch\utils\data\dataloader.py", line 1034, in __init__
    w.start()
  File "C:\Python310\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Python310\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Python310\lib\multiprocessing\context.py", line 336, in _Popen
    return Popen(process_obj)
  File "C:\Python310\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Python310\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Python310\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

I fixed it by setting num_workers = 0 like this:

dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, num_workers = 0, item_tfms=Resize(224))

I ran this code on a laptop with no GPU. Does anyone know if this is a bug, or is it supposed to happen?

1 Like