tfjs-tiny-yolov2
Version:
Tiny YOLO v2 object detection with tensorflow.js.
1 lines • 475 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.yolo=t.yolo||{})}(this,function(t){"use strict";var r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};function s(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var n=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t};function m(i,a,s,u){return new(s||(s=Promise))(function(t,e){function n(t){try{o(u.next(t))}catch(t){e(t)}}function r(t){try{o(u.throw(t))}catch(t){e(t)}}function o(e){e.done?t(e.value):new s(function(t){t(e.value)}).then(n,r)}o((u=u.apply(i,a||[])).next())})}function C(n,r){var o,i,a,t,s={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,i&&(a=2&e[0]?i.return:e[0]?i.throw||((a=i.return)&&a.call(i),0):i.next)&&!(a=a.call(i,e[1])).done)return a;switch(i=0,a&&(e=[2&e[0],a.value]),e[0]){case 0:case 1:a=e;break;case 4:return s.label++,{value:e[1],done:!1};case 5:s.label++,i=e[1],e=[0];continue;case 7:e=s.ops.pop(),s.trys.pop();continue;default:if(!(a=0<(a=s.trys).length&&a[a.length-1])&&(6===e[0]||2===e[0])){s=0;continue}if(3===e[0]&&(!a||e[1]>a[0]&&e[1]<a[3])){s.label=e[1];break}if(6===e[0]&&s.label<a[1]){s.label=a[1],a=e;break}if(a&&s.label<a[2]){s.label=a[2],s.ops.push(e);break}a[2]&&s.ops.pop(),s.trys.pop();continue}e=r.call(n,s)}catch(t){e=[6,t],i=0}finally{o=a=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var o={},i={alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0};function a(e){e in o||(document.createElement("canvas").addEventListener("webglcontextlost",function(t){t.preventDefault(),delete o[e]},!1),o[e]=function(t){if(1!==t&&2!==t)throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");var e=document.createElement("canvas");return 1===t?e.getContext("webgl",i)||e.getContext("experimental-webgl",i):e.getContext("webgl2",i)}(e));var t=o[e];return t.isContextLost()?(delete o[e],a(e)):(t.disable(t.DEPTH_TEST),t.disable(t.STENCIL_TEST),t.disable(t.BLEND),t.disable(t.DITHER),t.disable(t.POLYGON_OFFSET_FILL),t.disable(t.SAMPLE_COVERAGE),t.enable(t.SCISSOR_TEST),t.enable(t.CULL_FACE),t.cullFace(t.BACK),o[e])}function u(t){for(var e=t.length,n=0,r=0;0<e;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n}function c(t,e,n){return Math.max(t,Math.min(e,n))}function S(t){return t%2==0?t:t+1}function l(t){for(var e=0,n=0;n<t.length;n++)e+=t[n];return e}function h(t,e){var n=Math.random();return e*n+(1-n)*t}function M(t,e){if(!t)throw new Error("string"==typeof e?e:e())}function f(t,e,n){void 0===n&&(n=""),M(y(t,e),n+" Shapes "+t+" and "+e+" must match")}function p(t){M(null!=t,"The input to the tensor constructor must be a non-null value.")}function d(t,e){if(void 0===e&&(e=[]),Array.isArray(t)||A(t))for(var n=0;n<t.length;++n)d(t[n],e);else e.push(t);return e}function L(t){if(0===t.length)return 1;for(var e=t[0],n=1;n<t.length;n++)e*=t[n];return e}function y(t,e){if(t===e)return!0;if(null==t||null==e)return!1;if(t.length!==e.length)return!1;for(var n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function D(t){return t%1==0}function v(t){if(null!=Math.tanh)return Math.tanh(t);if(t===1/0)return 1;if(t===-1/0)return-1;var e=Math.exp(2*t);return(e-1)/(e+1)}function g(t){for(var e=Math.floor(Math.sqrt(t));1<e;--e)if(t%e==0)return[e,t/e];return[1,t]}function x(t,e){return e<=t.length?t:t+" ".repeat(e-t.length)}function w(i,a,s){return void 0===a&&(a=function(t){return 0}),new Promise(function(e,n){var r=0,o=function(){if(i())e();else{var t=a(++r);null!=s&&s<=r?n():setTimeout(o,t)}};o()})}function b(t,e){for(var n=1,r=-1,o=0;o<t.length;++o)if(0<=t[o])n*=t[o];else if(-1===t[o]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+o);r=o}else if(t[o]<0)throw Error("Shapes can not be < 0. Found "+t[o]+" at dim "+o);if(-1===r){if(0<e&&e!==n)throw Error("Size("+e+") must match the product of shape "+t);return t}if(0===n)throw Error("Cannot infer the missing size in ["+t+"] when there are 0 elements");if(e%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+e+" / "+n);var i=t.slice();return i[r]=e/n,i}function E(t,e){for(var n=[],r=[],o=0,i=0;i<t.length;++i){if(null!=e){if(e[o]===i&&1!==t[i])throw new Error("Can't squeeze axis "+i+" since its dim '"+t[i]+"' is not 1");(null==e[o]||e[o]>i)&&1===t[i]&&(n.push(t[i]),r.push(i)),e[o]<=i&&o++}1!==t[i]&&(n.push(t[i]),r.push(i))}return{newShape:n,keptDims:r}}function R(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else{if("bool"!==t)throw new Error("Unknown data type "+t);n=new Uint8Array(e)}return n}function N(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else if("bool"===t)n=new Uint8Array(e);else{if("string"!==t)throw new Error("Unknown data type "+t);n=new Array(e)}return n}function T(t,e,n){if("float32"===e)for(var r=0;r<t.length;r++)if(isNaN(t[r]))throw Error("The result of the '"+n+"' has NaNs.")}function k(t,e){if("float32"!==e)for(var n=0;n<t.length;n++)if(isNaN(t[n]))throw Error("NaN is not a valid value for dtype: '"+e+"'.")}function I(t,e){return!("complex64"===e||"float32"===e&&"complex64"!==t||"int32"===e&&"float32"!==t&&"complex64"!==t||"bool"===e&&"bool"===t)}function A(t){return t instanceof Float32Array||t instanceof Int32Array||t instanceof Uint8Array}function _(t){if("float32"===t||"int32"===t)return 4;if("complex64"===t)return 8;if("bool"===t)return 1;throw new Error("Unknown dtype "+t)}function O(t){if(null==t)return 0;var e=0;return t.forEach(function(t){return e+=2*t.length}),e}function F(t){return"string"==typeof t||t instanceof String}function e(t){return"boolean"==typeof t}function P(t){return"number"==typeof t}function B(t){return t instanceof Array?B(t[0]):t instanceof Float32Array?"float32":t instanceof Int32Array||t instanceof Uint8Array?"int32":P(t)?"float32":F(t)?"string":e(t)?"bool":"float32"}function z(t){return!!(t&&t.constructor&&t.call&&t.apply)}function W(t,e){for(var n=e;n<t;++n)if(t%n==0)return n;return t}function U(t){var e=t.length;if(e<2)return[];var n=new Array(e-1);n[e-2]=t[e-1];for(var r=e-3;0<=r;--r)n[r]=n[r+1]*t[r+1];return n}function V(t,e,n){if("string"===e)throw new Error("Cannot convert a string[] to a TypedArray");if(o=e,(r=t)instanceof Float32Array&&"float32"===o||r instanceof Int32Array&&"int32"===o||r instanceof Uint8Array&&"bool"===o)return t;var r,o;if(Array.isArray(t)&&(t=d(t)),null==e||"float32"===e||"complex64"===e)return new Float32Array(t);if("int32"===e)return n&&k(t,e),new Int32Array(t);if("bool"!==e)throw new Error("Unknown data type "+e);for(var i=new Uint8Array(t.length),a=0;a<i.length;++a)0!==Math.round(t[a])&&(i[a]=1);return i}function G(t,e){for(var n=H(t,e),r=0;r<n.length;r++)n[r]=1;return n}function H(t,e){if(null==e||"float32"===e||"complex64"===e)return new Float32Array(t);if("int32"===e)return new Int32Array(t);if("bool"===e)return new Uint8Array(t);throw new Error("Unknown data type "+e)}function q(){if("undefined"!=typeof performance)return performance.now();if("undefined"==typeof process)throw new Error("Cannot measure time in this environment. You should run tf.js in the browser or in Node.js");var t=process.hrtime();return 1e3*t[0]+t[1]/1e6}var j=Object.freeze({shuffle:u,clamp:c,nearestLargerEven:S,sum:l,randUniform:h,distSquared:function(t,e){for(var n=0,r=0;r<t.length;r++){var o=Number(t[r])-Number(e[r]);n+=o*o}return n},assert:M,assertShapesMatch:f,assertNonNull:p,flatten:d,sizeFromShape:L,isScalarShape:function(t){return 0===t.length},arraysEqual:y,isInt:D,tanh:v,sizeToSquarishShape:g,createShuffledIndices:function(t){for(var e=new Uint32Array(t),n=0;n<t;++n)e[n]=n;return u(e),e},rightPad:x,repeatedTry:w,inferFromImplicitShape:b,squeezeShape:E,getTypedArrayFromDType:R,getArrayFromDType:N,checkComputationForNaN:T,checkConversionForNaN:k,hasEncodingLoss:I,isTypedArray:A,bytesPerElement:_,bytesFromStringArray:O,isString:F,isBoolean:e,isNumber:P,inferDtype:B,isFunction:z,nearestDivisor:W,computeStrides:U,toTypedArray:V,makeOnesTypedArray:G,makeZerosTypedArray:H,now:q}),$=function(){function t(t,e){this.backendTimer=t,null==(this.logger=e)&&(this.logger=new K)}return t.prototype.profileKernel=function(o,t){var e,i=this,a=this.backendTimer.time(function(){e=t()});return(Array.isArray(e)?e:[e]).forEach(function(n){var r=n.dataSync();T(r,n.dtype,o),a.then(function(t){var e="";null!=t.getExtraProfileInfo&&(e=t.getExtraProfileInfo()),i.logger.logKernelProfile(o,n,r,t.kernelMs,e)})}),e},t}(),K=function(){function t(){}return t.prototype.logKernelProfile=function(t,e,n,r,o){var i=x(r+"ms",9),a=x(t,25),s=e.rank,u=e.size,c=x(e.shape.toString(),14);console.log("%c"+a+"\t%c"+i+"\t%c"+s+"D "+c+"\t%c"+u+"\t%c"+o,"font-weight:bold","color:red","color:blue","color: orange","color: green")},t}(),X=20,Y=3,Q=7;function J(t,e,n,r){var o=U(e),i=function(t,e,n,r){var o=L(e),i=r[r.length-1],a=new Array(i).fill(0),s=e.length,u="complex64"===n?tt(t):t;if(1<s)for(var c=0;c<o/i;c++)for(var l=c*i,h=0;h<i;h++)a[h]=Math.max(a[h],Z(u[l+h],0).length);return a}(t,e,n,o),a=e.length,s=function t(e,n,r,o,i,a){void 0===a&&(a=!0);var s="complex64"===r?2:1,u=n[0],c=n.length;if(0===c)return"complex64"===r?[Z(tt(e)[0],0)]:[e[0].toString()];if(1===c){if(X<u){var l=Y*s,h=Array.from(e.slice(0,l)),p=Array.from(e.slice(u-Y*s,u));return"complex64"===r&&(h=tt(h),p=tt(p)),["["+h.map(function(t,e){return Z(t,i[e])}).join(", ")+", ..., "+p.map(function(t,e){return Z(t,i[u-Y+e])}).join(", ")+"]"]}return["["+("complex64"===r?tt(e):Array.from(e)).map(function(t,e){return Z(t,i[e])}).join(", ")+"]"]}var f=n.slice(1),d=o.slice(1),m=o[0]*s,v=[];if(X<u){for(var g=0;g<Y;g++){var y=(x=g*m)+m;v.push.apply(v,t(e.slice(x,y),f,r,d,i,!1))}for(v.push("..."),g=u-Y;g<u;g++)y=(x=g*m)+m,v.push.apply(v,t(e.slice(x,y),f,r,d,i,g===u-1))}else for(g=0;g<u;g++){var x;y=(x=g*m)+m,v.push.apply(v,t(e.slice(x,y),f,r,d,i,g===u-1))}var w=2===c?",":"";v[0]="["+v[0]+w;for(g=1;g<v.length-1;g++)v[g]=" "+v[g]+w;var b=",\n";for(g=2;g<c;g++)b+="\n";return v[v.length-1]=" "+v[v.length-1]+"]"+(a?"":b),v}(t,e,n,o,i),u=["Tensor"];return r&&(u.push(" dtype: "+n),u.push(" rank: "+a),u.push(" shape: ["+e+"]"),u.push(" values:")),u.push(s.map(function(t){return" "+t}).join("\n")),u.join("\n")}function Z(t,e){return x(Array.isArray(t)?parseFloat(t[0].toFixed(Q))+" + "+parseFloat(t[1].toFixed(Q))+"j":F(t)?"'"+t+"'":parseFloat(t.toFixed(Q)).toString(),e)}function tt(t){for(var e=[],n=0;n<t.length;n+=2)e.push([t[n],t[n+1]]);return e}var et=function(){function t(t,e,n){if(this.dtype=e,this.shape=t.slice(),this.size=L(t),null!=n){var r=n.length;M(r===this.size,"Length of values '"+r+"' does not match the size inferred by the shape '"+this.size+"'.")}if("complex64"===e)throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||N(e,L(this.shape)),this.strides=U(t)}return t.prototype.set=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];0===e.length&&(e=[0]),M(e.length===this.rank,"The number of provided coordinates ("+e.length+") must match the rank ("+this.rank+")");var r=this.locToIndex(e);this.values[r]=t},t.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];0===t.length&&(t=[0]);for(var n=t[t.length-1],r=0;r<t.length-1;++r)n+=this.strides[r]*t[r];return this.values[n]},t.prototype.locToIndex=function(t){if(0===this.rank)return 0;if(1===this.rank)return t[0];for(var e=t[t.length-1],n=0;n<t.length-1;++n)e+=this.strides[n]*t[n];return e},t.prototype.indexToLoc=function(t){if(0===this.rank)return[];if(1===this.rank)return[t];for(var e=new Array(this.shape.length),n=0;n<e.length-1;++n)e[n]=Math.floor(t/this.strides[n]),t-=e[n]*this.strides[n];return e[e.length-1]=t,e},Object.defineProperty(t.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),t.prototype.toTensor=function(){return it.make(this.shape,{values:this.values},this.dtype)},t}(),nt=null,rt=null;function ot(t){nt=t}var it=function(){function r(t,e,n,r){this.isDisposedInternal=!1,this.shape=t.slice(),this.dtype=e||"float32",this.size=L(t),this.strides=U(t),this.dataId=null!=r?r:{},this.id=nt().nextTensorId(),this.rankType=this.rank<5?this.rank.toString():"higher",nt().registerTensor(this),null!=n&&nt().write(this.dataId,n)}return r.make=function(t,e,n){return new r(t,n,e.values,e.dataId)},r.prototype.flatten=function(){return this.throwIfDisposed(),this.as1D()},r.prototype.asScalar=function(){return this.throwIfDisposed(),M(1===this.size,"The array must have only 1 element."),this.reshape([])},r.prototype.as1D=function(){return this.throwIfDisposed(),this.reshape([this.size])},r.prototype.as2D=function(t,e){return this.throwIfDisposed(),this.reshape([t,e])},r.prototype.as3D=function(t,e,n){return this.throwIfDisposed(),this.reshape([t,e,n])},r.prototype.as4D=function(t,e,n,r){return this.throwIfDisposed(),this.reshape([t,e,n,r])},r.prototype.as5D=function(t,e,n,r,o){return this.throwIfDisposed(),this.reshape([t,e,n,r,o])},r.prototype.asType=function(t){return this.throwIfDisposed(),rt.cast(this,t)},Object.defineProperty(r.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),r.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];M(t.length===this.rank,"Number of coordinates in get() must match the rank of the tensor"),M("complex64"!==this.dtype,"Tensor.get() is not supported for complex64 tensors yet."),this.throwIfDisposed(),0===t.length&&(t=[0]);for(var n=t[t.length-1],r=0;r<t.length-1;++r)n+=this.strides[r]*t[r];return this.dataSync()[n]},r.prototype.buffer=function(){return rt.buffer(this.shape,this.dtype,this.dataSync())},r.prototype.data=function(){return m(this,void 0,void 0,function(){return C(this,function(t){return this.throwIfDisposed(),[2,nt().read(this.dataId)]})})},r.prototype.dataSync=function(){return this.throwIfDisposed(),nt().readSync(this.dataId)},r.prototype.dispose=function(){this.isDisposed||(nt().disposeTensor(this),this.isDisposedInternal=!0)},Object.defineProperty(r.prototype,"isDisposed",{get:function(){return this.isDisposedInternal},enumerable:!0,configurable:!0}),r.prototype.throwIfDisposed=function(){if(this.isDisposed)throw new Error("Tensor is disposed.")},r.prototype.toFloat=function(){return this.asType("float32")},r.prototype.toInt=function(){return this.asType("int32")},r.prototype.toBool=function(){return this.asType("bool")},r.prototype.print=function(t){return void 0===t&&(t=!1),rt.print(this,t)},r.prototype.reshape=function(t){return this.throwIfDisposed(),rt.reshape(this,t)},r.prototype.reshapeAs=function(t){return this.throwIfDisposed(),this.reshape(t.shape)},r.prototype.expandDims=function(t){return void 0===t&&(t=0),rt.expandDims(this,t)},r.prototype.cumsum=function(t,e,n){return void 0===t&&(t=0),void 0===e&&(e=!1),void 0===n&&(n=!1),rt.cumsum(this,t,e,n)},r.prototype.squeeze=function(t){return this.throwIfDisposed(),rt.squeeze(this,t)},r.prototype.clone=function(){return this.throwIfDisposed(),rt.clone(this)},r.prototype.toString=function(t){return void 0===t&&(t=!1),J(this.dataSync(),this.shape,this.dtype,t)},r.prototype.tile=function(t){return this.throwIfDisposed(),rt.tile(this,t)},r.prototype.gather=function(t,e){return void 0===e&&(e=0),this.throwIfDisposed(),rt.gather(this,t,e)},r.prototype.matMul=function(t,e,n){return void 0===e&&(e=!1),void 0===n&&(n=!1),this.throwIfDisposed(),rt.matMul(this,t,e,n)},r.prototype.dot=function(t){return this.throwIfDisposed(),rt.dot(this,t)},r.prototype.norm=function(t,e,n){return void 0===t&&(t="euclidean"),void 0===e&&(e=null),void 0===n&&(n=!1),this.throwIfDisposed(),rt.norm(this,t,e,n)},r.prototype.slice=function(t,e){return this.throwIfDisposed(),rt.slice(this,t,e)},r.prototype.reverse=function(t){return this.throwIfDisposed(),rt.reverse(this,t)},r.prototype.concat=function(t,e){return void 0===e&&(e=0),this.throwIfDisposed(),t instanceof r&&(t=[t]),rt.concat([this].concat(t),e)},r.prototype.split=function(t,e){return void 0===e&&(e=0),this.throwIfDisposed(),rt.split(this,t,e)},r.prototype.stack=function(t,e){return void 0===e&&(e=0),rt.stack([this,t],e)},r.prototype.unstack=function(t,e){return void 0===e&&(e=0),rt.unstack(this,e)},r.prototype.pad=function(t,e){return void 0===e&&(e=0),rt.pad(this,t,e)},r.prototype.batchNormalization=function(t,e,n,r,o){return void 0===n&&(n=.001),this.throwIfDisposed(),rt.batchNormalization(this,t,e,n,r,o)},r.prototype.all=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.all(this,t,e)},r.prototype.any=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.any(this,t,e)},r.prototype.logSumExp=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.logSumExp(this,t,e)},r.prototype.sum=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.sum(this,t,e)},r.prototype.prod=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.prod(this,t,e)},r.prototype.mean=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.mean(this,t,e)},r.prototype.min=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.min(this,t,e)},r.prototype.max=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),rt.max(this,t,e)},r.prototype.argMin=function(t){return void 0===t&&(t=null),this.throwIfDisposed(),rt.argMin(this,t)},r.prototype.argMax=function(t){return void 0===t&&(t=null),this.throwIfDisposed(),rt.argMax(this,t)},r.prototype.cast=function(t){return this.throwIfDisposed(),rt.cast(this,t)},r.prototype.add=function(t){return this.throwIfDisposed(),rt.add(this,t)},r.prototype.addStrict=function(t){return this.throwIfDisposed(),rt.addStrict(this,t)},r.prototype.atan2=function(t){return this.throwIfDisposed(),rt.atan2(this,t)},r.prototype.sub=function(t){return this.throwIfDisposed(),rt.sub(this,t)},r.prototype.subStrict=function(t){return this.throwIfDisposed(),rt.subStrict(this,t)},r.prototype.pow=function(t){return this.throwIfDisposed(),rt.pow(this,t)},r.prototype.powStrict=function(t){return this.throwIfDisposed(),rt.powStrict(this,t)},r.prototype.mul=function(t){return this.throwIfDisposed(),rt.mul(this,t)},r.prototype.mulStrict=function(t){return this.throwIfDisposed(),rt.mulStrict(this,t)},r.prototype.div=function(t){return this.throwIfDisposed(),rt.div(this,t)},r.prototype.floorDiv=function(t){return this.throwIfDisposed(),rt.floorDiv(this,t)},r.prototype.divStrict=function(t){return this.throwIfDisposed(),rt.divStrict(this,t)},r.prototype.minimum=function(t){return this.throwIfDisposed(),rt.minimum(this,t)},r.prototype.minimumStrict=function(t){return this.throwIfDisposed(),rt.minimumStrict(this,t)},r.prototype.maximum=function(t){return this.throwIfDisposed(),rt.maximum(this,t)},r.prototype.maximumStrict=function(t){return this.throwIfDisposed(),rt.maximumStrict(this,t)},r.prototype.mod=function(t){return this.throwIfDisposed(),rt.mod(this,t)},r.prototype.modStrict=function(t){return this.throwIfDisposed(),rt.modStrict(this,t)},r.prototype.squaredDifference=function(t){return this.throwIfDisposed(),rt.squaredDifference(this,t)},r.prototype.squaredDifferenceStrict=function(t){return this.throwIfDisposed(),rt.squaredDifferenceStrict(this,t)},r.prototype.transpose=function(t){return this.throwIfDisposed(),rt.transpose(this,t)},r.prototype.notEqual=function(t){return this.throwIfDisposed(),rt.notEqual(this,t)},r.prototype.notEqualStrict=function(t){return this.throwIfDisposed(),rt.notEqualStrict(this,t)},r.prototype.less=function(t){return this.throwIfDisposed(),rt.less(this,t)},r.prototype.lessStrict=function(t){return this.throwIfDisposed(),rt.lessStrict(this,t)},r.prototype.equal=function(t){return this.throwIfDisposed(),rt.equal(this,t)},r.prototype.equalStrict=function(t){return this.throwIfDisposed(),rt.equalStrict(this,t)},r.prototype.lessEqual=function(t){return this.throwIfDisposed(),rt.lessEqual(this,t)},r.prototype.lessEqualStrict=function(t){return this.throwIfDisposed(),rt.lessEqualStrict(this,t)},r.prototype.greater=function(t){return this.throwIfDisposed(),rt.greater(this,t)},r.prototype.greaterStrict=function(t){return this.throwIfDisposed(),rt.greaterStrict(this,t)},r.prototype.greaterEqual=function(t){return this.throwIfDisposed(),rt.greaterEqual(this,t)},r.prototype.greaterEqualStrict=function(t){return this.throwIfDisposed(),rt.greaterEqualStrict(this,t)},r.prototype.logicalAnd=function(t){return this.throwIfDisposed(),rt.logicalAnd(this,t)},r.prototype.logicalOr=function(t){return this.throwIfDisposed(),rt.logicalOr(this,t)},r.prototype.logicalNot=function(){return this.throwIfDisposed(),rt.logicalNot(this)},r.prototype.logicalXor=function(t){return this.throwIfDisposed(),rt.logicalXor(this,t)},r.prototype.where=function(t,e){return this.throwIfDisposed(),rt.where(t,this,e)},r.prototype.neg=function(){return this.throwIfDisposed(),rt.neg(this)},r.prototype.ceil=function(){return this.throwIfDisposed(),rt.ceil(this)},r.prototype.floor=function(){return this.throwIfDisposed(),rt.floor(this)},r.prototype.sign=function(){return this.throwIfDisposed(),rt.sign(this)},r.prototype.exp=function(){return this.throwIfDisposed(),rt.exp(this)},r.prototype.expm1=function(){return this.throwIfDisposed(),rt.expm1(this)},r.prototype.log=function(){return this.throwIfDisposed(),rt.log(this)},r.prototype.log1p=function(){return this.throwIfDisposed(),rt.log1p(this)},r.prototype.sqrt=function(){return this.throwIfDisposed(),rt.sqrt(this)},r.prototype.rsqrt=function(){return this.throwIfDisposed(),rt.rsqrt(this)},r.prototype.square=function(){return this.throwIfDisposed(),rt.square(this)},r.prototype.reciprocal=function(){return this.throwIfDisposed(),rt.reciprocal(this)},r.prototype.abs=function(){return this.throwIfDisposed(),rt.abs(this)},r.prototype.clipByValue=function(t,e){return this.throwIfDisposed(),rt.clipByValue(this,t,e)},r.prototype.relu=function(){return this.throwIfDisposed(),rt.relu(this)},r.prototype.elu=function(){return this.throwIfDisposed(),rt.elu(this)},r.prototype.selu=function(){return this.throwIfDisposed(),rt.selu(this)},r.prototype.leakyRelu=function(t){return void 0===t&&(t=.2),this.throwIfDisposed(),rt.leakyRelu(this,t)},r.prototype.prelu=function(t){return this.throwIfDisposed(),rt.prelu(this,t)},r.prototype.sigmoid=function(){return this.throwIfDisposed(),rt.sigmoid(this)},r.prototype.logSigmoid=function(){return this.throwIfDisposed(),rt.logSigmoid(this)},r.prototype.softplus=function(){return this.throwIfDisposed(),rt.softplus(this)},r.prototype.zerosLike=function(){return this.throwIfDisposed(),rt.zerosLike(this)},r.prototype.onesLike=function(){return this.throwIfDisposed(),rt.onesLike(this)},r.prototype.sin=function(){return this.throwIfDisposed(),rt.sin(this)},r.prototype.cos=function(){return this.throwIfDisposed(),rt.cos(this)},r.prototype.tan=function(){return this.throwIfDisposed(),rt.tan(this)},r.prototype.asin=function(){return this.throwIfDisposed(),rt.asin(this)},r.prototype.acos=function(){return this.throwIfDisposed(),rt.acos(this)},r.prototype.atan=function(){return this.throwIfDisposed(),rt.atan(this)},r.prototype.sinh=function(){return this.throwIfDisposed(),rt.sinh(this)},r.prototype.cosh=function(){return this.throwIfDisposed(),rt.cosh(this)},r.prototype.tanh=function(){return this.throwIfDisposed(),rt.tanh(this)},r.prototype.asinh=function(){return this.throwIfDisposed(),rt.asinh(this)},r.prototype.acosh=function(){return this.throwIfDisposed(),rt.acosh(this)},r.prototype.atanh=function(){return this.throwIfDisposed(),rt.atanh(this)},r.prototype.erf=function(){return this.throwIfDisposed(),rt.erf(this)},r.prototype.round=function(){return this.throwIfDisposed(),rt.round(this)},r.prototype.step=function(t){return void 0===t&&(t=0),this.throwIfDisposed(),rt.step(this,t)},r.prototype.softmax=function(t){return void 0===t&&(t=-1),this.throwIfDisposed(),rt.softmax(this,t)},r.prototype.logSoftmax=function(t){return void 0===t&&(t=-1),this.throwIfDisposed(),rt.logSoftmax(this,t)},r.prototype.resizeBilinear=function(t,e){return void 0===e&&(e=!1),this.throwIfDisposed(),rt.image.resizeBilinear(this,t,e)},r.prototype.resizeNearestNeighbor=function(t,e){return void 0===e&&(e=!1),this.throwIfDisposed(),rt.image.resizeNearestNeighbor(this,t,e)},r.prototype.conv1d=function(t,e,n,r,o,i){return void 0===r&&(r="NWC"),void 0===o&&(o=1),this.throwIfDisposed(),rt.conv1d(this,t,e,n,r,o,i)},r.prototype.conv2d=function(t,e,n,r,o,i){return void 0===r&&(r="NHWC"),void 0===o&&(o=[1,1]),this.throwIfDisposed(),rt.conv2d(this,t,e,n,r,o,i)},r.prototype.conv2dTranspose=function(t,e,n,r,o){return this.throwIfDisposed(),rt.conv2dTranspose(this,t,e,n,r,o)},r.prototype.depthwiseConv2D=function(t,e,n,r,o,i){return void 0===r&&(r="NHWC"),void 0===o&&(o=[1,1]),this.throwIfDisposed(),rt.depthwiseConv2d(this,t,e,n,r,o,i)},r.prototype.separableConv2d=function(t,e,n,r,o,i){return void 0===o&&(o=[1,1]),void 0===i&&(i="NHWC"),this.throwIfDisposed(),rt.separableConv2d(this,t,e,n,r,o,i)},r.prototype.avgPool=function(t,e,n,r){return this.throwIfDisposed(),rt.avgPool(this,t,e,n,r)},r.prototype.maxPool=function(t,e,n,r){return this.throwIfDisposed(),rt.maxPool(this,t,e,n,r)},r.prototype.localResponseNormalization=function(t,e,n,r){return void 0===t&&(t=5),void 0===e&&(e=1),void 0===n&&(n=1),void 0===r&&(r=.5),rt.localResponseNormalization(this,t,e,n,r)},r.prototype.pool=function(t,e,n,r,o){return this.throwIfDisposed(),rt.pool(this,t,e,n,r,o)},r.prototype.variable=function(t,e,n){return void 0===t&&(t=!0),this.throwIfDisposed(),at.variable(this,t,e,n)},r.prototype.unsortedSegmentSum=function(t,e){return this.throwIfDisposed(),rt.unsortedSegmentSum(this,t,e)},r.prototype.batchToSpaceND=function(t,e){return this.throwIfDisposed(),rt.batchToSpaceND(this,t,e)},r.prototype.spaceToBatchND=function(t,e){return this.throwIfDisposed(),rt.spaceToBatchND(this,t,e)},r.prototype.topk=function(t,e){return void 0===t&&(t=1),void 0===e&&(e=!0),this.throwIfDisposed(),rt.topk(this,t,e)},r.prototype.stridedSlice=function(t,e,n,r,o){return void 0===r&&(r=0),void 0===o&&(o=0),this.throwIfDisposed(),rt.stridedSlice(this,t,e,n,r,o)},r.prototype.depthToSpace=function(t,e){return this.throwIfDisposed(),rt.depthToSpace(this,t,e)},r.prototype.fft=function(){return this.throwIfDisposed(),rt.spectral.fft(this)},r.prototype.ifft=function(){return this.throwIfDisposed(),rt.spectral.ifft(this)},r}();Object.defineProperty(it,Symbol.hasInstance,{value:function(t){return!!t&&null!=t.shape&&null!=t.dtype}});var at=function(o){function i(t,e,n){void 0===e&&(e=!0);var r=o.call(this,t.shape,t.dtype,null,t.dataId)||this;r.trainable=e,r.name=n,null==r.name&&(r.name=nt().nextVariableId().toString());try{nt().registerVariable(r)}catch(t){throw nt().disposeTensor(r),t}return r}return s(i,o),i.variable=function(t,e,n,r){return void 0===e&&(e=!0),null!=r&&r!==t.dtype&&(t=t.asType(r)),new i(t,e,n)},i.prototype.assign=function(t){if(t.dtype!==this.dtype)throw new Error("dtype of the new value ("+t.dtype+") and previous value ("+this.dtype+") must match");if(!y(t.shape,this.shape))throw new Error("shape of the new value ("+t.shape+") and previous value ("+this.shape+") must match");nt().disposeTensor(this),this.dataId=t.dataId,nt().registerTensor(this)},i}(it);Object.defineProperty(at,Symbol.hasInstance,{value:function(t){return t instanceof it&&null!=t.assign&&t.assign instanceof Function}});var st,ut,ct,lt,ht,pt,ft,dt,mt,vt,gt=at.variable;(vt=st||(st={})).R0="R0",vt.R1="R1",vt.R2="R2",vt.R3="R3",vt.R4="R4",vt.R5="R5",vt.R6="R6",(mt=ut||(ut={})).float32="float32",mt.int32="int32",mt.bool="int32",mt.complex64="complex64",(dt=ct||(ct={})).float32="float32",dt.int32="int32",dt.bool="bool",dt.complex64="complex64",(ft=lt||(lt={})).float32="float32",ft.int32="float32",ft.bool="float32",ft.complex64="complex64",(pt=ht||(ht={})).float32="complex64",pt.int32="complex64",pt.bool="complex64",pt.complex64="complex64";var yt={float32:lt,int32:ut,bool:ct,complex64:ht};function xt(t,e){if("string"!==t&&"string"!==e)return yt[t][e];if("string"===t&&"string"===e)return"string";throw new Error("Can not upcast "+t+" with "+e)}function wt(t){return xt(t,"int32")}function bt(t,e){if(t.dtype===e.dtype)return[t,e];var n=xt(t.dtype,e.dtype);return[t.cast(n),e.cast(n)]}function Et(t){var e=[];return function t(e,n,r){if(null!=e)if(e instanceof it)n.push(e);else if(s=e,Array.isArray(s)||"object"==typeof s){var o=e;for(var i in o){var a=o[i];r.has(a)||(r.add(a),t(a,n,r))}}var s}(t,e,new Set),e}var Ct,St,Rt=function(){function t(t,e,n){this.backend=t,this.safeMode=e,this.debugMode=n,this.registeredVariables={},this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numStringTensors=0,this.numDataBuffers=0,this.profiling=!1,this.gradientScopeCount=0,this.customGradientDepth=0,this.scopeStack=[],this.keepTensors=new Set,this.tensorInfo=new WeakMap,this.profiler=new $(t),this.activeProfile={newBytes:0,newTensors:0,peakBytes:0,kernels:[],result:null}}return t.prototype.moveData=function(t){this.write(t,this.readSync(t))},t.prototype.tidy=function(t,e,n){var r=this;void 0===n&&(n=!1);var o,i=null;if(null==e){if("function"!=typeof t)throw new Error("Please provide a function to tidy()");e=t}else{if("string"!=typeof t&&!(t instanceof String))throw new Error("When calling with two arguments, the first argument to tidy() must be a string");if("function"!=typeof e)throw new Error("When calling with two arguments, the 2nd argument to tidy() must be a function");i=t}return this.scopedRun(function(){return r.startScope(i,n)},function(){return r.endScope(o,n)},function(){return(o=e())instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),o})},t.prototype.scopedRun=function(t,e,n){t();try{var r=n();return e(),r}catch(t){throw e(),t}},t.prototype.nextTensorId=function(){return t.nextTensorId++},t.prototype.nextVariableId=function(){return t.nextVariableId++},t.prototype.runKernel=function(t,e,n){var r,o=this,i=[],a=function(t){return i.push(t),t},s=this.activeScope.name,u=this.numBytes,c=this.numTensors;if(this.scopedRun(function(){return o.customGradientDepth++},function(){return o.customGradientDepth--},function(){r=o.debugMode()?o.profiler.profileKernel(s,function(){return t(o.backend,a)}):t(o.backend,a)}),this.shouldRecord()){var l={id:this.nextTapeNodeId++,name:s,inputs:e,outputs:Array.isArray(r)?r:[r]};null!=n&&(l.gradient=function(t){return n(t,i)}),this.activeTape.push(l)}return this.profiling&&this.activeProfile.kernels.push({name:s,bytesAdded:this.numBytes-u,totalBytesSnapshot:this.numBytes,tensorsAdded:this.numTensors-c,totalTensorsSnapshot:this.numTensors,inputShapes:Object.keys(e).map(function(t){return e[t].shape}),outputShape:Array.isArray(r)?r.map(function(t){return t.shape}):r.shape}),r},t.prototype.registerTensor=function(t){var e=this.tensorInfo.has(t.dataId)?this.tensorInfo.get(t.dataId).refCount:0;if(this.numTensors++,"string"===t.dtype&&this.numStringTensors++,0===e){this.numDataBuffers++;var n=0;"complex64"!==t.dtype&&"string"!==t.dtype&&(n=L(t.shape)*_(t.dtype)),this.tensorInfo.set(t.dataId,{backend:this.backend,dtype:t.dtype,shape:t.shape,bytes:n,refCount:0}),this.numBytes+=n,this.backend.register(t.dataId,t.shape,t.dtype)}this.tensorInfo.get(t.dataId).refCount++,t instanceof at||this.track(t)},t.prototype.registerVariable=function(t){if(null!=this.registeredVariables[t.name])throw new Error("Variable with name "+t.name+" was already registered");this.registeredVariables[t.name]=t},t.prototype.disposeTensor=function(t){if(this.tensorInfo.has(t.dataId)){this.keepTensors.has(t.id)&&this.keepTensors.delete(t.id),this.numTensors--,"string"===t.dtype&&this.numStringTensors--;var e=this.tensorInfo.get(t.dataId);e.refCount<=1?("complex64"!==t.dtype&&(this.numBytes-=e.bytes),this.numDataBuffers--,e.backend.disposeData(t.dataId),this.tensorInfo.delete(t.dataId)):this.tensorInfo.get(t.dataId).refCount--}},t.prototype.disposeVariables=function(){for(var t in this.registeredVariables){var e=this.registeredVariables[t];this.disposeTensor(e),delete this.registeredVariables[t]}},t.prototype.memory=function(){var t=this.backend.memory();return t.numTensors=this.numTensors,t.numDataBuffers=this.numDataBuffers,t.numBytes=this.numBytes,0<this.numStringTensors&&(t.unreliable=!0,null==t.reasons&&(t.reasons=[]),t.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),t},t.prototype.profile=function(r){return m(this,void 0,void 0,function(){var e,n;return C(this,function(t){return this.profiling=!0,e=this.numBytes,n=this.numTensors,this.activeProfile.kernels=[],this.activeProfile.result=r(),this.profiling=!1,this.activeProfile.peakBytes=Math.max.apply(Math,this.activeProfile.kernels.map(function(t){return t.totalBytesSnapshot})),this.activeProfile.newBytes=this.numBytes-e,this.activeProfile.newTensors=this.numTensors-n,[2,this.activeProfile]})})},t.prototype.shouldRecord=function(){return null!=this.activeTape&&0===this.customGradientDepth},t.prototype.addTapeNode=function(t,e,r){var n={};t.forEach(function(t,e){n[e]=t});var o={id:this.nextTapeNodeId++,name:this.activeScope.name,inputs:n,outputs:[e],gradient:function(t){var n={};return r(t).forEach(function(t,e){n[e]=function(){return t}}),n}};this.activeTape.push(o)},t.prototype.keep=function(t){if(1===this.scopeStack.length&&this.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {...}) to avoid memory leaks.");return this.keepTensors.add(t.id),t},t.prototype.startScope=function(t,e){void 0===e&&(e=!1),e&&0===this.gradientScopeCount&&(this.activeTape=[]),e&&this.gradientScopeCount++;var n={track:[],name:"unnamed scope"};t&&(n.name=t),this.scopeStack.push(n),this.activeScope=n},t.prototype.endScope=function(t,e){var n=this;void 0===e&&(e=!1),e&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),o=Et(t);o.forEach(function(t){return r.add(t.id)});for(var i=0;i<this.activeScope.track.length;i++){var a=this.activeScope.track[i];r.has(a.id)||(null!=this.activeTape?o.push(a):a.dispose())}var s=this.scopeStack.pop();this.activeScope=0===this.scopeStack.length?null:this.scopeStack[this.scopeStack.length-1],o.forEach(function(t){!n.keepTensors.has(t.id)&&function(t,e){for(var n=0;n<e.length;n++)if(e[n].id===t.id)return!0;return!1}(t,s.track)&&n.track(t)})},t.prototype.gradients=function(i,a,s,u){var c=this;if(void 0===u&&(u=!1),M(0<a.length,"gradients() received an empty list of xs."),null!=s&&"float32"!==s.dtype)throw new Error("dy must have 'float32' dtype, but has '"+s.dtype+"'");return this.tidy("gradients",function(){var t=i();M(t instanceof it,"The result y returned by f() must be a tensor.");var e=function(t,e,n){for(var r={},o={},i=0;i<e.length;i++)r[e[i].id]=!0;for(i=0;i<t.length;i++){var a=(d=t[i]).inputs;for(var s in a){for(var u=a[s],c=!1,l=0;l<e.length;l++)if(r[u.id]){d.outputs.forEach(function(t){return r[t.id]=!0}),c=!0,o[d.id]=!0;break}if(c)break}}var h={};h[n.id]=!0;var p={};for(i=t.length-1;0<=i;i--)for(a=(d=t[i]).inputs,l=0;l<d.outputs.length;l++)if(h[d.outputs[l].id]){for(var s in a)h[a[s].id]=!0,p[d.id]=!0;break}var f=[];for(i=0;i<t.length;i++){var d;if(o[(d=t[i]).id]&&p[d.id]){var m={};for(var s in d.inputs){var v=d.inputs[s];r[v.id]&&(m[s]=v)}var g=Object.assign({},d);g.inputs=m,g.outputs=d.outputs,f.push(g)}}return f}(c.activeTape,a,t);if(!u&&0===e.length&&0<a.length)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");var n,r,o={};return o[t.id]=null==s?(n=t.shape,r=G(L(n),"float32"),it.make(n,{values:r})):s,function(u,c){for(var t=function(t){var e=c[t],r=[];if(e.outputs.forEach(function(t){var e=u[t.id];if(null!=e)r.push(e);else{var n=it.make(t.shape,{values:H(t.size,t.dtype)},t.dtype);r.push(n)}}),null==e.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+e.name+".");var n=e.gradient(1===e.outputs.length?r[0]:r);for(var o in e.inputs){if(!(o in n))throw new Error("Cannot backprop through input "+o+". Available gradients found: "+Object.keys(n)+".");var i=n[o]();if("float32"!==i.dtype)throw new Error("Error in gradient for op "+e.name+". The gradient of input "+o+" must have 'float32' dtype, but has '"+i.dtype+"'");var a=e.inputs[o];if(!y(i.shape,a.shape))throw new Error("Error in gradient for op "+e.name+". The gradient of input '"+o+"' has shape '"+i.shape+"', which does not match the shape of the input '"+a.shape+"'");if(null==u[a.id])u[a.id]=i;else{var s=u[a.id];u[a.id]=s.add(i),s.dispose()}}},e=c.length-1;0<=e;e--)t(e)}(o,e),{value:t,grads:a.map(function(t){return o[t.id]})}},!0)},t.prototype.customGrad=function(i){var n=this;return M(z(i),"The f passed in customGrad(f) must be a function."),function(){for(var r,t,o=[],e=0;e<arguments.length;e++)o[e]=arguments[e];return M(o.every(function(t){return t instanceof it}),"The args passed in customGrad(f)(x1, x2,...) must all be tensors"),n.scopedRun(function(){return n.customGradientDepth++},function(){return n.customGradientDepth--},function(){t=n.tidy(i.name,function(){var t=i.apply(void 0,o),e=t.value,n=t.gradFunc;return M(e instanceof it,"The function f passed in customGrad(f) must return an object where `obj.value` is a tensor"),M(z(n),"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function."),r=n,e},!0)}),n.shouldRecord()&&n.addTapeNode(o,t,function(t){var e=r(t),n=Array.isArray(e)?e:[e];return M(n.length===o.length,"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns the same number of tensors as inputs passed to f(...)."),M(n.every(function(t){return t instanceof it}),"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns a list of only tensors."),n}),t}},t.prototype.write=function(t,e){var n=this.tensorInfo.get(t);if("string"===n.dtype){var r=O(e);this.numBytes+=r-n.bytes,n.bytes=r}this.backend!==n.backend&&(n.backend.disposeData(t),n.backend=this.backend,this.backend.register(t,n.shape,n.dtype)),this.backend.write(t,e)},t.prototype.readSync=function(t){return this.tensorInfo.get(t).backend.readSync(t)},t.prototype.read=function(t){return this.tensorInfo.get(t).backend.read(t)},t.prototype.fromPixels=function(t,e){return this.backend.fromPixels(t,e)},t.prototype.time=function(r){return m(this,void 0,void 0,function(){var e,n;return C(this,function(t){switch(t.label){case 0:return e=q(),[4,this.backend.time(r)];case 1:return(n=t.sent()).wallMs=q()-e,[2,n]}})})},t.prototype.track=function(t){if(1===this.scopeStack.length&&this.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {op();...}); to avoid memory leaks.");return null!=this.activeScope&&this.activeScope.track.push(t),t},t.nextTensorId=0,t.nextVariableId=0,t}();(St=Ct||(Ct={}))[St.NUMBER=0]="NUMBER",St[St.BOOLEAN=1]="BOOLEAN",St[St.STRING=2]="STRING";var Nt,Tt=[{name:"DEBUG",type:Ct.BOOLEAN},{name:"IS_BROWSER",type:Ct.BOOLEAN},{name:"WEBGL_LAZILY_UNPACK",type:Ct.BOOLEAN},{name:"WEBGL_CPU_FORWARD",type:Ct.BOOLEAN},{name:"WEBGL_PACK",type:Ct.BOOLEAN},{name:"WEBGL_PACK_BATCHNORMALIZATION",type:Ct.BOOLEAN},{name:"WEBGL_PACK_CLIP",type:Ct.BOOLEAN},{name:"WEBGL_PACK_DEPTHWISECONV",type:Ct.BOOLEAN},{name:"WEBGL_CONV_IM2COL",type:Ct.BOOLEAN},{name:"WEBGL_MAX_TEXTURE_SIZE",type:Ct.NUMBER},{name:"WEBGL_PAGING_ENABLED",type:Ct.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:Ct.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:Ct.BOOLEAN},{name:"WEBGL_VERSION",type:Ct.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:Ct.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:Ct.BOOLEAN},{name:"WEBGL_FENCE_API_ENABLED",type:Ct.BOOLEAN},{name:"WEBGL_SIZE_UPLOAD_UNIFORM",type:Ct.NUMBER},{name:"BACKEND",type:Ct.STRING},{name:"EPSILON",type:Ct.NUMBER},{name:"PROD",type:Ct.BOOLEAN},{name:"TENSORLIKE_CHECK_SHAPE_CONSISTENCY",type:Ct.BOOLEAN}];function kt(t){try{if(null!=a(t))return!0}catch(t){return!1}return!1}var It="tfjsflags";function Dt(){var e={};if("undefined"==typeof window||void 0===window.location||void 0===window.location.search)return e;var t,a,n=(t=window.location.search,a={},t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return r=a,o=e[0],i=e[1],r[decodeURIComponent(o)]=decodeURIComponent(i||""),e.join("=");var r,o,i}),a);if(It in n){var o={};n[It].split(",").forEach(function(t){var e=t.split(":"),n=e[0],r=e[1];o[n]=r}),Tt.forEach(function(t){t.name in o&&(console.log("Setting feature override from URL "+t.name+": "+o[t.name]),t.type===Ct.NUMBER?e[t.name]=+o[t.name]:t.type===Ct.BOOLEAN?e[t.name]="true"===o[t.name]:t.type===Ct.STRING?e[t.name]=o[t.name]:console.warn("Unknown URL param: "+t.name+"."))})}return e}function At(t,e){return null!=t.getExtension(e)}function Mt(t,e){var n=t.createFramebuffer(),r=t.createTexture();t.bindTexture(t.TEXTURE_2D,r);var o=2===e?t.RGBA32F:t.RGBA;t.texImage2D(t.TEXTURE_2D,0,o,1,1,0,t.RGBA,t.FLOAT,null),t.bindFramebuffer(t.FRAMEBUFFER,n),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r,0);var i=t.checkFramebufferStatus(t.FRAMEBUFFER)===t.FRAMEBUFFER_COMPLETE;return t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,null),t.deleteTexture(r),t.deleteFramebuffer(n),i}var _t=function(){function t(t){this.features={},this.registry={},null!=t&&(this.features=t),this.get("DEBUG")&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.")}return t.setBackend=function(t,e){if(void 0===e&&(e=!1),!(t in Ft.registry))throw new Error("Backend name '"+t+"' not found in registry");Ft.engine.backend=Ft.findBackend(t),Ft.backendName=t},t.getBackend=function(){return Ft.initEngine(),Ft.backendName},t.disposeVariables=function(){Ft.engine.disposeVariables()},t.memory=function(){return Ft.engine.memory()},t.profile=function(t){return Ft.engine.profile(t)},t.tidy=function(t,e){return Ft.engine.tidy(t,e)},t.dispose=function(t){Et(t).forEach(function(t){return t.dispose()})},t.keep=function(t){return Ft.engine.keep(t)},t.time=function(t){return Ft.engine.time(t)},t.prototype.get=function(t){return t in this.features||(this.features[t]=this.evaluateFeature(t)),this.features[t]},t.prototype.getFeatures=function(){return this.features},t.prototype.set=function(t,e){this.features[t]=e},t.prototype.getBestBackendName=function(){var e=this;if(0===Object.keys(this.registry).length)throw new Error("No backend found in registry.");return Object.keys(this.registry).map(function(t){return{name:t,entry:e.registry[t]}}).sort(function(t,e){return e.entry.priority-t.entry.priority})[0].name},t.prototype.evaluateFeature=function(t){if("DEBUG"===t)return!1;if("IS_BROWSER"===t)return"undefined"!=typeof window;if("IS_NODE"===t)return"undefined"!=typeof process&&void 0!==process.versions&&void 0!==process.versions.node;if("IS_CHROME"===t)return"undefined"!=typeof navigator&&null!=navigator&&null!=navigator.userAgent&&/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor);if("WEBGL_CPU_FORWARD"===t)return!0;if("WEBGL_PACK"===t)return!1;if("WEBGL_PACK_BATCHNORMALIZATION"===t)return this.get("WEBGL_PACK");if("WEBGL_PACK_CLIP"===t)return this.get("WEBGL_PACK");if("WEBGL_PACK_DEPTHWISECONV"===t)return this.get("WEBGL_PACK");if("WEBGL_LAZILY_UNPACK"===t)return this.get("WEBGL_PACK");if("WEBGL_CONV_IM2COL"===t)return this.get("WEBGL_PACK");if("WEBGL_PAGING_ENABLED"===t)return this.get("IS_BROWSER")&&!this.get("PROD");if("WEBGL_MAX_TEXTURE_SIZE"===t)return function(t){if(null==Nt){var e=a(t);Nt=e.getParameter(e.MAX_TEXTURE_SIZE)}return Nt}(this.get("WEBGL_VERSION"));if("IS_TEST"===t)return!1;if("BACKEND"===t)return this.getBestBackendName();if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"===t){var e=this.get("WEBGL_VERSION");return 0===e?0:function(t){if(0===t)return 0;var e=a(t);return At(e,"EXT_disjoint_timer_query_webgl2")&&2===t?2:At(e,"EXT_disjoint_timer_query")?1:0}(e)}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return 0<this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")&&(n=navigator.userAgent||navigator.vendor||window.opera,!(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(n)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(n.substr(0,4))));var n,r;if("HAS_WEBGL"===t)return 0<this.get("WEBGL_VERSION");if("WEBGL_VERSION"===t)return kt(2)?2:kt(1)?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(t){if(0===t)return!1;var e=a(t);if(1===t){if(!At(e,"OES_texture_float"))return!1}else if(!At(e,"EXT_color_buffer_float"))return!1;return Mt(e,t)}(this.get("WEBGL_VERSION"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(t){if(0===t)return!1;var e=a(t);if(1===t){if(!At(e,"OES_texture_float"))return!1;if(!At(e,"WEBGL_color_buffer_float"))return!1}else if(!At(e,"EXT_color_buffer_float"))return!1;return Mt(e,t)}(this.get("WEBGL_VERSION"));if("WEBGL_FENCE_API_ENABLED"===t)return 2===(r=this.get("WEBGL_VERSION"))&&null!=a(r).fenceSync;if("WEBGL_SIZE_UPLOAD_UNIFORM"===t)return this.get("WEBGL_RENDER_FLOAT32_ENABLED")?4:0;if("TEST_EPSILON"===t)return 32===this.backend.floatPrecision()?.001:.1;if("EPSILON"===t)return 32===this.backend.floatPrecision()?1e-7:1e-4;if("PROD"===t)return!1;if("TENSORLIKE_CHECK_SHAPE_CONSISTENCY"===t)return!this.get("PROD");throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(t){this.features=Object.assign({},t)},t.prototype.reset=function(){this.features=Dt(),null!=this.globalEngine&&(this.globalEngine=null)},Object.defineProperty(t.prototype,"backend",{get:function(){return this.engine.backend},enumerable:!0,configurable:!0}),t.prototype.findBackend=function(t){return t in this.registry?this.registry[t].backend:null},t.prototype.registerBackend=function(t,e,n,r){var o=this;if(void 0===n&&(n=1),t in this.registry)return console.warn(t+" backend was already registered. Reusing existing backend"),null!=r&&r(function(){return o.engine}),!1;try{var i=e();return i.setDataMover({moveData:function(t){return o.engine.moveData(t)}}),this.registry[t]={backend:i,priority:n},!0}catch(e){return console.warn("Registration of backend "+t+" failed"),console.warn(e.stack||e.message),!1}},t.prototype.removeBackend=function(t){if(!(t in this.registry))throw new Error(t+" backend not found in registry");this.registry[t].backend.dispose(),delete this.registry[t]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initEngine(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initEngine=function(){var t=this;if(null==this.globalEngine){this.backendName=this.get("BACKEND");var e=this.findBackend(this.backendName);this.globalEngine=new Rt(e,!1,function(){return t.get("DEBUG")})}},t}();var Ot,Ft=(null==(Ot=function(){var t;if("undefined"!=typeof window)t=window;else{if("undefined"==typeof process)throw new Error("Could not find a global object");t=process}return t}()).ENV&&(Ot.ENV=new _t(Dt()),ot(function(){return Ot.ENV.engine})),Ot.ENV),Pt=Object.freeze({EPSILON_FLOAT16:1e-4,EPSILON_FLOAT32:1e-7,Environment:_t,ENV:Ft});function Bt(t,