Skip to content

Commit afdabd1

Browse files
committed
add data
reference: Jianwei Yang Implementation for Face alignment at 3000 FPS via Regressing Local Binary Features To fit the data, need to revise the input of the functions
1 parent a275a34 commit afdabd1

File tree

7 files changed

+231
-10
lines changed

7 files changed

+231
-10
lines changed

ESR_Train.m

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
function ESR_Train()
2-
% load data
3-
% load('../Data/toyData.mat', 'images', 'bbx', 'pts');
4-
load('../../Data/toyData', 'images_aug', 'bbx_aug', 'pts_aug', 'current_shapes','ground_truth');
5-
2+
%% load parameters
63
params = Train_params;
7-
params.N_img = size(images_aug, 1);
84
% create paralllel local jobs note
95
if isempty(gcp('nocreate'))
106
parpool(2);
117
end
8+
%% load data
9+
if exist('Data/train_init.mat', 'file')
10+
load('Data/train_init.mat', 'data');
11+
else
12+
data = loadsamples('D:\Dataset\lfpw\annotations\trainset', 'png');
13+
%mkdir Data;
14+
save('Data/train_init.mat', 'data');
15+
end
16+
17+
load('Data/InitialShape_68');
18+
params.meanshape = S0(params.ind_usedpts, :);
19+
params.N_fp = size(params.meanshape, 1);
20+
%% flip data
21+
if params.flip
22+
data_flip = fliplrdata(data);
23+
else
24+
data_flip = [];
25+
end
26+
Data = [data; data_flip];
27+
%% choose corresponding points for training
28+
parfor i = 1:length(Data)
29+
Data{i}.shape_gt = Data{i}.shape_gt(params.ind_usedpts, :);
30+
Data{i}.bbox_gt = getbbox(Data{i}.shape_gt);
31+
end
32+
%% augment the data
33+
Data = augmtdata(Data, params);
34+
%% Explicit Shape Regression
35+
params.N_img = size(images_aug, 1);
36+
1237

1338
Y = cell(params.N_img, 1);
1439
Error = zeros(1, params.T+1);

Train_params.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
% the local scale of search, it set 0.3 times of the distance between two pupils on the mean shape
77
params.F = 5; % the number of features in fern
88

9-
load('../../Data/mean_shape.mat', 'S0');
10-
params.mean_shape = S0;
9+
%params.mean_shape = S0;
1110

12-
params.N_fp = size(params.mean_shape, 1);
11+
params.N_aug = 20;
12+
13+
params.N_fp = 0; %size(params.mean_shape, 1);
1314
params.N_img = 0;
1415

15-
params.k = params.k*pdist([mean([params.mean_shape(20, :); params.mean_shape(23, :)]);...
16-
mean([params.mean_shape(26, :); params.mean_shape(29, :)])])/2;
16+
params.flip = 1; % mirror the data
17+
params.ind_usedpts = 18:68;
18+
%params.k = params.k*pdist([mean([params.mean_shape(20, :); params.mean_shape(23, :)]);...
19+
% mean([params.mean_shape(26, :); params.mean_shape(29, :)])])/2;
1720
end

