Skip to content

test_dnn_api.c

This is an example code of classification task using DNN.

Usage

$ test_dnn_api MODEL IMAGE

Arguments

  • MODEL: A DNN model file (*.dnn).
  • IMAGE: An image file (*.jpg).

If the DNN is classification model and has appropriate labels, this sample shows the probability the input image matches each label.

Example Result

$ test_dnn_api mobilenet.dnn church224.jpg
SoftNeuro 5.0.45104d9

compile done
image load done
forward done

0000: 0.00000 tench
0001: 0.00000 goldfish
...
0496: 0.00000 Christmas_stocking
0497: 0.41759 church
0498: 0.00002 cinema
...

Sample Code

#include "morapi_core.h"
#include "morapi_image.h"
#include "morapi_image_io.h"
#include "morapi_softneuro.h"
#include <stdio.h>
#include <stdlib.h>


void
usage()
{
    printf("test_dnn_api MODEL IMAGE\n"
           "  [Arguments]\n"
           "    MODEL:  a model data.\n"
           "    IMAGE:  an image data.\n"
        );
}


int
main(int argc, char **argv)
{
    morapi_Result ret = MORAPI_OK;
    morapi_Int32 i, j;
    morapi_Int32 batch_size = 1;
    morapi_Env *env;
    morapi_Dnn *dnn;
    morapi_Tensor *sink_tensor;
    morapi_Image *image;
    morapi_Bool has_label = MORAPI_FALSE;
    morapi_Param *labels;
    const morapi_Char *routine = "cpu/naive";
    const morapi_Char *password = NULL;
    //const morapi_Char *password = "password";  // for password locked dnn file

    if (3 != argc) {
        usage();
        return 0;
    }

    printf("%s\n\n", morapi_Dnn_static_getVersion());

    env = morapi_create_Env(NULL, 0);
    dnn = morapi_create_Dnn(env);

    ret |= morapi_Dnn_initialize(dnn);

    ret |= morapi_Dnn_load(dnn, argv[1], password, 0, MORAPI_TRUE);
    if (MORAPI_OK != ret) {
        printf("read model error.\n");
        goto ERROR;
    }

    ret |= morapi_Dnn_setThreadNum(dnn, 8);

    for (i = 0; i < morapi_Dnn_getNetNum(dnn); ++i) {
        morapi_DnnNet *net = morapi_Dnn_getNet(dnn, i);
        for (j = 0; j < morapi_DnnNet_getLayerNum(net); ++j) {
            morapi_DnnLayer *layer = morapi_DnnNet_getLayer(net, j);
            ret |= morapi_DnnLayer_setRoutineDesc(layer, routine);
        }
    }

    ret |= morapi_Dnn_setBatchSize(dnn, batch_size);

    /* compile */
    ret |= morapi_Dnn_compile(dnn);
    if (MORAPI_OK != ret) {
        printf("compile error.\n");
        goto ERROR;
    }
    printf("compile done\n");

    /* load image */

    image = morapi_create_Image(env);
    ret |= morapi_ImageIO__load(image, MORAPI_IMAGE_FORMAT_RGB8, argv[2]);
    ret |= morapi_Dnn_setInputImage(dnn, image, 0, 0);
    ret |= morapi_destroy_Image(env, image);
    if (MORAPI_OK != ret) {
        printf("image load error.\n");
        goto ERROR;
    }
    printf("image load done\n");

    /* predict. */
    ret |= morapi_Dnn_forward(dnn);
    if (MORAPI_OK != ret) {
        printf("forward error.\n");
        goto ERROR;
    }
    printf("forward done\n\n");

    if ((labels = morapi_Params_find(morapi_Dnn_getOutputAttrs(dnn, 0), "labels"))) {
        has_label = MORAPI_TRUE;
    }

    sink_tensor = morapi_Dnn_getOutputBlob(dnn, 0);
    for(i = 0; i < morapi_Tensor_getNum(sink_tensor); ++i) {
        if (has_label) {
            printf("%04d: %7.5f %s\n", i, morapi_Tensor_getFloat32(sink_tensor)[i], labels->texts[i]);
        } else {
            printf("%04d: %7.5f\n", i, morapi_Tensor_getFloat32(sink_tensor)[i]);
        }
    }

ERROR:
    ret |= morapi_destroy_Dnn(env, dnn);
    ret |= morapi_destroy_Env(env);

    return ret;
}