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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268740.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

新书预告

新书预告依2009年版考试大纲 重编的《网络工程师考试考前冲刺预测卷及考点解析(第2版,含2008年下半年试题解析)》、《软件设计师考试考前冲刺预测卷及考点解析(第2版)》,试题更新率可达80%以上,…

JS学习记录(BOM部分)

BOOM部分 Screen <html lang"en"> <head><meta charset"UTF-8"><title>Screen</title> </head> <body></body> <script>console.log(screen.width);console.log(screen.height);console.log(screen…

Java中JFrame怎样控制闪烁_在JFrame Java中闪烁

大家好,我正在做一个线程来更新JFrame上的球,所以我重新绘制屏幕…然后将球更新到它的位置……然后再次绘制屏幕……画出球和同样的周期……这是代码private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {Thread t new Thread(){public void run(){while(t…

基于Linux的虚拟主机搭建

为了建设二级网站建设&#xff0c;我考察了多个开源和商业的虚拟主机和控制面板。主要察看了Linux下的。很多工具&#xff0c;例如开源的VHCS&#xff0c;商业的Plesk功能虽然十分强大&#xff0c;但他们的主要服务对象是商业虚拟主机客户&#xff0c;和我们的集中管理模式不太…

在linux上安装jdk(转载)

软件环境&#xff1a; 虚拟机&#xff1a;VMware Workstation 10 操作系统&#xff1a;Ubuntu-12.04-desktop-amd64 JAVA版本&#xff1a;jdk-7u55-linux-x64 软件下载地址&#xff1a; JDK&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/index.html &a…

cryptico.js java解密_從Cryptico.js中提取RSA私鑰

我相信這是一個非常基本的問題&#xff0c;但我開始使用JavaScript和RSA進行研究&#xff0c;所以我有點失落。我剛剛下載了Cryptico庫&#xff0c;它爲我提供了一個易於使用的RSA密鑰生成/加密/解密。從Cryptico.js中提取RSA私鑰publicKeyString(RsaKey)哪個&#xff1a;my.pu…

WebService大讲堂之Axis2(1):用POJO实现0配置的WebService

Axis2是一套崭新的WebService引擎&#xff0c;该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和SOAP1.2&#xff0c;还集成了非常流行的REST WebService&#xff0c;同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。在本文中主要介绍了如何使用Axis2开…

Unity中UGUI之Canvas属性解读版本二

Canvas Render Modes(渲染模式) 1.在screen空间中渲染2.在world空间中渲染 Screen Space-Overlay 在这个渲染模式中&#xff0c;UI元素将在场景的上面。如果场景改变大小或改变分辨率&#xff0c;Canvas将自动改变大小去适配。 Screen Space-Camera 这和Screen Space-Overlay类…

docker 搭建nginx php mysql_Docker搭建PHP/Nginx/MySQL/Composer环境

一、安装PHP&#xff1a;#PHP镜像docker pull php:7.2-fpm#PHP容器&#xff0c;监听9000端口&#xff0c;映射主机项目目录/data/wwwdocker run --name php -d -p 9000:9000 -v /data/www:/www php:7.2-fpm二、安装Nginxdocker pull nginxdocker run --name php-nginx -d -p 80…

红帽企业linux4参考指南读书笔记-GRUB引导器

一个引导加载器通常存在于系统的主硬盘驱动器中&#xff0c;通过它来加载linux内核跟一些需要的文件或其它操作系统到系统内存中。在各种系统架构上运行RedHat企业Linux系统会使用不同的引导加载器&#xff0c;一般在X86系统架构和AMD、AMD64系统架构中使用GRUB引导器&#xff…

内容拾遗之字符串与数据结构

一、整数 如&#xff1a; 18、73、84 每一个整数都具备如下功能&#xff1a; 浅copy >>> dict {"a":("apple",),"bo":{"b":"banna","o":"orange"},"g":["grape","g…

int size java_int size()

int size()描述 (Description)size()方法用于获取此哈希表中的键数。声明 (Declaration)以下是java.util.Hashtable.size()方法的声明。public int size()参数 (Parameters)NA返回值 (Return Value)方法调用返回此哈希表中的键数。异常 (Exception)NA例子 (Example)以下示例显示…

智能化建筑

智能化建筑&#xff08;2009-1-7&#xff09;现在的建筑都朝着智能化发展&#xff0c;作为工程师更应该走在社会脚步发展的前面。智能化楼宇自动化&#xff08;BA&#xff09;通讯自动化 &#xff08;CA&#xff09;办公自动化 &#xff08;OA&am…

java 新区 老区_优学院《土地资源学》答案在线查题2020高校邦《Java核心开发技术【实境编程】》章测试答案...

优学院《土地资源学》答案在线查题2020高校邦《Java核心开发技术【实境编程】》章测试答案更多相关问题[问答题] 老区的汽油分馏塔(DA&#xff0d;101)原设计结构如何&#xff1f;[问答题] SRT&#xff0d;I型炉与SRT&#xff0d;I改进型炉(乙烷炉)的烧焦过程有何不同&#xff…

用subline添加单引号和逗号,在sql中使用

subline Sublime编辑器批量在行首或者行尾添加字符串的方法&#xff1a; 1、选中要操作的行&#xff0c;全选就按CtrlA&#xff1b; 2、使用快捷键CtrlShiftL&#xff0c;进入待操作状态&#xff1b; 3、使用左右方向键&#xff08;←或者→&#xff09;&#xff0c;批量操作…

[独库骑行之我们路过沙漠]塔克拉玛干的边缘

塔克拉玛干的边缘 在计划这段旅行时我最憧憬的路段就是横穿塔克拉玛干的沙漠公路&#xff0c;缘于影视里的茫茫大漠上金色的胡杨林的印象。 由于时间关系&#xff0c;我们没有走那条沙漠公路。倒是从沙雅到阿拉尔之间有部分沙漠公路&#xff0c;但完全没有这种诗情画意&#xf…