Java Code Examples for android.support.v8.renderscript.Allocation#destroy()

The following examples show how to use android.support.v8.renderscript.Allocation#destroy() . 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: RenderscriptBlurringMachine.java    From fogger with Apache License 2.0 6 votes vote down vote up
@Override
protected Bitmap blur(Context context, Bitmap bitmapToBlur, int radius) {
    Log.i(TAG, "Current build version sdk " + Build.VERSION.SDK_INT);
    Bitmap bitmap = bitmapToBlur.copy(bitmapToBlur.getConfig(), true);

    final RenderScript renderScript = RenderScript.create(context, Build.VERSION.SDK_INT);
    final Allocation input = Allocation.createFromBitmap(renderScript, bitmapToBlur,
                                                            Allocation.MipmapControl.MIPMAP_NONE,
                                                            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    try {
        final ScriptIntrinsicBlur script = createBlurringScript(radius, renderScript, input);
        script.forEach(output);
        renderScript.finish();
        output.copyTo(bitmap);
    } finally {
        input.destroy();
        output.destroy();
        bitmapToBlur.recycle();
        renderScript.destroy();
    }
    return bitmap;
}
 
Example 2
Source File: Convolution2D.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public Allocation process(Allocation input, int img_h, int img_w) {
    // Set the input variables to the convolve kernel.
    mConvovle.set_img_h(img_h);
    mConvovle.set_img_w(img_w);
    mConvovle.set_img_alloc(input);

    // Calculate the dimensions of the image after padding.
    int padded_h = img_h + 2 * pad;
    int padded_w = img_w + 2 * pad;

    // Create Allocation to hold the padded image.
    Allocation img_padded = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), padded_h * padded_w, in_channels));
    // Initialize the padded Allocation to zero.
    mConvovle.forEach_zero(img_padded, img_padded);
    mConvovle.set_padded_alloc(img_padded);

    // Invoked the padding kernel.
    mConvovle.invoke_padd();

    // TODO Step2: Use convolve2DGEMM instead.
    Allocation out_alloc = convolve2D(img_padded, img_h, img_w);

    // Destroy the intermediate Allocations.
    img_padded.destroy();

    return out_alloc;
}
 
Example 3
Source File: Blur.java    From AllAngleExpandableButton with Apache License 2.0 5 votes vote down vote up
@WorkerThread
private Bitmap getBlurBitmap(Context context, Bitmap inBitmap, float radius) {
    if (context == null || inBitmap == null) {
        throw new IllegalArgumentException("have not called setParams() before call execute()");
    }

    int width = Math.round(inBitmap.getWidth() * SCALE);
    int height = Math.round(inBitmap.getHeight() * SCALE);

    Bitmap in = Bitmap.createScaledBitmap(inBitmap, width, height, false);
    Bitmap out = Bitmap.createBitmap(in);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allocationIn = Allocation.createFromBitmap(rs, in);
    Allocation allocationOut = Allocation.createFromBitmap(rs, out);

    blurScript.setRadius(radius);
    blurScript.setInput(allocationIn);
    blurScript.forEach(allocationOut);
    allocationOut.copyTo(out);

    allocationIn.destroy();
    allocationOut.destroy();
    blurScript.destroy();
    rs.destroy();

    return out;
}
 
Example 4
Source File: Convolution2D.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
private Allocation convolve2DGEMM(Allocation img_padded, int img_h, int img_w) {
    // Calculate the dimensions of image after convolution.
    int out_h = ConvolveUtil.get_conv_outsize(img_h, ksize, stride, pad);
    int out_w = ConvolveUtil.get_conv_outsize(img_w, ksize, stride, pad);
    Log.v(TAG, "convolve size: " + out_h + " " + out_w);
    // Create the column Allocation.
    Allocation col_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), out_h * out_w, padded_Y_blas));

    long time = System.currentTimeMillis();
    // Invoke im2col kernel, to transform padded image to column image:
    mConvovle.set_outW(out_w);
    mConvovle.set_outH(out_h);
    mConvovle.forEach_im2col(col_alloc);

    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        im2colTime += time;
        Log.v(TAG, "Convolution2D, channels: " + in_channels + ", " + out_channels + " size: " + img_h + ", " + img_w + " im2col process time: " + time);
    }

    // Create the output Allocation for SGEMM operation.
    Allocation out_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), out_h * out_w, out_channels));

    time = System.currentTimeMillis();
    // Conduct the convolution by matrix multiplication, using SGEMM (BLAS API).
    mBlas.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.NO_TRANSPOSE,
            1.0f, W_alloc, col_alloc, 0.0f, out_alloc);

    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        sgemmTime += time;
        Log.v(TAG, "Convolution2D, channels: " + in_channels + ", " + out_channels + " size: " + img_h + ", " + img_w + " SGEMM process time: " + time);
    }

    time = System.currentTimeMillis();
    // Add beta to the results for each channel.
    mConvovle.forEach_addBeta(out_alloc, out_alloc);
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        betaTime += time;
        Log.v(TAG, "Convolution2D, channels: " + in_channels + ", " + out_channels + " size: " + img_h + ", " + img_w + " initBeta process time: " + time);
    }

    // Destroy the intermediate Allocations.
    col_alloc.destroy();

    // Update the output dimensions.
    outH = out_h;
    outW = out_w;

    return out_alloc;
}
 
