Java Code Examples for android.renderscript.Element#F32

The following examples show how to use android.renderscript.Element#F32 . 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: Layer.java    From rscnn with MIT License 6 votes vote down vote up
protected void allocFeatureMap()
{
    Type.Builder outputType = new Type.Builder(renderScript, Element.F32(renderScript));
    outputType.setZ(outputShape[0]);
    outputType.setY(outputShape[1] * outputShape[2]);
    outputType.setX(getOutputChannelAligned());
    Allocation outAllocation = Allocation.createTyped(renderScript, outputType.create());
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outputShape[0]);
    output.setH(outputShape[1]);
    output.setW(outputShape[2]);
    output.setC(outputShape[3]);
    output.setPad4(true);
    if(this.featureMapOutput!=null){
        ((FeatureMap)featureMapOutput).getFeatureMap().destroy();
    }
    this.featureMapOutput = output;
}
 
Example 2
Source File: Layer.java    From rscnn with MIT License 6 votes vote down vote up
protected void allocFeatureMapNoPad()
{
    Type.Builder outputType = new Type.Builder(renderScript, Element.F32(renderScript));
    outputType.setZ(outputShape[0]);
    outputType.setY(outputShape[1] * outputShape[2]);
    outputType.setX(outputShape[3]);
    Allocation outAllocation = Allocation.createTyped(renderScript, outputType.create());
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outputShape[0]);
    output.setH(outputShape[1]);
    output.setW(outputShape[2]);
    output.setC(outputShape[3]);
    output.setPad4(false);
    if(this.featureMapOutput!=null){
        ((FeatureMap)featureMapOutput).getFeatureMap().destroy();
    }
    this.featureMapOutput = output;
}
 
Example 3
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 4
Source File: Convolution.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void computeFeatureMap() {
    int outputHeight = outputShape[1];
    int outputWidth = outputShape[2];
    int inputChannel = inputShape[0][3];
    int outputChannel = outputShape[3];

    int outputChannelAligned = getOutputChannelAligned();
    int inputChannelAligned = getInputChannelAligned();

    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;

    Allocation inputFeatureMap = input.getFeatureMap();
    Allocation outputFeatureMap = output.getFeatureMap();

    scriptConvolution.set_InputData(inputFeatureMap);
    scriptConvolution.set_OutputData(outputFeatureMap);
    ScriptC.LaunchOptions option = new Script.LaunchOptions();
    boolean useIntrinsicBlas = false;

    if(conv1x1UserIntrinsic && kernelH==1 && kernelW==1){
        useIntrinsicBlas = true;
    }
    if(convnxnUseIntrinsic && kernelH!=1 && kernelW!=1){
        conv1x1UserIntrinsic = true;
    }

    if(useIntrinsicBlas){
        if(kernelH==1 && kernelW==1){
            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, inputFeatureMap, kernelAllocation, 0.f, outputFeatureMap);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
            return;
        }
        else {
            Type.Builder colType = new Type.Builder(renderScript, Element.F32(renderScript));
            colType.setX(kernelH * kernelW * inputChannelAligned).setY(outputHeight * outputWidth);
            Allocation colAllocation = Allocation.createTyped(renderScript, colType.create());
            scriptConvolution.set_ColData(colAllocation);

            option.setX(0, kernelH * kernelW).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_im2col2(option);

            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, colAllocation, kernelAllocation, 0.f, outputFeatureMap);

            colAllocation.destroy();
        }

        if(nextRelu && biasTerm){
            scriptConvolution.forEach_conv_bias_relu(outputFeatureMap, outputFeatureMap);
        }
        else if(biasTerm){
            scriptConvolution.forEach_conv_bias(outputFeatureMap, outputFeatureMap);
        }
        else if(nextRelu){
            scriptConvolution.forEach_conv_relu(outputFeatureMap, outputFeatureMap);
        }
    }
    else {
        if(kernelH==1 && kernelW==1){
            option.setX(0, getOutputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv1x1(option);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
        }
        else {
            int blockSize = 4;
            int[] blockSizeList = {256, 128, 96, 64, 48, 32, 16, 8};
            for (int blk : blockSizeList) {
                if (outputChannelAligned % blk == 0) {
                    blockSize = blk;
                    break;
                }
            }
            scriptConvolution.set_nblock(blockSize);
            option.setX(0, outputChannelAligned / blockSize).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv4n(option);
        }
    }
}