Pytorch安装及学习人脸特征点识别

640?wx_fmt=png

Pytorch学习

0.说在前面1.pytorch安装及测试2.pytorch官网学习3.作者的话

0.说在前面

继续cs231n的课程,我们学习到pytorch,这里给出安装过程及pytorch基本使用!

1.pytorch安装及测试

  • 检查cuda版本

640?wx_fmt=png

  • Pytorch官网下载

640?wx_fmt=png

  • 输入官网下载命令

  • 测试

输入

import torch
torch.__version__

输出

'0.4.1'
  • 测试cuda可用性

输入

torch.cuda.is_available()

输出

True

2.pytorch官网学习

安装包

学习官网实例,需要安装scikit-image包,安装方式如下:

pip install -U scikit-image

导包

from __future__ import print_function, division
import os
import torch
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
# 忽略警告
import warnings
warnings.filterwarnings("ignore")
plt.ion()   # 交互模式

数据集

数据集下载地址:

https://download.pytorch.org/tutorial/faces.zip

加载数据

landmarks_frame = pd.read_csv('faces/face_landmarks.csv')

查看数据格式

查看前5行数据:

landmarks_frame.head(5)

5行137列,去掉第一列图像名字,后面总共136列,每一个点包含x与y坐标,表示总共有68个特征点!

绘制图像特征点

提取图像名字

n = 4
img_name = landmarks_frame.iloc[n, 0]
img_name

输出

'1198_0_861.jpg'

将图像的特征点放到一个数组中

# 并不是矩阵化,而是一个数组中!
landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
landmarks

输出

array([138, 392, 141, 427, 145, 464, 152, 501, 166, 536, 186, 567, 214,
       594, 249, 614, 290, 616, 329, 608, 363, 589, 390, 562, 411, 529,
       ...
       294, 516, 279, 518, 265, 516], dtype=object)

将每个点坐标表示一行数据

# 一行表示点的x与y坐标,形成(n,2)数组!
# -1 表示模糊匹配
landmarks = landmarks.astype('float').reshape(-1, 2)
# 打印输出前5行数据
landmarks[:5,:]

输出

array([[138., 392.],
       [141., 427.],
       [145., 464.],
       [152., 501.],
       [166., 536.]])

定义特征点绘制方法

def show_landmarks(image, landmarks):
    """Show image with landmarks"""
    plt.imshow(image)
    plt.scatter(landmarks[:, 0], landmarks[:, 1], s=10, marker='.', c='r')
    plt.pause(0.001)  # pause a bit so that plots are updated

特征点绘制

plt.figure()
show_landmarks(io.imread(os.path.join('faces/', img_name)),landmarks)
plt.show()

输出

640?wx_fmt=jpeg

3.作者的话

如果您觉得本公众号对您有帮助,欢迎转发支持,谢谢!更多内容关注本公众号深度学习系列!

640?wx_fmt=png

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值