从零单排fastai脚本(3)

2019-05-30  本文已影响0人  深度学习模型优化

使用BIWI人脑姿态数据集来回归人脑中心位置。

有请人工智能四大天王。

下面分别介绍fastai解决BIWI的套路。

1 数据

数据集的下载地址BIWI head pose。

当然数据集也可以直接使用python下载

%reload_ext autoreload
%aureload 2
%matplotlib inline

from fastai.visoion import *
path = untar_data(URLs.BIWI_HEAD_POSE)

数据注意坐标变换,rgb.cal中包含中心位置转换的坐标值。
每一个单独的文件夹下独有一个rgb.cal

cal = np.genfromtxt(path/'01'/'rgb.cal', skip_footer=6)

为了由数据集中的姿态数据得到人头的中心位置,脚本做了如下变换

def convert_biwi(coords):
    c1 = coords[0] * cal[0][0]/coords[2] + cal[0][2]
    c2 = coords[1] * cal[1][1]/coords[2] + cal[1][2]
    return tensor([c2,c1])

def get_ctr(f):
    ctr = np.genfromtxt(img2txt_name(f), skip_header=3)
    return convert_biwi(ctr)

def get_ip(img,pts): return ImagePoints(FlowField(img.size, pts), scale=True)

其中get_ctr(f)从图片文件名,得到图片中人脸的中心位置坐标。

数据加载使用PointItemList

data = (PointsItemList.from_folder(path)
        .split_by_valid_func(lambda o: o.parent.name=='13')
        .label_from_func(get_ctr)
        .transform(get_transforms(), tfm_y=True, size=(120,160))
        .databunch().normalize(imagenet_stats)
       )

这里简单分析下上面的数据加载代码。使用了PointsItemList的from_folder来加载数据,直接从文件夹中提取数据。然后将文件夹名为13的文件夹下的数据作为验证集数据。标签使用get_ctr函数来从txt文件中读取人脸中心坐标,数据增强需要设置tfm_y=True,因为原始图像变换了,坐标也要跟这边,不然不在人脸中心位置了。后面的databunch和normalize都是常规操作了。

图1 部分训练数据,注意人脸中心红点

2

图2 识别结果
上一篇 下一篇

猜你喜欢

热点阅读