使用python3和pytorch1.7.1运行dface

0 准备工作

运行环境:

  • Alienware GTX1070
  • Ubuntu 18.04
  • cuda 10.1

创建虚拟环境:

conda create -n py37_dface python=3.7

conda activate py37_dface


pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

pip install opencv-python

pip install matplotlib

1 错误

(py37_dface) ➜  DFace git:(zxdev) python test_image.py  
/home/zhangxin/github/DFace/dface/core/models.py:8: UserWarning: nn.init.xavier_uniform is now deprecated in favor of nn.init.xavier_uniform_.
  nn.init.xavier_uniform(m.weight.data)
/home/zhangxin/github/DFace/dface/core/models.py:9: UserWarning: nn.init.constant is now deprecated in favor of nn.init.constant_.
  nn.init.constant(m.bias, 0.1)
Traceback (most recent call last):
  File "test_image.py", line 18, in <module>
    bboxs, landmarks = mtcnn_detector.detect_face(img)
  File "/home/zhangxin/github/DFace/dface/core/detect.py", line 609, in detect_face
    boxes, boxes_align = self.detect_pnet(img)
  File "/home/zhangxin/github/DFace/dface/core/detect.py", line 270, in detect_pnet
    cls_map, reg = self.pnet_detector(feed_imgs)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/zhangxin/github/DFace/dface/core/models.py", line 97, in forward
    x = self.pre_layer(x)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 423, in forward
    return self._conv_forward(input, self.weight)
  File "/home/zhangxin/miniconda3/envs/py37_dface/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 420, in _conv_forward
    self.padding, self.dilation, self.groups)
RuntimeError: expected scalar type Double but found Float

2 解决方法

2.1

test_image.py添加

torch.set_default_tensor_type(torch.FloatTensor)

不行

2.2

dface/core/detect.py

            # import torchvision.transforms as transforms
            # t = transforms.ToTensor()
            # image_tensor = t(im_resized.astype(np.double))

也不行

2.3

def convert_image_to_tensor(image):
    """convert an image to pytorch tensor

        Parameters:
        ----------
        image: numpy array , h * w * c

        Returns:
        -------
        image_tensor: pytorch.FloatTensor, c * h * w
        """
    image = image.astype(np.float32)
    return transform(image)

通过调试代码发现,输入的图像变成64位了。把它改成32位。原来用的np.float(默认是64位),改为np.float32。

在pytorch 0.3.0中,文档这么写:

Convert a PIL Image or numpy.ndarray to tensor.

Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].

而在pytorch 1.7.1中,文档这么写:

Convert a PIL Image or numpy.ndarray to tensor. This transform does not support torchscript.

Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8

In the other cases, tensors are returned without scaling.

也就是在新版本1.7.1中,numpy.ndarray只有是np.uint8类型时,才会归一化到 [ 0 , 1 ] [0,1] [0,1]

已标记关键词 清除标记
问题描述:同一段代码,使用单显卡时没有问题,使用多张显卡时出现问题: ``` Traceback (most recent call last): File "trainer.py", line 370, in <module> trainer.train() File "trainer.py", line 263, in train self.x_tilde = self.G(self.z) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "G:\anaconda\lib\site-packages\torch\nn\parallel\data_parallel.py", line 152, in forward outputs = self.parallel_apply(replicas, inputs, kwargs) File "G:\anaconda\lib\site-packages\torch\nn\parallel\data_parallel.py", line 162, in parallel_apply return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)]) File "G:\anaconda\lib\site-packages\torch\nn\parallel\parallel_apply.py", line 85, in parallel_apply output.reraise() File "G:\anaconda\lib\site-packages\torch\_utils.py", line 394, in reraise raise self.exc_type(msg) RuntimeError: Caught RuntimeError in replica 1 on device 1. Original Traceback (most recent call last): File "G:\anaconda\lib\site-packages\torch\nn\parallel\parallel_apply.py", line 60, in _worker output = module(*input, **kwargs) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "E:\Work_place\pggan-pytorch-master的副本\network.py", line 181, in forward x = self.model(x.view(x.size(0), -1, 1, 1)) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "G:\anaconda\lib\site-packages\torch\nn\modules\container.py", line 100, in forward input = module(input) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "G:\anaconda\lib\site-packages\torch\nn\modules\container.py", line 100, in forward input = module(input) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "E:\Work_place\pggan-pytorch-master的副本\custom_layers.py", line 113, in forward x = self.conv(x.mul(self.scale)) File "G:\anaconda\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__ result = self.forward(*input, **kwargs) File "G:\anaconda\lib\site-packages\torch\nn\modules\conv.py", line 345, in forward return self.conv2d_forward(input, self.weight) File "G:\anaconda\lib\site-packages\torch\nn\modules\conv.py", line 342, in conv2d_forward self.padding, self.dilation, self.groups) RuntimeError: cuDNN error: CUDNN_STATUS_BAD_PARAM ``` 双显卡的型号为: 0号显卡GTX1660, 1号显卡GTX1060 两张显卡都是6G版本。 不知道这是哪里出问题了,求各路大神指点。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页