一、 cpu版本

1. 安装torch

经过安装的经验,发现应该先安装torchvision

1、下载
下载whl类型的文件:torch

cu表示的是CUDA的版本,cp表示的是python的版本。

2、安装pytorch

将下载的文件放入home下,打开终端进行安装:

pip3 install torch-0.4.0-cp35-cp35m-linux_x86_64.whl
wf@ubuntu:~/Downloads$ pip install  torch-0.4.0-cp36-cp36m-linux_x86_64.whl 
Processing ./torch-0.4.0-cp36-cp36m-linux_x86_64.whl
distributed 1.21.8 requires msgpack, which is not installed.
Installing collected packages: torch
Successfully installed torch-0.4.0

2. 安装torchvision

1、 使用pip命令安装

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   torchvision
wf@ubuntu:~/Downloads$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   torchvision
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting torchvision
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/43/aaa740c406b1832adc6ff9d5e71c23fd2af2ebd436c42d76d85809ec8be9/torchvision-0.8.1-cp36-cp36m-manylinux1_x86_64.whl (12.8 MB)
     |████████████████████████████████| 12.8 MB 609 kB/s 
Requirement already satisfied: numpy in /home/wf/anaconda3/lib/python3.6/site-packages (from torchvision) (1.14.3)
Collecting torch==1.7.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/80/2a/58f8078744e0408619c63148f7a2e8e48cf007e4146b74d4bb67c56d161b/torch-1.7.0-cp36-cp36m-manylinux1_x86_64.whl (776.7 MB)
     |████████████████████████████████| 776.7 MB 180 bytes/s 
Requirement already satisfied: pillow>=4.1.1 in /home/wf/anaconda3/lib/python3.6/site-packages (from torchvision) (5.1.0)
Collecting dataclasses
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl (19 kB)
Collecting future
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829 kB)
     |████████████████████████████████| 829 kB 22.4 MB/s 
Collecting typing-extensions
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/60/7a/e881b5abb54db0e6e671ab088d079c57ce54e8a01a3ca443f561ccadb37e/typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Building wheels for collected packages: future
  Building wheel for future (setup.py) ... done
  Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491096 sha256=31d1f47130bdeaa8134996f7868c3c69a6edf38d7c2212dc63d36767331f02cf
  Stored in directory: /home/wf/.cache/pip/wheels/f3/f6/70/8b2bba44d8bf6ee5b7eac561416d9ef5c28dd2320490fc0f1f
Successfully built future
Installing collected packages: dataclasses, future, typing-extensions, torch, torchvision
  Attempting uninstall: torch
    Found existing installation: torch 0.4.0
    Uninstalling torch-0.4.0:
      Successfully uninstalled torch-0.4.0
Successfully installed dataclasses-0.8 future-0.18.2 torch-1.7.0 torchvision-0.8.1 typing-extensions-3.7.4.3

2、验证及版本匹配

但是发现安装 torchvision时,默认安装的torchvision是0.8.1版本的,但是在安装的时候,自动卸载了torch-0.4.0,安装了torch-1.7.0,这并不是我想要的版本,于是再次安装torch-0.4.0。

2.1 torch-0.4.0+torchvision-0.8.1:module ‘torch’ has no attribute ‘ops’

安装好torchvision-0.8.1 后,重新安装torch-0.4.0:

pip3 install torch-0.4.0-cp35-cp35m-linux_x86_64.whl

安装torch-0.4.0 + torchvision-0.8.1后导入import torchvision会报错:

>>> import  torch
>>> import torchvision
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/__init__.py", line 4, in <module>
    from .extension import _HAS_OPS
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/extension.py", line 51, in <module>
    _register_extensions()
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/extension.py", line 47, in _register_extensions
    torch.ops.load_library(ext_specs.origin)
AttributeError: module 'torch' has no attribute 'ops'

说明版本不匹配。

2.2 torch-0.4.0+torchvision-0.4.0:module ‘torch.nn’ has no attribute ‘ModuleDict’
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   torchvision==0.4.0
pip3 install torch-0.4.0-cp35-cp35m-linux_x86_64.whl

安装torch-0.4.0+torchvision-0.4.0后导入import torchvision会报错:

>>> import torch
>>> import torchvision
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/__init__.py", line 1, in <module>
    from torchvision import models
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/models/__init__.py", line 11, in <module>
    from . import segmentation
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/models/segmentation/__init__.py", line 1, in <module>
    from .segmentation import *
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/models/segmentation/segmentation.py", line 1, in <module>
    from .._utils import IntermediateLayerGetter
  File "/home/wf/anaconda3/lib/python3.6/site-packages/torchvision/models/_utils.py", line 7, in <module>
    class IntermediateLayerGetter(nn.ModuleDict):
AttributeError: module 'torch.nn' has no attribute 'ModuleDict'

版本不匹配,降低torchvision。

2.3 torch0.4.1+torchvision0.2.2
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   torchvision==0.2.2
pip3 install torch-0.4.0-cp35-cp35m-linux_x86_64.whl

测试:

(base) wf@ubuntu:~/Downloads$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
>>> 

版本匹配,成功。

二、gpu版本

1. 下载torch

1、从清华源下载:torch
2、安装

(CCNet36) bit@bit-613:~/下载$ conda install pytorch-0.4.1-py36_cuda8.0.61_cudnn7.1.2_1.tar.bz2 

2. 下载torchvision

刚刚的清华源中下载压缩包或者通过conda下载(上面的方式):

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