当前位置: 首页 > 工具软件 > DFace > 使用案例 >

python3.7.1使用_使用python3和pytorch1.7.1运行dface

戚成礼
2023-12-01

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

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]。

原文链接:https://blog.csdn.net/sdlypyzq/article/details/113090708

 类似资料: