6

我正在尝试安装 FastAi 库并将其与 Google Colab 一起使用。我是

使用:

!pip3 install fastai
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4-{platform}-linux_x86_64.whl torchvision
import torch
!pip install Pillow==4.1.1
!pip install image
%matplotlib inline

导入/安装所有东西,但是当我运行时

from fastai.imports import *

最后一行给出了一个错误,内容如下:

ImportError:无法导入名称“as_tensor”

有谁知道我做错了什么/为什么在安装的第一件东西时它不导入 fastai 库?

我收到的完整错误如下:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-10-9f9378ae0f2a> in <module>()
----> 1 from fastai.imports import *

/usr/local/lib/python3.6/dist-packages/fastai/__init__.py in <module>()
----> 1 from .basic_train import *
  2 from .callback import *
  3 from .callbacks import *
  4 from .core import *
  5 from .data import *

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in <module>()
  1 "Provides basic training and validation with `Learner`"
----> 2 from .torch_core import *
  3 from .data import *
  4 from .callback import *
  5 

/usr/local/lib/python3.6/dist-packages/fastai/torch_core.py in <module>()
  1 "Utility functions to help deal with tensors"
----> 2 from .imports.torch import *
  3 from .core import *
  4 
  5 AffineMatrix = Tensor

/usr/local/lib/python3.6/dist-packages/fastai/imports/__init__.py in 
<module>()
  1 from .core import *
----> 2 from .torch import *

/usr/local/lib/python3.6/dist-packages/fastai/imports/torch.py in <module>()
  1 import torch, torch.nn.functional as F
  2 from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, 
LongTensor, ShortTensor, Tensor
----> 3 from torch import nn, optim, as_tensor, tensor
  4 from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, 
TensorDataset

ImportError: cannot import name 'as_tensor'
4

4 回答 4

6

您正在使用torch-0.3.0.post4没有as_tensor.

所以你可以使用其他版本的手电筒,比如torch-0.4.1有这种方法的。

运行以下代码段后:

!pip3 install fastai
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
# !pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4-{platform}-linux_x86_64.whl torchvision
!pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-linux_x86_64.whl

import torch
!pip install Pillow==4.1.1
!pip install image
%matplotlib inline

您可以使用 :

from fastai.imports import *
于 2018-10-15T15:39:37.007 回答
2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
!pip3 install torch_nightly -f https://download.pytorch.org/whl/nightly/{accelerator}/torch_nightly.html


import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.backends.cudnn.enabled)

如果这部分没问题,到今天为止,您应该会看到以下输出:

1.0.0.dev20181019
True
True

然后继续安装最新版本的 fastai:

!pip3 install fastai

并测试:

from fastai.imports import *
于 2018-10-20T18:41:10.310 回答
1

我缺少依赖项。我做了一个尴尬的解决方法,安装以前版本的库(带有依赖项),然后卸载库并用更新版本替换它。

代码如下。

!pip install "fastai==0.7.0"
!pip uninstall "fastai==0.7.0"
!pip install fastai 
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
!apt update && apt install -y libsm6 libxext6

accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4- 
{platform}-linux_x86_64.whl torchvision
import torch
!pip install Pillow==4.1.1
!pip install image

%matplotlib inline
from fastai.imports import *
于 2018-10-11T14:57:21.907 回答
1

目前 Google Colab 附带 FastAi,您无需单独安装。您可以立即开始使用它并像这样导入模块

from fastai.vision import *
from fastai.metrics import *
于 2020-05-07T10:23:17.597 回答