文件最后提交记录最后更新时间
1 个月前
22 天前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
1 个月前
README

基于SqueezeNet模型实现图像分类

介绍

本示例展示了使用CANN API提供的模型推理能力。

本示例展示了对图片中的物体超分的能力展示。

需要编译依赖CANN的动态库libhiai_foundation.so和AI 领域公共动态库libneural_network_core.so。

效果预览

主界面 超分

使用说明:

  1. 在手机的主屏幕,点击“cannDemo”,启动应用。
  2. 点击“超分”,进行加载模型 模型推理对当前图片进行超分,推理完成后会超分放大的图片下方, 卸载模型。
  3. 退出应用时。

工程目录

└── entry/src/main                      // 代码区
    ├── cpp
    │    ├── types/libentry
    │    │     └── index.d.ts           // native层接口注册文件
    │    ├── SuperResolution.cpp         // native api层接口的具体实现函数
    │    ├── CMakeLists.txt             // native层编译配置
    │    ├── HIAIModelManager.cpp       // 模型管理类的实现
    │    ├── HIAIModelManager.h         // 模型管理类的定义
    ├── ets
    │    ├── entryability 
    │    │     └── EntryAbility.ets      // 程序入口类
    │    ├── pages 
    │    │     └── Index.ets            // 主界面展示类
    └── resources
    │    ├── base/media                 // 图片资源
    │    │   ├── 128.png
    │    ├── rawfile 
    │    │    ├── real_esrgan_x4plus.om   // 模型文件

具体实现

点击按钮: hiai.LoadModel(context.resourceManager) 加载模型 hiai.InitIOTensors(rgbData, height1, width1); 图片作为输入数据传入 hiai.RunModel() 推理模型 hiai.GetResult() 获得推理输出数据 hiai.UnloadModel();卸载模型

从API12版本开始,本示例展示的功能使用了在CANN和NNCore中定义的以下API:

  • HiAI_Compatibility HMS_HiAICompatibility_CheckFromBuffer(const void* data, size_t size);
  • OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount);
  • OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name);
  • void *OH_NNTensor_GetDataBuffer(const NN_Tensor *tensor);
  • OH_NN_ReturnCode OH_NNTensor_GetSize(const NN_Tensor *tensor, size_t *size);
  • OH_NN_ReturnCode OH_NNTensor_Destroy(NN_Tensor **tensor);
  • OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelBuffer(const void *modelBuffer, size_t modelSize);
  • void OH_NNCompilation_Destroy(OH_NNCompilation **compilation);
  • OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID);
  • HiAI_AippParam* HMS_HiAIAippParam_Create(uint32_t batchNum);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetInputIndex(HiAI_AippParam * aippParam, uint32_t inputIndex);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetInputAippIndex(HiAI_AippParam * aippParam, uint32_t inputAippIndex);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetInputFormat(HiAI_AippParam * aippParam, HiAI_ImageFormat inputFormat);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetInputShape(HiAI_AippParam * aippParam, uint32_t srcImageW, uint32_t srcImageH);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetResizeConfig(HiAI_AippParam * aippParam, uint32_t batchIndex, uint32_t resizedW, uint32_t resizedH);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcMeanPixel(HiAI_AippParam * aippParam, uint32_t batchIndex, uint32_t meanPixel[], uint32_t channelCount);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcMinPixel(HiAI_AippParam * aippParam, uint32_t batchIndex, float minPixel[], uint32_t channelCount);
  • OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcVarReciPixel(HiAI_AippParam * aippParam, uint32_t batchIndex, float varReciPixel[], uint32_t channelCount);
  • OH_NN_ReturnCode HMS_HiAITensor_SetAippParams(NN_Tensor * tensor, HiAI_AippParam * aippParams[], size_t aippNum);
  • size_t HMS_HiAITensor_GetSizeWithImageFormat(NN_TensorDesc * desc, HiAI_ImageFormat format);
  • OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation);
  • OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation);
  • OH_NN_ReturnCode OH_NNExecutor_GetInputCount(const OH_NNExecutor *executor, size_t *inputCount);
  • NN_TensorDesc *OH_NNExecutor_CreateInputTensorDesc(const OH_NNExecutor *executor, size_t index);
  • NN_Tensor *OH_NNTensor_Create(size_t deviceID, NN_TensorDesc *tensorDesc);
  • NN_Tensor* OH_NNTensor_CreateWithSize(size_t deviceID, NN_TensorDesc *tensorDesc, size_t size);
  • OH_NN_ReturnCode OH_NNTensorDesc_Destroy(NN_TensorDesc **tensorDesc);
  • OH_NN_ReturnCode OH_NNExecutor_GetOutputCount(const OH_NNExecutor *executor, size_t *outputCount);
  • NN_TensorDesc *OH_NNExecutor_CreateOutputTensorDesc(const OH_NNExecutor *executor, size_t index);
  • OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor, NN_Tensor *inputTensor[], size_t inputCount, NN_Tensor *outputTensor[], size_t outputCount);
  • void OH_NNExecutor_Destroy(OH_NNExecutor **executor);
  • void HMS_HiAIAippParam_Destroy(HiAI_AippParam ** aippParam);

调用OH_NNExecutor_RunSync等接口,传入想要超分的图片,得到显示超分后的图片。参考entry/src/main/ets/pages/Index.ets.

具体实现

相关权限

不涉及。

依赖

不涉及。

约束与限制

  1. 本示例仅支持标准系统上运行,支持设备:华为手机、平板、2in1和智慧屏。
  2. HarmonyOS系统:HarmonyOS 5.1.0 Release及以上。
  3. DevEco Studio版本:DevEco Studio 6.1.0 Release及以上。
  4. HarmonyOS SDK版本:HarmonyOS 6.1.0 Release SDK及以上。