keras-js
Version:
Run Keras models in the browser, with GPU support using WebGL
147 lines (114 loc) • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Layer = _interopRequireDefault(require("../../Layer"));
var _Tensor = _interopRequireDefault(require("../../Tensor"));
var _WebGL = require("../../WebGL2");
var _ndarrayOps = _interopRequireDefault(require("ndarray-ops"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const mapInputProgramSource = "#version 300 es\nprecision highp float;\nprecision highp isampler2D;\n\nin vec2 outTex;\nuniform sampler2D x;\nuniform isampler2D indexMap;\nuniform int inputCols;\nout vec4 outColor;\n\nvoid main() {\n ivec2 size = textureSize(indexMap, 0);\n int out_x = int(float(size[0]) * outTex.x);\n int out_y = int(float(size[1]) * outTex.y);\n\n int index = texelFetch(indexMap, ivec2(out_x, out_y), 0).r;\n\n if (index != -1) {\n int rowIndex = int(floor(float(index) / float(inputCols)));\n int colIndex = int(mod(float(index), float(inputCols)));\n float val = texelFetch(x, ivec2(colIndex, rowIndex), 0).r;\n outColor = vec4(val);\n } else {\n outColor = vec4(0.0);\n }\n}\n";
class ZeroPadding1D extends _Layer.default {
constructor(attrs = {}) {
super(attrs);
this.layerClass = 'ZeroPadding1D';
const {
padding = [1, 1]
} = attrs;
if (Array.isArray(padding)) {
this.padding = padding;
} else {
this.padding = [padding, padding];
}
this.description = `${JSON.stringify(this.padding)}`;
if (this.gpu) {
this.mapInputProgram = _WebGL.webgl2.compileProgram(mapInputProgramSource);
}
}
call(x) {
if (this.gpu) {
this._callGPU(x);
} else {
this._callCPU(x);
}
return this.output;
}
_callCPU(x) {
this.inputShape = x.tensor.shape;
this.outputShape = [this.inputShape[0] + this.padding[0] + this.padding[1], this.inputShape[1]];
this.output = new _Tensor.default([], this.outputShape);
_ndarrayOps.default.assign(this.output.tensor.hi(this.inputShape[0] + this.padding[0], this.inputShape[1]).lo(this.padding[0], 0), x.tensor);
}
_createIndexMap() {
if (this.indexMap) {
return;
}
const indices = new _Tensor.default([], this.inputShape, {
type: Int32Array
});
const indicesRow = new _Tensor.default([], this.inputShape, {
type: Int32Array
});
const indicesCol = new _Tensor.default([], this.inputShape, {
type: Int32Array
});
for (let i = 0; i < this.inputShape[0]; i++) {
_ndarrayOps.default.assigns(indicesRow.tensor.pick(i, null), i);
}
for (let j = 0; j < this.inputShape[1]; j++) {
_ndarrayOps.default.assigns(indicesCol.tensor.pick(null, j), j);
}
_ndarrayOps.default.muls(indices.tensor, indicesRow.tensor, this.inputShape[1]);
_ndarrayOps.default.addeq(indices.tensor, indicesCol.tensor);
this.indexMap = new _Tensor.default([], this.outputShape, {
type: Int32Array
});
const sliceStart = [this.padding[0], 0];
const sliceEnd = [this.inputShape[0] + this.padding[0], this.inputShape[1]];
_ndarrayOps.default.assigns(this.indexMap.tensor, -1);
_ndarrayOps.default.assign(this.indexMap.tensor.hi(...sliceEnd).lo(...sliceStart), indices.tensor);
this.indexMap.createGLTexture({
type: '2d',
format: 'int'
});
}
_callGPU(x) {
if (!x.glTexture) {
x.createGLTexture({
type: '2d',
format: 'float'
});
}
this.inputShape = x.tensor.shape;
this.outputShape = [this.inputShape[0] + this.padding[0] + this.padding[1], this.inputShape[1]];
this._createIndexMap();
if (!this.output) {
this.output = new _Tensor.default([], this.outputShape);
this.output.createGLTexture({
type: '2d',
format: 'float'
});
}
_WebGL.webgl2.runProgram({
program: this.mapInputProgram,
output: this.output,
inputs: [{
input: x,
name: 'x'
}, {
input: this.indexMap,
name: 'indexMap'
}],
uniforms: [{
value: x.glTextureShape[1],
type: 'int',
name: 'inputCols'
}]
});
if (this.outbound.length === 0) {
this.output.transferFromGLTexture();
}
}
}
exports.default = ZeroPadding1D;