Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core & gl): 适配face detect & face landmark model #461

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/paddlejs-backend-webgl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paddlejs/paddlejs-backend-webgl",
"version": "1.2.9",
"version": "1.2.10-beta.0",
"description": "",
"main": "lib/index",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions packages/paddlejs-backend-webgl/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,13 @@ export default class WebGLBackend extends PaddlejsBackend {
return loc;
}

dispose() {

dispose({ inputTensors }) {
for (const tensor of inputTensors) {
const tensorName = tensor.tensorId;
this.texturesMap[tensorName] && this.gl.deleteTexture(this.texturesMap[tensorName]);
this.cacheTextures[tensorName] && this.gl.deleteTexture(this.cacheTextures[tensorName]);
this.cacheTextures[tensorName] = null;
this.texturesMap[tensorName] = null;
}
}
}
6 changes: 6 additions & 0 deletions packages/paddlejs-backend-webgl/src/ops/atom/common_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ float exp_func(float x, float y, float z) {
return result;
}`;

const abs_func = `
float abs_func(float x, float y, float z) {
return abs(x);
}`;

export {
prelu,
relu6,
Expand All @@ -87,6 +92,7 @@ export {
sqrt,
pow_func,
tanh_func,
abs_func,
transferFromNHWCtoNCHW
};

6 changes: 5 additions & 1 deletion packages/paddlejs-backend-webgl/src/ops/atom/fuse_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export default function genFuseOpCode(params: OpParams) {
act_name = 'exp_func';
break;

case 'abs':
act_name = 'abs_func';
break;

default:
break;
}
Expand Down Expand Up @@ -127,7 +131,7 @@ export default function genFuseOpCode(params: OpParams) {

return `
${activation_func}

