python3.7.1使用_使用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
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
總結
以上是生活随笔為你收集整理的python3.7.1使用_使用python3和pytorch1.7.1运行dface的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java关键字整理_【java基础知识整
- 下一篇: 惊了!最通俗易懂的Djongo入门竟然在