android.renderscript.Script Java Examples

The following examples show how to use android.renderscript.Script. 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: Deconvolution.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;

    scriptDeconvolution.set_inputHeight(inputShape[0][1]);
    scriptDeconvolution.set_inputWidth(inputShape[0][2]);
    scriptDeconvolution.set_outputHeight(outputShape[1]);
    scriptDeconvolution.set_outputWidth(outputShape[2]);

    scriptDeconvolution.set_InputData(input.getFeatureMap());
    scriptDeconvolution.set_OutputData(output.getFeatureMap());
    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(0, getOutputChannelAligned() / 4).setY(0, outputShape[1] * outputShape[2]);
    //TODO: support no grouped deconvolution
    scriptDeconvolution.forEach_deconv_dw4(options);
}
 
Example #2
Source File: Flatten.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;
    scriptCFlatten.set_InputData(input.getFeatureMap());
    scriptCFlatten.set_OutputData(output.getFeatureMap());
    Allocation frameAllocation = input.getFeatureMap();
    if(!input.isMatrix2D()){
        output.setFeatureMap(input.getFeatureMap());
        return;
    }
    if (inputShape[0][3] % 4 == 0) {
        Script.LaunchOptions options = new Script.LaunchOptions();
        options.setX(0, getInputChannelAligned() / 4)
                .setY(0, inputShape[0][1] * inputShape[0][2]);
        scriptCFlatten.forEach_compute_vector4(options);
    } else {
        scriptCFlatten.forEach_compute(frameAllocation);
    }
}
 
Example #3
Source File: LRN.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap(){
    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;
    lrnScript.set_in_blob(input.getFeatureMap());
    lrnScript.set_out_blob(output.getFeatureMap());
    Script.LaunchOptions option = new Script.LaunchOptions();
    int channel = outputShape[3];
    int channelAligned = getInputChannelAligned();
    switch (normRegion) {
        case "ACROSS_CHANNELS":
            option.setX(0, channel).setY(0, outputShape[1] * outputShape[2]);
            lrnScript.forEach_cross_channel(option);
            break;
        case "WITHIN_CHANNEL":
            option.setX(0, channelAligned / 4).setY(0, outputShape[1] * outputShape[2]);
            lrnScript.forEach_within_channel(option);
            break;
        default:
            LogUtil.e(TAG, "lrn type illegal:" + normRegion);
            break;
    }
}
 
Example #4
Source File: ROIPooling.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap fm = (FeatureMap) featureMapInput[0];
    Allocation fmInput = fm.getFeatureMap();
    FeatureMap roi = (FeatureMap) featureMapInput[1];
    Allocation roiInput = roi.getFeatureMap();

    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();

    roiPoolScript.set_fmap_blob(fmInput);
    roiPoolScript.set_roi_blob(roiInput);
    roiPoolScript.set_out_blob(outAllocation);
    int roiCount = inputShape[1][0];
    int channel = inputShape[1][3];
    if(channel==256){
        Script.LaunchOptions option = new Script.LaunchOptions();
        option.setX(0, roiCount * pooledH * pooledW);
        roiPoolScript.forEach_compute_channel256(option);
    }
    else {
        roiPoolScript.forEach_compute_vector(outAllocation);
    }
}
 
Example #5
Source File: Pooling.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap input = (FeatureMap) featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();
    scriptPooling.set_in_blob(frameAllocation);
    scriptPooling.set_out_blob(outAllocation);
    int outputChannelAligned = getOutputChannelAligned();
    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(0, outputChannelAligned / 4).setY(0, outputShape[1] * outputShape[2]);

    if(globalPooling){
        options.setX(0, outputChannelAligned / 4).setY(0, 1);
        scriptPooling.forEach_global_pooling_2d(options);
        float[][][][] data = input.getData();
        float[][][][] data2 = output.getData();
        int len = data.length;
    }
    else if(kernelType.equalsIgnoreCase("MAX")) {
        scriptPooling.forEach_max_pooling_2d(options);
    }
    else if(kernelType.equalsIgnoreCase("AVE")){
        scriptPooling.forEach_mean_pooling_2d(options);
    }
}
 
Example #6
Source File: BatchNorm.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap()
{
    FeatureMap input = (FeatureMap)featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();

    if(input.isPad4()) {
        Script.LaunchOptions option = new Script.LaunchOptions();
        option.setX(0, getOutputChannelAligned() / 4).setY(0, outputShape[1] * outputShape[2]);
        scriptBatchNorm.set_InputData(frameAllocation);
        scriptBatchNorm.forEach_compute_vector4(option);
    }
    else {
        scriptBatchNorm.forEach_compute(frameAllocation, frameAllocation);
    }
    featureMapOutput = featureMapInput[0];
}
 
