• Lv1
    粉丝0

积分0 / 贡献0

提问1答案被采纳0文章0

作者动态

    【ArkTS应用开发】【AI服务】MindSpore Lite图像分类:如何获取模型标签文件?

    yuyuzururu 显示全部楼层 发表于 2025-12-14 11:26:52
    【问题描述】
    问题现象和发生的背景
    我正在复现文档中的内容:docs.openharmony.cn/pages/v5.1.0/zh-cn/application-dev/ai/mindspore/mindspore-guidelines-based-js.md,使用了文档中的模型文件:mobilenetv2.ms,点击就直接下载好模型文件了,在代码运行成功后,发现推理出来的只有物品索引号,但是我想知道索引号对应的具体物品类别或名称。因为之后我想把它应用到具体的服务上。

    相关的代码,截图,日志信息
    我把Index上的代码放在这里,其他地方的代码直接复制文档即可:
    1. // Index.ets
    2. import { photoAccessHelper } from '@kit.MediaLibraryKit';
    3. import { BusinessError } from '@kit.BasicServicesKit';
    4. import { image } from '@kit.ImageKit';
    5. import { fileIo } from '@kit.CoreFileKit';
    6. import modelPredict from './model';

    7. @Entry
    8. @Component
    9. struct Index {
    10.   @State modelName: string = 'mobilenetv2.ms';
    11.   @State modelInputHeight: number = 224;
    12.   @State modelInputWidth: number = 224;
    13.   @State uris: Array<string> = [];
    14.   @State max: number = 0;
    15.   @State maxIndex: number = 0;
    16.   @State maxArray: Array<number> = [];
    17.   @State maxIndexArray: Array<number> = [];

    18.   build() {
    19.     Row() {
    20.       Column() {
    21.         Button() {
    22.           Text('photo')
    23.             .fontSize(30)
    24.             .fontWeight(FontWeight.Bold)
    25.         }
    26.         .type(ButtonType.Capsule)
    27.         .margin({
    28.           top: 20
    29.         })
    30.         .backgroundColor('#0D9FFB')
    31.         .width('40%')
    32.         .height('5%')
    33.         .onClick(() => {
    34.           let resMgr = this.getUIContext()?.getHostContext()?.getApplicationContext().resourceManager;
    35.           resMgr?.getRawFileContent(this.modelName).then(modelBuffer => {
    36.             let float32View = new Float32Array(this.modelInputHeight * this.modelInputWidth * 3);
    37.             // 获取相册图片
    38.             // 1.创建图片文件选择实例
    39.             let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();

    40.             // 2.设置选择媒体文件类型为IMAGE,设置选择媒体文件的最大数目
    41.             photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    42.             photoSelectOptions.maxSelectNumber = 1;

    43.             // 3.创建图库选择器实例,调用select()接口拉起图库界面进行文件选择。文件选择成功后,返回photoSelectResult结果集。
    44.             let photoPicker = new photoAccessHelper.PhotoViewPicker();
    45.             photoPicker.select(photoSelectOptions, async (
    46.               err: BusinessError, photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
    47.               if (err) {
    48.                 console.error('MS_LITE_ERR: PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
    49.                 return;
    50.               }
    51.               console.info('MS_LITE_LOG: PhotoViewPicker.select successfully, ' +
    52.                 'photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
    53.               console.info('MS_LITE_LOG: 图库选择器选择成功... ' +
    54.                 'photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
    55.               this.uris = photoSelectResult.photoUris;
    56.               console.info('MS_LITE_LOG: uri: ' + this.uris);

    57.               // 预处理图片数据
    58.               try{
    59.                 // 1.使用fileIo.openSync接口,通过uri打开这个文件得到fd
    60.                 let file = fileIo.openSync(this.uris[0], fileIo.OpenMode.READ_ONLY);
    61.                 console.info('MS_LITE_LOG: file fd: ' + file.fd);
    62.                 console.info('MS_LITE_LOG: 文件描述符 fd:: ' + file.fd);

    63.                 // 2.通过fd使用fileIo.readSync接口读取这个文件内的数据
    64.                 let inputBuffer = new ArrayBuffer(4096000);
    65.                 let readLen = fileIo.readSync(file.fd, inputBuffer);
    66.                 console.info('MS_LITE_LOG: readSync data to file succeed and inputBuffer size is:' + readLen);
    67.                 console.info('MS_LITE_LOG: 读取文件数据成功...and inputBuffer size is:' + readLen);

    68.                 // 3.通过PixelMap预处理
    69.                 let imageSource = image.createImageSource(file.fd);
    70.                 imageSource.createPixelMap().then((pixelMap) => {
    71.                   pixelMap.getImageInfo().then((info) => {
    72.                     console.info('MS_LITE_LOG: info.width = ' + info.size.width);
    73.                     console.info('MS_LITE_LOG: 原始图片宽度 = ' + info.size.width);
    74.                     console.info('MS_LITE_LOG: info.height = ' + info.size.height);
    75.                     // 4.根据模型输入的尺寸,将图片裁剪为对应的size,获取图片buffer数据readBuffer
    76.                     pixelMap.scale(256.0 / info.size.width, 256.0 / info.size.height).then(() => {
    77.                       pixelMap.crop(
    78.                         { x: 16, y: 16, size: { height: this.modelInputHeight, width: this.modelInputWidth } }
    79.                       ).then(async () => {
    80.                         let info = await pixelMap.getImageInfo();
    81.                         console.info('MS_LITE_LOG: crop info.width = ' + info.size.width);
    82.                         console.info('MS_LITE_LOG: 裁剪后图片宽度 = ' + info.size.width);
    83.                         console.info('MS_LITE_LOG: crop info.height = ' + info.size.height);
    84.                         // 需要创建的像素buffer大小
    85.                         let readBuffer = new ArrayBuffer(this.modelInputHeight * this.modelInputWidth * 4);
    86.                         await pixelMap.readPixelsToBuffer(readBuffer);
    87.                         console.info('MS_LITE_LOG: Succeeded in reading image pixel data, buffer: ' +
    88.                         readBuffer.byteLength);
    89.                         console.info('MS_LITE_LOG: 读取图片像素数据成功... buffer: ' +
    90.                         readBuffer.byteLength);
    91.                         // 处理readBuffer,转换成float32格式,并进行标准化处理
    92.                         const imageArr = new Uint8Array(
    93.                           readBuffer.slice(0, this.modelInputHeight * this.modelInputWidth * 4));
    94.                         console.info('MS_LITE_LOG: imageArr length: ' + imageArr.length);
    95.                         console.info('MS_LITE_LOG: 图像数组长度: ' + imageArr.length);
    96.                         let means = [0.485, 0.456, 0.406];
    97.                         let stds = [0.229, 0.224, 0.225];
    98.                         let float32View = new Float32Array(this.modelInputHeight * this.modelInputWidth * 3);
    99.                         let index = 0;
    100.                         for (let i = 0; i < imageArr.length; i++) {
    101.                           if ((i + 1) % 4 == 0) {
    102.                             float32View[index] = (imageArr[i - 3] / 255.0 - means[0]) / stds[0]; // B
    103.                             float32View[index+1] = (imageArr[i - 2] / 255.0 - means[1]) / stds[1]; // G
    104.                             float32View[index+2] = (imageArr[i - 1] / 255.0 - means[2]) / stds[2]; // R
    105.                             index += 3;
    106.                           }
    107.                         }
    108.                         console.info('MS_LITE_LOG: float32View length: ' + float32View.length);
    109.                         console.info('MS_LITE_LOG: 浮点数组长度: ' + float32View.length);
    110.                         let printStr = 'float32View data:';
    111.                         for (let i = 0; i < 20; i++) {
    112.                           printStr += ' ' + float32View[i];
    113.                         }
    114.                         console.info('MS_LITE_LOG: float32View data: ' + printStr);
    115.                         console.info('MS_LITE_LOG: 浮点数组数据: ' + printStr);

    116.                         let inputs: ArrayBuffer[] = [float32View.buffer];
    117.                         // predict
    118.                         modelPredict(modelBuffer.buffer.slice(0), inputs).then(outputs => {
    119.                           console.info('=========MS_LITE_LOG: 模型推理成功=====');
    120.                           // 结果打印
    121.                           for (let i = 0; i < outputs.length; i++) {
    122.                             let out = new Float32Array(outputs[i].getData());
    123.                             let printStr = outputs[i].name + ':';
    124.                             for (let j = 0; j < out.length; j++) {
    125.                               printStr += out[j].toString() + ',';
    126.                             }
    127.                             console.info('MS_LITE_LOG: ' + printStr);
    128.                             // 取分类占比的最大值
    129.                             this.max = 0;
    130.                             this.maxIndex = 0;
    131.                             this.maxArray = [];
    132.                             this.maxIndexArray = [];
    133.                             let newArray = out.filter(value => value !== this.max)
    134.                             for (let n = 0; n < 5; n++) {
    135.                               this.max = out[0];
    136.                               this.maxIndex = 0;
    137.                               for (let m = 0; m < newArray.length; m++) {
    138.                                 if (newArray[m] > this.max) {
    139.                                   this.max = newArray[m];
    140.                                   this.maxIndex = m;
    141.                                 }
    142.                               }
    143.                               this.maxArray.push(Math.round(this.max * 10000))
    144.                               this.maxIndexArray.push(this.maxIndex)
    145.                               // filter数组过滤函数
    146.                               newArray = newArray.filter(value => value !== this.max)
    147.                             }
    148.                             console.info('MS_LITE_LOG: max:最大概率值:' + this.maxArray);
    149.                             console.info('MS_LITE_LOG: maxIndex:对应类别索引:' + this.maxIndexArray);
    150.                           }
    151.                           console.info('=========MS_LITE_LOG END=========');
    152.                         })
    153.                       })
    154.                     })
    155.                   })
    156.                 })
    157.               } catch (err) {
    158.                 console.error('MS_LITE_LOG: uri: open file fd failed.' + err);
    159.               }



    160.             })



    161.           })
    162.         })
    163.       }
    164.       .width('100%')
    165.     }
    166.     .height('100%')
    167.   }
    168. }
    复制代码
    部分日志:
    1. <blockquote>12-14 11:15:57.813   1234-1234     C02b71/MediaLibraryNapi         com.examp...oretuili  I     MediaLibraryNapi:{StartPhotoPickerAsyncCallbackComplete:9943} StartPhotoPickerAsyncCallbackComplete start
    复制代码
    12-14 11:15:58.069   1234-1234     A02102/MS_LITE                  com.examp...oretuili  I     <private>:<private> "ThreadInfo, Num: [<private>], CoreNum: [<private>]"
    12-14 11:15:58.069   1234-1234     A02102/MS_LITE                  com.examp...oretuili  I     <private>:<private> "create kernel thread[<private>]"
    12-14 11:15:58.069   1234-1234     A02102/MS_LITE                  com.examp...oretuili  E     <private>:<private> "bind id is empty"
    12-14 11:15:58.069   1234-1234     A02102/MS_LITE                  com.examp...oretuili  E     <private>:<private> "bind id is empty"
    12-14 11:15:58.153   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5129 apply qos failed, errno = 4
    12-14 11:15:58.157   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5128 apply qos failed, errno = 4
    12-14 11:15:58.163   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5130 apply qos failed, errno = 4
    12-14 11:15:58.198   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5129 apply qos failed, errno = 4
    12-14 11:15:58.205   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5128 apply qos failed, errno = 4
    12-14 11:15:58.211   1234-1234     C01707/CONCUR                   com.examp...oretuili  E     [Interface] task 5130 apply qos failed, errno = 4
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] Has checked a long time gc
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc]  [ HPP OldGC ] 7.3065 (9.5) -> 6.9369 (9) MB, 64.615(+11.874)ms, Memory reach limit
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] IsInBackground: 0; SensitiveStatus: 0; StartupStatus: 0; BundleName: com.example.mindsporetuili; Young: 262144; Old: 262144; TotalCommit9437184
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] /***************** GC Duration statistic: ****************/
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] TotalGC:                 64.615  ms
                                                                                                   Initialize:              0       ms
                                                                                                   Mark:                    0.791   ms
                                                                                                   MarkRoots:               0.017   ms
                                                                                                   ConcurrentMark pause:    11.874  ms
                                                                                                   WaitConcurrentMarkFinish:0.003   ms
                                                                                                   ReMark:                  0.79    ms
                                                                                                   ProcessSharedGCRSetWorkList:0       ms
                                                                                                   Sweep:                   0.898   ms
                                                                                                   ClearNativeObject:       0.185   ms
                                                                                                   Evacuate:                11.133  ms
                                                                                                   UpdateReference:         9.321   ms
                                                                                                   UpdateWeekRef:           0.4     ms
                                                                                                   UpdateRoot:              4.632   ms
                                                                                                   ProceeWorkload:          0       ms
                                                                                                   EvacuateSpace:           1.664   ms
                                                                                                   EvacuateRegion:          1.424   ms
                                                                                                   WaitFinish:              0.031   ms
                                                                                                   Finish:                  0.436   ms
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] /****************** GC Memory statistic: *****************/
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] AllSpaces        used: 7103.38KB     committed:    9216KB
                                                                                                   ActiveSemiSpace  used:     248KB     committed:     256KB
                                                                                                   OldSpace         used: 34.8125KB     committed:     256KB
                                                                                                   HugeObjectSpace  used:     512KB     committed:     512KB
                                                                                                   NonMovableSpace  used:  752.93KB     committed:    2304KB
                                                                                                   MachineCodeSpace used:       0KB     committed:       0KB
                                                                                                   HugeMachineCodeSpace used:       0KB     committed:       0KB
                                                                                                   SnapshotSpace    used:       0KB     committed:       0KB
                                                                                                   AppSpawnSpace    used: 5299.64KB     committed:    5632KB
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] Anno memory usage size:  9       MB
                                                                                                   Native memory usage size:56.5509 MB
                                                                                                   NativeBindingSize:       23339.3 KB
                                                                                                   NativeLimitSize:         146783  KB
                                                                                                   ArrayBufferNativeSize:   23339.1 KB
                                                                                                   RegExpByteCodeNativeSize:0       KB
                                                                                                   ChunkNativeSize:         8       KB
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] Heap alive rate:         0.7302  
    12-14 11:15:58.212   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] /***************** GC summary statistic: *****************/
    12-14 11:15:58.213   1234-1234     C03f00/ArkCompiler              com.examp...oretuili  I     [gc] OldGC occurs count       1      
                                                                                                   OldGC max pause:         64.615  ms
                                                                                                   OldGC min pause:         64.615  ms
                                                                                                   OldGC average pause:     64.615  ms
                                                                                                   Heap average alive rate: 0.7302  
    12-14 11:15:58.217   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     =========MS_LITE_LOG: MS_LITE predict start=====
    12-14 11:15:58.221   1234-1234     C02d06/XCollie                  com.examp...oretuili  I     StartProfileMainThread durationTime: 187 ms, sampleInterval: 150.
    12-14 11:15:58.225   1234-5126     C02d06/XCollie                  com.examp...oretuili  I     Create ThreadSampler.
    12-14 11:15:58.225   1234-5126     C02d06/XCollie                  com.examp...oretuili  E     ThreadSampler has been successfully loaded.
    12-14 11:15:58.258   1234-1234     A02102/MS_LITE                  com.examp...oretuili  I     <private>:<private> "destruct success"
    12-14 11:15:58.303   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     =========MS_LITE_LOG: 模型推理成功=====
    12-14 11:15:58.304   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     MS_LITE_LOG: Default/head-MobileNetV2Head/Sigmoid-op466:0.00005112186408950947,0.000004309365067456383,0.00004761384116136469,0.000012533486369648017,0.000010943564120680094,0.000008747037099965382,0.0008382059168070555,0.003049953142181039,0.0030673022847622633,0.000941587844863534,0.0004201275878585875,0.00008831115701468661,0.0000455311055702623,0.0002820474619511515,0.0003318746166769415,0.00006833303632447496,0.0000033886228720803047,0.000002168247192457784,0.0002320071216672659,0.0000034468046123947715,0.00006323103298200294,0.8157758116722107,0.000009899546057567932,0.00012184563820483163,0.00009498560393694788,0.002491916064172983,0.00012056092236889526,0.00006491437670774758,0.000019718014300451614,0.000020903422409901395,0.00003597176691982895,0.0000028528922939585755,0.0000411565306421835,0.0000053606522669724654,0.0000554296602786053,0.000014628511053160764,0.000005371511633711634,0.0002822793321684003,0.000026457524654688314,0.00023366455570794642,0.0000089445475168759,0.000006585858045582427,0.007318656425923109,0.000009545911780151073,0.000035271594242658466,0.00007079460192471743,0.0013558962382376194,0.00004584292400977574,0.0003712800971698016,0.0033895240630954504,0.00026044846163131297,0.00005401599992183037,0.000005840493486175546,0.000005146782768861158,0.00005270805559121072,0.000001561473254696466,0.000005737342689826619,0.000008662173058837652,0.000277024635579437,0.00020457473874557763,0.000026135407097171992,0.0004394273564685136,0.000024914457753766328,0.000002020957481363439,0.00001056523160514189,0.00019798114954028279,0.0002924944565165788,0.00002427023537165951,0.0000021902853859501192,0.000023515463908552192,0.001690089819021523,0.00011985986202489585,0.00000856361657497473,0.00001169649840448983,0.000002400216317255399,0.00016773374227341264,0.000041139344830298796,0.001231257920153439,0.000002536924966989318,0.00045496426173485816,0.000015181975868472364,0.00006826120807090774,0.000006756614766345592,0.00000108169615486986,0.0005324469530023634,0.000022531949070980772,0.000042892523197224364,0.000015559455277980305,0.00003382137219887227,0.00003163992732879706,0.0021837102249264717,0.000022232425180845894,0.00002684746687009465,0.000016781827071099542,0.00007297813863260671,0.00004307720155338757,0.001505242777056992,0.000008662825166538823,0.000030092882298049517,0.0001393252023262903,0.0000045331935325521044,0.00019925033848267049,0.0000035749264952755766,0.0000308732851408422,0.0001943713432410732,0.000008126637112582102,0.00002300742744409945,0.000006185328402352752,0.00005585127291851677,0.000005680032700183801,0.00005863007027073763,0.000013422420124697965,0.00000717248440196272,0.0000030075830181885976,0.00001978299769689329,0.00025387151981703937,0.0026654864195734262,6.345952101582952e-7,0.000838499516248703,0.000003653091425803723,0.0000030573203275707783,0.0007526954868808389,0.000021249203200568445,0.0002972060174215585,0.0001043263400788419,0.000001564354420224845,0.0009340073447674513,0.00002432071960356552,0.0002345817774767056,0.0002165132318623364,0.0000015381727962449077,0.000010115401892107911,0.00003510913302307017,0.000017817828847910278,0.00013874491560272872,0.00001105432693293551,0.00002386384403507691,0.000008569441888539586,0.0002003520494326949,0.0006750589236617088,0.000005528856490855105,0.0002017947263084352,0.000004186115802440327,0.000004320417701819679,0.0000038015889458620222,0.00007445310620823875,0.00034172795130871236,0.000010586322787276004,0.0000015047394299472217,0.000002821834641508758,0.00008784267265582457,0.000006457891686295625,0.00005276730371406302,0.000022308882762445137,0.000027567646611714736,0.000059168603911530226,0.00004888026887783781,0.00006462494638981298,0.000010467824722582009,0.000060473372286651284,0.00001173504460894037,0.000004094717496627709,0.00003905618723365478,0.00006452285015257075,0.00003854453461826779,0.000011223937690374441,0.0027842477429658175,0.00002910478360718116,0.000020204273823765106,0.000014769430890737567,0.00007954759348649532,0.0011194328544661403,0.0001630424812901765,0.00018976176215801388,0.000056655
    12-14 11:15:58.307   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     MS_LITE_LOG: max:最大概率值:9192,8158,5492,2606,157
    12-14 11:15:58.308   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     MS_LITE_LOG: maxIndex:对应类别索引:292,21,321,399,346
    12-14 11:15:58.308   1234-1234     A03d00/JSAPP                    com.examp...oretuili  I     =========MS_LITE_LOG END=========
    12-14 11:15:58.398   1234-5126     C02d06/XCollie                  com.examp...oretuili  I     Destroy ThreadSampler.



    我尝试过的解决方法和结果
    网上搜索标准ImageNet标签文件找到了1000个类别的列表,但不确定是否与模型完全匹配
    我想要达到的结果
    获取文档中模型对应的标签文件,在运行程序后,识别出来的不是索引号,而是具体的类别


    【运行环境】
    硬件:
    ROM版本:
    DevEvoStudio版本:
    fakename.png
    SDK版本:
    fakename.png
    联想截图_20251214105737.png
    fakename.png
    fakename.png

    您尚未登录,无法参与评论,登录后可以:
    参与开源共建问题交流
    认同或收藏高质量问答
    获取积分成为开源共建先驱

    精彩评论1

    yuyuzururu

    沙发 发表于 2025-12-14 12:01:30
    会不会有什么接口方法我不知道,可以直接显示名字啊

    Copyright   ©2025  OpenHarmony开发者论坛  京ICP备2020036654号-3 | 京公网安备11030102011662号 |技术支持 Discuz!

    返回顶部