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

The following examples show how to use android.support.v8.renderscript.Allocation#createTyped() . 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: RSGaussian5x5Blur.java    From BlurView with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    radius = Math.min(radius,25);

    Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicConvolve5x5 script = ScriptIntrinsicConvolve5x5.create(rs, Element.U8_4(rs));
    script.setCoefficients(BlurKernels.GAUSSIAN_5x5);
    for (int i = 0; i < radius; i++) {
        script.setInput(input);
        script.forEach(output);
        input = output;
    }
    output.copyTo(bitmapOriginal);
    return bitmapOriginal;
}
 
Example 2
Source File: Convolution2D.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public void loadModel(String path) throws IOException {
    mInputStream = mContext.getAssets().open(path + "/W", AssetManager.ACCESS_BUFFER);
    ByteBuffer bb = readInput(mInputStream);
    FloatBuffer.wrap(W).put(bb.asFloatBuffer());

    // padding for GPU BLAS when necessary.
    int W_height_input = in_channels * ksize * ksize;
    if (padded_Y_blas == W_height_input) {
        // If the input width already satisfies the requirement, just copy to the Allocation.
        W_alloc.copyFrom(W);
    } else {
        // If not, a temp allocation needs to be created.
        Allocation input = Allocation.createTyped(mRS,
                Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
        input.copyFrom(W);
        W_alloc.copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
    }

    mInputStream = mContext.getAssets().open(path + "/b", AssetManager.ACCESS_BUFFER);
    bb = readInput(mInputStream);
    FloatBuffer.wrap(b).put(bb.asFloatBuffer());
    b_alloc.copyFrom(b);

    mInputStream.close();
    Log.v(TAG, "Convolution2D loaded: " + b[0]);
}
 
Example 3
Source File: BlurTransformation.java    From android-tutorials-glide with MIT License 6 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example 4
Source File: BlurTransformation.java    From Beautiful-Photos with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
	// Create another bitmap that will hold the results of the filter.
	Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

	// Allocate memory for Renderscript to work with
	Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT);
	Allocation output = Allocation.createTyped(rs, input.getType());

	// Load up an instance of the specific script that we want to use.
	ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
	script.setInput(input);

	// Set the blur radius
	script.setRadius(RADIUS);

	// Start the ScriptIntrinisicBlur
	script.forEach(output);

	// Copy the output to the blurred bitmap
	output.copyTo(blurredBitmap);

	return blurredBitmap;
}
 
Example 5
Source File: Lut3DParams.java    From easyrs with MIT License 5 votes vote down vote up
public Allocation createAllocation(RenderScript rs) {
    final int sx = xSize;
    final int sy = ySize;
    final int sz = zSize;
    Type.Builder tb = new Type.Builder(rs, Element.U8_4(rs));
    tb.setX(sx);
    tb.setY(sy);
    tb.setZ(sz);
    Type t = tb.create();
    Allocation mCube = Allocation.createTyped(rs, t);
    mCube.copyFromUnchecked(getCube());

    return mCube;
}
 
Example 6
Source File: Lut3DTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsic3DLUT script3dLut = ScriptIntrinsic3DLUT.create(rs, ain.getElement());
    script3dLut.setLUT(SampleParams.Lut3D.swapRedAndBlueCube().createAllocation(rs));
    script3dLut.forEach(ain, aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 7
Source File: RenderScriptBlurHelper.java    From BlurDialogFragment with Apache License 2.0 5 votes vote down vote up
/**
 * blur a given bitmap
 *
 * @param sentBitmap       bitmap to blur
 * @param radius           blur radius
 * @param canReuseInBitmap true if bitmap must be reused without blur
 * @param context          used by RenderScript, can be null if RenderScript disabled
 * @return blurred bitmap
 */
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
    Bitmap bitmap;

    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }

    if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
        // RenderScript hates RGB_565 so we convert it to ARGB_8888
        // (see http://stackoverflow.com/questions/21563299/
        // defect-of-image-with-scriptintrinsicblur-from-support-library)
        bitmap = convertRGB565toARGB888(bitmap);
    }

    try {
        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        return bitmap;
    } catch (RSRuntimeException e) {
        Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
            + "continue with the FastBlur approach.");
    }

    return null;
}
 
Example 8
Source File: Utils.java    From BlurZoomGallery with MIT License 5 votes vote down vote up
public static void blurImage(RenderScript renderScript, Bitmap bmp, float radius) {
    final Allocation input = Allocation.createFromBitmap(renderScript, bmp);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bmp);
}
 