Example #7
Source File: Dropout.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap()
{
    FeatureMap input = (FeatureMap)featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();
    if (!input.isPad4()) {
        Script.LaunchOptions options = new Script.LaunchOptions();
        options.setX(0, getOutputChannelAligned() / 4).setY(0, outputShape[1] * outputShape[2]);
        scriptDropout.set_InputData(frameAllocation);
        scriptDropout.set_OutputData(frameAllocation);
        scriptDropout.forEach_compute_vector4(options);
    } else {
        scriptDropout.forEach_compute(frameAllocation, frameAllocation);
    }

    featureMapOutput = featureMapInput[0];
}
 
Example #8
Source File: InnerProduct.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap(){
    FeatureMap input = (FeatureMap) featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();
    innerProductScript.set_In_Blob(frameAllocation);
    innerProductScript.set_Out_Blob(outAllocation);
    Script.LaunchOptions option = new Script.LaunchOptions();
    if(numOutput % 128==0){
        int thread_group = 1;
        option.setX(0, outputShape[0] * numOutput / 4 / thread_group);
        //option.setY(0, numOutput / 4 / thread_group);
        innerProductScript.set_thread_group(thread_group);
        innerProductScript.forEach_compute_f8fn_1(option);
    }
    else{
        option.setX(0, outputShape[0]);
        option.setY(0, numOutput);
        innerProductScript.forEach_compute_f8f1(option);
    }
}
 
Example #9
Source File: PSROIPooling.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void computeFeatureMap(){
    int count = inputShape[0][0];
    Allocation bottomData = ((FeatureMap)featureMapInput[0]).getFeatureMap();
    Allocation bottomRois = ((FeatureMap)featureMapInput[0]).getFeatureMap();
    Allocation topData = ((FeatureMap)featureMapInput[0]).getFeatureMap();
    psroiPooling.set_bottom_data(bottomData);
    psroiPooling.set_bottom_rois(bottomRois);
    psroiPooling.set_top_data(topData);
    Script.LaunchOptions option = new Script.LaunchOptions();
    option.setX(0, pooledH * pooledW * outputDim);
    psroiPooling.forEach_compute(option);

}
 
Example #10
Source File: Scale.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void computeFeatureMap()
{
    Allocation frameAllocation = ((FeatureMap)featureMapInput[0]).getFeatureMap();
    Script.LaunchOptions option = new Script.LaunchOptions();
    option.setX(0, getOutputChannelAligned() / 4).setY(0, outputShape[1] * outputShape[2]);
    scriptScale.set_InputData(frameAllocation);
    scriptScale.forEach_compute_vector4(option);
    featureMapOutput = featureMapInput[0];
}
 
Example #11
Source File: Softmax.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void computeFeatureMap() {
    int n = inputShape[0][0];
    int h = inputShape[0][1];
    int w = inputShape[0][2];
    int c = inputShape[0][3];

    int channelAligned = getInputChannelAligned();

    FeatureMap input = (FeatureMap) featureMapInput[0];

    Allocation inAllocation = input.getFeatureMap();

    if(inAllocation.getElement().getVectorSize()==4){
        softmaxScript.set_channelAligned(channelAligned);
    }
    else{
        softmaxScript.set_channelAligned(c);
    }

    softmaxScript.set_inBlob(inAllocation);
    Script.LaunchOptions option = new Script.LaunchOptions();
    option.setX(0, n * h * w * c);
    softmaxScript.forEach_compute_exp(option);
    option.setX(0, n * h * w);
    softmaxScript.forEach_compute_exp_sum(option);
    option.setX(0, n * h * w * c);
    softmaxScript.forEach_compute(option);
    featureMapOutput = featureMapInput[0];
}
 
Example #12
Source File: Layer.java    From rscnn with MIT License 4 votes vote down vote up
protected Script.LaunchOptions getLaunchOptionVector4(){
    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(0, getOutputChannelAligned() / 4)
            .setY(0, outputShape[1] * outputShape[2]);
    return options;
}
 
Example #13
Source File: Concat.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void computeFeatureMap() {
    int blockOffset = 0;
    int channelOffset = 0;
    boolean useVector = true;
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();

    int h = outputShape[1];
    int w = outputShape[2];

    scriptConcat.set_out_Blob(outAllocation);

    for(int i = 0; i< featureMapInput.length; i++){
        FeatureMap input = (FeatureMap) featureMapInput[i];
        Allocation frameAllocation = input.getFeatureMap();
        int inChannel = inputShape[i][3];
        int inChannelAligned = inChannel;
        if(inChannel % 4!=0){
            inChannelAligned = inChannel + 4 - inChannel % 4;
            useVector = false;
        }
        int inBlock = inChannelAligned / 4;// channels / 4
        scriptConcat.set_inBlockAligned(inBlock);
        scriptConcat.set_blockOffset(blockOffset);
        scriptConcat.set_inChannel(inChannel);
        scriptConcat.set_inChannelAligned(inChannelAligned);
        scriptConcat.set_channelOffset(channelOffset);
        scriptConcat.set_in_Blob(frameAllocation);
        Script.LaunchOptions option = new Script.LaunchOptions();
        if(useVector) {
            option.setX(0, inBlock).setY(0, h * w);
            scriptConcat.forEach_compute_in4out4(option);
        }
        else{
            option.setX(0, inChannel).setY(0, h * w);
            scriptConcat.forEach_compute(option);
        }
        blockOffset += inBlock;
        channelOffset += inChannel;
    }
}
 