Example 5
Source File: Convolution2DTiled.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public Allocation process(Allocation input, int img_h, int img_w) {
    // Set the input variables to the convolve kernel.
    mConvovle.set_img_h(img_h);
    mConvovle.set_img_w(img_w);
    mConvovle.set_img_alloc(input);


    // Calculate the dimensions of the image after padding.
    int padded_h = img_h + 2 * pad;
    int padded_w = img_w + 2 * pad;

    // Calculate the dimensions of image after convolution.
    outH = ConvolveUtil.get_conv_outsize(img_h, ksize, stride, pad);
    outW = ConvolveUtil.get_conv_outsize(img_w, ksize, stride, pad);
    // Create the final output Allocation.
    Allocation out_all = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), outH * outW, out_channels));


    // Create Allocation to hold the padded image.
    Allocation img_padded = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), padded_h * padded_w, in_channels));
    // Initialize the padded Allocation to zero.
    mConvovle.forEach_zero(img_padded, img_padded);
    mConvovle.set_padded_alloc(img_padded);

    // Invoked the padding kernel.
    mConvovle.invoke_padd();


    // Tiling in Y dimension
    int out_h_tile = ConvolveUtil.get_conv_outsize(TILE_Y, ksize, stride, pad);
    int out_w_tile = outW;
    Log.v(TAG, "tiled convolve size: " + out_h_tile + " " + out_w_tile);
    // Create the tiled column Allocation.
    Allocation col_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), out_h_tile * out_w_tile, padded_Y_blas));
    // Create the tiled output Allocation.
    Allocation out_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), out_h_tile * out_w_tile, out_channels));

    // Setup the parameters for paralleled im2col
    mConvovle.set_outH(out_h_tile);
    mConvovle.set_outW(out_w_tile);

    long time;

    // The number of tiles, minimum 1.
    int nTiles = img_h / TILE_Y;
    if (nTiles == 0) nTiles = 1;

    // Iterate each tile for 2D convolution and copy to the final output.
    for (int it = 0; it < nTiles; it++) {
        // Set the current tile number;
        mConvovle.set_tile_num(it);
        time = System.currentTimeMillis();

        // Invoke im2col kernel, to transform padded image to column image:
        mConvovle.forEach_im2col(col_alloc);
        if (LOG_TIME) {
            mRS.finish();
            time = System.currentTimeMillis() - time;
            im2colTime += time;
        }

        time = System.currentTimeMillis();

        // Conduct the convolution by matrix multiplication, using SGEMM (BLAS API).
        mBlas.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.NO_TRANSPOSE,
                1.0f, W_alloc, col_alloc, 0.0f, out_alloc);
        if (LOG_TIME) {
            mRS.finish();
            time = System.currentTimeMillis() - time;
            sgemmTime += time;
        }

        // Copy the tiled results to final output.
        out_all.copy2DRangeFrom(it * out_h_tile * out_w_tile, 0, out_h_tile * out_w_tile, out_channels, out_alloc, 0, 0);
    }

    // Destroy the intermediate Allocations.
    img_padded.destroy();
    col_alloc.destroy();
    out_alloc.destroy();

    time = System.currentTimeMillis();

    // Add beta to the results for each channel.
    mConvovle.forEach_addBeta(out_all, out_all);
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        betaTime += time;
    }

    // Return the final output.
    return out_all;
}
 
