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 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)

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\'
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\", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python310\lib\", 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\", line 39, in <module>
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\", line 430, in main
  File "c:\Users\XXXX\.vscode\extensions\ms-python.python-2022.20.1\pythonFiles\lib\python\debugpy/..\debugpy\server\", 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\", 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\", 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\", line 124, in _run_code
    exec(code, run_globals)
  File "c:\Users\XXXX\Documents\Code\fastbook\", line 9, in <module>
    dls = ImageDataLoaders.from_name_func(
  File "C:\Python310\lib\site-packages\fastai\vision\", line 149, in from_name_func
    return cls.from_path_func(path, fnames, f, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\vision\", line 135, in from_path_func
    return cls.from_dblock(dblock, fnames, path=path, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\data\", 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\", 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\", line 334, in dataloaders
    dls = [dl] + [, **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
  File "C:\Python310\lib\site-packages\fastai\data\", line 334, in <listcomp>
    dls = [dl] + [, **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
  File "C:\Python310\lib\site-packages\fastai\data\", line 97, in new
  File "C:\Python310\lib\site-packages\fastai\data\", line 78, in _one_pass
    b = self.do_batch([self.do_item(None)])
  File "C:\Python310\lib\site-packages\fastai\data\", line 153, in do_item
    try: return self.after_item(self.create_item(s))
  File "C:\Python310\lib\site-packages\fastcore\", 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\", line 158, in compose_tfms
    x = f(x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\", line 81, in __call__
    def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\", line 91, in _call
    return self._do_call(getattr(self, fn), x, **kwargs)
  File "C:\Python310\lib\site-packages\fastcore\", line 98, in _do_call
    res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
  File "C:\Python310\lib\site-packages\fastcore\", line 98, in <genexpr>
    res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
  File "C:\Python310\lib\site-packages\fastcore\", line 97, in _do_call
    return retain_type(f(x, **kwargs), x, ret)
  File "C:\Python310\lib\site-packages\fastcore\", line 120, in __call__
    return f(*args, **kwargs)
  File "C:\Python310\lib\site-packages\fastai\vision\", line 236, in encodes
    def encodes(self, o:PILBase): return o._tensor_cls(image2tensor(o))
  File "C:\Python310\lib\site-packages\fastai\vision\", line 106, in image2tensor
    res = tensor(img)
  File "C:\Python310\lib\site-packages\fastai\", line 154, in tensor
    else _array2tensor(array(x), **kwargs))
  File "C:\Python310\lib\site-packages\fastai\", line 136, in _array2tensor
    if sys.platform == "win32" and x = x.astype(np.int64)
  File "C:\Python310\lib\site-packages\numpy\", 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 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 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

What version might that be?


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 with the move to 1.24 - simply downgrading to numpy~=1.23 should be enough to solve the issue.