index.es6 4.1 KB
Newer Older
W
wangqun 已提交
1 2
import 'babel-polyfill';
import Paddle from '../../src/paddle/paddle';
W
wangqun 已提交
3
import IO from '../../src/feed/ImageFeed';
W
wangqun 已提交
4 5
import Utils from '../../src/utils/utils';
// 获取map表
W
wangqun 已提交
6 7
// import Map from '../../test/data/map';
let map = {};
W
wangqun 已提交
8 9
const fileDownload = require('js-file-download');

W
wangqun 已提交
10 11 12 13 14 15 16
/**
 * @file model demo 入口文件
 * @author wangqun@baidu.com
 *
 */
// 模型feed数据
const feedShape = {
W
wangqun 已提交
17 18 19
    'mobilenetv2': {
        fw: 224,
        fh: 224
W
wangqun 已提交
20 21
    }
};
W
wangqun 已提交
22 23 24 25 26 27 28

// 模型fetch数据
const fetchShape = {
    'mobilenetv2': [1, 1000, 1, 1]
};

const modelType = 'mobilenetv2';
W
wangqun 已提交
29
const {fw, fh} = feedShape[modelType];
W
wangqun 已提交
30 31
const outputShape = fetchShape[modelType];

W
wangqun 已提交
32 33 34 35
// 统计参数
let loaded = false;
let model = {};
window.statistic = [];
W
wangqun 已提交
36 37 38
let loading = document.getElementsByClassName('data-loading');
let modelTxt = document.getElementById('model-txt');

W
wangqun 已提交
39 40 41 42 43 44
async function run(input) {
    // const input = document.getElementById('mobilenet');
    const io = new IO();
    let feed = io.process({
        input: input,
        params: {
W
wangqun 已提交
45 46 47 48 49
            gapFillWith: '#000', // 缩放后用什么填充不足方形部分
            targetSize: {
                height: fh,
                width: fw
            },
W
wangqun 已提交
50
            scale: 256, // 缩放尺寸
W
wangqun 已提交
51 52 53 54 55
            targetShape: [1, 3, fh, fw], // 目标形状 为了兼容之前的逻辑所以改个名
            mean: [0.485, 0.456, 0.406],
            std: [0.229, 0.224, 0.225]
        }
    });
W
wangqun 已提交
56

57
    const path = 'https://paddlejs.cdn.bcebos.com/models/mobileNetV2';
W
wangqun 已提交
58 59

    if (!loaded) {
W
wangqun 已提交
60 61 62 63

        fetch(path + '/map.json')
            .then(response => response.json())
            .then(data => (map = data));
W
wangqun 已提交
64
        const MODEL_CONFIG = {
65
            dir: `${path}/`, // 存放模型的文件夹
W
wangqun 已提交
66 67 68
            main: 'model.json', // 主文件
        };
        loaded = true;
W
wangqun 已提交
69 70
        modelTxt.style.visibility = 'visible';
        loading[0].style.visibility = 'visible';
W
wangqun 已提交
71 72 73 74
        const paddle = new Paddle({
            urlConf: MODEL_CONFIG,
            options: {
                multipart: true,
W
wangqun 已提交
75 76
                dataType: 'binary',
                options: {
W
wangqun 已提交
77 78
                    // fileCount: 4, // 切成了多少文件
                    getFileName(i) { // 自定义chunk文件名,获取第i个文件的名称
W
wangqun 已提交
79 80 81 82
                        return 'chunk_' + i + '.dat';
                    }
                },
                feed
W
wangqun 已提交
83 84 85
            }
        });
        model = await paddle.load();
W
wangqun 已提交
86 87
        loading[0].style.visibility = 'hidden';
        modelTxt.innerText = '模型加载完成!';
W
wangqun 已提交
88
    }
W
wangqun 已提交
89 90 91
    loading[1].style.visibility = 'visible';
    loading[2].style.visibility = 'visible';
    window.statistic.startTime = (+new Date());
W
wangqun 已提交
92 93 94 95 96
    let inst = model.execute({
        input: feed
    });

    let result = await inst.read();
W
wangqun 已提交
97 98 99
    loading[1].style.visibility = 'hidden';
    loading[2].style.visibility = 'hidden';
    window.statistic.endTime = (+new Date()) - window.statistic.startTime;
W
wangqun 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
    let N = outputShape[0];
    let C = outputShape[1];
    let H = outputShape[2];
    let W = outputShape[3];
    let nhwcShape = [N, H, W, C];
    console.log(nhwcShape);

    let nchwData = Utils.nhwc2nchw(result, nhwcShape);
    Utils.stridePrint(nchwData);
    Utils.continuousPrint(nchwData);

    // for test
    // fileDownload(nchwData, "paddlejs-0.txt");
W
wangqun 已提交
113

W
wangqun 已提交
114 115
    let maxItem = Utils.getMaxItem(nchwData);
    console.log(maxItem);
W
wangqun 已提交
116 117 118
    document.getElementById('txt').innerHTML = map['' + maxItem.index];
    document.getElementById('all-performance-time').innerHTML = '计算时间是' + window.statistic.endTime;
    console.log('识别出的结果是' + map['' + maxItem.index]);
W
wangqun 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
};
var image = '';
function selectImage(file) {
    if (!file.files || !file.files[0]) {
        return;
    }
    let reader = new FileReader();
    reader.onload = function (evt) {
        let img = document.getElementById('image');
        img.src = evt.target.result;
        img.onload = function() {
            run(img);
        };
        image = evt.target.result;
    }
    reader.readAsDataURL(file.files[0]);
}
// selectImage
document.getElementById("uploadImg").onchange = function () {
    selectImage(this);
};