Java Code Examples for android.renderscript.Allocation#copyFrom()

The following examples show how to use android.renderscript.Allocation#copyFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FeatureMap.java    From rscnn with MIT License 6 votes vote down vote up
private static void copyToAllocation(float[][][][] input, Allocation allocation)
{
    int n = input.length;
    int h = input[0].length;
    int w = input[0][0].length;
    int c = input[0][0][0].length;

    float[] output = new float[n * h * w * c];


    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<h;j++){
            for(int k=0;k<w;k++){
                for(int l=0;l<c;l++){
                    output[count++] = input[i][j][k][l];
                }
            }
        }
    }
    allocation.copyFrom(output);
}
 
Example 2
Source File: FeatureMap.java    From rscnn with MIT License 6 votes vote down vote up
private static void copyToAllocation(float[][] input, Allocation allocation)
{
    int n = input.length;
    int c = input[0].length;

    float[] output = new float[n * c];


    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<c;j++){
            output[count++] = input[i][j];
        }
    }
    allocation.copyFrom(output);
}
 
Example 3
Source File: CameraSource.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    in.copyFrom(nv21);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.setInput(in);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.forEach(out);
    }
    return out;
}
 
Example 4
Source File: FullyConnected.java    From CNNdroid with MIT License 6 votes vote down vote up
void initKernelF4F1(float[] myWeight, float[] myBias)
{
    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    Type kernelType,biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4);
    biasType = Type.createX(myRS, Element.F32(myRS), h_w);


    kernelAllocation = Allocation.createTyped(myRS, kernelType);
    kernelAllocation.copyFrom(myWeight);

    biasAllocation = Allocation.createTyped(myRS, biasType);
    biasAllocation.copyFrom(myBias);

    myScriptF4 = new ScriptC_innerProductInF4OutF1(myRS);


    myScriptF4.set_Bias_Blob(biasAllocation);
    myScriptF4.set_Kernel_Blob(kernelAllocation);
    myScriptF4.set_w_w(w_w);
    myScriptF4.set_c_o(h_w);
}
 
Example 5
Source File: FullyConnected.java    From CNNdroid with MIT License 6 votes vote down vote up
void initKernelF8F1(float[] myWeight, float[] myBias)
{
    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    Type kernelType,biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4);
    biasType = Type.createX(myRS, Element.F32(myRS), h_w);


    kernelAllocation = Allocation.createTyped(myRS, kernelType);
    kernelAllocation.copyFrom(myWeight);

    biasAllocation = Allocation.createTyped(myRS, biasType);
    biasAllocation.copyFrom(myBias);

    myScriptF8 = new ScriptC_innerProductInF8OutF1(myRS);


    myScriptF8.set_Bias_Blob(biasAllocation);
    myScriptF8.set_Kernel_Blob(kernelAllocation);
    myScriptF8.set_w_w(w_w);
    myScriptF8.set_c_o(h_w);
}
 
Example 6
Source File: FeatureMap.java    From rscnn with MIT License 5 votes vote down vote up
private static void copyToAllocationVector4(float[][][][] input, Allocation allocation)
{
    int n = input.length;
    int h = input[0].length;
    int w = input[0][0].length;
    int c = input[0][0][0].length;

    int channelAlign = c;
    int skip = 0;
    if(channelAlign % 4 !=0) {
        channelAlign = c + 4 - c % 4;
        skip = 4 - c % 4;
    }

    float[] output = new float[n * h * w * channelAlign];

    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<h;j++){
            for(int k=0;k<w;k++){
                for(int l=0;l<c;l++){
                    output[count++] = input[i][j][k][l];
                }
                count += skip;
            }
        }
    }
    allocation.copyFrom(output);
}
 
