目录
如下图所示:

可以注意到,有一个有PowerShell ,还有一个没有PowerShell,这两个在功能上没有直接区别,都可以使用,不过有PowerShell 的包含更多的linux命令,比如ls之类的,会更方便一些,所以推荐使用带PowerShell的命令行。
conda info --envs
默认会得到一个基本的环境,笔者因为已经创建了一个名为pytorch的环境,所以会显示有两个。

conda create -n pytorch python=3.6
conda activate pytorch
此时已经成功进入名为pytorch的虚拟环境中了,接下来的操作都是在这个虚拟环境中进行的,我们安装的库都是保存在虚拟环境中的。
https://pytorch.org/官网
这里需要注意的是,如果之前第二步查看时有GPU,则CUDA选择11即可,因为10已经不再支持;如果只有集显,那么CUDA选择None即可
打开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/
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
最后我们检验安装是否成功,先键入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
因为BIT_CD工程使用了opencv等库,所以还需要继续安装下面这些款,如果不安装,则会碰到模块未识别的错误。
- pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
- conda install tifffile -c conda-forge

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

全部安装完成之后,就可以直接使用命令:
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)
至此,可以直接执行
python .\demo.py
然后,成功得到验证结果:

注意,图中当时因为cuda版本安装不正确,导致无法使用gpu跑,AttributeError: module 'torch._C' has no attribute '_cuda_setDevice'(在python命令后面加上 --gpu_ids -1),这样就可以直接使用cpu跑了,如果cuda正常使用,则不需要增加该命令。
为训练模型做准备。
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/.
数据集下载完成后,也可以直接执行
sh scripts/run_cd.sh
进行训练。
但是此时会碰到一个"trainval.txt"无法找到的错误。
解决方法如下:《Remote Sensing Image Change Detection with Transformers复现遇到的问题》。
至此,本次任务到此结束,欢迎交流与讨论。