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

The following examples show how to use android.support.v8.renderscript.Allocation#copy2DRangeFrom() . 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: 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 2
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;
}