使用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]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张欣-男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值