Example 9
Source File: ConvolveTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap5x5(RenderScript rs, Bitmap bmpFromNv21) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsicConvolve5x5 convolve5x5Script = ScriptIntrinsicConvolve5x5.create(rs, ain.getElement());
    convolve5x5Script.setInput(ain);
    convolve5x5Script.setCoefficients(SampleParams.Convolve.Kernels5x5.SOBEL_X);
    convolve5x5Script.forEach(aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 10
Source File: DefaultBlur.java    From ngAndroid with Apache License 2.0 5 votes vote down vote up
private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context){
    RenderScript rs = RenderScript.create(context);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);

    final Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());

    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(blurRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
 
Example 11
Source File: Convolution2DTiled.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public Convolution2DTiled(Context ctx, RenderScript rs, int in_channels, int out_channels, int ksize, int stride, int pad) {
    super(ctx, rs);

    this.in_channels = in_channels;
    this.out_channels = out_channels;
    this.ksize = ksize;
    this.stride = stride;
    this.pad = pad;
    // X dimension for W: in_channels * ksize * ksize
    // Y dimension for W: out_channels
    this.W = new float[out_channels * in_channels * ksize * ksize];
    this.b = new float[out_channels];

    // Pad the width of W to be multiple of 8.
    padded_Y_blas = in_channels * ksize * ksize;
    if (padded_Y_blas % 8 > 0) {
        padded_Y_blas = (padded_Y_blas / 8 + 1) * 8;
    }

    // Create Allocations for W and b.
    W_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), padded_Y_blas, out_channels));
    b_alloc = Allocation.createSized(mRS, Element.F32(mRS), out_channels);

    // Initialize the 2D convolution kernel;
    mConvovle = new ScriptC_convolve2d(mRS);

    // Set the global variables for the RS kernel.
    mConvovle.set_kernel_h(ksize);
    mConvovle.set_kernel_w(ksize);
    mConvovle.set_step_x(stride);
    mConvovle.set_step_y(stride);
    mConvovle.set_pad_h(pad);
    mConvovle.set_pad_w(pad);

    mConvovle.set_beta_alloc(b_alloc);
    mConvovle.set_img_channel(in_channels);
    mConvovle.set_tile_h(TILE_Y);
}
 
Example 12
Source File: ImageHelper.java    From FrostyBackgroundTestApp with Apache License 2.0 5 votes vote down vote up
public static void blurBitmapWithRenderscript(RenderScript rs, Bitmap bitmap2) {
    //this will blur the bitmapOriginal with a radius of 25 and save it in bitmapOriginal
    final Allocation input = Allocation.createFromBitmap(rs, bitmap2); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    // must be >0 and <= 25
    script.setRadius(25f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap2);
}
 
Example 13
Source File: MizLib.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a blurred bitmap. It uses a RenderScript to blur the bitmap very fast.
 * @param context
 * @param originalBitmap
 * @param radius
 * @return
 */
public static Bitmap fastBlur(Context context, Bitmap originalBitmap, int radius) {
    final RenderScript rs = RenderScript.create(context);

    final Allocation input = Allocation.createFromBitmap(rs, originalBitmap);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(originalBitmap);

    return originalBitmap;
}
 
Example 14
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 15
Source File: Nv21Image.java    From easyrs with MIT License 4 votes vote down vote up
/**
 * Converts an android Bitmap image to NV21 format. If the image has odd dimensions the
 * conversion process will round down each dimension to its closest even integer.
 * @param dstArray is an optional byte array to receive the converted NV21 data. It
 *                 must be (1.5 * number_of_pixels) bytes long. If null is passed,
 *                 a new byte array will be created and returned.
 */
