您现在的位置是:首页 >学无止境 >mmsegmentation修仙之路-bug篇(2)网站首页学无止境

mmsegmentation修仙之路-bug篇(2)

石头变钻石? 2023-06-03 20:00:03
简介mmsegmentation修仙之路-bug篇(2)

RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

 File "D:ProgramDataAnaconda3envsopenmmlablibmultiprocessingspawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        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.

报错原因:
pytorch在Linux中和windows/MacOS中支持多线程的是存在差异的,Linux中,子线程是直接通过克隆地址来获取dataset和python函数参数的。在window下,python解释器运行后,如果没有指定主入口,python会按顺序执行代码,此时新创建进程后,子进程会再次导入运行的文件并按顺序全部执行代码,多进程代码会再次被执行,但multiprocessing.Process的源码中是对子进程再次产生子进程是做了限制的,是不允许的,所以会报错提示。

解决办法:
1、在主程序加入if __name__ == '__main__': ,指定程序入口,此时主程序的命名空间为‘__main__’,继续使用多线程加载
2、设置num_workers=0并且,persistent_workers=False 转为单线程加载

Python中if name==“main” 语句在调用多进程Process过程中的作用分析
Python 中的 if name == ‘main’ 该如何理解
python官方说明
pytorch官方说明

RuntimeError: Found dtype Float but expected Half/RuntimeError: Found dtype Double but expected Float

Traceback (most recent call last):
  File "F:/train_amp.py", line 214, in <module>
    train_net(net=net, device=device, train_data_path=train_data_path, val_data_path=val_data_path,
  File "F:/train_amp.py", line 101, in train_net
    loss = criterion(pred, label)
  File "C:UsersAdministrator.condaenvsmmlablibsite-packages	orch
nmodulesmodule.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:UsersAdministrator.condaenvsmmlablibsite-packages	orch
nmodulesloss.py", line 613, in forward
    return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
  File "C:UsersAdministrator.condaenvsmmlablibsite-packages	orch
nfunctional.py", line 3083, in binary_cross_entropy
    return torch._C._nn.binary_cross_entropy(input, target, weight, reduction_enum)
RuntimeError: Found dtype Float but expected Half

Process finished with exit code 1

这两个都一样,检查报错部分的数据类型,比如我的报错,就是在计算loss时,pred和label的数据类型不一致导致的,使用pred.float()就可以消除报错。

TypeError:‘tuple‘ object is not callable

# 报错代码
class DoubleConv(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(DoubleConv, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, out_ch, 3, padding=1),
        self.batch = nn.BatchNorm2d(out_ch),
        self.ReLU = nn.ReLU(inplace=True),
        self.conv2 = nn.Conv2d(out_ch, out_ch, 3, padding=1),

    def forward(self, input):
        x = self.conv1(input)
        x = self.batch(x)
        x = self.ReLU(x)
        x = self.conv2(x)
        x = self.batch(x)
        x = self.ReLU(x)

        return x

检查网络定义是否正确,错误代码中定义的每一层后面多了一个逗号,python读入时将整个定义的层当成一个元组读入了,因此报错。

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。