使用fastai库时,numpy报错没有int属性。完整报错如下:
Could not do one pass in your dataloader, there is something wrong in it. Please see the stack trace below:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[12], line 2
1 def is_cat(x): return x[0].isupper()
----> 2 dls = ImageDataLoaders.from_name_func(
3 path, get_image_files(path), valid_pct=0.2, seed=42,
4 label_func=is_cat, item_tfms=Resize(224))
6 learn = vision_learner(dls, resnet34, metrics=error_rate)
7 learn.fine_tune(1)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\vision\data.py:149, in ImageDataLoaders.from_name_func(cls, path, fnames, label_func, **kwargs)
147 raise ValueError("label_func couldn't be lambda function on Windows")
148 f = using_attr(label_func, 'name')
--> 149 return cls.from_path_func(path, fnames, f, **kwargs)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\vision\data.py:135, in ImageDataLoaders.from_path_func(cls, path, fnames, label_func, valid_pct, seed, item_tfms, batch_tfms, img_cls, **kwargs)
129 "Create from list of `fnames` in `path`s with `label_func`"
130 dblock = DataBlock(blocks=(ImageBlock(img_cls), CategoryBlock),
131 splitter=RandomSplitter(valid_pct, seed=seed),
132 get_y=label_func,
133 item_tfms=item_tfms,
134 batch_tfms=batch_tfms)
--> 135 return cls.from_dblock(dblock, fnames, path=path, **kwargs)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\core.py:281, in DataLoaders.from_dblock(cls, dblock, source, path, bs, val_bs, shuffle, device, **kwargs)
270 @classmethod
271 def from_dblock(cls,
272 dblock, # `DataBlock` object
(...)
279 **kwargs
280 ):
--> 281 return dblock.dataloaders(source, path=path, bs=bs, val_bs=val_bs, shuffle=shuffle, device=device, **kwargs)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\block.py:157, in DataBlock.dataloaders(self, source, path, verbose, **kwargs)
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)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\core.py:334, in FilteredBase.dataloaders(self, bs, shuffle_train, shuffle, val_shuffle, n, path, dl_type, dl_kwargs, device, drop_last, val_bs, **kwargs)
332 dl = dl_type(self.subset(0), **merge(kwargs,def_kwargs, dl_kwargs[0]))
333 def_kwargs = {'bs':bs if val_bs is None else val_bs,'shuffle':val_shuffle,'n':None,'drop_last':False}
--> 334 dls = [dl] + [dl.new(self.subset(i), **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
335 for i in range(1, self.n_subsets)]
336 return self._dbunch_type(*dls, path=path, device=device)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\core.py:334, in <listcomp>(.0)
332 dl = dl_type(self.subset(0), **merge(kwargs,def_kwargs, dl_kwargs[0]))
333 def_kwargs = {'bs':bs if val_bs is None else val_bs,'shuffle':val_shuffle,'n':None,'drop_last':False}
--> 334 dls = [dl] + [dl.new(self.subset(i), **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))
335 for i in range(1, self.n_subsets)]
336 return self._dbunch_type(*dls, path=path, device=device)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\core.py:97, in TfmdDL.new(self, dataset, cls, **kwargs)
95 if not hasattr(self, '_n_inp') or not hasattr(self, '_types'):
96 try:
---> 97 self._one_pass()
98 res._n_inp,res._types = self._n_inp,self._types
99 except Exception as e:
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\core.py:78, in TfmdDL._one_pass(self)
77 def _one_pass(self):
---> 78 b = self.do_batch([self.do_item(None)])
79 if self.device is not None: b = to_device(b, self.device)
80 its = self.after_batch(b)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\data\load.py:153, in DataLoader.do_item(self, s)
152 def do_item(self, s):
--> 153 try: return self.after_item(self.create_item(s))
154 except SkipItemException: return None
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:208, in Pipeline.__call__(self, o)
--> 208 def __call__(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:158, in compose_tfms(x, tfms, is_enc, reverse, **kwargs)
156 for f in tfms:
157 if not is_enc: f = f.decode
--> 158 x = f(x, **kwargs)
159 return x
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:81, in Transform.__call__(self, x, **kwargs)
79 @property
80 def name(self): return getattr(self, '_name', _get_name(self))
---> 81 def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
82 def decode (self, x, **kwargs): return self._call('decodes', x, **kwargs)
83 def __repr__(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:91, in Transform._call(self, fn, x, split_idx, **kwargs)
89 def _call(self, fn, x, split_idx=None, **kwargs):
90 if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 91 return self._do_call(getattr(self, fn), x, **kwargs)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:98, in Transform._do_call(self, f, x, **kwargs)
96 ret = f.returns(x) if hasattr(f,'returns') else None
97 return retain_type(f(x, **kwargs), x, ret)
---> 98 res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
99 return retain_type(res, x)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:98, in <genexpr>(.0)
96 ret = f.returns(x) if hasattr(f,'returns') else None
97 return retain_type(f(x, **kwargs), x, ret)
---> 98 res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
99 return retain_type(res, x)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\transform.py:97, in Transform._do_call(self, f, x, **kwargs)
95 if f is None: return x
96 ret = f.returns(x) if hasattr(f,'returns') else None
---> 97 return retain_type(f(x, **kwargs), x, ret)
98 res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
99 return retain_type(res, x)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastcore\dispatch.py:120, in TypeDispatch.__call__(self, *args, **kwargs)
118 elif self.inst is not None: f = MethodType(f, self.inst)
119 elif self.owner is not None: f = MethodType(f, self.owner)
--> 120 return f(*args, **kwargs)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\vision\core.py:236, in encodes(self, o)
235 ToTensor
--> 236 def encodes(self, o:PILBase): return o._tensor_cls(image2tensor(o))
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\vision\core.py:106, in image2tensor(img)
104 def image2tensor(img):
105 "Transform image to byte tensor in `c*h*w` dim order."
--> 106 res = tensor(img)
107 if res.dim()==2: res = res.unsqueeze(-1)
108 return res.permute(2,0,1)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\torch_core.py:154, in tensor(x, *rest, **kwargs)
146 if len(rest): x = (x,)+rest
147 # There was a Pytorch bug in dataloader using num_workers>0. Haven't confirmed if fixed
148 # if isinstance(x, (tuple,list)) and len(x)==0: return tensor(0)
149 res = (x if isinstance(x, Tensor)
150 else torch.tensor(x, **kwargs) if isinstance(x, (tuple,list,numbers.Number))
151 else _array2tensor(x, **kwargs) if isinstance(x, ndarray)
152 else as_tensor(x.values, **kwargs) if isinstance(x, (pd.Series, pd.DataFrame))
153 # else as_tensor(array(x, **kwargs)) if hasattr(x, '__array__') or is_iter(x)
--> 154 else _array2tensor(array(x), **kwargs))
155 if res.dtype is torch.float64: return res.float()
156 return res
File D:\Program Files\anaconda\envs\torch\lib\site-packages\fastai\torch_core.py:136, in _array2tensor(x, requires_grad, pin_memory, **kwargs)
133 if x.dtype==np.uint16: x = x.astype(np.float32)
134 # windows default numpy int dtype is int32, while torch tensor default int dtype is int64
135 # https://github.com/numpy/numpy/issues/9464
--> 136 if sys.platform == "win32" and x.dtype==np.int: x = x.astype(np.int64)
137 t = torch.as_tensor(x, **kwargs)
138 t.requires_grad_(requires_grad)
File D:\Program Files\anaconda\envs\torch\lib\site-packages\numpy\__init__.py:284, in __getattr__(attr)
281 from .testing import Tester
282 return Tester
--> 284 raise AttributeError("module {!r} has no attribute "
285 "{!r}".format(__name__, attr))
AttributeError: module 'numpy' has no attribute 'int'
解决方法:
参考How to Fix AttributeError: module ‘numpy’ has no attribute ‘float’:
As of numpy v1.20, numpy.float as well as similar aliases (including numpy.int) were deprecated.
可知,自1.20版本后,np.int
已经废弃,所以安装旧版本的numpy即可:
pip install numpy==1.19
安装时报错很长,有以下字段:
No module named ‘numpy.distutils._msvccompiler’ in numpy.distutils; trying from distutils
参考git issue可知,是python版本太高了。重装环境使用python3.6后解决问题。
总结
要顺利运行fastai,建议使用以下配置:
- python 3.7
- numpy 1.19 更高的版本可能导致numpy找不到int属性
这样的配置能更顺利地运行fastai代码,减少不必要的报错debug时间。