日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2023/12/10 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3.7.1使用_使用python3和pytorch1.7.1运行dface 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

0 準(zhǔn)備工作

運(yùn)行環(huán)境:

Alienware GTX1070

Ubuntu 18.04

cuda 10.1

創(chuàng)建虛擬環(huán)境:

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 錯(cuò)誤

(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)

通過(guò)調(diào)試代碼發(fā)現(xiàn),輸入的圖像變成64位了。把它改成32位。原來(lái)用的np.float(默認(rèn)是64位),改為np.float32。

在pytorch 0.3.0中,文檔這么寫(xiě):

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中,文檔這么寫(xiě):

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類(lèi)型時(shí),才會(huì)歸一化到[

0

,

1

]

[0,1][0,1]。

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

總結(jié)

以上是生活随笔為你收集整理的python3.7.1使用_使用python3和pytorch1.7.1运行dface的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。