tensor.es6 4.8 KB
Newer Older
W
wangqun 已提交
1
/* eslint-disable */
W
wangqun 已提交
2 3 4
import Utils from './utils';
/**
 * @file Tensor类
W
wangqun 已提交
5
 * @author yangmingming
W
wangqun 已提交
6 7 8 9 10 11 12 13
 */
export default class Tensor {
    constructor(opts = {}) {
        this.opts = opts;
        // 数据存储方式
        this.isPacked = this.isPacked || false;
        // 设置tensor名字
        this.name = opts.name;
W
wangqun 已提交
14 15
        // 设置 tensorId
        this.tensorId = opts.type;
W
wangqun 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        // tensor的形状
        let shape = this.shape = opts.shape;
        // 原始数据个数
        this.total = shape.reduce((all, num) => all * num);
        // 图像tensor是否带有batch
        if (opts.needBatch && shape.length < 4) {
            let batch = [];
            for (let i = 0; i < (4 - shape.length); i++) {
                batch.push(1);
            }
            shape = batch.concat(shape);
            this.shape = shape;
        }
        // 获取转换到texture后的信息
        let {offsetX, offsetY, exceedMax, zeroNumber, shape: shape_texture} = Utils.getTextureInfoFromTensorShape(shape, opts.isPacked);
        this.shape_texture = shape_texture;
        this.exceedMax = exceedMax;
        this.offsetX = offsetX;
        this.offsetY = offsetY;
        // tensor数据
        let data;
        if (opts.type === 'image' || opts.type === 'x') {
            this.data = opts.data;
        }
        else if (opts.data && opts.data.length) {
            data = new Float32Array(opts.data.length);
            if (!opts.notCompressed) {
                let b = shape[0];
                let c = shape[1];
                let h = shape[2];
                let w = shape[3];
                if (w) {
                    for (let i = 0; i < opts.data.length; i++) {
                        let j = i / (c * w) | 0;
                        let k = i % (c * w);
                        let b1 = j / h | 0;
                        let h1 = j % h;
                        let c1 = k % c;
                        let w1 = k / c | 0;
                        let l = b1 * (c * h * w) + c1 * (h * w) + h1 * (w) + w1;
                        data[i] = opts.data[l];
                    }
                    this.data = data;
                }
                else {
                    if (opts.data.length > this.total) {
                        opts.data = opts.data.slice(0, this.total);
                    }
                    this.data = new Float32Array(opts.data);
                    debugger;
                }

            } else {
                // batchnorm的scale
                this.shape_texture = [4, 1, this.total / 4];
                // data = [].concat(opts.data);
                this.data = new Float32Array(opts.data);
            }

            // this.data = new Float32Array(data);
            // console.log('this.data.length', this.data.length);
            // 清理缓存
            opts.data = null;
        }
    }

    /**
     * 获取数组下标, shape例子[M, W, H, D]
     * @param pos {Array} tensor坐标索引
     * @return {Number} tensor数据
     */
    getValue(pos = []) {
        let p = [].concat(pos);
        let len = p.length;
        let sLen = this.shape.length;
        // 补齐
        for (let i = 0; i < (sLen - len); i++) {
            p.unshift(0);
        }
        let index = 0;
        for (let i = 0; i < sLen; i++) {
            index += p[i] * this.shapeNumbers[i];
        }
        return this.data[index];
    }

    get width_texture() {
        let length = this.shape_texture.length;
        return this.shape_texture[length - 1];
    }

    get height_texture() {
        let length = this.shape_texture.length;
        return this.shape_texture[length - 2];
    }

    get width_shape() {
        let length = this.shape.length;
        return this.shape[length - 1];
    }

    get height_shape() {
        let length = this.shape.length;
        return this.shape[length - 2];
    }

    get channel() {
        let length = this.shape.length;
        if (length >= 3) {
            return this.shape[length - 3];
        }
        return 0;
    }

    get offset_x() {
        return this.offsetX;
    }

    get offset_y() {
        return this.offsetY;
    }

    get limit() {
        return this.exceedMax ? 'Limit' : '';
    }

    get length_shape() {
        return this.shape.length || 0;
    }

    /**
     * 获取shape对应的个数
     * @return {Array} 和shape长度相等的对应个数
     */
    get numbers_shape() {
        let numbers = [];
        let sLen = this.shape.length;
        for (let i = 0; i < (sLen - 1); i++) {
            let number = this.shape.slice(i + 1).reduce((total, num) => total * num);
            numbers.push(number);
        }
        // 和shape长度保持一致
        numbers.push(1);
        return numbers;
    }

    get total_shape() {
        return this.total;
    }

    dispose() {
        if (this.data) {
            this.data = null;
        }
    }
}
W
wangqun 已提交
172
/* eslint-enable */