augmtdata.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function data_aug = augmtdata(data, params)
2+
data_len = length(data);
3+
data_aug = cell(params.N_aug*data_len, 1);
4+
5+
6+
for i = 1: data_len
7+
% random select initial shape without replacement
8+
rand_index_ = randperm(data_len, params.N_aug);
9+
while ismember(i, rand_index_) % if rand_index contain i, rand select again
10+
rand_index_ = randperm(data_len, params.N_aug);
11+
end
12+
% expand the data
13+
for j = 1: params.N_aug
14+
r_index = rand_index_(j);
15+
% copy the original stuff
16+
data_index = (j-1)*data_len + i;
17+
data_aug{data_index}.img_gray = data{i}.img_gray;
18+
data_aug{data_index}.width_orig = data{i}.width_orig;
19+
data_aug{data_index}.height_orig = data{i}.height_orig;
20+
data_aug{data_index}.width = data{i}.width;
21+
data_aug{data_index}.height = data{i}.height;
22+
data_aug{data_index}.shape_gt = data{i}.shape_gt;
23+
data_aug{data_index}.bbox_gt = data{i}.bbox_gt;
24+
% add the new stuff
25+
data_aug{data_index}.intermediate_shapes = cell(1, params.T);
26+
data_aug{data_index}.intermediate_bboxes = cell(1, params.T);
27+
% scale and translate the sampled shape to ground-truth
28+
% face rectangle region
29+
select_shape = resetshape(data{i}.bbox_gt, [data{r_index}.shape_gt]);
30+
31+
data_aug{data_index}.intermediate_shapes{1} = select_shape;
32+
data_aug{data_index}.intermediate_bboxes{1} = getbbox(select_shape);
33+
34+
meanshape_resize = resetshape(data_aug{data_index}.intermediate_bboxes{1}, params.meanshape);
35+
36+
data_aug{data_index}.tf2meanshape = fitgeotrans(bsxfun(@minus, ...
37+
data_aug{data_index}.intermediate_shapes{1}, mean(data_aug{data_index}.intermediate_shapes{1})), ...
38+
bsxfun(@minus, meanshape_resize, mean(meanshape_resize)),...
39+
'nonreflectivesimilarity');
40+
data_aug{data_index}.meanshape2tf = fitgeotrans(bsxfun(@minus, meanshape_resize, mean(meanshape_resize)), ...
41+
bsxfun(@minus, data_aug{data_index}.intermediate_shapes{1}, mean(data_aug{data_index}.intermediate_shapes{1})),...
42+
'nonreflectivesimilarity');
43+
44+
45+
shape_residual = bsxfun(@rdivide, data_aug{data_index}.shape_gt - data_aug{data_index}.intermediate_shapes{1},...
46+
[data_aug{data_index}.intermediate_bboxes{1}(3) data_aug{data_index}.intermediate_bboxes{1}(4)]);
47+
48+
[u, v] = transformPointsForward(data_aug{data_index}.tf2meanshape, shape_residual(:, 1), shape_residual(:, 2));
49+
data_aug{data_index}.shapes_residual = [u, v];
50+
end
51+
end
52+
53+
end

fliplrdata.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function data_flip = fliplrdata(data)
2+
data_len = size(data, 1);
3+
data_flip = cell(data_len, 1);
4+
parfor i = 1:data_len
5+
data_flip{i}.img_gray = fliplr(data{i}.img_gray);
6+
data_flip{i}.width_orig = data{i}.width_orig;
7+
data_flip{i}.height_orig = data{i}.height_orig;
8+
data_flip{i}.width = data{i}.width;
9+
data_flip{i}.height = data{i}.height;
10+
11+
data_flip{i}.shape_gt = flipshape(data{i}.shape_gt);
12+
data_flip{i}.shape_gt(:, 1) = data{i}.width - data_flip{i}.shape_gt(:, 1);
13+
14+
data_flip{i}.bbox_gt = data{i}.bbox_gt;
15+
data_flip{i}.bbox_gt(1) = data_flip{i}.width - data_flip{i}.bbox_gt(1) - data_flip{i}.bbox_gt(3);
16+
17+
end
18+
end

flipshape.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function shape_flipped = flipshape(shape)
2+
%FLIPSHAPE Summary of this function goes here
3+
% Function: flip input shape horizonically
4+
% Detailed explanation goes here
5+
6+
if size(shape, 1) == 68
7+
shape_flipped = shape;
8+
% flip check
9+
shape_flipped(1:17, :) = shape(17:-1:1, :);
10+
% flip eyebows
11+
shape_flipped(18:27, :) = shape(27:-1:18, :);
12+
% flip eyes
13+
shape_flipped(32:36, :) = shape(36:-1:32, :);
14+
% flip eyes
15+
shape_flipped(37:40, :) = shape(46:-1:43, :);
16+
shape_flipped(41:42, :) = shape(48:-1:47, :);
17+
shape_flipped(43:46, :) = shape(40:-1:37, :);
18+
shape_flipped(47:48, :) = shape(42:-1:41, :);
19+
20+
% flip mouth
21+
shape_flipped(49:55, :) = shape(55:-1:49, :);
22+
shape_flipped(56:60, :) = shape(60:-1:56, :);
23+
24+
shape_flipped(61:65, :) = shape(65:-1:61, :);
25+
shape_flipped(66:68, :) = shape(68:-1:66, :);
26+
27+
else
28+
disp('The Flip Funtion is Error!')
29+
end
30+
31+
end
32+

