Skip to content

Commit 26e1559

Browse files
Merge pull request #151 from JingyuanZhang/master
fix mediaProcessor param scale & add op bilinear_interp_v2
2 parents 88c5098 + 453c136 commit 26e1559

File tree

8 files changed

+35
-41
lines changed

8 files changed

+35
-41
lines changed

packages/paddlejs-backend-webgl/src/ops/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import split from './shader/split';
2323
import batchnorm from './shader/batchnorm';
2424
import reshape2 from './shader/reshape2';
2525
import bilinear_interp from './shader/bilinear_interp';
26+
import bilinear_interp_v2 from './shader/bilinear_interp_v2';
2627
import transpose2 from './shader/transpose2';
2728
import softmax from './shader/softmax';
2829
import dynamic from './shader/dynamic';
@@ -88,7 +89,8 @@ const ops = {
8889
pow: dynamic('pow'),
8990
sqrt: dynamic('sqrt'),
9091
squeeze2,
91-
pad3d
92+
pad3d,
93+
bilinear_interp_v2
9294
};
9395
export {
9496
ops

packages/paddlejs-backend-webgl/src/ops/shader/bilinear_interp.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ function mainFunc(
2525
void main(void) {
2626
// 输出数据
2727
ivec4 oPos = getOutputTensorPos();
28-
// 输出坐标转换为输入坐标
29-
//int sumVal = oPos.g
30-
+ oPos.a * ${out.channel}
31-
+ oPos.b * ${out.channel} * ${out.width_shape}
32-
+ oPos.r * ${out.channel} * ${out.width_shape} * ${out.height_shape};
33-
3428
3529
bool align_flag = ${align_mode} == 0 && !${align_corners};
3630
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @file bilinear_interp_v2
3+
* @implements 差值实现方式(BilinearInterpolation)与 bilinear_interp 一致,唯一的区别是 outshape 计算
4+
*/
5+
6+
import bilinear_interp from './bilinear_interp';
7+
8+
export default bilinear_interp;

packages/paddlejs-backend-webgl/src/webgl/buildShader.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import * as textureFunc from '../ops/atom/common_func_with_texture';
1313

1414
export default function buildShader(textureConf, type, inputTensors, fShaderParams, runtime: number, isPacked = false) {
1515
let code = '';
16+
const opName = getExactOpName(type, isPacked);
1617
try {
1718

18-
const opName = getExactOpName(type, isPacked);
19-
2019
const { params = {}, mainFunc, textureFuncConf = {}, commonFuncConf } = ops[opName];
2120

2221
// textureList: [filter, origin, bias]
@@ -50,7 +49,7 @@ export default function buildShader(textureConf, type, inputTensors, fShaderPara
5049
code = populateData(code);
5150
}
5251
catch (e) {
53-
console.error(e);
52+
console.error(`[${opName}]: ` + e);
5453
}
5554

5655
return code;

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Color = string;
88

99
export default class MediaProcessor {
1010
targetContext: CanvasRenderingContext2D = {} as CanvasRenderingContext2D;
11-
gapFillWith: Color = '#000';
11+
gapFillWith: Color = '#fff';
1212
mean: number[] = [0, 0, 0];
1313
std: number[] = [1, 1, 1];
1414
bgr: boolean = false;
@@ -53,7 +53,9 @@ export default class MediaProcessor {
5353
let data: ImageData | number[] = [];
5454
let scaleSize;
5555

56-
if (!(pixels instanceof HTMLImageElement || pixels instanceof HTMLVideoElement)) {
56+
if (!(pixels instanceof HTMLImageElement
57+
|| pixels instanceof HTMLVideoElement
58+
|| pixels instanceof HTMLCanvasElement)) {
5759
return [{
5860
data: data,
5961
shape: opt.shape || opt.targetShape,
@@ -63,19 +65,17 @@ export default class MediaProcessor {
6365

6466
this.pixelWidth = pixels.width;
6567
this.pixelHeight = pixels.height;
66-
67-
if (opt.scale && opt.targetSize) { // Moblienet的情况
68+
if (opt.scale && opt.targetSize) {
6869
data = this.resizeAndFitTargetSize(pixels, opt);
6970
}
70-
else if (opt.targetSize) { // 如果有targetSize,就是装在目标宽高里的模式 TinyYolo的情况
71+
else if (opt.targetSize) { // 如果有 targetSize,就是装在目标宽高里的模式
7172
scaleSize = this.fitToTargetSize(pixels, opt);
7273
data = this.getImageData(0, 0, scaleSize);
7374
}
7475
else {
7576
scaleSize = this.reSize(pixels, opt);
7677
data = this.getImageData(0, 0, scaleSize);
7778
}
78-
7979
if (opt.gray) {
8080
data = this.grayscale(data);
8181
}
@@ -142,18 +142,17 @@ export default class MediaProcessor {
142142
* @param opt.mean 均值
143143
* @param opt.std 方差
144144
* @param opt.targetShape 输出shape
145-
* @param opt.normalizeType 0:将数据映射为0~1, 1:映射为-1~1之间
146145
*/
147146
allReshapeToRGB(imageData, opt) {
148147

149148
// mean和std是介于0-1之间的
150-
const { mean, std, normalizeType = 0, targetShape } = opt;
149+
const { mean, std, targetShape } = opt;
151150
const [, c, h, w] = targetShape;
152151
const data = imageData.data || imageData;
153152

154153
const result = this.result;
155154
let offset = 0;
156-
155+
const normalizeType = 0; // 将数据映射为0~1, 1:映射为-1~1之间
157156
// h w c
158157
for (let i = 0; i < h; ++i) {
159158
const iw = i * w;
@@ -168,7 +167,6 @@ export default class MediaProcessor {
168167
}
169168
}
170169
}
171-
172170
return result;
173171
}
174172

@@ -261,7 +259,7 @@ export default class MediaProcessor {
261259
/**
262260
* 缩放成目标尺寸并居中
263261
*/
264-
fitToTargetSize(image, params, center?) {
262+
fitToTargetSize(image, params) {
265263
// 目标尺寸
266264
const targetWidth = params.targetSize.width;
267265
const targetHeight = params.targetSize.height;
@@ -284,12 +282,9 @@ export default class MediaProcessor {
284282
sh = Math.round(sw * this.pixelHeight / this.pixelWidth);
285283
y = Math.floor((targetHeight - sh) / 2);
286284
}
287-
if (center) {
288-
this.targetContext.drawImage(image, x, y, sw, sh);
289-
}
290-
else {
291-
this.targetContext.drawImage(image, 0, 0, sw, sh);
292-
}
285+
286+
this.targetContext.drawImage(image, x, y, sw, sh);
287+
293288
return { sw: targetWidth, sh: targetHeight };
294289
}
295290

packages/paddlejs-core/src/runner.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ export default class Runner {
5050
needPreheat: boolean = true;
5151

5252
constructor(options: ModelConfig | null) {
53-
const opts = {
54-
fill: '#fff',
55-
scale: 256
56-
};
57-
this.modelConfig = Object.assign(opts, options);
53+
this.modelConfig = Object.assign({}, options);
5854
this.needPreheat = options.needPreheat === undefined ? true : options.needPreheat;
5955
this.modelName = options.modelName || Date.now().toString();
6056
this.weightMap = [];
@@ -209,7 +205,6 @@ export default class Runner {
209205
});
210206
}
211207
}
212-
213208
else {
214209
preheatFeedData = vars.find(item => item.name === 'image');
215210
if (preheatFeedData) {
@@ -264,6 +259,7 @@ export default class Runner {
264259
if (op.type === 'fetch') {
265260
return;
266261
}
262+
267263
op.execute(this.isExecuted);
268264
if (env.get('debug')
269265
&& op.opData?.outputTensors

packages/paddlejs-core/src/transform/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
import { GLOBALS } from '../globals';
31
import TexturePacking from './texturePacking';
42
import FormatInputsX from './formatInputsX';
53
import type Transformer from './transformer';
@@ -16,14 +14,10 @@ const actions: TransformerAction = {
1614
new SplitOp()
1715
],
1816
transforms: [
19-
new FormatInputsX()
17+
new FormatInputsX(),
18+
new TexturePacking()
2019
],
2120
postTransforms: []
2221
};
2322

24-
// wegbl backend单独处理, 在第一个transform后面添加texturePacking
25-
if (GLOBALS.backend === 'webgl') {
26-
actions.transforms.splice(1, 0, new TexturePacking());
27-
}
28-
2923
export default actions;

packages/paddlejs-core/src/transform/texturePacking.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @file texture_packing
33
*/
4+
5+
import { GLOBALS } from '../globals';
46
import OpExecutor from '../opFactory/opExecutor';
57
import Transformer from './transformer';
68

@@ -107,6 +109,10 @@ export default class TexturePacking extends Transformer {
107109
}
108110

109111
transform(...args: any) {
112+
// wegbl backend 单独处理
113+
if (GLOBALS.backend !== 'webgl') {
114+
return;
115+
}
110116
const [originOp, vars, opsMap] = args;
111117

112118
if (!(packedOpConditions[originOp.type] && packedOpConditions[originOp.type](originOp, vars))) {

0 commit comments

Comments
 (0)