float fuse_op(float x) {
float res = x;
${calculation_str}
Expand Down
11 changes: 9 additions & 2 deletions packages/paddlejs-backend-webgl/src/ops/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import batchnorm from './shader/batchnorm';
import reshape2 from './shader/reshape2';
import bilinear_interp from './shader/bilinear_interp';
import bilinear_interp_v2 from './shader/bilinear_interp_v2';
import linear_interp from './shader/linear_interp';
import linear_interp_v2 from './shader/linear_interp_v2';
import transpose2 from './shader/transpose2';
import softmax from './shader/softmax';
import dynamic from './shader/dynamic';
Expand All @@ -36,11 +38,12 @@ import arg_min from './shader/arg_min';
import unsqueeze2 from './shader/unsqueeze2';
import flatten_contiguous_range from './shader/flatten_contiguous_range';
import greater_than from './shader/greater_than';
import reduce_sum from './shader/reduce_sum';
import where from './shader/where';
import connect from './shader/connect';
import squeeze2 from './shader/squeeze2';
import pad3d from './shader/pad3d';
import reduce_sum from './shader/reduce_sum';
import reduce_max from './shader/reduce_max';
import reduce_mean from './shader/reduce_mean';
import shuffle_channel from './shader/shuffle_channel';
import hard_swish from './shader/hard_swish';
Expand Down Expand Up @@ -97,6 +100,7 @@ const ops = {
reshape: reshape2,
reshape2,
bilinear_interp,
linear_interp,
transpose2,
unpacked_2_packed,
packed_2_unpacked,
Expand All @@ -105,9 +109,10 @@ const ops = {
flatten2: reshape2,
greater_than,
reduce_sum,
reduce_mean,
reduce_max,
where,
connect,
reduce_mean,
hard_swish,
nearest_interp,
nearest_interp_v2,
Expand All @@ -130,9 +135,11 @@ const ops = {
sqrt: dynamic('sqrt'),
tanh: dynamic('tanh'),
exp: dynamic('exp'),
abs: dynamic('abs'),
squeeze2,
pad3d,
bilinear_interp_v2,
linear_interp_v2,
shuffle_channel,
pack_out,
nhwc_2_nchw,
Expand Down
17 changes: 12 additions & 5 deletions packages/paddlejs-backend-webgl/src/ops/shader/batchnorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
*/

function mainFunc(
{ bias, scale, mean, variance },
{ bias, scale, mean, variance, origin },
{ epsilon }
) {
// calc the real pos of channel
const len = origin.length_unformatted_shape;
const realCPos = 4 - len + 1;
const posStr = `oPos[${realCPos}]`;
return `
// start函数
void main(void) {
Expand All @@ -14,11 +18,14 @@ function mainFunc(
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);

// 归一化数据
vec4 scale = getPixelsFromTexturePos_scale(vec2( float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
vec4 bias = getPixelsFromTexturePos_bias(vec2( float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
vec4 mean = getPixelsFromTexturePos_mean(vec2((float(oPos.g)) / float(${mean.width_texture}) + 0.00001, 0.0));
vec4 scale = getPixelsFromTexturePos_scale(
vec2(float(${posStr}) / float(${scale.width_texture}) + 0.00001, 0.0));
vec4 bias = getPixelsFromTexturePos_bias(
vec2(float(${posStr}) / float(${bias.width_texture}) + 0.00001, 0.0));
vec4 mean = getPixelsFromTexturePos_mean(
vec2((float(${posStr})) / float(${mean.width_texture}) + 0.00001, 0.0));
vec4 variance = getPixelsFromTexturePos_variance(
vec2((float(oPos.g)) / float(${variance.width_texture}) + 0.00001,
vec2((float(${posStr})) / float(${variance.width_texture}) + 0.00001,
0.0)
);

Expand Down
3 changes: 2 additions & 1 deletion packages/paddlejs-backend-webgl/src/ops/shader/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const commonFuncBehaviors = {
pow: ['transToPow'],
exp: ['transToExp'],
sqrt: ['transToSqrt'],
tanh: ['transToTanh']
tanh: ['transToTanh'],
abs: ['transToAbs']
};

function mainFunc(
Expand Down
58 changes: 58 additions & 0 deletions packages/paddlejs-backend-webgl/src/ops/shader/linear_interp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file bilinear_interp
*/

function mainFunc(
{ out, origin },
{ align_mode = 1, align_corners = true }
) {
const outW = out.width_shape;
const inW = origin.width_shape;
const scale = align_corners ? (outW - 1) / (inW - 1) : (outW / inW);

return `
// start函数

vec4 getData(float n, float scale, bool align_flag, int in_len) {
float m = align_flag ? ((n + 0.5) / scale - 0.5) : (n / scale);
// int a1 = int(floor(m + 0.5));
int a1 = int(m);
a1 = a1 > 0 ? a1 : 0;
int a2 = a1 < (in_len - 1) ? (a1 + 1) : in_len;

float idx_src = (n + 0.5) / scale - 0.5;
idx_src = idx_src > 0.0 ? idx_src : 0.0;
float b1 = align_flag ? (idx_src - float(a1)) : (n / scale - float(a1));
float b2 = 1.0 - b1;
return vec4(float(a1), float(a2), b1, b2);
}

void main(void) {
// 输出数据
ivec4 oPos = getOutputTensorPos();

bool align_flag = ${align_mode} == 0 && !${align_corners};

float scale = float(${scale});

vec4 v = getData(float(oPos.a), scale, align_flag, ${inW});

float v1 = v.r;
float v2 = v.g;
float v3 = v.b;
float v4 = v.a;

float value1 = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, int(v1));
float value2 = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, int(v2));
float value = v4 * value1 + v3 * value2;
// setOutput(float(int(v1)));
setOutput(float(value));
}
`;
}
export default {
mainFunc,
textureFuncConf: {
origin: ['getValueFromTensorPos']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @file bilinear_interp_v2
* @implements 差值实现方式(BilinearInterpolation)与 bilinear_interp 一致,唯一的区别是 outshape 计算
*/

import linear_interp from './linear_interp';

export default linear_interp;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function mainFunc(
index = ${originShape[2] * originShape[3]} * out_pos[1] + ${originShape[3]} * oy + ox;
}
`;
outputCode = 'setOutput(float(index));';
outputCode = 'setOutput(float(res));';
}
return `
// start函数
Expand Down
32 changes: 32 additions & 0 deletions packages/paddlejs-backend-webgl/src/ops/shader/reduce_max.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

/**
* @file concat
*/

function mainFunc(
{},
{ inputs_dim, dim }
) {
return `
// start函数
void main(void) {
ivec4 oPos = getOutputTensorPos();
// 输出坐标转换为输入坐标
float o = 0.0;
for (int i = 0; i < ${inputs_dim}; i++) {
oPos[${dim}] = i;
o = max(o, getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a));
}
setOutput(float(o));
}
`;
}
export default {
mainFunc,
textureFuncConf: {
origin: ['getValueFromTensorPos']
},
behaviors: [
'normalizeDim'
]
};
28 changes: 20 additions & 8 deletions packages/paddlejs-backend-webgl/src/ops/shader/reshape2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@
*/

function mainFunc(
{ out },
{ origin, out },
{}
) {
return `
// start函数
void main(void) {
vec2 outCoord = vCoord.xy * (_2d_shape_texture_out);
int index = int(outCoord.x) + int(outCoord.y) * int(${out.width_texture});
ivec4 originPos = getTensorPosFromArrayIndex_origin(index);
float res = getValueFromTensorPos_origin(originPos[0], originPos[1], originPos[2], originPos[3]);
setOutput(res);
// 输出数据
ivec4 oPos = getOutputTensorPos();
// 输出坐标转换为输入坐标
int sumVal = oPos.a
+ oPos.b * ${out.width_shape}
+ oPos.g * ${out.height_shape} * ${out.width_shape}
+ oPos.r * ${out.channel} * ${out.width_shape} * ${out.height_shape};
ivec4 new_oPos = transferFromNHWCtoNCHW(
sumVal,
${origin.channel},
${origin.width_shape},
${origin.height_shape},
${origin.total_shape}
);
float o = getValueFromTensorPos_origin(new_oPos.r, new_oPos.g, new_oPos.b, new_oPos.a);
setOutput(float(o));
}
`;
}
export default {
mainFunc,
textureFuncConf: {
origin: ['getTensorPosFromArrayIndex', 'getValueFromTensorPos']
}
origin: ['getValueFromTensorPos']
},
commonFuncConf: ['transferFromNHWCtoNCHW']
};
Loading