FastAI 库 v1 与 Google Colab

FastAI library v1 with Google Colab

我正在尝试安装 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: cannot import name 'as_tensor'

有谁知道我在做什么wrong/why它不是在第一次安装时导入 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'

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

代码如下。

!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 *

您使用的是以前版本的 torch 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 *
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 *

目前GoogleColab自带FastAi,不需要单独安装。您可以立即开始使用它并像这样导入模块

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