loadsamples.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function data = loadsamples(datapath, imgtype)
2+
dd = dir(fullfile(datapath,['*.', imgtype]));
3+
data_len = length(dd);
4+
data = cell(data_len, 1);
5+
6+
parfor i = 1: data_len
7+
8+
img = im2uint8(imread(fullfile(datapath, dd(i).name)));
9+
data{i}.height_orig = size(img, 1);
10+
data{i}.width_orig = size(img, 2);
11+
shapepath = strrep(fullfile(datapath, dd(i).name), imgtype, 'pts');
12+
data{i}.shape_gt = double(loadshape(shapepath));
13+
data{i}.bbox_gt = getbbox(data{i}.shape_gt);
14+
15+
region = enlargingbbox(data{i}.bbox_gt, 2.0);
16+
17+
region(2) = double(max(region(2), 1));
18+
region(1) = double(max(region(1), 1));
19+
20+
bottom_y = double(min(region(2) + region(4) - 1, data{i}.height_orig));
21+
right_x = double(min(region(1) + region(3) - 1, data{i}.width_orig));
22+
23+
img_region = img(region(2):bottom_y, region(1):right_x, :);
24+
25+
% recalculate the location of groundtruth shape and bounding box
26+
data{i}.shape_gt = bsxfun(@minus, data{i}.shape_gt, double([region(1) region(2)]));
27+
data{i}.bbox_gt = getbbox(data{i}.shape_gt);
28+
29+
% only use inner points
30+
data{i}.shape_gt = data{i}.shape_gt;
31+
32+
data{i}.isdet = 0;
33+
34+
if size(img_region, 3) == 1
35+
data{i}.img_gray = img_region;
36+
else
37+
% hsv = rgb2hsv(img_region);
38+
data{i}.img_gray = rgb2gray(img_region);
39+
end
40+
41+
data{i}.width = size(img_region, 2);
42+
data{i}.height = size(img_region, 1);
43+
end
44+
end
45+
46+
function shape = loadshape(path)
47+
% function: load shape from pts file
48+
file = fopen(path);
49+
50+
if ~isempty(strfind(path, 'COFW'))
51+
shape = textscan(file, '%d16 %d16 %d8', 'HeaderLines', 3, 'CollectOutput', 3);
52+
else
53+
shape = textscan(file, '%d16 %d16', 'HeaderLines', 3, 'CollectOutput', 2);
54+
end
55+
fclose(file);
56+
57+
shape = shape{1};
58+
end
59+
60+
function region = enlargingbbox(bbox, scale)
61+
62+
region(1) = floor(bbox(1) - (scale - 1)/2*bbox(3));
63+
region(2) = floor(bbox(2) - (scale - 1)/2*bbox(4));
64+
65+
region(3) = floor(scale*bbox(3));
66+
region(4) = floor(scale*bbox(4));
67+
68+
end

resetshape.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [shape_initial] = resetshape(bbox, shape_union)
2+
%RESETSHAPE Summary of this function goes here
3+
% Function: reset the initial shape according to the groundtruth shape and union shape for all faces
4+
% Detailed explanation goes here
5+
% Input:
6+
% bbox: bbounding box of groundtruth shape
7+
% shape_union: uniionshape
8+
% Output:
9+
% shape_initial: reset initial shape
10+
% bbox: bounding box of face image
11+
12+
% get the bounding box according to the ground truth shape
13+
width_union = (max(shape_union(:, 1)) - min(shape_union(:, 1)));
14+
height_union = (max(shape_union(:, 2)) - min(shape_union(:, 2)));
15+
16+
shape_union = bsxfun(@minus, (shape_union), (min(shape_union)));
17+
18+
shape_initial = bsxfun(@times, shape_union, [(bbox(3)/width_union) (bbox(4)/height_union)]);
19+
shape_initial = bsxfun(@plus, shape_initial, double([bbox(1) bbox(2)]));
20+
21+
end
22+

0 commit comments

Comments
 (0)