public static Nv21Image bitmapToNV21(RenderScript rs, Bitmap bitmap, byte[] dstArray) {
    long startTime = System.currentTimeMillis();

    Bitmap croppedBitmap = bitmap;

    if (bitmap.getWidth() % 2 > 0 || bitmap.getHeight() % 2 > 0) {
        croppedBitmap = Bitmap.createBitmap(bitmap, 0, 0, (bitmap.getWidth() / 2) * 2,
                (bitmap.getHeight() / 2) * 2);
    }
    Bitmap yuvImage = ColorMatrix.applyMatrix(rs, croppedBitmap,
            ColorMatrixParams.rgbToNv21Matrix(), new Float4(0.0f, 0.5f, 0.5f, 0.0f));

    RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, yuvImage);
    ScriptC_channel channelScript = new ScriptC_channel(bitmapRSContext.rs);
    Type outType = Type.createXY(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs),
            yuvImage.getWidth(), yuvImage.getHeight());
    Allocation aout = Allocation.createTyped(bitmapRSContext.rs, outType);
    channelScript.forEach_channelR(bitmapRSContext.ain, aout);
    int size = croppedBitmap.getWidth() * croppedBitmap.getHeight();

    byte[] yByteArray;
    if (dstArray == null)
        yByteArray = new byte[size + size / 2];
    else
        yByteArray = dstArray;
    aout.copyTo(yByteArray);

    Bitmap.Config config = yuvImage.getConfig();
    Bitmap resizedBmp = Bitmap.createBitmap(yuvImage.getWidth()/2, yuvImage.getHeight()/2, config);
    Type resizeoutType = Type.createXY(bitmapRSContext.rs, bitmapRSContext.ain.getElement(),
            yuvImage.getWidth()/2, yuvImage.getHeight()/2);
    Allocation resizeaout = Allocation.createTyped(bitmapRSContext.rs, resizeoutType);
    ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(bitmapRSContext.rs);
    resizeScript.setInput(bitmapRSContext.ain);
    resizeScript.forEach_bicubic(resizeaout);
    resizeaout.copyTo(resizedBmp);

    Allocation resizedIn = Allocation.createFromBitmap(bitmapRSContext.rs, resizedBmp);
    ScriptC_uvencode encodeScript = new ScriptC_uvencode(bitmapRSContext.rs);
    Type uvtype = Type.createX(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs),
            size / 2);
    Allocation uvAllocation = Allocation.createTyped(bitmapRSContext.rs, uvtype);
    encodeScript.set_width(yuvImage.getWidth());
    encodeScript.set_height(yuvImage.getHeight());
    encodeScript.set_gOut(uvAllocation);
    encodeScript.forEach_root(resizedIn);

    byte[] uvByteArray = new byte[size / 2];

    uvAllocation.copyTo(uvByteArray);
    System.arraycopy(uvByteArray, 0, yByteArray, size, uvByteArray.length);

    Log.d("NV21", "Conversion to NV21: " + (System.currentTimeMillis() - startTime) + "ms");
    return new Nv21Image(yByteArray, yuvImage.getWidth(), yuvImage.getHeight());
}
 