Example 7
Source File: Scale.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void setup()
{
    int channel = outputShape[3];
    int channelAlign = channel;
    if(channel % 4 != 0){
        channelAlign = channel + 4 - channel % 4;
    }

    scriptScale = new ScriptC_Scale(renderScript);

    float[] scaleArray = new float[channelAlign];
    float[] biasArray = new float[channelAlign];

    for(int i=0;i<channel;i++){
        scaleArray[i] = scale[i];
        biasArray[i] = bias[i];
    }

    Allocation scaleAllocation;
    Allocation biasAllocation;
    Type scaleType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4);
    Type biasType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4);

    scaleAllocation = Allocation.createTyped(renderScript, scaleType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    scaleAllocation.copyFrom(scaleArray);

    biasAllocation = Allocation.createTyped(renderScript, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    scriptScale.set_scale(scaleAllocation);
    scriptScale.set_bias(biasAllocation);

}
 
Example 8
Source File: FindRegion.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
Allocation allocFloat2(float[] p, RenderScript rs) {
    Type.Builder builderF32_2 = new Type.Builder(rs, Element.F32_2(rs));
    builderF32_2.setX(p.length / 2);
    Allocation ret = Allocation.createTyped(rs, builderF32_2.create());
    ret.copyFrom(p);
    return ret;
}
 
Example 9
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF8F2(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_8 = c_k;
    if (c_k % 8 != 0)
        c_k_8 = c_k + 8 - c_k % 8;

    int n_k_2 = n_k;
    if (n_k % 2 != 0)
        n_k_2 = n_k + 2 - n_k % 2;


    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_2 * c_k_8 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_2(myRS), n_k_2 / 2);

    float[] kernelMatrix = new float[n_k_2 * h_k * w_k * c_k_8];
    float[] biasArray = new float[n_k_2];
    int delta_n = (n_k_2 - n_k) / group;
    for (int i = 0; i < n_k_2; i++)
        for (int j = 0; j < c_k_8; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0;
                    else if (i >= n_k_2 / group)
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_2; i++) {
        if (((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_2 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }
    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript82 = new ScriptC_convRolledInF8OutF2(myRS);
    myScript82.set_Bias_Blob(biasAllocation);
    myScript82.set_Kernel_Blob(kernelAllocation);
    myScript82.set_n_k(n_k_2);
    myScript82.set_c_k(c_k_8);
    myScript82.set_h_k(h_k);
    myScript82.set_w_k(w_k);
    myScript82.set_pad_x(pad[0]);
    myScript82.set_pad_y(pad[1]);
    myScript82.set_stride_x(stride[0]);
    myScript82.set_stride_y(stride[1]);
    myScript82.set_group(group);
}
 
Example 10
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF8F4(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_8 = c_k;
    if (c_k % 8 != 0)
        c_k_8 = c_k + 8 - c_k % 8;

    int n_k_4 = n_k;
    if (n_k % 4 != 0)
        n_k_4 = n_k + 4 - n_k % 4;


    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 * c_k_8 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 / 4);

    float[] kernelMatrix = new float[n_k_4 * h_k * w_k * c_k_8];
    float[] biasArray = new float[n_k_4];
    int delta_n = (n_k_4 - n_k) / group;
    for (int i = 0; i < n_k_4; i++)
        for (int j = 0; j < c_k_8; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0;
                    else if (i >= n_k_4 / group)
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_4; i++) {
        if (((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_4 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }
    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript84 = new ScriptC_convRolledInF8OutF4(myRS);
    myScript84.set_Bias_Blob(biasAllocation);
    myScript84.set_Kernel_Blob(kernelAllocation);
    myScript84.set_n_k(n_k_4);
    myScript84.set_c_k(c_k_8);
    myScript84.set_h_k(h_k);
    myScript84.set_w_k(w_k);
    myScript84.set_pad_x(pad[0]);
    myScript84.set_pad_y(pad[1]);
    myScript84.set_stride_x(stride[0]);
    myScript84.set_stride_y(stride[1]);
    myScript84.set_group(group);

}
 
Example 11
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF4F8(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_4 = c_k;
    if (c_k % 4 != 0)
        c_k_4 = c_k + 4 - c_k % 4;

    int n_k_8 = n_k;
    if (n_k % 8 != 0)
        n_k_8 = n_k + 8 - n_k % 8;


    Type kernelType, biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 * c_k_4 * h_k * w_k / 4);
    biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 / 4);

    float[] kernelMatrix = new float[n_k_8 * h_k * w_k * c_k_4];
    float[] biasArray = new float[n_k_8];

    int delta_n = (n_k_8 - n_k) / group;
    for (int i = 0; i < n_k_8; i++)
        for (int j = 0; j < c_k_4; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0;
                    else if (i >= n_k_8 / group)
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_8; i++) {
        if (((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_8 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }


    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript48 = new ScriptC_convRolledInF4OutF8(myRS);
    myScript48.set_Bias_Blob(biasAllocation);
    myScript48.set_Kernel_Blob(kernelAllocation);
    myScript48.set_n_k(n_k_8);
    myScript48.set_c_k(c_k_4);
    myScript48.set_h_k(h_k);
    myScript48.set_w_k(w_k);
    myScript48.set_pad_x(pad[0]);
    myScript48.set_pad_y(pad[1]);
    myScript48.set_stride_x(stride[0]);
    myScript48.set_stride_y(stride[1]);
    myScript48.set_group(group);

}
 
Example 12
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF4F4(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_4 = c_k;
    if (c_k % 4 != 0)
        c_k_4 = c_k + 4 - c_k % 4;

    int n_k_4 = n_k;
    if (n_k % 4 != 0)
        n_k_4 = n_k + 4 - n_k % 4;


    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 * c_k_4 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 / 4);

    float[] kernelMatrix = new float[n_k_4 * h_k * w_k * c_k_4];
    float[] biasArray = new float[n_k_4];
    int delta_n = (n_k_4 - n_k) / group;
    for (int i = 0; i < n_k_4; i++)
        for (int j = 0; j < c_k_4; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0;
                    else if (i >= n_k_4 / group)
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_4; i++) {
        if (((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_4 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }
    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript44 = new ScriptC_convRolledInF4OutF4(myRS);
    myScript44.set_Bias_Blob(biasAllocation);
    myScript44.set_Kernel_Blob(kernelAllocation);
    myScript44.set_n_k(n_k_4);
    myScript44.set_c_k(c_k_4);
    myScript44.set_h_k(h_k);
    myScript44.set_w_k(w_k);
    myScript44.set_pad_x(pad[0]);
    myScript44.set_pad_y(pad[1]);
    myScript44.set_stride_x(stride[0]);
    myScript44.set_stride_y(stride[1]);
    myScript44.set_group(group);

}
 
Example 13
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF4F2(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_4 = c_k;
    if (c_k % 4 != 0)
        c_k_4 = c_k + 4 - c_k % 4;

    int n_k_2 = n_k;
    if (n_k % 2 != 0)
        n_k_2 = n_k + 2 - n_k % 2;


    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_2 * c_k_4 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_2(myRS), n_k_2 / 2);

    float[] kernelMatrix = new float[n_k_2 * h_k * w_k * c_k_4];
    float[] biasArray = new float[n_k_2];
    int delta_n = (n_k_2 - n_k) / group;
    for (int i = 0; i < n_k_2; i++)
        for (int j = 0; j < c_k_4; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0;
                    else if (i >= n_k_2 / group)
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_2; i++) {
        if (((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_2 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }
    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript42 = new ScriptC_convRolledInF4OutF2(myRS);
    myScript42.set_Bias_Blob(biasAllocation);
    myScript42.set_Kernel_Blob(kernelAllocation);
    myScript42.set_n_k(n_k_2);
    myScript42.set_c_k(c_k_4);
    myScript42.set_h_k(h_k);
    myScript42.set_w_k(w_k);
    myScript42.set_pad_x(pad[0]);
    myScript42.set_pad_y(pad[1]);
    myScript42.set_stride_x(stride[0]);
    myScript42.set_stride_y(stride[1]);
    myScript42.set_group(group);

}
 
Example 14
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF4F1(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_4 = c_k;
    if (c_k % 4 != 0)
        c_k_4 = c_k + 4 - c_k % 4;

    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k * c_k_4 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32(myRS), n_k);

    float[] kernelMatrix = new float[n_k * h_k * w_k * c_k_4];
    float[] biasArray = new float[n_k];
    int delta_n = (n_k - n_k) / group;
    for (int i = 0; i < n_k; i++)
        for (int j = 0; j < c_k_4; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0;
                    else if (i >= n_k / group)
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k; i++) {
        if (((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }

    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript41 = new ScriptC_convRolledInF4OutF1(myRS);
    myScript41.set_Bias_Blob(biasAllocation);
    myScript41.set_Kernel_Blob(kernelAllocation);
    myScript41.set_n_k(n_k);
    myScript41.set_c_k(c_k_4);
    myScript41.set_h_k(h_k);
    myScript41.set_w_k(w_k);
    myScript41.set_pad_x(pad[0]);
    myScript41.set_pad_y(pad[1]);
    myScript41.set_stride_x(stride[0]);
    myScript41.set_stride_y(stride[1]);
    myScript41.set_group(group);

}
 
Example 15
Source File: FullyConnected.java    From CNNdroid with MIT License 4 votes vote down vote up
private float[][] fullyConnectedLayerInF8OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) {
    // fully connected layer

    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    // Calculate sizes.
    int n_i, c_i;
    n_i = inputBlob4.length;
    c_i = inputBlob4[0].length;

    int c_i_8 = c_i;
    if (c_i % 8 != 0)
        c_i_8 = c_i + 8 - c_i % 8;

    int n_o = n_i;
    int c_o = h_w;

    // Initialize the result.
    float[][] outputBlob = new float[n_o][c_o];

    //initialize Renderscript
    Type inputType, outType;
    Allocation frameAllocation;
    Allocation outAllocation;
    inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_8 / 4);
    outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o);

    frameAllocation = Allocation.createTyped(myRS, inputType);
    outAllocation = Allocation.createTyped(myRS, outType);

    myScriptF8.set_c_i(c_i_8);
    // calculate the result

    float[] frameMatrix = new float[n_i * c_i_8];
    for (int n = 0 ; n < n_i ; n++)
        for (int i = 0 ; i < c_i_8 ; i++)
            if (i < c_i)
                frameMatrix[n * w_w + i] = inputBlob4[n][i];
            else
                frameMatrix[n * w_w + i] = 0;

    frameAllocation.copyFrom(frameMatrix);
    myScriptF8.set_In_Blob(frameAllocation);


    myScriptF8.forEach_root(outAllocation);

    float[] outMatrix = new float[n_o * c_o];
    outAllocation.copyTo(outMatrix);

    for (int n = 0 ; n < n_i ; n++)
        for (int c = 0 ; c < c_o ; c++) {
            outputBlob[n][c] = outMatrix[n * c_o + c];
            if (nonLinear) {
                switch (nonLinearType) {
                    case RectifiedLinearUnit:
                        if (outputBlob[n][c] < 0)
                            outputBlob[n][c] = 0;
                        break;
                }
            }
        }

    frameAllocation.destroy();
    outAllocation.destroy();

    inputType.destroy();
    outType.destroy();

    if (destroy) {
        myScriptF8.destroy();
        myScriptF8 = null;
    }

    // return the result
    return outputBlob;
}
 
Example 16
Source File: FullyConnected.java    From CNNdroid with MIT License 4 votes vote down vote up
private float[][] fullyConnectedLayerInF4OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) {
    // fully connected layer

    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    // Calculate sizes.
    int n_i, c_i;
    n_i = inputBlob4.length;
    c_i = inputBlob4[0].length;

    int c_i_4 = c_i;
    if (c_i % 4 != 0)
        c_i_4 = c_i + 4 - c_i % 4;

    int n_o = n_i;
    int c_o = h_w;

    // Initialize the result.
    float[][] outputBlob = new float[n_o][c_o];

    //initialize Renderscript
    Type inputType, outType;
    Allocation frameAllocation;
    Allocation outAllocation;
    inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_4 / 4);
    outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o);

    frameAllocation = Allocation.createTyped(myRS, inputType);
    outAllocation = Allocation.createTyped(myRS, outType);

    myScriptF4.set_c_i(c_i_4);
    // calculate the result

    float[] frameMatrix = new float[n_i * c_i_4];
    for (int n = 0 ; n < n_i ; n++)
        for (int i = 0 ; i < c_i_4 ; i++)
            if (i < c_i)
                frameMatrix[n * w_w + i] = inputBlob4[n][i];
            else
                frameMatrix[n * w_w + i] = 0;

    frameAllocation.copyFrom(frameMatrix);
    myScriptF4.set_In_Blob(frameAllocation);


    myScriptF4.forEach_root(outAllocation);

    float[] outMatrix = new float[n_o * c_o];
    outAllocation.copyTo(outMatrix);

    for (int n = 0 ; n < n_i ; n++)
        for (int c = 0 ; c < c_o ; c++) {
            outputBlob[n][c] = outMatrix[n * c_o + c];
            if (nonLinear) {
                switch (nonLinearType) {
                    case RectifiedLinearUnit:
                        if (outputBlob[n][c] < 0)
                            outputBlob[n][c] = 0;
                        break;
                }
            }
        }

    frameAllocation.destroy();
    outAllocation.destroy();

    inputType.destroy();
    outType.destroy();

    if (destroy) {
        myScriptF4.destroy();
        myScriptF4 = null;
    }

    // return the result
    return outputBlob;
}
 
Example 17
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF8F8(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_8 = c_k;
    if (c_k % 8 != 0)
        c_k_8 = c_k + 8 - c_k % 8;

    int n_k_8 = n_k;
    if (n_k % 8 != 0)
        n_k_8 = n_k + 8 - n_k % 8;

    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 * c_k_8 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 / 4);


    float[] kernelMatrix = new float[n_k_8 * h_k * w_k * c_k_8];
    float[] biasArray = new float[n_k_8];

    int delta_n = (n_k_8 - n_k) / group;
    for (int i = 0; i < n_k_8; i++)
        for (int j = 0; j < c_k_8; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0;
                    else if (i >= n_k_8 / group)
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_8; i++) {
        if (((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_8 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }


    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript88 = new ScriptC_convRolledInF8OutF8(myRS);
    myScript88.set_Bias_Blob(biasAllocation);
    myScript88.set_Kernel_Blob(kernelAllocation);
    myScript88.set_n_k(n_k_8);
    myScript88.set_c_k(c_k_8);
    myScript88.set_h_k(h_k);
    myScript88.set_w_k(w_k);
    myScript88.set_pad_x(pad[0]);
    myScript88.set_pad_y(pad[1]);
    myScript88.set_stride_x(stride[0]);
    myScript88.set_stride_y(stride[1]);
    myScript88.set_group(group);
}
 
Example 18
Source File: InnerProduct.java    From rscnn with MIT License 4 votes vote down vote up
private void initKernel() {
    int n = outputShape[0];// in=out=200
    int c = outputShape[3];// 512

    int inh = inputShape[0][1];
    int inw = inputShape[0][2];
    int inc = inputShape[0][3];

    int inputChannelAlign = inc;
    if (inc % inputAlign != 0)
        inputChannelAlign = inc + inputAlign - inc % inputAlign;

    Type kernelType, biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(renderScript, Element.F32_4(renderScript), c * inh * inw * inc / 4);
    biasType = Type.createX(renderScript, Element.F32(renderScript), c);

    kernelAllocation = Allocation.createTyped(renderScript, kernelType);
    biasAllocation = Allocation.createTyped(renderScript, biasType);

    if(weightBuffer==null) {
        kernelAllocation.copyFrom(weight);
        biasAllocation.copyFrom(bias);
    }
    else{
        kernelAllocation.copyFromUnchecked(weightBuffer);
        biasAllocation.copyFromUnchecked(biasBuffer);
    }
    innerProductScript = new ScriptC_InnerProduct(renderScript);
    innerProductScript.set_Bias_Blob(biasAllocation);
    innerProductScript.set_Kernel_Blob(kernelAllocation);
    innerProductScript.set_w_w(inc * inh * inw);
    innerProductScript.set_c_o(c);
    innerProductScript.set_c_i(inputChannelAlign * inh * inw);
    if(nextRelu){
        innerProductScript.set_relu(1);
    }
    else{
        innerProductScript.set_relu(0);
    }

    weight = null;
    bias = null;
}
 
Example 19
Source File: BatchNorm.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void setup(){
    int channel = outputShape[3];
    int channelAlign = channel;
    if(channel % 4 != 0){
        channelAlign = channel + 4 - channel % 4;
    }

    float scaleFactor = scale == 0 ? 0 : 1.f / scale;
    for (int i = 0; i < mean.length; i++) {
        mean[i] *= scaleFactor;
        variance[i] *= scaleFactor;
    }

    this.revStandard = new float[mean.length];//compute something for cache
    for (int i = 0; i < mean.length; i++) {
        float std = (float)Math.sqrt(variance[i] + epsilon);
        revStandard[i] = 1.0f / std;
    }
    scriptBatchNorm = new ScriptC_BatchNorm(renderScript);

    float[] meanArray = new float[channelAlign];
    float[] rstdArray = new float[channelAlign];

    for(int i=0;i<channel;i++){
        meanArray[i] = mean[i];
        rstdArray[i] = revStandard[i];
    }

    Allocation meanAllocation;
    Allocation rstdAllocation;
    Type scaleType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4);
    Type biasType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4);

    meanAllocation = Allocation.createTyped(renderScript, scaleType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    meanAllocation.copyFrom(meanArray);

    rstdAllocation = Allocation.createTyped(renderScript, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    rstdAllocation.copyFrom(rstdArray);

    scriptBatchNorm.set_mean_blob(meanAllocation);
    scriptBatchNorm.set_reverse_std_blob(rstdAllocation);
}
 
Example 20
Source File: Deconvolution.java    From rscnn with MIT License 4 votes vote down vote up
private void initKernel() {
    int inputChannel = inputShape[0][3];
    int kernelHeight = kernelH;
    int kernelWidth = kernelW;
    int kernelSize = kernelHeight * kernelWidth;
    int inputChannelAligned = getInputChannelAligned();

    int totalKernelSize = inputChannelAligned * kernelSize;

    Allocation kernelAllocation;
    Allocation biasAllocation;

    Type.Builder kernelType = new Type.Builder(renderScript, Element.F32(renderScript));
    kernelType.setX(inputChannelAligned);
    kernelType.setY(kernelH * kernelW);
    Type biasType = Type.createX(renderScript, Element.F32(renderScript), inputChannelAligned);
    kernelAllocation = Allocation.createTyped(renderScript, kernelType.create(), Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation = Allocation.createTyped(renderScript, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);

    if(weightBuffer==null) {
        float[] kernelMatrix = new float[totalKernelSize];
        float[] biasArray = new float[inputChannelAligned];

        int count = 0;

        for (int j = 0; j < kernelHeight; j++) {
            for (int k = 0; k < kernelWidth; k++) {
                for (int i = 0; i < inputChannelAligned; i++) {
                    if (i >= inputChannel) {
                        kernelMatrix[count++] = 0;
                    } else {
                        kernelMatrix[count++] = weight[i][0][j][k];
                    }
                }
            }
        }

        for (int i = 0; i < inputChannelAligned; i++) {
            if (i >= inputChannel) {
                biasArray[i] = 0;
            } else {
                biasArray[i] = bias[i];
            }
        }

        kernelAllocation.copyFrom(kernelMatrix);
        biasAllocation.copyFrom(biasArray);
    }
    else {
        kernelAllocation.copyFromUnchecked(weightBuffer);
        biasAllocation.copyFromUnchecked(biasBuffer);
    }
    scriptDeconvolution = new ScriptC_Deconvolution(renderScript);
    scriptDeconvolution.set_BiasData(biasAllocation);
    scriptDeconvolution.set_KernelData(kernelAllocation);
    scriptDeconvolution.set_channelAligned(inputChannelAligned);
    scriptDeconvolution.set_padH(padH);
    scriptDeconvolution.set_padW(padW);
    scriptDeconvolution.set_strideH(strideH);
    scriptDeconvolution.set_strideW(strideW);
    scriptDeconvolution.set_kernelH(kernelH);
    scriptDeconvolution.set_kernelW(kernelW);

}