index.es6 3.2 KB
Newer Older
W
wangqun 已提交
1 2 3 4 5 6
import 'babel-polyfill';
import Paddle from '../../src/paddle/paddle';
import IO from '../../src/feed/imageFeed';
import Utils from '../../src/utils/utils';
// 获取map表
import Map from '../../test/data/map';
W
wangqun 已提交
7 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 36 37 38 39 40 41
// 统计参数
let loaded = false;
let model = {};
window.statistic = [];
async function run(input) {
    // const input = document.getElementById('mobilenet');
    const io = new IO();
    let feed = io.process({
        input: input,
        params: {
W
wangqun 已提交
42 43 44 45 46
            gapFillWith: '#000', // 缩放后用什么填充不足方形部分
            targetSize: {
                height: fh,
                width: fw
            },
W
wangqun 已提交
47
            scale: 256, // 缩放尺寸
W
wangqun 已提交
48 49 50 51 52
            targetShape: [1, 3, fh, fw], // 目标形状 为了兼容之前的逻辑所以改个名
            mean: [0.485, 0.456, 0.406],
            std: [0.229, 0.224, 0.225]
        }
    });
W
wangqun 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65

    const path = 'model/mobileNet';

    if (!loaded) {
        const MODEL_CONFIG = {
            dir: `/${path}/`, // 存放模型的文件夹
            main: 'model.json', // 主文件
        };
        loaded = true;
        const paddle = new Paddle({
            urlConf: MODEL_CONFIG,
            options: {
                multipart: true,
W
wangqun 已提交
66 67 68 69 70 71 72 73
                dataType: 'binary',
                options: {
                    fileCount: 4, // 切成了多少文件
                    getFileName(i) { // 获取第i个文件的名称
                        return 'chunk_' + i + '.dat';
                    }
                },
                feed
W
wangqun 已提交
74 75 76 77 78 79 80 81 82 83 84
            }
        });
        model = await paddle.load();
    }

    let inst = model.execute({
        input: feed
    });

    let result = await inst.read();

W
wangqun 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    let N = outputShape[0];
    let C = outputShape[1];
    let H = outputShape[2];
    let W = outputShape[3];
    console.log(outputShape);
    let nhwcShape = [N, H, W, C];
    console.log(nhwcShape);
    console.log(result.length);

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

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

W
wangqun 已提交
101 102 103 104
    let maxItem = Utils.getMaxItem(nchwData);
    console.log(maxItem);
    document.getElementById('txt').innerHTML = Map['' + maxItem.index];
    console.log('识别出的结果是' + Map['' + maxItem.index]);
W
wangqun 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
};
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);
};