前言
因为没有申请到北京超算,所以一直使用的是自己电脑显卡进行训练。但是windows系统训练linux系统中的示例代码时会有一些问题出现,在此记录一下。
模型训练
pytorch环境配置问题
本机安装pytorch环境需要根据本机的cuda版本安装,但是可以安装低于本机cuda版本的pytorch。我在安装cuda11.7对应的pytorch时,不管换源还是直接开加速器下载经常会卡住报错,猜测可能是资源支持问题。使用下面的命令容易卡住无法安装。
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
使用以下命令安装11.6版本的pytorch可以顺利安装。
# CUDA 11.6
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch -c conda-forge
模型训练多线程问题
若按照给出的教程代码直接训练,则会出现报错:
RuntimeError: class `IterBasedTrainLoop` in mmengine/runner/loops.py:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
这是因为多线程任务出现报错,windows系统和linux系统对于多线程的支持有所不同。解决问题也比较简单,将模型启动的代码:
# register all modules in mmseg into the registries
# do not init the default scope here because it will be init in the runner
register_all_modules(init_default_scope=False)
runner = Runner.from_cfg(cfg)
runner.train()
改为:
# register all modules in mmseg into the registries
# do not init the default scope here because it will be init in the runner
register_all_modules(init_default_scope=False)
runner = Runner.from_cfg(cfg)
if __name__ == '__main__':
runner.train()
这样就可以解决多线程报错的问题。