Java Code Examples for android.renderscript.Type#createX()

The following examples show how to use android.renderscript.Type#createX() . 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: 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 2
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 3
Source File: Layer.java    From rscnn with MIT License 5 votes vote down vote up
protected void allocFeatureMapNoBlock()
{
    int outNum = outputShape[0];
    int outHeight = outputShape[1];
    int outWidth = outputShape[2];
    int outChannel = outputShape[3];

    if(featureMapOutput!=null){
        FeatureMap old = (FeatureMap)featureMapOutput;
        if(old.getFeatureMap()!=null){
            Allocation out = old.getFeatureMap();
            if(out.getBytesSize()==outNum * outHeight * outWidth * outChannel * 4){
                old.setN(outNum);
                old.setH(outHeight);
                old.setW(outWidth);
                old.setC(outChannel);
                return;
            }
            else{
                out.destroy();
            }
        }
    }

    Type outType = Type.createX(renderScript, Element.F32(renderScript), outNum * outHeight * outWidth * outChannel);
    //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outNum);
    output.setH(outHeight);
    output.setW(outWidth);
    output.setC(outChannel);
    output.setMatrix2D(false);
    featureMapOutput = output;
}
 
Example 4
Source File: Softmax.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void computeOutputShape() {
    outputShape = inputShape[0];
    if(softmaxScript!=null){
        int n = inputShape[0][0];
        int h = inputShape[0][1];
        int w = inputShape[0][2];
        int c = inputShape[0][3];
        Type expSumType = Type.createX(renderScript, Element.F32(renderScript), n * h * w);
        expSumAlloc = Allocation.createTyped(renderScript, expSumType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
        softmaxScript.set_channel(c);
        softmaxScript.set_expSum(expSumAlloc);
    }
}
 
Example 5
Source File: Softmax.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void setup(){

    int n = inputShape[0][0];
    int h = inputShape[0][1];
    int w = inputShape[0][2];
    int c = inputShape[0][3];

    Type expSumType = Type.createX(renderScript, Element.F32(renderScript), n * h * w);
    expSumAlloc = Allocation.createTyped(renderScript, expSumType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    softmaxScript = new ScriptC_Softmax(renderScript);
    softmaxScript.set_channel(c);
    softmaxScript.set_expSum(expSumAlloc);
}
 
Example 6
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 7
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 8
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 9
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 10
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 11
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);

}
 
Example 12
Source File: Layer.java    From rscnn with MIT License 4 votes vote down vote up
protected void allocFeatureMapBlock4()
{
    int outNum = outputShape[0];
    int outHeight = outputShape[1];
    int outWidth = outputShape[2];
    int outChannel = outputShape[3];

    int outChannelAlign = outChannel;
    if(outChannelAlign % 4 !=0) {
        outChannelAlign = outChannel + 4 - (outChannel % 4);
    }

    if(featureMapOutput!=null){
        FeatureMap old = (FeatureMap)featureMapOutput;
        if(old.getFeatureMap()!=null){
            Allocation out = old.getFeatureMap();
            if(out.getBytesSize()==outNum * outHeight * outWidth * outChannelAlign * 4){
                old.setN(outNum);
                old.setH(outHeight);
                old.setW(outWidth);
                old.setC(outChannel);
                return;
            }
            else{
                out.destroy();
            }
        }
    }

    Type outType = Type.createX(renderScript, Element.F32_4(renderScript), outNum * outHeight * outWidth * outChannelAlign / 4);
    //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outNum);
    output.setH(outHeight);
    output.setW(outWidth);
    output.setC(outChannel);
    output.setPad4(true);
    output.setMatrix2D(false);
    featureMapOutput = output;

}
 
Example 13
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 14
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 15
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 16
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 17
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF8F1(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;

    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k * c_k_8 * 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_8];
    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_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 / group - delta_n) && (i < n_k / group)) || (i >= n_k - 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 / 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; 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);

    myScript81 = new ScriptC_convRolledInF8OutF1(myRS);
    myScript81.set_Bias_Blob(biasAllocation);
    myScript81.set_Kernel_Blob(kernelAllocation);
    myScript81.set_n_k(n_k);
    myScript81.set_c_k(c_k_8);
    myScript81.set_h_k(h_k);
    myScript81.set_w_k(w_k);
    myScript81.set_pad_x(pad[0]);
    myScript81.set_pad_y(pad[1]);
    myScript81.set_stride_x(stride[0]);
    myScript81.set_stride_y(stride[1]);
    myScript81.set_group(group);
}
 
Example 18
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 19
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 20
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);
}