Example 16
Source File: ResidualBlockChained.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public void loadModel(String path) throws IOException {
    for (int i = 0; i < mNumBlocks; i++) {
        for (int j = 0; j < 2; j++) {
            // Read all convolution blocks.
            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/c" + (j + 1) + "/W", AssetManager.ACCESS_BUFFER);
            ByteBuffer bb = readInput(mInputStream);
            FloatBuffer.wrap(W).put(bb.asFloatBuffer());

            // padding for GPU BLAS
            int W_height_input = in_channels * ksize * ksize;
            if (padded_Y_blas == W_height_input) {
                // If the input width already satisfies the requirement, just copy to the Allocation.
                W_alloc[i * 2 + j].copyFrom(W);
            } else {
                // If not, a temp allocation needs to be created.
                Allocation input = Allocation.createTyped(mRS,
                        Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
                input.copyFrom(W);
                W_alloc[i * 2 + j].copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
            }

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/c" + (j + 1) + "/b", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(b).put(bb.asFloatBuffer());
            b_alloc[i * 2 + j].copyFrom(b);

            // Read all batch normalization blocks;
            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/gamma", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(gamma).put(bb.asFloatBuffer());
            gamma_alloc[i * 2 + j].copyFrom(gamma);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/beta", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(beta).put(bb.asFloatBuffer());
            beta_alloc[i * 2 + j].copyFrom(beta);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/avg_mean", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(avg_mean).put(bb.asFloatBuffer());
            avg_mean_alloc[i * 2 + j].copyFrom(avg_mean);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/avg_var", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(avg_var).put(bb.asFloatBuffer());
            avg_var_alloc[i * 2 + j].copyFrom(avg_var);

        }

    }
    mInputStream.close();
    Log.v(TAG, "ResidualBlockChained loaded: " + b[0]);
}
 
Example 17
Source File: FastStyleModelTiled.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
private Allocation processImgChunk(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    mImg2Alloc.set_height(height);
    mImg2Alloc.set_weight(width);

    Bitmap outImg = Bitmap.createBitmap(bitmap);
    // RGB bitmap Allocation.
    Allocation imgAlloc = Allocation.createFromBitmap(mRS, bitmap);
    mImg2Alloc.set_img_alloc(imgAlloc);
    // Float input Allocation.
    Allocation result = Allocation.createTyped(mRS, Type.createXY(mRS, Element.F32(mRS), height * width, 3));
    // convert the bitmap to 3 * (h * w) float Allocation;
    mImg2Alloc.forEach_img2alloc(result);

    // Actual computation;
    // 1st Convolution layer.
    result = mConvLayer[0].process(result, height, width);
    // Use ELU for activation.
    mActivation.forEach_elu(result, result);
    // 1st Batch Normalization.
    mBatchNormLayer[0].process(result);

    // 2nd Convolution layer.
    result = mConvLayer[1].process(result, mConvLayer[0].outH, mConvLayer[0].outW);
    mActivation.forEach_elu(result, result);
    // 2nd Batch Normalization.
    mBatchNormLayer[1].process(result);

    // 3rd Convolution layer.
    result = mConvLayer[2].process(result, mConvLayer[1].outH, mConvLayer[1].outW);
    mActivation.forEach_elu(result, result);
    // 3rd Batch Normalization.
    mBatchNormLayer[2].process(result);

    // Process through the entire residual block.
    result = mResidualLayer.process(result, mConvLayer[2].outH, mConvLayer[2].outW);

    // 1st Deconvolution layer.
    result = mDeconvLayer[0].process(result, mResidualLayer.outH, mResidualLayer.outW);
    mActivation.forEach_elu(result, result);
    // 4th Batch Normalization.
    mBatchNormLayer[3].process(result);

    // 2nd Deconvolution layer.
    result = mDeconvLayer[1].process(result, mDeconvLayer[0].outH, mDeconvLayer[0].outW);
    mActivation.forEach_elu(result, result);
    // 5th Batch Normalization.
    mBatchNormLayer[4].process(result);

    // 3rd Deconvolution layer.
    result = mDeconvLayer[2].process(result, mDeconvLayer[1].outH, mDeconvLayer[1].outW);

    // Convert floating point result to RGB image.
    mImg2Alloc.set_nn_alloc(result);
    Allocation outAlloc = Allocation.createFromBitmap(mRS, outImg);
    mImg2Alloc.forEach_alloc2img(outAlloc);
    return outAlloc;
}
 
Example 18
Source File: FastStyleModelTiled.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public Bitmap processImage(Bitmap bitmap) {
    ScriptC_network script = new ScriptC_network(mRS);

    int numElements = 100;
    Element floatElement = Element.I32(mRS);
    Type arrayType = Type.createX(mRS, floatElement,  numElements);
    Allocation inputAlloc = Allocation.createTyped(mRS, arrayType);
    Allocation outputAlloc = Allocation.createTyped(mRS, arrayType);

    script.forEach_mapper(inputAlloc, outputAlloc);

    int[] output = new int[numElements];
    outputAlloc.copyTo(output);
    Log.i(TAG, output[0] + " " + output[99]);


    if (!mLoaded) {
        try {
            loadModel();
        } catch (IOException e) {

        }
    }
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    // Crop the image.
    Bitmap outImgBig = Bitmap.createBitmap(bitmap, (width - MAX_IMG_SIZE) / 2,
            (height - MAX_IMG_SIZE) / 2, MAX_IMG_SIZE, MAX_IMG_SIZE);
    // Process the cropped image through the neural net.
    Allocation outImgBigAlloc = processImgChunk(outImgBig);

    // Blur the output image a bit.
    Allocation blurredAlloc = Allocation.createFromBitmap(mRS, outImgBig);
    ScriptIntrinsicBlur mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mBlur.setInput(outImgBigAlloc);
    mBlur.setRadius(1.5f);
    mBlur.forEach(blurredAlloc);
    blurredAlloc.copyTo(outImgBig);

    // outImgBigAlloc.copyTo(outImgBig);
    ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
    float[] matrix_sharpen =
                    { 0, -1, 0,
                     -1, 5, -1,
                      0, -1, 0};
    convolution.setInput(blurredAlloc);
    convolution.setCoefficients(matrix_sharpen);
    convolution.forEach(outImgBigAlloc);
    outImgBigAlloc.copyTo(outImgBig);

    logBenchmarkResult();
    return outImgBig;
}
 
Example 19
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;
}
 
Example 20
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;
}