Example 6
Source File: Deconvolution2DTiled.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public Allocation process(Allocation input, int col_h, int col_w) {
    // Calculate the dimensions of image after deconvolution.
    outH = ConvolveUtil.get_deconv_outsize(col_h, ksize, stride, pad);
    outW = ConvolveUtil.get_deconv_outsize(col_w, ksize, stride, pad);

    // Set the global variables for the RS kernel.
    mConvovle.set_col_w(col_w);
    mConvovle.set_col_channel(out_channels);
    mConvovle.set_img_channel(out_channels);
    mConvovle.set_img_h(outH);
    mConvovle.set_img_w(outW);


    int tiledDimX = TILE_Y * col_w;
    int tiledDimY = in_channels;
    // Create the tiled input Allocation.
    Allocation tiledIn_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), tiledDimX, tiledDimY));

    // Create the tiled output Allocation.
    Allocation tiledOut_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), tiledDimX, padded_Y_blas));
    mConvovle.set_col_alloc(tiledOut_alloc);

    // Create Allocation to hold the padded image.
    int padded_h = outH + 2 * pad;
    int padded_w = outW + 2 * pad;
    Allocation img_padded = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), padded_h * padded_w, out_channels));
    // Initialize the padded Allocation to zero.
    mConvovle.forEach_zero(img_padded, img_padded);
    mConvovle.set_padded_alloc(img_padded);

    // Create final output image Allocation
    Allocation img_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), outH * outW, out_channels));
    mConvovle.set_img_alloc(img_alloc);

    // The number of tiles, minimum 1.
    int nTiles = col_h / TILE_Y;
    if (nTiles == 0) nTiles = 1;

    long time;

    // Iterate each tile for 2D deconvolution and copy to the final output.
    for (int it = 0; it < nTiles; it++) {
        // Set the current tile number;
        mConvovle.set_tile_num(it);

        // Copy data to the tiled input Allocation.
        tiledIn_alloc.copy2DRangeFrom(0, 0, tiledDimX, in_channels, input, it * tiledDimX, 0);
        time = System.currentTimeMillis();

        // Conduct the deconvolution by matrix multiplication, using SGEMM (BLAS API).
        mBlas.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.NO_TRANSPOSE,
                1.0f, W_alloc, tiledIn_alloc, 0.0f, tiledOut_alloc);
        if (LOG_TIME) {
            mRS.finish();
            time = System.currentTimeMillis() - time;
            sgemmTime += time;
        }

        time = System.currentTimeMillis();
        // Invoke col2im kernel, to transform column image to padded image:
        mConvovle.invoke_col2im_tileY();
        if (LOG_TIME) {
            mRS.finish();
            time = System.currentTimeMillis() - time;
            col2imTime += time;
        }

    }

    // Invoked the unpadding kernel.
    mConvovle.invoke_unpadd();

    time = System.currentTimeMillis();
    // Add beta to the results for each channel.
    mConvovle.forEach_addBeta(img_alloc, img_alloc);
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        betaTime += time;
    }

    // Destroy the intermediate Allocations.
    tiledOut_alloc.destroy();
    tiledIn_alloc.destroy();
    img_padded.destroy();

    return img_alloc;
}
 
Example 7
Source File: Deconvolution2D.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public Allocation process(Allocation input, int col_h, int col_w) {
    // Create the output Allocation for SGEMM operation.
    Allocation out_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), input.getType().getX(), W_alloc.getType().getY()));

    long time = System.currentTimeMillis();
    Log.v(TAG, "Deconvolution2D: " + input.getType().getX() + " " + input.getType().getY() + " " + W_alloc.getType().getX() + " " +  W_alloc.getType().getY());
    // Conduct the deconvolution by matrix multiplication, using SGEMM (BLAS API).
    mBlas.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.NO_TRANSPOSE,
            1.0f, W_alloc, input, 0.0f, out_alloc);
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        sgemmTime += time;
        Log.v(TAG, "Deconvolution2D, channels: " + in_channels + ", " + out_channels + " size: " + col_h + ", " + col_w + " SGEMM process time: " + time);
    }

    Log.v(TAG, "Deconvolution2D: SGEMM");
    // Calculate the dimensions of image after deconvolution.
    int img_h = ConvolveUtil.get_deconv_outsize(col_h, ksize, stride, pad);
    int img_w = ConvolveUtil.get_deconv_outsize(col_w, ksize, stride, pad);

    // Set the global input variables for the RS kernel.
    mConvovle.set_col_h(col_h);
    mConvovle.set_col_w(col_w);
    mConvovle.set_col_channel(out_channels);
    mConvovle.set_img_channel(out_channels);
    mConvovle.set_img_h(img_h);
    mConvovle.set_img_w(img_w);
    mConvovle.set_col_alloc(out_alloc);

    // Calculate the dimensions of the padded image.
    int padded_h = img_h + 2 * pad;
    int padded_w = img_w + 2 * pad;
    // Create Allocation to hold the padded image.
    Allocation img_padded = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), padded_h * padded_w, out_channels));
    // Initialize the padded Allocation to zero.
    mConvovle.forEach_zero(img_padded, img_padded);
    mConvovle.set_padded_alloc(img_padded);

    // Create output image Allocation.
    Allocation img_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), img_h * img_w, out_channels));
    mConvovle.set_img_alloc(img_alloc);
    time = System.currentTimeMillis();

    // Invoke col2im kernel, to transform column image to padded image:
    mConvovle.invoke_col2im();
    // mConvovle.set_tile_h(col_h);
    // mConvovle.forEach_col2imPar(img_padded, img_padded);

    // Invoked the unpadding kernel.
    mConvovle.invoke_unpadd();
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        col2imTime += time;
        Log.v(TAG, "Deconvolution2D, channels: " + in_channels + ", " + out_channels + " size: " + col_h + ", " + col_w + " col2im process time: " + time);
    }

    time = System.currentTimeMillis();
    // Add beta to the results for each channel.
    mConvovle.forEach_addBeta(img_alloc, img_alloc);
    if (LOG_TIME) {
        mRS.finish();
        time = System.currentTimeMillis() - time;
        betaTime += time;
        Log.v(TAG, "Deconvolution2D, channels: " + in_channels + ", " + out_channels + " size: " + col_h + ", " + col_w + " addBeta process time: " + time);
    }

    // Destroy the intermediate Allocations.
    out_alloc.destroy();
    img_padded.destroy();

    // Update the output dimensions.
    outH = img_h;
    outW = img_w;

    return img_alloc;
}