Quickly build ML Kit custom models to realize image/text classification in specific fields

I. Introduction

How to develop a custom deep learning model at the lowest cost? You can use the custom model service recently launched by Huawei Machine Learning. The size of the model produced by this service is controllable and can be run to the end side with minimal cost. It can be inferred only with a simple interface call, and it mainly supports image classification and text classification to process the specific scene classification defined by itself. Let's take a look at the training and usage methods of the custom model by taking picture classification as an example.

Two, training and use

1. First install HMS Toolkit through Android Studio's Marketplace, and restart Android Studio after installation.

Insert picture description here

2. Complete the migration through the AI ​​Create function

(1) Basic configuration

The AI ​​Create training framework uses MindSpore, and the reasoning framework uses MindSpore Lite. In the Coding Assistant, choose AI> AI Create. To use transfer learning, select "Image" or "Text" and click "Confirm". You need to install the python environment in advance, and then restart the IDE. Select "Image" or "Text" again, click "Confirm", and MindSpore tool will be installed automatically. HMS Toolkit also provides one-click generation of API files for model calls and the function of model calling Demo example projects, which is convenient for developers to quickly verify and call image classification AI models in applications. Before using the image classification transfer learning ability, you need to prepare image resources for training as required. The training images need to be classified according to the images, and appropriate clear images should be placed in each category.

Insert picture description here

(2) Model training

Image classification performs minute-level learning and training on a hundred sheets of data in a specific field (such as cars, animals, etc.), and automatically generates a new model for image classification and recognition. The generated new model can automatically recognize the category of the image. In Coding Assistant, select "AI> AI Create> Image", set the operation type and model deployment location of the image training model, and then click "Confirm". Fixed selection of "New Model" in the Operation type section. Model Deployment Location (model deployment location) always select "Deployment Cloud". Drag or add the classified image folder to the "Please select train image folder", and set the output model file path and training parameters of the generated model. Keep the training parameters at the default values. Iteration count: the number of iterations, the default value is 100. Learning rate: learning rate, the default value is 0.01. Click "Create Model" to start training and generate an image classification and recognition model. After waiting for the model to be generated, check the results of model learning (training accuracy and verification accuracy), corresponding learning parameters, training data and other information.

Insert picture description here

(3) Model verification

After the model training is completed, add the image folder to be tested in the "Please select test image folder" of "Add test image" for model verification. The tool will automatically use the trained model to test and display the test results. Click "Generate Demo" in the model training results, HMS Toolkit will automatically generate a Demo project, which automatically integrates the trained image classification and recognition model, you can run and compile the Demo project directly, and generate APK files in the simulator or real Run on the device to view the application effect of image classification and recognition.

Insert picture description here

3. Use model

(1) Upload model

By categorizing the entity objects in the picture and adding annotation information, such as: people, objects, environment, activities, art forms and other information, help define the subject of the picture and applicable scenarios. Picture classification supports end-side recognition and cloud-side recognition. At the same time, the service provides the ability to preset models. On the Huawei Developer Alliance website, go to "My Projects", select "Machine Learning Service> Custom ML" to enter the model upload interface, and upload the model to the cloud side. In addition, the existing model can also be updated through this interface.

Insert picture description here

(2) Load the remote model

First judge whether the remote model has been downloaded, then load the model, and load the local model when the remote model is not downloaded.

localModel = new MLCustomLocalModel.Factory("localModelName")
        .setAssetPathFile("assetpathname")
        .create();
    remoteModel =new MLCustomRemoteModel.Factory("yourremotemodelname").create();
    MLLocalModelManager.getInstance()
        // 判断远程模型是否存在。
        .isModelExist(remoteModel)
        .addOnSuccessListener(new OnSuccessListener<Boolean>() {
            @Override
            public void onSuccess(Boolean isDownloaded) {
                MLModelExecutorSettings settings;
                // 如果远程模型存在,优先加载本地已有的远程模型,否则加载本地已有的本地模型。
                if (isDownloaded) {
                    settings = new MLModelExecutorSettings.Factory(remoteModel).create();
                } else {
                    settings = new MLModelExecutorSettings.Factory(localModel).create();
                }
                final MLModelExecutor modelExecutor = MLModelExecutor.getInstance(settings);
                executorImpl(modelExecutor, bitmap);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                // 异常处理。
            }
        });

(3) Use model reasoner to reason

Set the input and output format, input the image data to the inference device, and then use the loaded modelExecutor (MLModelExecutor) to perform inference.

private void executorImpl(final MLModelExecutor modelExecutor, Bitmap bitmap){
    // 准备输入数据。
    final Bitmap inputBitmap = Bitmap.createScaledBitmap(srcBitmap, 224, 224, true);
    final float[][][][] input = new float[1][224][224][3];
    for (int i = 0; i < 224; i++) {
        for (int j = 0; j < 224; j++) {
            int pixel = inputBitmap.getPixel(i, j);
            input[batchNum][j][i][0] = (Color.red(pixel) - 127) / 128.0f;
            input[batchNum][j][i][1] = (Color.green(pixel) - 127) / 128.0f;
            input[batchNum][j][i][2] = (Color.blue(pixel) - 127) / 128.0f;
        }
    }
    MLModelInputs inputs = null;
    try {
        inputs = new MLModelInputs.Factory().add(input).create();
        // 若模型需要多路输入,您需要多次调用add()以便图片数据能够一次输入到推理器。
    } catch (MLException e) {
        // 处理输入数据格式化异常。
    }

// 执行推理。您可以通过“addOnSuccessListener”来监听推理成功,在“onSuccess”回调中处理推理成功。同时,可以通过“addOnFailureListener”来监听推理失败,在“onFailure”中处理推理失败。
    modelExecutor.exec(inputs, inOutSettings).addOnSuccessListener(new OnSuccessListener<MLModelOutputs>() {
        @Override
        public void onSuccess(MLModelOutputs mlModelOutputs) {
            float[][] output = mlModelOutputs.getOutput(0);
                // 这里推理的返回结果在output数组里,可以进一步处理。
                }
        }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // 推理异常。
        }
    });
}

Three, summary

In summary, using Huawei's deep learning framework, you can create and use your own business deep learning model in a few simple steps. Is it very efficient? At the same time, Huawei's machine learning service custom model supports all mainstream model inferences including MindSpore, TensorFlow Lite, Caffe, and Onnx. After being converted to MS format, it can be run on the end-side inference framework. In addition, through quantitative compression, you can use a smaller volume to deploy on the end side. If you want to further reduce the APK size, you can also directly host it to the cloud. In this way, even if you don't understand deep learning, you can quickly build AI applications in a specific field.


Original link:
https://developer.huawei.com/consumer/cn/forum/topic/0204424578775070680?fid=18
Author: timer

Guess you like

Origin blog.51cto.com/14772288/2573157