function init_shadow() { function load_weight(path, shape, is_integer) { var deferred = new $.Deferred(); var oReq = new XMLHttpRequest(); oReq.open("GET", path, true); oReq.responseType = "arraybuffer"; oReq.onload = function (oEvent) { var arrayBuffer = oReq.response; // Note: not oReq.responseText if (arrayBuffer) { if(is_integer) { var weights = new Int32Array(arrayBuffer); deferred.resolve({"d": weights, "t": "i", "s": shape}); }else{ var weights = new Float32Array(arrayBuffer); deferred.resolve({"d": weights, "t": "f", "s": shape}); } }else{ deferred.reject(); } }; oReq.send(null); return deferred.promise(); } function load_descriptor(path) { var deferred = new $.Deferred(); var oReq = new XMLHttpRequest(); oReq.open("GET", path, true); oReq.responseType = "text"; oReq.onload = function (oEvent) { if (this.readyState == 4 && this.status == 200) { var weight_set = JSON.parse(this.responseText); var children = []; for(i in weight_set) { weight = weight_set[i]; if(weight["t"] == "n") { children.push(load_descriptor(weight["path"])); }else{ children.push(load_weight(weight["path"], weight["shape"], weight["t"] == "i")); } } $.when.apply($, children).then(function(){ results = []; for(i in arguments) { results.push(arguments[i]); } deferred.resolve(results); }).fail(function(){ deferred.reject(); }); }else{ deferred.reject(); } }; oReq.send(null); return deferred.promise(); } load_descriptor("model/model.json").then(on_ready); // const v = tf.tensor2d([[1, 2], [3, 4]]); // const b = tf.tensor1d([1, 2]); // const W = [tf.tensor2d([[1, 2], [3, 4]])]; // const U = [tf.tensor2d([[1, 2], [3, 4]])]; // const Ub = [tf.tensor1d([1, 2])]; // const Wb = [tf.tensor1d([1, 2])]; function transform_to_tensor(ary, two_ds) { out = []; for(var i = 0;i { r = tf.tensor2d(r, [1, size]); t = tf.tensor2d(t, [1, size]); m = tf.mean(r, axis=1, keepDims=true); r = tf.div(r, m); out = tf.concat([r, tf.mul(t, r)], 0) return tf.reshape(out, [-1, size*2]); }); } function compute(input) { return tf.tidy(() => { function residue_layer(input, i) { const w = W[i]; const u = U[i]; const ub = Ub[i]; const wb = Wb[i]; const residue = tf.mul(tf.elu(tf.add(tf.matMul(input, u), ub)), input) const output = tf.add(tf.elu(tf.add(tf.matMul(residue, w), wb)), residue) return output; } a = input; for(var i = 0;i { return tf.exp(tf.sum(tf.squaredDifference(f0.expandDims(1), f1.expandDims(0)), 2).neg()); }); } function get_class_score(response, class_indices) { return tf.tidy(() => { var class_scores = []; for(var c in class_indices) { class_scores.push(tf.max(response.gather(class_indices[c], 1), 1)); } return tf.stack(class_scores, 1); }); } function point_dist(p0, p1) { return Math.sqrt((p0[0] - p1[0])*(p0[0] - p1[0]) + (p0[1] - p1[1])*(p0[1] - p1[1])); } function point_radian(p0, p1){ return Math.atan2(p0[1] - p1[1], p0[0] - p1[0]); } function radian_diff(r0, r1) { delta = r0 - r1; sign = (delta < 0? -1.0: 1.0); abs_delta = Math.abs(delta); while(abs_delta >= 2 * Math.PI) abs_delta = abs_delta - 2 * Math.PI; return (abs_delta < - (abs_delta - 2 * Math.PI)? sign * abs_delta : sign * (abs_delta - 2 * Math.PI)); } function get_polar_stat(contour) { sx = 0; sy = 0; len_contour = contour.length; size = Math.max(len_contour, 1) for(var i = 0;i