Win10下pytorch环境搭建详细教程以及示例测试(二)

发布于:2023-01-04 ⋅ 阅读:(341) ⋅ 点赞:(0)

目录

1.启动anaconda

2.执行

3.创建沙盒环境

4.激活环境

5.准备在虚拟环境中安装库

6.下载pytorch

7.使用上交镜像

8.在镜像中安装pytorch

9.检查安装结果

10.安装opencv和tifffile库

11.安装git和matplotlib

12.初步测试demo

13.执行测试demo

14.下载数据集

15.执行训练


1.启动anaconda

如下图所示:

可以注意到,有一个有PowerShell ,还有一个没有PowerShell,这两个在功能上没有直接区别,都可以使用,不过有PowerShell 的包含更多的linux命令,比如ls之类的,会更方便一些,所以推荐使用带PowerShell的命令行。

2.执行

conda info --envs

默认会得到一个基本的环境,笔者因为已经创建了一个名为pytorch的环境,所以会显示有两个。

3.创建沙盒环境

conda create -n pytorch python=3.6

4.激活环境

conda activate pytorch

5.准备在虚拟环境中安装库

此时已经成功进入名为pytorch的虚拟环境中了,接下来的操作都是在这个虚拟环境中进行的,我们安装的库都是保存在虚拟环境中的。

6.下载pytorch

https://pytorch.org/官网
这里需要注意的是,如果之前第二步查看时有GPU,则CUDA选择11即可,因为10已经不再支持;如果只有集显,那么CUDA选择None即可

7.使用上交镜像

打开anaconda prompt,输入下列指令:

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
    conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
    conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/

8.在镜像中安装pytorch

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

9.检查安装结果

最后我们检验安装是否成功,先键入python进入python工作环境,然后键入import torch,如果未报错,则pytorch安装成功,之后输入指令torce.cuda.is_available(),如果显示True,则GPU可被pytorch使用。

这里补充一下,如果你安装错了pytorch版本,则可以通过以下命令卸载:

卸载 安装:
1 用pip卸载PyTorch

pip uninstall torch

2 用conda卸载PyTorch

conda uninstall pytorch
conda uninstall libtorch

10.安装opencv和tifffile库

因为BIT_CD工程使用了opencv等库,所以还需要继续安装下面这些款,如果不安装,则会碰到模块未识别的错误。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
conda install tifffile -c conda-forge

11.安装git和matplotlib

为了后期训练可以直接运行,还需要额外在该环境中安装git和matplotlib,其中git是为了方便在虚拟环境中直接执行.sh文件,否则命令无法被识别。

conda install git
pip install matplotlib

12.初步测试demo

全部安装完成之后,就可以直接使用命令:

python .\demo.py

进行测试验证,然而你会发现一个错误,一个nn.GELU()函数未识别。

具体解决方法如下,修改help_funcs.py中的内容如下图:

 

具体内容为:

class GELU(nn.Module):
    def forward(self, input: Tensor) -> Tensor:
        return F.gelu(input)

class FeedForward(nn.Module):
    def __init__(self, dim, hidden_dim, dropout = 0.):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(dim, hidden_dim),
            GELU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, dim),
            nn.Dropout(dropout)
        )
    def forward(self, x):
        return self.net(x)

13.执行测试demo

至此,可以直接执行

python .\demo.py

然后,成功得到验证结果:

 注意,图中当时因为cuda版本安装不正确,导致无法使用gpu跑,AttributeError: module 'torch._C' has no attribute '_cuda_setDevice'(在python命令后面加上 --gpu_ids -1),这样就可以直接使用cpu跑了,如果cuda正常使用,则不需要增加该命令。

14.下载数据集

为训练模型做准备。

We have some samples from the LEVIR-CD dataset in the folder samples for a quick start.

Firstly, you can download our BIT pretrained model——by baidu drive, code: 2lyz or google drive. After downloaded the pretrained model, you can put it in checkpoints/BIT_LEVIR/.

15.执行训练

数据集下载完成后,也可以直接执行

sh scripts/run_cd.sh

进行训练。

但是此时会碰到一个"trainval.txt"无法找到的错误。

解决方法如下:《Remote Sensing Image Change Detection with Transformers复现遇到的问题》。

至此,本次任务到此结束,欢迎交流与讨论。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到