Example #14
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);
        }
    }
}
 
Example #15
Source File: Eltwise.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();
    scriptEltwise.set_OutputData(outAllocation);
    Script.LaunchOptions options = new Script.LaunchOptions();
    int outputChannelAligned = getOutputChannelAligned();
    options.setX(0, outputChannelAligned / 4).setY(0, outputShape[1] * outputShape[2]);

    for(int i = 0; i< featureMapInput.length; i++){
        FeatureMap input = (FeatureMap) featureMapInput[i];
        Allocation frameAllocation = input.getFeatureMap();
        scriptEltwise.set_InputData(frameAllocation);
        switch (operation){
            case "SUM":
                float coeffFactor = 1.f;
                if(coeff!=null && i < coeff.length){
                    coeffFactor = coeff[i];
                }
                if(i==0){
                    scriptEltwise.forEach_set_zero_vector4(options);
                }
                scriptEltwise.set_coeff(coeffFactor);
                scriptEltwise.forEach_compute_sum_vector4(options);
                break;
            case "MAX":
                if(i==0){
                    scriptEltwise.forEach_copy_vector4(options);
                    continue;
                }
                scriptEltwise.forEach_compute_max_vector4(options);
                break;
            case "PROD":
                if(i==0){
                    scriptEltwise.forEach_copy_vector4(options);
                    continue;
                }
                scriptEltwise.forEach_compute_mul_vector4(options);
                break;
            default:
                LogUtil.e("Eltwise", "Eltwise type "+operation+" has not implemented");
                break;
        }
    }
}
 
Example #16
Source File: Healing.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
/**
 * This function only assumes mPointsXY, mPasteOffX, mPasteOffY
 *
 * @param healing
 * @param rs
 * @param image
 */
public void heal_orig(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) {
    long time = System.nanoTime();
    Type.Builder floatImage = new Type.Builder(rs, Element.F32_3(rs));
    floatImage.setX(mRoiBounds.width());
    floatImage.setY(mRoiBounds.height());

    Bitmap maskBitmap = buildMask(mRoiBounds, mPointsXY);

    Allocation dest1 = Allocation.createTyped(rs, floatImage.create());
    Allocation dest2 = Allocation.createTyped(rs, floatImage.create());
    healing.set_dest1(dest1);
    healing.set_dest2(dest2);

    Bitmap destBitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation dest_uc4 = Allocation.createFromBitmap(rs, destBitmap);
    healing.forEach_convert_to_f(dest_uc4, dest1);

    Bitmap src = createMutableBitmap(image, mCutOffsetX, mCutOffsetY,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation src_f3 = Allocation.createTyped(rs, floatImage.create());
    Allocation src_uc4 = Allocation.createFromBitmap(rs, src);
    healing.forEach_convert_to_f(src_uc4, src_f3);
    healing.set_src(src_f3);

    Allocation mask = Allocation.createFromBitmap(rs, maskBitmap);
    healing.set_mask(mask);

    Allocation laplace_f3 = Allocation.createTyped(rs, floatImage.create());
    healing.set_laplace(laplace_f3);

    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(1, mRoiBounds.width() - 1);
    options.setY(1, mRoiBounds.height() - 1);
    healing.forEach_laplacian(laplace_f3, options);
    healing.forEach_copyMasked(mask, dest1);

    int steps = (int) Math.hypot(mRoiBounds.width(), mRoiBounds.height()); // match RS Single source
    Log.v(TAG, "Healing_orig  :steps = " + steps);
    for (int i = 0; i < steps; i++) {
        healing.forEach_solve1(mask, dest2);
        healing.forEach_solve2(mask, dest1);
    }

    healing.forEach_convert_to_uc(dest1, dest_uc4);
    rs.finish();

    healing.forEach_alphaMask(dest_uc4, dest_uc4);
    rs.finish();

    dest_uc4.copyTo(destBitmap);
    rs.finish();
    destBitmap.setHasAlpha(true);
    rs.finish();
    // build the undo
    mUndoBitmap = Bitmap.createBitmap(mRoiBounds.width(), mRoiBounds.height(),
            Bitmap.Config.ARGB_8888);
    Canvas undoCanvas = new Canvas(mUndoBitmap);
    Rect undoRect = new Rect(0, 0, mRoiBounds.width(), mRoiBounds.height());
    undoCanvas.drawBitmap(output, mRoiBounds, undoRect, null);

    Canvas c = new Canvas(output);
    c.drawBitmap(image, 0, 0, null);
    c.drawBitmap(destBitmap, mRoiBounds.left, mRoiBounds.top, null);
    Log.v(TAG, " time to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms");
}