1 Star 0 Fork 1.3K

Lzq05/manifest

forked from OpenHarmony/manifest 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Ability.test.ets 95.69 KB
一键复制 编辑 原始数据 按行查看 历史
Lzq05 提交于 2023-09-18 07:21 . CI测试,请勿合入!!!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
/*
* Copyright (C) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import hilog from '@ohos.hilog';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
import mindSporeLite from '@ohos.ai.mindSporeLite';
import fs from '@ohos.file.fs';
export default function abilityTest() {
//test
describe('ActsAbilityTest', function () {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(function () {
let dir = globalThis.abilityContext.filesDir + "/";
try {
let ml_face_model_file = dir + "ml_face_isface.ms";
globalThis.context.resourceManager.getRawFileContent("ml_face_isface.ms", (error, model_buffer) => {
if (error != null) {
//getRawFileDescriptor运行失败
console.log(
"[rawfile_copy_to_sandbox] ml_face_isface.ms is copy " +
"failed:${error.code}, message: ${error.message}.");
} else {
//getRawFileDescriptor运行成功
let file = fs.openSync(ml_face_model_file, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, model_buffer.buffer);
fs.closeSync(file);
console.log("[rawfile_copy_to_sandbox] ml_face_isface.ms is copy success");
}
});
let mnet_caffemodel_bin_file = dir + "ml_face_isface_0.input";
globalThis.context.resourceManager.getRawFileContent("ml_face_isface_0.input", (error, model_buffer) => {
if (error != null) {
//getRawFileDescriptor运行失败
console.log(
"[rawfile_copy_to_sandbox] ml_face_isface_0.input is copy " +
"failed:${error.code}, message: ${error.message}.");
} else {
//getRawFileDescriptor运行成功
let file = fs.openSync(mnet_caffemodel_bin_file, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, model_buffer.buffer);
fs.closeSync(file);
console.log("[rawfile_copy_to_sandbox] ml_face_isface_0.input is copy success");
}
});
let ml_ocr_model_file = dir + "ml_ocr_cn.ms";
globalThis.context.resourceManager.getRawFileContent("ml_ocr_cn.ms", (error, model_buffer) => {
if (error != null) {
//getRawFileDescriptor运行失败
console.log(
"[rawfile_copy_to_sandbox] ml_ocr_cn.ms is copy " +
"failed:${error.code}, message: ${error.message}.");
} else {
//getRawFileDescriptor运行成功
let file = fs.openSync(ml_ocr_model_file, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, model_buffer.buffer);
fs.closeSync(file);
console.log("[rawfile_copy_to_sandbox] ml_ocr_cn.ms is copy success");
}
});
} catch (error) {
console.info("[rawfile_copy_to_sandbox] getRawFileDescriptor api run failed" + error);
}
console.info("[rawfile_copy_to_sandbox] sandbox path:" + dir);
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
})
beforeEach(async function () {
let dir = globalThis.abilityContext.filesDir + "/";
let ml_face_model = dir + "ml_face_isface.ms";
await fs.access(ml_face_model).then(async (res) => {
if (res) {
console.info("ml_face_isface.ms file exists");
}
}).catch((err) => {
console.info("ml_face_isface.ms file does not exists! access failed with error message: " +
err.message + ", error code: " + err.code);
});
let mnet_caffemodel_bin_file = dir + "ml_face_isface_0.input";
await fs.access(mnet_caffemodel_bin_file).then(async (res) => {
if (res) {
console.info("ml_face_isface_0.input file exists");
}
}).catch((err) => {
console.info("ml_face_isface_0.input file does not exist! access failed with error message: " +
err.message + ", error code: " + err.code);
});
let ml_ocr_model = dir + "ml_ocr_cn.ms";
await fs.access(ml_ocr_model).then(async (res) => {
if (res) {
console.info("ml_ocr_cn.ms file exists");
}
}).catch((err) => {
console.info("ml_ocr_cn.ms file does not exists! access failed with error message: " +
err.message + ", error code: " + err.code);
});
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
})
afterEach(function () {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
})
afterAll(function () {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
})
// 正常场景:ModelBuild,调用buffer方法,正常推理
it('Test_load_model_param_model_buffer', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_buffer");
let modelName = 'ml_face_isface.ms';
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let modelBuffer = await syscontext.resourceManager.getRawFileContent(modelName);
let context: mindSporeLite.Context = {};
context.target = ["cpu", "nnrt"];
context.nnrt = {};
context.cpu = {};
context.cpu.threadNum = 1;
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
context.cpu.precisionMode = "preferred_fp16";
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
let msliteModel = await mindSporeLite.loadModelFromBuffer(modelBuffer.buffer, context)
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:ModelBuild,调用fd方法,正常推理
it('Test_load_model_param_model_fd', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_fd");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let file = fs.openSync(model_file, fs.OpenMode.READ_ONLY);
let msliteModel = await mindSporeLite.loadModelFromFd(file.fd);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:ModelBuild,调用loadModelFromFile方法,正常推理
it('Test_load_model_param_model_path', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu", "nnrt"];
context.nnrt = {};
context.cpu = {};
context.cpu.threadNum = 1;
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
context.cpu.precisionMode = "preferred_fp16";
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
console.log('=========MSLITE loadModel start=====');
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,4线程
it('Test_load_model_param_model_path_settings_threads_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_threads_001");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu = {
"threadNum": 4,
}
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,2线程
it('Test_load_model_param_model_path_settings_threads_002', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_threads_002");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu = {
"threadNum": 2,
}
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,1线程
it('Test_load_model_param_model_path_settings_threads_003', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_threads_003");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength)
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu = {
"threadNum": 1,
}
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,0线程
it('Test_load_model_param_model_path_settings_threads_004', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_threads_004");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu = {
"threadNum": 0,
}
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//异常场景:Context设置CPU,绑核设置为3,绑核失败
it('Test_load_model_param_model_path_settings_affinity_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_affinity_001");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu={};
context.cpu.threadAffinityMode = 3;
console.log("MSLITE api test: set threadAffinityMode=3.");
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
///异常场景:Context设置CPU,绑核设置为2,绑小核
it('Test_load_model_param_model_path_settings_affinity_002', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_affinity_002");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu={};
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.LITTLE_CORES_FIRST;
console.log("MSLITE api test: set threadAffinityMode=2.");
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//异常场景:Context设置CPU,绑核设置为1,绑大核
it('Test_load_model_param_model_path_settings_affinity_003', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_affinity_003");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength)
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu={};
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
console.log("MSLITE api test: set threadAffinityMode=1.");
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,绑核设置为0,不绑核
it('Test_load_model_param_model_path_settings_affinity_004', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_affinity_004");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu={};
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.NO_AFFINITIES;
console.log("MSLITE api test: set threadAffinityMode=0.");
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//正常场景:Context设置CPU,绑核列表[0,1,2,3]
it('Test_load_model_param_model_path_settings_affinity_list_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_settings_affinity_list_001");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu={};
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
console.log("MSLITE api test: set threadAffinityCoreList=[0, 1, 2, 3].");
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
//异常场景:ModelBuild,调用model path方法,path为空
it('Test_load_model_param_model_path_is_None_001',0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_is_None_001");
let model_file = "";
let context:mindSporeLite.Context={};
context.target = ["cpu"];
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
console.log('=========MSLITE loadModel end=====');
expect(msliteModel).assertUndefined();
done();
})
//异常场景:ModelBuild,调用buffer方法,modelBuffer为None
it('Test_load_model_param_model_buffer_is_None_001',0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_buffer_is_None_001");
let context:mindSporeLite.Context={};
context.target = ["cpu"];
let msliteModel = await mindSporeLite.loadModelFromBuffer(null, context);
console.log('=========MSLITE loadModel end=====');
expect(msliteModel).assertUndefined();
done();
})
//异常场景:ModelBuild,context为null
it('Test_load_model_param_model_path_context_is_None_001',0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_context_is_None_001");
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let context:mindSporeLite.Context=null;
let msliteModel = await mindSporeLite.loadModelFromFile(model_file, context);
expect(msliteModel).assertUndefined();
console.log('=========MSLITE loadModel end=====');
done();
})
// 正常场景:ModelResize,shape与之前一致
it('Test_load_model_param_model_path_resize_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_resize_001");
let inputName = 'ml_ocr_cn_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_ocr_cn.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,32,512,1");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("16384");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("65536");
console.log('=========MSLITE resize start=====');
let new_dim = new Array([1, 32, 512, 1]);
let resize_result = msliteModel.resize(modelInputs, new_dim);
expect(resize_result).assertTrue();
console.log('=========MSLITE resize success=====');
const modelInputs2 = msliteModel.getInputs();
let input_name2 = modelInputs2[0].name;
console.log(input_name2.toString());
expect(input_name2.toString()).assertEqual("data");
let input_shape2 = modelInputs2[0].shape;
console.log(input_shape2.toString());
expect(input_shape2.toString()).assertEqual("1,32,512,1");
let input_elementNum2 = modelInputs2[0].elementNum;
console.log(input_elementNum2.toString());
expect(input_elementNum2.toString()).assertEqual("16384");
let input_dtype2 = modelInputs2[0].dtype;
console.log(input_dtype2.toString());
expect(input_dtype2).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format2 = modelInputs2[0].format;
console.log(input_format2.toString());
expect(input_format2).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize2 = modelInputs2[0].dataSize;
console.log(input_dataSize2.toString());
expect(input_dataSize2.toString()).assertEqual("65536");
modelInputs2[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs2);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:ModelResize,shape与之前不一致
it('Test_load_model_param_model_path_resize_002', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_resize_002");
let inputName = 'ml_ocr_cn_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_ocr_cn.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,32,512,1");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("16384");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("65536");
console.log('=========MSLITE resize start=====');
let new_dim = new Array([1,64,256,1]);
let resize_result = msliteModel.resize(modelInputs, new_dim);
expect(resize_result).assertTrue();
console.log('=========MSLITE resize success=====');
const modelInputs2 = msliteModel.getInputs();
let input_name2 = modelInputs2[0].name;
console.log(input_name2.toString());
expect(input_name2.toString()).assertEqual("data");
let input_shape2 = modelInputs2[0].shape;
console.log(input_shape2.toString());
expect(input_shape2.toString()).assertEqual("1,64,256,1");
let input_elementNum2 = modelInputs2[0].elementNum;
console.log(input_elementNum2.toString());
expect(input_elementNum2.toString()).assertEqual("16384");
let input_dtype2 = modelInputs2[0].dtype;
console.log(input_dtype2.toString());
expect(input_dtype2).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format2 = modelInputs2[0].format;
console.log(input_format2.toString());
expect(input_format2).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize2 = modelInputs2[0].dataSize;
console.log(input_dataSize2.toString());
expect(input_dataSize2.toString()).assertEqual("65536");
modelInputs2[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs2);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 异常场景:ModelResize,shape为三维
it('Test_load_model_param_model_path_resize_003', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_resize_003");
let inputName = 'ml_ocr_cn_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_ocr_cn.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,32,512,1");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("16384");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("65536");
console.log('=========MSLITE resize start=====');
let new_dim = new Array([1,32,1]);
let resize_result = msliteModel.resize(modelInputs, new_dim);
expect(resize_result).assertFalse;
console.log('=========MSLITE resize failed=====');
})
// 异常场景:ModelResize,不支持resize的模型
it('Test_load_model_param_model_path_resize_004', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_resize_004");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
console.log(modelInputs[0].shape.toString());
expect(modelInputs[0].shape.toString()).assertEqual("1,48,48,3");
console.log(modelInputs[0].elementNum.toString());
expect(modelInputs[0].elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
console.log('=========MSLITE resize start=====');
let new_dim = new Array([1,96,96,1]);
let resize_result = msliteModel.resize(modelInputs, new_dim);
expect(resize_result).assertFalse();
console.log('=========MSLITE resize failed=====');
})
// 异常场景:ModelResize,shape值有负数
it('Test_load_model_param_model_path_resize_005', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_resize_005");
let inputName = 'ml_ocr_cn_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_ocr_cn.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,32,512,1");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("16384");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("65536");
console.log('=========MSLITE resize start=====');
let new_dim = new Array([1,-32,32,1]);
let resize_result = msliteModel.resize(modelInputs, new_dim);
expect(resize_result).assertFalse();
console.log('=========MSLITE resize failed=====');
})
// 正常场景:Build一次,Predict多次
it('Test_load_model_param_model_path_much_predict_001',0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_much_predict_001");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let num = 0;
for (var i = 0; i < 10; i++) {
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var z = 0; z < 2; z++) {
console.log(output0[z].toString());
expect(output0[z].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
console.log('=========i.toString()=====');
console.log(i.toString());
++num;
console.log('=========num.toString()=====');
console.log(num.toString());
}
})
// 异常场景:Build多次
it('Test_load_model_param_model_path_much_build_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_much_build_001");
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
for (var i = 0; i < 10; i++) {
mindSporeLite.loadModelFromFile(model_file);
}
let msliteModel = await mindSporeLite.loadModelFromFile(model_file);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:单输入模型
it('Test_load_model_param_model_path_model_001', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_model_001");
let modelName = 'ml_face_isface.ms';
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let modelBuffer = await syscontext.resourceManager.getRawFileContent(modelName);
let msliteModel = await mindSporeLite.loadModelFromBuffer(modelBuffer.buffer);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
console.log(modelInputs[0].shape.toString());
expect(modelInputs[0].shape.toString()).assertEqual("1,48,48,3");
console.log(modelInputs[0].elementNum.toString());
expect(modelInputs[0].elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
let Inputs2 = new Float32Array(modelInputs[0].getData());
for (var i = 0; i < 5; i++) {
console.log(Inputs2[i].toString());
}
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:多输入模型
it('Test_load_model_param_model_path_model_002', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_model_002");
let modelName = 'ml_video_edit_face_cutout_portraitSeg_deconv.ms';
let inputName01 = 'ml_video_edit_face_cutout_portraitSeg_deconv_0.input';
let inputName02 = 'ml_video_edit_face_cutout_portraitSeg_deconv_1.input';
let syscontext = globalThis.context;
let inputBuffer01 = await syscontext.resourceManager.getRawFileContent(inputName01);
let inputBuffer02 = await syscontext.resourceManager.getRawFileContent(inputName02);
console.log('=========MSLITE success, input01 bin bytelength: ' + inputBuffer01.byteLength);
console.log('=========MSLITE success, input02 bin bytelength: ' + inputBuffer02.byteLength);
let modelBuffer = await syscontext.resourceManager.getRawFileContent(modelName);
let msliteModel = await mindSporeLite.loadModelFromBuffer(modelBuffer.buffer);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
console.log(modelInputs[0].name);
expect(modelInputs[0].name.toString()).assertEqual("a");
console.log(modelInputs[0].shape.toString());
expect(modelInputs[0].shape.toString()).assertEqual("1,512,512,3");
console.log(modelInputs[0].elementNum.toString());
expect(modelInputs[0].elementNum.toString()).assertEqual("786432");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("3145728");
console.log(modelInputs[1].name);
expect(modelInputs[1].name.toString()).assertEqual("b");
console.log(modelInputs[1].shape.toString());
expect(modelInputs[1].shape.toString()).assertEqual("1,512,512,1");
console.log(modelInputs[1].elementNum.toString());
expect(modelInputs[1].elementNum.toString()).assertEqual("262144");
console.log(modelInputs[1].dtype.toString());
expect(modelInputs[1].dtype.toString()).assertEqual("43");
console.log(modelInputs[1].format.toString());
expect(modelInputs[1].format.toString()).assertEqual("1");
console.log(modelInputs[1].dataSize.toString());
expect(modelInputs[1].dataSize.toString()).assertEqual("1048576");
modelInputs[0].setData(inputBuffer01.buffer);
modelInputs[1].setData(inputBuffer02.buffer);
let Inputs2 = new Float32Array(modelInputs[0].getData());
for (var i = 0; i < 5; i++) {
console.log(Inputs2[i].toString());
}
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:输入为uint8模型
it('Test_load_model_param_model_path_model_003', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_model_003");
let modelName = 'aiy_vision_classifier_plants_V1_3.ms';
let inputName = 'aiy_vision_classifier_plants_V1_3_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
let modelBuffer = await syscontext.resourceManager.getRawFileContent(modelName);
let msliteModel = await mindSporeLite.loadModelFromBuffer(modelBuffer.buffer);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
console.log(modelInputs[0].name);
expect(modelInputs[0].name.toString()).assertEqual("module/hub_input/images_uint8");
console.log(modelInputs[0].shape.toString());
expect(modelInputs[0].shape.toString()).assertEqual("1,224,224,3");
console.log(modelInputs[0].elementNum.toString());
expect(modelInputs[0].elementNum.toString()).assertEqual("150528");
console.log(modelInputs[0].dtype.toString());
expect(modelInputs[0].dtype.toString()).assertEqual("37");
console.log(modelInputs[0].format.toString());
expect(modelInputs[0].format.toString()).assertEqual("1");
console.log(modelInputs[0].dataSize.toString());
expect(modelInputs[0].dataSize.toString()).assertEqual("150528");
modelInputs[0].setData(inputBuffer.buffer);
let Inputs2 = new Uint8Array(modelInputs[0].getData());
for (var i = 0; i < 5; i++) {
console.log(Inputs2[i].toString());
}
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Uint8Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:多输入单输出
it('Test_load_model_param_model_path_model_004', 0, async function () {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_model_004");
let modelName = 'ml_headpose_pb2tflite.ms';
let inputName01 = 'ml_headpose_pb2tflite_0.input';
let inputName02 = 'ml_headpose_pb2tflite_1.input';
let inputName03 = 'ml_headpose_pb2tflite_2.input';
let syscontext = globalThis.context
let inputBuffer01 = await syscontext.resourceManager.getRawFileContent(inputName01);
let inputBuffer02 = await syscontext.resourceManager.getRawFileContent(inputName02);
let inputBuffer03 = await syscontext.resourceManager.getRawFileContent(inputName03);
console.log('=========MSLITE success, input01 bin bytelength: ' + inputBuffer01.byteLength);
console.log('=========MSLITE success, input02 bin bytelength: ' + inputBuffer02.byteLength);
console.log('=========MSLITE success, input03 bin bytelength: ' + inputBuffer03.byteLength);
let modelBuffer = await syscontext.resourceManager.getRawFileContent(modelName);
let msliteModel = await mindSporeLite.loadModelFromBuffer(modelBuffer.buffer);
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
console.log(modelInputs[0].name);
expect(modelInputs[0].name.toString()).assertEqual("input_1");
console.log(modelInputs[0].shape.toString());
expect(modelInputs[0].shape.toString()).assertEqual("1,64,64,3");
console.log(modelInputs[0].elementNum.toString());
expect(modelInputs[0].elementNum.toString()).assertEqual("12288");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("49152");
console.log(modelInputs[1].name);
expect(modelInputs[1].name.toString()).assertEqual("batch_normalization_8/batchnorm/add");
console.log(modelInputs[1].shape.toString());
expect(modelInputs[1].shape.toString()).assertEqual("16");
console.log(modelInputs[1].elementNum.toString());
expect(modelInputs[1].elementNum.toString()).assertEqual("16");
console.log(modelInputs[1].dtype.toString());
expect(modelInputs[1].dtype.toString()).assertEqual("43");
console.log(modelInputs[1].format.toString());
expect(modelInputs[1].format.toString()).assertEqual("1");
console.log(modelInputs[1].dataSize.toString());
expect(modelInputs[1].dataSize.toString()).assertEqual("64");
console.log(modelInputs[2].name);
expect(modelInputs[2].name.toString()).assertEqual("batch_normalization_1/batchnorm/add");
console.log(modelInputs[2].shape.toString());
expect(modelInputs[2].shape.toString()).assertEqual("16");
console.log(modelInputs[2].elementNum.toString());
expect(modelInputs[2].elementNum.toString()).assertEqual("16");
console.log(modelInputs[2].dtype.toString());
expect(modelInputs[2].dtype.toString()).assertEqual("43");
console.log(modelInputs[2].format.toString());
expect(modelInputs[2].format.toString()).assertEqual("1");
console.log(modelInputs[2].dataSize.toString());
expect(modelInputs[2].dataSize.toString()).assertEqual("64");
modelInputs[0].setData(inputBuffer01.buffer);
modelInputs[1].setData(inputBuffer02.buffer);
modelInputs[2].setData(inputBuffer03.buffer);
let Inputs2 = new Float32Array(modelInputs[0].getData());
for (var i = 0; i < 5; i++) {
console.log(Inputs2[i].toString());
}
console.log('=========MSLITE predict start=====');
let modelOutputs = await msliteModel.predict(modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
})
// 正常场景:调用loadModelFromFile callback接口设置context
it('Test_load_model_param_model_path_callback', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_callback");
function load_model_from_file() {
return new Promise((resolve) => {
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let context: mindSporeLite.Context = {};
context.target = ["cpu"];
context.cpu = {
"threadNum": 4,
}
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
context.cpu.precisionMode = "preferred_fp16";
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
mindSporeLite.loadModelFromFile(model_file, context, (msliteModel) => {
resolve(msliteModel);
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_file();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:调用loadModelFromBuffer callback接口设置context
it('Test_load_model_param_model_buffer_callback', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_buffer_callback");
function load_model_from_buffer() {
return new Promise((resolve) => {
let modelName = 'ml_face_isface.ms';
syscontext.resourceManager.getRawFileContent(modelName).then((model_buffer) => {
let modelBuffer = model_buffer
let context: mindSporeLite.Context = {};
context.target = ["cpu"];
context.cpu = {
"threadNum": 4,
}
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
context.cpu.precisionMode = "preferred_fp16";
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
mindSporeLite.loadModelFromBuffer(modelBuffer.buffer, context, (msliteModel) => {
resolve(msliteModel);
})
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_buffer();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:调用loadModelFromFd callback接口设置context
it('Test_load_model_param_model_fd_callback', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_fd_callback");
function load_model_from_fd() {
return new Promise((resolve) => {
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let file = fs.openSync(model_file, fs.OpenMode.READ_ONLY);
let context:mindSporeLite.Context={};
context.target = ["cpu"];
context.cpu = {
"threadNum": 4,
}
context.cpu.threadAffinityMode = mindSporeLite.ThreadAffinityMode.BIG_CORES_FIRST;
context.cpu.precisionMode = "preferred_fp16";
context.cpu.threadAffinityCoreList = [0, 1, 2, 3];
mindSporeLite.loadModelFromFd(file.fd, context, (msliteModel) => {
resolve(msliteModel);
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_fd();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:调用loadModelFromFile callback接口未设置context
it('Test_load_model_param_model_path_callback_no_context', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_path_callback_no_context");
function load_model_from_file() {
return new Promise((resolve) => {
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
mindSporeLite.loadModelFromFile(model_file, (msliteModel) => {
resolve(msliteModel);
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_file();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:调用loadModelFromBuffer callback接口未设置context
it('Test_load_model_param_model_buffer_callback_no_context', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_buffer_callback_no_context");
function load_model_from_buffer() {
return new Promise((resolve) => {
let modelName = 'ml_face_isface.ms';
syscontext.resourceManager.getRawFileContent(modelName).then((model_buffer) => {
let modelBuffer = model_buffer
mindSporeLite.loadModelFromBuffer(modelBuffer.buffer, (msliteModel) => {
resolve(msliteModel);
})
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_buffer();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3")
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:调用loadModelFromFd callback接口未设置context
it('Test_load_model_param_model_fd_callback_no_context', 0, async function (done) {
console.log("MSLITE api test: loadModel param model file.Test_load_model_param_model_fd_callback_no_context");
function load_model_from_fd() {
return new Promise((resolve) => {
let model_file = globalThis.abilityContext.filesDir + '/' + 'ml_face_isface.ms';
let file = fs.openSync(model_file, fs.OpenMode.READ_ONLY);
mindSporeLite.loadModelFromFd(file.fd, (msliteModel) => {
resolve(msliteModel);
})
})
}
function case_predict(msliteModel, modelInputs) {
return new Promise((resolve) => {
msliteModel.predict(modelInputs, (modelOutputs) => {
resolve(modelOutputs);
})
})
}
let inputName = 'ml_face_isface_0.input';
let syscontext = globalThis.context;
let inputBuffer = await syscontext.resourceManager.getRawFileContent(inputName);
console.log('=========MSLITE success, input bin bytelength: ' + inputBuffer.byteLength);
console.log('=========MSLITE loadModel start=====');
let msliteModel = null;
msliteModel = await load_model_from_fd();
expect(msliteModel !== null).assertTrue();
console.log('=========MSLITE loadModel end=====');
const modelInputs = msliteModel.getInputs();
let input_name = modelInputs[0].name;
console.log(input_name.toString());
expect(input_name.toString()).assertEqual("data");
let input_shape = modelInputs[0].shape;
console.log(input_shape.toString());
expect(input_shape.toString()).assertEqual("1,48,48,3");
let input_elementNum = modelInputs[0].elementNum;
console.log(input_elementNum.toString());
expect(input_elementNum.toString()).assertEqual("6912");
let input_dtype = modelInputs[0].dtype;
console.log(input_dtype.toString());
expect(input_dtype).assertEqual(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32);
let input_format = modelInputs[0].format;
console.log(input_format.toString());
expect(input_format).assertEqual(mindSporeLite.Format.NHWC);
let input_dataSize = modelInputs[0].dataSize;
console.log(input_dataSize.toString());
expect(input_dataSize.toString()).assertEqual("27648");
modelInputs[0].setData(inputBuffer.buffer);
console.log('=========MSLITE predict start=====');
let modelOutputs = await case_predict(msliteModel, modelInputs);
expect(modelOutputs !== null).assertTrue();
console.log('=========MSLITE new Float32Array start=====');
let output0 = new Float32Array(modelOutputs[0].getData());
expect(output0.length).assertLarger(0);
console.log('output0.length:' + output0.length);
for (var i = 0; i < 2; i++) {
console.log(output0[i].toString());
expect(output0[i].toString() !== null).assertTrue();
}
const modelInputs0 = msliteModel.getInputs();
console.log(modelInputs0[0].name.toString());
console.log('=========MSLITE new Float32Array end=====');
done();
})
// 正常场景:头文件枚举值测试
it('Test_enumerated_value', 0, async function (done) {
try{
expect(mindSporeLite.Format.NCHW).assertEqual(0);
expect(mindSporeLite.Format.NHWC).assertEqual(1);
expect(mindSporeLite.Format.NHWC4).assertEqual(2);
expect(mindSporeLite.Format.HWKC).assertEqual(3);
expect(mindSporeLite.Format.HWCK).assertEqual(4);
expect(mindSporeLite.Format.KCHW).assertEqual(5);
expect(mindSporeLite.Format.DEFAULT_FORMAT).assertEqual(-1);
expect(mindSporeLite.DataType.TYPE_UNKNOWN).assertEqual(0);
expect(mindSporeLite.DataType.NUMBER_TYPE_INT8).assertEqual(32);
expect(mindSporeLite.DataType.NUMBER_TYPE_INT16).assertEqual(33);
expect(mindSporeLite.DataType.NUMBER_TYPE_INT32).assertEqual(34);
expect(mindSporeLite.DataType.NUMBER_TYPE_INT64).assertEqual(35);
expect(mindSporeLite.DataType.NUMBER_TYPE_UINT8).assertEqual(37);
expect(mindSporeLite.DataType.NUMBER_TYPE_UINT16).assertEqual(38);
expect(mindSporeLite.DataType.NUMBER_TYPE_UINT32).assertEqual(39);
expect(mindSporeLite.DataType.NUMBER_TYPE_UINT64).assertEqual(40);
expect(mindSporeLite.DataType.NUMBER_TYPE_FLOAT16).assertEqual(42);
expect(mindSporeLite.DataType.NUMBER_TYPE_FLOAT32).assertEqual(43);
expect(mindSporeLite.DataType.NUMBER_TYPE_FLOAT64).assertEqual(44);
done()
} catch (error) {
console.info("The enumerated are changed:" + error)
done()
}
})
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lzq05/manifest.git
git@gitee.com:lzq05/manifest.git
lzq05
manifest
manifest
master

搜索帮助