Call to ImageDataLoaders.from_name_func fails with "module 'numpy' has no attribute 'int'"

Hi, I am working through some fastai example code and have encountered an error. I ran the following code (that I copied and pasted from the fastai website):

from fastai.vision.all import *
from fastai.text.all import *
from fastai.collab import *
from fastai.tabular.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 the following error message from the call to from_name_func:

PS C:\Users\XXXX\Documents\Code\fastbook>  & 'C:\Python310\python.exe' 'c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '64021' '--' 'c:\Users\XXXX\Documents\Code\fastbook\fastaitest.py'
Could not do one pass in your dataloader, there is something wrong in it. Please see the stack trace below:
Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
  File "C:\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "c:\Users\XXXX\Documents\Code\fastbook\fastaitest.py", line 9, in <module>
    dls = ImageDataLoaders.from_name_func(
  File "C:\Python310\lib\site-packages\fastai\vision\data.py", line 149, in from_name_func
    return cls.from_path_func(path, fnames, f, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\vision\data.py", line 135, in from_path_func
    return cls.from_dblock(dblock, fnames, path=path, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\data\core.py", line 281, in from_dblock
    return dblock.dataloaders(source, path=path, bs=bs, val_bs=val_bs, shuffle=shuffle, device=device, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\data\block.py", line 157, in dataloaders
    return dsets.dataloaders(path=path, after_item=self.item_tfms, after_batch=self.batch_tfms, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\data\core.py", line 334, in dataloaders
    dls = [dl] + [dl.new(self.subset(i), **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
  File "C:\Python310\lib\site-packages\fastai\data\core.py", line 334, in <listcomp>
    dls = [dl] + [dl.new(self.subset(i), **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
  File "C:\Python310\lib\site-packages\fastai\data\core.py", line 97, in new
    self._one_pass()
  File "C:\Python310\lib\site-packages\fastai\data\core.py", line 78, in _one_pass
    b = self.do_batch([self.do_item(None)])
  File "C:\Python310\lib\site-packages\fastai\data\load.py", line 153, in do_item
    try: return self.after_item(self.create_item(s))
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 208, in __call__
    def __call__(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 158, in compose_tfms
    x = f(x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 81, in __call__
    def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 91, in _call
    return self._do_call(getattr(self, fn), x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 98, in _do_call
    res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 98, in <genexpr>
    res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
  File "C:\Python310\lib\site-packages\fastcore\transform.py", line 97, in _do_call
    return retain_type(f(x, **kwargs), x, ret)
  File "C:\Python310\lib\site-packages\fastcore\dispatch.py", line 120, in __call__
    return f(*args, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\vision\core.py", line 236, in encodes
    def encodes(self, o:PILBase): return o._tensor_cls(image2tensor(o))
  File "C:\Python310\lib\site-packages\fastai\vision\core.py", line 106, in image2tensor
    res = tensor(img)
  File "C:\Python310\lib\site-packages\fastai\torch_core.py", line 154, in tensor
    else _array2tensor(array(x), **kwargs))
  File "C:\Python310\lib\site-packages\fastai\torch_core.py", line 136, in _array2tensor
    if sys.platform == "win32" and x.dtype==np.int: x = x.astype(np.int64)
  File "C:\Python310\lib\site-packages\numpy\__init__.py", line 284, in __getattr__
    raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'int'. Did you mean: 'inf'?

I had a look at torch_core.py and according to intellisense np has no member called int. So it looks like numpy has changed and has broken fastai?

I got my code running by changing torch_core.py line 136 to:

if sys.platform == "win32" and x.dtype==np.int32: x = x.astype(np.int64)

Pip package versions:

  • numpy 1.24.0
  • fastai 2.7.10
  • torch 1.13.1

Python version is 3.10.7

Hi, I am just getting started and ran into this as well.

I’ll try to make the adjustment, but is there a better method such as aligning to the currently supported version of numpy for fast.ai?

What version might that be?

Thx,

Hi there,

this is simple and complicated at the same time. Had the same issue one a project were an old version of sklearn and the newest numpy, 1.24.0, were used.

The reason that this is happening is simply that numpy removed the type np.int with the move to 1.24 - simply downgrading to numpy~=1.23 should be enough to solve the issue.