Export a neural network trained with MATLAB in other programming languages

前端 未结 5 1608
失恋的感觉
失恋的感觉 2020-12-13 02:44

I trained a neural network using the MATLAB Neural Network Toolbox, and in particular using the command nprtool, which provides a simple GUI to use the toolbox

5条回答
  •  旧巷少年郎
    2020-12-13 03:45

    I tried to implement a simply 2-layer NN in C++ using OpenCV and then exported the weights to Android which worked quiet well. I wrote a small script which generates a header file with the learned weights and this is used in the following code snipped.

    // Map Minimum and Maximum Input Processing Function
    Mat mapminmax_apply(Mat x, Mat settings_gain, Mat settings_xoffset, double settings_ymin){
    
        Mat y;
    
        subtract(x, settings_xoffset, y);
        multiply(y, settings_gain, y);
        add(y, settings_ymin, y);
    
        return y;
    
    
        /* MATLAB CODE
         y = x - settings_xoffset;
         y = y .* settings_gain;
         y = y + settings_ymin;
         */
    }
    
    
    
    
    // Sigmoid Symmetric Transfer Function
    Mat transig_apply(Mat n){
        Mat tempexp;
        exp(-2*n, tempexp);
        Mat transig_apply_result = 2 /(1 + tempexp) - 1;
        return transig_apply_result;
    }
    
    
    // Map Minimum and Maximum Output Reverse-Processing Function
    Mat mapminmax_reverse(Mat y, Mat settings_gain, Mat settings_xoffset, double settings_ymin){
    
        Mat x;
    
        subtract(y, settings_ymin, x);
        divide(x, settings_gain, x);
        add(x, settings_xoffset, x);
    
        return x;
    
    
    /* MATLAB CODE
    function x = mapminmax_reverse(y,settings_gain,settings_xoffset,settings_ymin)
    x = y - settings_ymin;
    x = x ./ settings_gain;
    x = x + settings_xoffset;
    end
    */
    
    }
    
    
    Mat getNNParameter (Mat x1)
    {
    
        // convert double array to MAT
    
        // input 1
        Mat x1_step1_xoffsetM = Mat(1, 48, CV_64FC1, x1_step1_xoffset).t();
        Mat x1_step1_gainM = Mat(1, 48, CV_64FC1, x1_step1_gain).t();
        double x1_step1_ymin = -1;
    
        // Layer 1
        Mat b1M = Mat(1, 25, CV_64FC1, b1).t();
        Mat IW1_1M = Mat(48, 25, CV_64FC1, IW1_1).t();
    
        // Layer 2
        Mat b2M = Mat(1, 48, CV_64FC1, b2).t();
        Mat LW2_1M = Mat(25, 48, CV_64FC1, LW2_1).t();
    
        // input 1
        Mat y1_step1_gainM = Mat(1, 48, CV_64FC1, y1_step1_gain).t();
        Mat y1_step1_xoffsetM = Mat(1, 48, CV_64FC1, y1_step1_xoffset).t();
        double y1_step1_ymin = -1;
    
    
    
        // ===== SIMULATION ========
    
    
        // Input 1
        Mat xp1 = mapminmax_apply(x1, x1_step1_gainM, x1_step1_xoffsetM, x1_step1_ymin);
    
        Mat  temp = b1M + IW1_1M*xp1;
    
        // Layer 1
        Mat a1M = transig_apply(temp);
    
        // Layer 2
        Mat a2M = b2M + LW2_1M*a1M;
    
        // Output 1
        Mat y1M = mapminmax_reverse(a2M, y1_step1_gainM, y1_step1_xoffsetM, y1_step1_ymin);
    
        return y1M;
    }
    

    example for a bias in the header could be this:

    static double b2[1][48] = {
            {-0.19879, 0.78254, -0.87674, -0.5827, -0.017464, 0.13143, -0.74361, 0.4645, 0.25262, 0.54249, -0.22292, -0.35605, -0.42747, 0.044744, -0.14827, -0.27354, 0.77793, -0.4511, 0.059346, 0.29589, -0.65137, -0.51788, 0.38366, -0.030243, -0.57632, 0.76785, -0.36374, 0.19446, 0.10383, -0.57989, -0.82931, 0.15301, -0.89212, -0.17296, -0.16356, 0.18946, -1.0032, 0.48846, -0.78148, 0.66608, 0.14946, 0.1972, -0.93501, 0.42523, -0.37773, -0.068266, -0.27003, 0.1196}};
    

    Now, that Google published Tensorflow, this became obsolete.

提交回复
热议问题