带有Google Colab的FastAI库v1

时间:2018-10-11 14:02:42

标签: python python-3.x google-colaboratory fast-ai

我正在尝试安装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 个答案:

答案 0 :(得分:5)

您正在使用的割炬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 *

答案 1 :(得分: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 *

答案 2 :(得分: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 *

答案 3 :(得分:1)

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

from fastai.vision import *
from fastai.metrics import *