android.renderscript.Type Java Examples
The following examples show how to use
android.renderscript.Type.
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: YuvToRgb.java From unity-android-native-camera with MIT License | 6 votes |
private void createAllocations(RenderScript rs) { final int width = mInputSize.getWidth(); final int height = mInputSize.getHeight(); mOutBufferInt = new int[width * height]; Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs)); yuvTypeBuilder.setX(width); yuvTypeBuilder.setY(height); yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888); mInputAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT); Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), width, height); Type intType = Type.createXY(rs, Element.U32(rs), width, height); mOutputAllocation = Allocation.createTyped(rs, rgbType, Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT); mOutputAllocationInt = Allocation.createTyped(rs, intType, Allocation.USAGE_SCRIPT); }
Example #2
Source File: FullyConnected.java From CNNdroid with MIT License | 6 votes |
void initKernelF8F1(float[] myWeight, float[] myBias) { int h_w = myBias.length; int w_w = myWeight.length / h_w; Type kernelType,biasType; Allocation kernelAllocation; Allocation biasAllocation; kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4); biasType = Type.createX(myRS, Element.F32(myRS), h_w); kernelAllocation = Allocation.createTyped(myRS, kernelType); kernelAllocation.copyFrom(myWeight); biasAllocation = Allocation.createTyped(myRS, biasType); biasAllocation.copyFrom(myBias); myScriptF8 = new ScriptC_innerProductInF8OutF1(myRS); myScriptF8.set_Bias_Blob(biasAllocation); myScriptF8.set_Kernel_Blob(kernelAllocation); myScriptF8.set_w_w(w_w); myScriptF8.set_c_o(h_w); }
Example #3
Source File: Layer.java From rscnn with MIT License | 6 votes |
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 #4
Source File: Layer.java From rscnn with MIT License | 6 votes |
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 #5
Source File: FullyConnected.java From CNNdroid with MIT License | 6 votes |
void initKernelF4F1(float[] myWeight, float[] myBias) { int h_w = myBias.length; int w_w = myWeight.length / h_w; Type kernelType,biasType; Allocation kernelAllocation; Allocation biasAllocation; kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4); biasType = Type.createX(myRS, Element.F32(myRS), h_w); kernelAllocation = Allocation.createTyped(myRS, kernelType); kernelAllocation.copyFrom(myWeight); biasAllocation = Allocation.createTyped(myRS, biasType); biasAllocation.copyFrom(myBias); myScriptF4 = new ScriptC_innerProductInF4OutF1(myRS); myScriptF4.set_Bias_Blob(biasAllocation); myScriptF4.set_Kernel_Blob(kernelAllocation); myScriptF4.set_w_w(w_w); myScriptF4.set_c_o(h_w); }
Example #6
Source File: STUtils.java From Fatigue-Detection with MIT License | 6 votes |
@SuppressLint("NewApi") public static Bitmap NV21ToRGBABitmap(byte []nv21, int width, int height, Context context) { TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap"); Rect rect = new Rect(0, 0, width, height); try { Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV"); Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB"); byte[] imageData = nv21; if (mRS == null) { mRS = RenderScript.create(context); mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS)); Type.Builder tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); tb.setX(width); tb.setY(height); tb.setMipmaps(false); tb.setYuvFormat(ImageFormat.NV21); ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); timings.addSplit("Prepare for ain"); Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS)); tb2.setX(width); tb2.setY(height); tb2.setMipmaps(false); aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED); timings.addSplit("Prepare for aOut"); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); timings.addSplit("Create Bitmap"); } ain.copyFrom(imageData); timings.addSplit("ain copyFrom"); mYuvToRgb.setInput(ain); timings.addSplit("setInput ain"); mYuvToRgb.forEach(aOut); timings.addSplit("NV21 to ARGB forEach"); aOut.copyTo(bitmap); timings.addSplit("Allocation to Bitmap"); } catch (Exception e) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); timings.addSplit("NV21 bytes to YuvImage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(rect, 90, baos); byte[] cur = baos.toByteArray(); timings.addSplit("YuvImage crop and compress to Jpeg Bytes"); bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length); timings.addSplit("Jpeg Bytes to Bitmap"); } timings.dumpToLog(); return bitmap; }
Example #7
Source File: TflitePlugin.java From flutter_tflite with MIT License | 6 votes |
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) { // https://stackoverflow.com/a/36409748 RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length); Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); in.copyFrom(nv21); yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out); return out; }
Example #8
Source File: CameraSource.java From Machine-Learning-Projects-for-Mobile-Applications with MIT License | 6 votes |
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) { RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); } Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length); Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); in.copyFrom(nv21); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { yuvToRgbIntrinsic.setInput(in); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { yuvToRgbIntrinsic.forEach(out); } return out; }
Example #9
Source File: ViewfinderProcessor.java From android-HdrViewfinder with Apache License 2.0 | 5 votes |
public ViewfinderProcessor(RenderScript rs, Size dimensions) { Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs)); yuvTypeBuilder.setX(dimensions.getWidth()); yuvTypeBuilder.setY(dimensions.getHeight()); yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888); mInputHdrAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT); mInputNormalAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT); Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs)); rgbTypeBuilder.setX(dimensions.getWidth()); rgbTypeBuilder.setY(dimensions.getHeight()); mPrevAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT); mOutputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(), Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT); HandlerThread processingThread = new HandlerThread("ViewfinderProcessor"); processingThread.start(); mProcessingHandler = new Handler(processingThread.getLooper()); mHdrMergeScript = new ScriptC_hdr_merge(rs); mHdrMergeScript.set_gPrevFrame(mPrevAllocation); mHdrTask = new ProcessingTask(mInputHdrAllocation, dimensions.getWidth()/2, true); mNormalTask = new ProcessingTask(mInputNormalAllocation, 0, false); setRenderMode(MODE_NORMAL); }
Example #10
Source File: FindRegion.java From style-transfer with Apache License 2.0 | 5 votes |
Allocation allocFloat2(float[] p, RenderScript rs) { Type.Builder builderF32_2 = new Type.Builder(rs, Element.F32_2(rs)); builderF32_2.setX(p.length / 2); Allocation ret = Allocation.createTyped(rs, builderF32_2.create()); ret.copyFrom(p); return ret; }
Example #11
Source File: Healing.java From style-transfer with Apache License 2.0 | 5 votes |
/** * This function only assumes mPointsXY, mPasteOffX, mPasteOffY * * @param healing * @param rs * @param image */ public void heal(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 mask_bitmap = buildMask(mRoiBounds, mPointsXY); Bitmap dest_bitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top, mRoiBounds.width(), mRoiBounds.height()); Allocation dest_alloc = Allocation.createFromBitmap(rs, dest_bitmap); Bitmap src_bitmap = createMutableBitmap(image, mCutOffsetX, mCutOffsetY, mRoiBounds.width(), mRoiBounds.height()); Allocation src_alloc = Allocation.createFromBitmap(rs, src_bitmap); Allocation mask_alloc = Allocation.createFromBitmap(rs, mask_bitmap); healing.invoke_heal(mask_alloc, src_alloc, dest_alloc); dest_alloc.copyTo(dest_bitmap); dest_bitmap.setHasAlpha(true); // 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(dest_bitmap, mRoiBounds.left, mRoiBounds.top, null); Log.v(TAG, " time ss to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms"); heal_orig(healing, rs, image, output); }
Example #12
Source File: Softmax.java From rscnn with MIT License | 5 votes |
@Override public void setup(){ int n = inputShape[0][0]; int h = inputShape[0][1]; int w = inputShape[0][2]; int c = inputShape[0][3]; Type expSumType = Type.createX(renderScript, Element.F32(renderScript), n * h * w); expSumAlloc = Allocation.createTyped(renderScript, expSumType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); softmaxScript = new ScriptC_Softmax(renderScript); softmaxScript.set_channel(c); softmaxScript.set_expSum(expSumAlloc); }
Example #13
Source File: Scale.java From rscnn with MIT License | 5 votes |
@Override public void setup() { int channel = outputShape[3]; int channelAlign = channel; if(channel % 4 != 0){ channelAlign = channel + 4 - channel % 4; } scriptScale = new ScriptC_Scale(renderScript); float[] scaleArray = new float[channelAlign]; float[] biasArray = new float[channelAlign]; for(int i=0;i<channel;i++){ scaleArray[i] = scale[i]; biasArray[i] = bias[i]; } Allocation scaleAllocation; Allocation biasAllocation; Type scaleType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4); Type biasType = Type.createX(renderScript, Element.F32_4(renderScript), channelAlign / 4); scaleAllocation = Allocation.createTyped(renderScript, scaleType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); scaleAllocation.copyFrom(scaleArray); biasAllocation = Allocation.createTyped(renderScript, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); scriptScale.set_scale(scaleAllocation); scriptScale.set_bias(biasAllocation); }
Example #14
Source File: Layer.java From rscnn with MIT License | 5 votes |
protected void allocFeatureMapNoBlock() { int outNum = outputShape[0]; int outHeight = outputShape[1]; int outWidth = outputShape[2]; int outChannel = outputShape[3]; if(featureMapOutput!=null){ FeatureMap old = (FeatureMap)featureMapOutput; if(old.getFeatureMap()!=null){ Allocation out = old.getFeatureMap(); if(out.getBytesSize()==outNum * outHeight * outWidth * outChannel * 4){ old.setN(outNum); old.setH(outHeight); old.setW(outWidth); old.setC(outChannel); return; } else{ out.destroy(); } } } Type outType = Type.createX(renderScript, Element.F32(renderScript), outNum * outHeight * outWidth * outChannel); //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); FeatureMap output = new FeatureMap(); output.setFeatureMap(outAllocation); output.setN(outNum); output.setH(outHeight); output.setW(outWidth); output.setC(outChannel); output.setMatrix2D(false); featureMapOutput = output; }
Example #15
Source File: Softmax.java From rscnn with MIT License | 5 votes |
@Override public void computeOutputShape() { outputShape = inputShape[0]; if(softmaxScript!=null){ int n = inputShape[0][0]; int h = inputShape[0][1]; int w = inputShape[0][2]; int c = inputShape[0][3]; Type expSumType = Type.createX(renderScript, Element.F32(renderScript), n * h * w); expSumAlloc = Allocation.createTyped(renderScript, expSumType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); softmaxScript.set_channel(c); softmaxScript.set_expSum(expSumAlloc); } }
Example #16
Source File: STUtils.java From TikTok with Apache License 2.0 | 4 votes |
@SuppressLint("NewApi") public static Bitmap NV21ToRGBABitmap(byte []nv21, int width, int height, Context context) { TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap"); Rect rect = new Rect(0, 0, width, height); try { Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV"); Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB"); byte[] imageData = nv21; if (mRS == null) { mRS = RenderScript.create(context); mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS)); Type.Builder tb = new Type.Builder(mRS, Element .createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); tb.setX(width); tb.setY(height); tb.setMipmaps(false); tb.setYuvFormat(ImageFormat.NV21); ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); timings.addSplit("Prepare for ain"); Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS)); tb2.setX(width); tb2.setY(height); tb2.setMipmaps(false); aOut = Allocation .createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED); timings.addSplit("Prepare for aOut"); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); timings.addSplit("Create Bitmap"); } ain.copyFrom(imageData); timings.addSplit("ain copyFrom"); mYuvToRgb.setInput(ain); timings.addSplit("setInput ain"); mYuvToRgb.forEach(aOut); timings.addSplit("NV21 to ARGB forEach"); aOut.copyTo(bitmap); timings.addSplit("Allocation to Bitmap"); } catch (Exception e) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); timings.addSplit("NV21 bytes to YuvImage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(rect, 90, baos); byte[] cur = baos.toByteArray(); timings.addSplit("YuvImage crop and compress to Jpeg Bytes"); bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length); timings.addSplit("Jpeg Bytes to Bitmap"); } timings.dumpToLog(); return bitmap; }
Example #17
Source File: Camera.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
/** * <p>Create a {@link android.renderscript RenderScript} * {@link android.renderscript.Allocation Allocation} to use as a * destination of preview callback frames. Use * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use * the created Allocation as a destination for camera preview frames.</p> * * <p>The Allocation will be created with a YUV type, and its contents must * be accessed within Renderscript with the {@code rsGetElementAtYuv_*} * accessor methods. Its size will be based on the current * {@link Parameters#getPreviewSize preview size} configured for this * camera.</p> * * @param rs the RenderScript context for this Allocation. * @param usage additional usage flags to set for the Allocation. The usage * flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always * be set on the created Allocation, but additional flags may be provided * here. * @return a new YUV-type Allocation with dimensions equal to the current * preview size. * @throws RSIllegalArgumentException if the usage flags are not compatible * with an YUV Allocation. * @see #setPreviewCallbackAllocation * @hide */ public final Allocation createPreviewAllocation(RenderScript rs, int usage) throws RSIllegalArgumentException { Parameters p = getParameters(); Size previewSize = p.getPreviewSize(); Type.Builder yuvBuilder = new Type.Builder(rs, Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); // Use YV12 for wide compatibility. Changing this requires also // adjusting camera service's format selection. yuvBuilder.setYuvFormat(ImageFormat.YV12); yuvBuilder.setX(previewSize.width); yuvBuilder.setY(previewSize.height); Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), usage | Allocation.USAGE_IO_INPUT); return a; }
Example #18
Source File: Filter.java From OnionCamera with MIT License | 4 votes |
public void reset(int width, int height) { if (mAllocationOut != null) { mAllocationOut.destroy(); } mWidth = width; mHeight = height; mSize = width * height; Type.Builder tb; tb = new Type.Builder(mRS, Element.U8(mRS)).setX(mWidth).setY(mHeight); mAllocationIn = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); tb = new Type.Builder(mRS, Element.F32(mRS)).setX(mWidth).setY(mHeight); mAllocationBlurred = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); mAllocationMagnitude = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); tb = new Type.Builder(mRS, Element.I32(mRS)).setX(mWidth).setY(mHeight); mAllocationDirection = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); mAllocationEdge = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); tb = new Type.Builder(mRS, Element.I32(mRS)).setX(256); mAllocationHistogram = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); tb = new Type.Builder(mRS, Element.RGBA_8888(mRS)).setX(mWidth).setY(mHeight); mAllocationOut = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT); setupSurface(); mHistogram.setOutput(mAllocationHistogram); mEffects.invoke_set_histogram(mAllocationHistogram); mEffects.invoke_set_blur_input(mAllocationIn); mEffects.invoke_set_compute_gradient_input(mAllocationBlurred); mEffects.invoke_set_suppress_input(mAllocationMagnitude, mAllocationDirection); mEffects.invoke_set_hysteresis_input(mAllocationEdge); mEffects.invoke_set_thresholds(0.2f, 0.6f); sc = new LaunchOptions(); sc.setX(2, mWidth - 3); sc.setY(2, mHeight - 3); histo = new int[256]; }
Example #19
Source File: ResizeBitmapHelper.java From Hentoid with Apache License 2.0 | 4 votes |
static Bitmap resizeNice(@NonNull final RenderScript rs, final Bitmap src, float xScale, float yScale) { // Calculate gaussian's radius float sigma = (1 / xScale) / (float) Math.PI; // https://android.googlesource.com/platform/frameworks/rs/+/master/cpu_ref/rsCpuIntrinsicBlur.cpp float radius = 2.5f * sigma/* - 1.5f*/; // Works better that way radius = Math.min(25, Math.max(0.0001f, radius)); Timber.d(">> using sigma=%s for xScale=%s => radius=%s", sigma, xScale, radius); // Defensive programming in case the threading/view recycling recycles a bitmap just before that methods is reached if (null == src || src.isRecycled()) return src; Bitmap.Config bitmapConfig = src.getConfig(); int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); int dstWidth = Math.round(srcWidth * xScale); int dstHeight = Math.round(srcHeight * yScale); src.setHasAlpha(false); // Gaussian filter Allocation tmpIn = Allocation.createFromBitmap(rs, src); Allocation tmpFiltered = Allocation.createTyped(rs, tmpIn.getType()); ScriptIntrinsicBlur blurInstrinsic = ScriptIntrinsicBlur.create(rs, tmpIn.getElement()); blurInstrinsic.setRadius(radius); blurInstrinsic.setInput(tmpIn); blurInstrinsic.forEach(tmpFiltered); src.recycle(); tmpIn.destroy(); blurInstrinsic.destroy(); // Resize Bitmap dst = Bitmap.createBitmap(dstWidth, dstHeight, bitmapConfig); Type t = Type.createXY(rs, tmpFiltered.getElement(), dstWidth, dstHeight); Allocation tmpOut = Allocation.createTyped(rs, t); ScriptIntrinsicResize resizeIntrinsic = ScriptIntrinsicResize.create(rs); resizeIntrinsic.setInput(tmpFiltered); resizeIntrinsic.forEach_bicubic(tmpOut); tmpOut.copyTo(dst); tmpFiltered.destroy(); resizeIntrinsic.destroy(); tmpOut.destroy(); /* // Additional sharpen script just in case (WIP) Allocation tmpSharpOut = Allocation.createTyped(rs, t); //ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, tmpOut.getElement()); ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs)); sharpen.setCoefficients(getSharpenCoefficients()); sharpen.setInput(tmpOut); sharpen.forEach(tmpSharpOut); tmpSharpOut.copyTo(dst); tmpOut.destroy(); tmpSharpOut.destroy(); sharpen.destroy(); */ return dst; }
Example #20
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF8F8(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_8 = c_k; if (c_k % 8 != 0) c_k_8 = c_k + 8 - c_k % 8; int n_k_8 = n_k; if (n_k % 8 != 0) n_k_8 = n_k + 8 - n_k % 8; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 * c_k_8 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 / 4); float[] kernelMatrix = new float[n_k_8 * h_k * w_k * c_k_8]; float[] biasArray = new float[n_k_8]; int delta_n = (n_k_8 - n_k) / group; for (int i = 0; i < n_k_8; i++) for (int j = 0; j < c_k_8; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0; else if (i >= n_k_8 / group) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_8; i++) { if (((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n)) biasArray[i] = 0; else if (i >= n_k_8 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript88 = new ScriptC_convRolledInF8OutF8(myRS); myScript88.set_Bias_Blob(biasAllocation); myScript88.set_Kernel_Blob(kernelAllocation); myScript88.set_n_k(n_k_8); myScript88.set_c_k(c_k_8); myScript88.set_h_k(h_k); myScript88.set_w_k(w_k); myScript88.set_pad_x(pad[0]); myScript88.set_pad_y(pad[1]); myScript88.set_stride_x(stride[0]); myScript88.set_stride_y(stride[1]); myScript88.set_group(group); }
Example #21
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF8F4(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_8 = c_k; if (c_k % 8 != 0) c_k_8 = c_k + 8 - c_k % 8; int n_k_4 = n_k; if (n_k % 4 != 0) n_k_4 = n_k + 4 - n_k % 4; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 * c_k_8 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 / 4); float[] kernelMatrix = new float[n_k_4 * h_k * w_k * c_k_8]; float[] biasArray = new float[n_k_4]; int delta_n = (n_k_4 - n_k) / group; for (int i = 0; i < n_k_4; i++) for (int j = 0; j < c_k_8; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0; else if (i >= n_k_4 / group) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_4; i++) { if (((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n)) biasArray[i] = 0; else if (i >= n_k_4 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript84 = new ScriptC_convRolledInF8OutF4(myRS); myScript84.set_Bias_Blob(biasAllocation); myScript84.set_Kernel_Blob(kernelAllocation); myScript84.set_n_k(n_k_4); myScript84.set_c_k(c_k_8); myScript84.set_h_k(h_k); myScript84.set_w_k(w_k); myScript84.set_pad_x(pad[0]); myScript84.set_pad_y(pad[1]); myScript84.set_stride_x(stride[0]); myScript84.set_stride_y(stride[1]); myScript84.set_group(group); }
Example #22
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF8F2(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_8 = c_k; if (c_k % 8 != 0) c_k_8 = c_k + 8 - c_k % 8; int n_k_2 = n_k; if (n_k % 2 != 0) n_k_2 = n_k + 2 - n_k % 2; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_2 * c_k_8 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32_2(myRS), n_k_2 / 2); float[] kernelMatrix = new float[n_k_2 * h_k * w_k * c_k_8]; float[] biasArray = new float[n_k_2]; int delta_n = (n_k_2 - n_k) / group; for (int i = 0; i < n_k_2; i++) for (int j = 0; j < c_k_8; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0; else if (i >= n_k_2 / group) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_2; i++) { if (((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n)) biasArray[i] = 0; else if (i >= n_k_2 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript82 = new ScriptC_convRolledInF8OutF2(myRS); myScript82.set_Bias_Blob(biasAllocation); myScript82.set_Kernel_Blob(kernelAllocation); myScript82.set_n_k(n_k_2); myScript82.set_c_k(c_k_8); myScript82.set_h_k(h_k); myScript82.set_w_k(w_k); myScript82.set_pad_x(pad[0]); myScript82.set_pad_y(pad[1]); myScript82.set_stride_x(stride[0]); myScript82.set_stride_y(stride[1]); myScript82.set_group(group); }
Example #23
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF8F1(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_8 = c_k; if (c_k % 8 != 0) c_k_8 = c_k + 8 - c_k % 8; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k * c_k_8 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32(myRS), n_k); float[] kernelMatrix = new float[n_k * h_k * w_k * c_k_8]; float[] biasArray = new float[n_k]; int delta_n = (n_k - n_k) / group; for (int i = 0; i < n_k; i++) for (int j = 0; j < c_k_8; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n)) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0; else if (i >= n_k / group) kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k; i++) { if (((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n)) biasArray[i] = 0; else if (i >= n_k / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript81 = new ScriptC_convRolledInF8OutF1(myRS); myScript81.set_Bias_Blob(biasAllocation); myScript81.set_Kernel_Blob(kernelAllocation); myScript81.set_n_k(n_k); myScript81.set_c_k(c_k_8); myScript81.set_h_k(h_k); myScript81.set_w_k(w_k); myScript81.set_pad_x(pad[0]); myScript81.set_pad_y(pad[1]); myScript81.set_stride_x(stride[0]); myScript81.set_stride_y(stride[1]); myScript81.set_group(group); }
Example #24
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF4F8(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_4 = c_k; if (c_k % 4 != 0) c_k_4 = c_k + 4 - c_k % 4; int n_k_8 = n_k; if (n_k % 8 != 0) n_k_8 = n_k + 8 - n_k % 8; Type kernelType, biasType; Allocation kernelAllocation; Allocation biasAllocation; kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 * c_k_4 * h_k * w_k / 4); biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_8 / 4); float[] kernelMatrix = new float[n_k_8 * h_k * w_k * c_k_4]; float[] biasArray = new float[n_k_8]; int delta_n = (n_k_8 - n_k) / group; for (int i = 0; i < n_k_8; i++) for (int j = 0; j < c_k_4; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0; else if (i >= n_k_8 / group) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_8; i++) { if (((i >= n_k_8 / group - delta_n) && (i < n_k_8 / group)) || (i >= n_k_8 - delta_n)) biasArray[i] = 0; else if (i >= n_k_8 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript48 = new ScriptC_convRolledInF4OutF8(myRS); myScript48.set_Bias_Blob(biasAllocation); myScript48.set_Kernel_Blob(kernelAllocation); myScript48.set_n_k(n_k_8); myScript48.set_c_k(c_k_4); myScript48.set_h_k(h_k); myScript48.set_w_k(w_k); myScript48.set_pad_x(pad[0]); myScript48.set_pad_y(pad[1]); myScript48.set_stride_x(stride[0]); myScript48.set_stride_y(stride[1]); myScript48.set_group(group); }
Example #25
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF4F4(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_4 = c_k; if (c_k % 4 != 0) c_k_4 = c_k + 4 - c_k % 4; int n_k_4 = n_k; if (n_k % 4 != 0) n_k_4 = n_k + 4 - n_k % 4; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 * c_k_4 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32_4(myRS), n_k_4 / 4); float[] kernelMatrix = new float[n_k_4 * h_k * w_k * c_k_4]; float[] biasArray = new float[n_k_4]; int delta_n = (n_k_4 - n_k) / group; for (int i = 0; i < n_k_4; i++) for (int j = 0; j < c_k_4; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0; else if (i >= n_k_4 / group) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_4; i++) { if (((i >= n_k_4 / group - delta_n) && (i < n_k_4 / group)) || (i >= n_k_4 - delta_n)) biasArray[i] = 0; else if (i >= n_k_4 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript44 = new ScriptC_convRolledInF4OutF4(myRS); myScript44.set_Bias_Blob(biasAllocation); myScript44.set_Kernel_Blob(kernelAllocation); myScript44.set_n_k(n_k_4); myScript44.set_c_k(c_k_4); myScript44.set_h_k(h_k); myScript44.set_w_k(w_k); myScript44.set_pad_x(pad[0]); myScript44.set_pad_y(pad[1]); myScript44.set_stride_x(stride[0]); myScript44.set_stride_y(stride[1]); myScript44.set_group(group); }
Example #26
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF4F2(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_4 = c_k; if (c_k % 4 != 0) c_k_4 = c_k + 4 - c_k % 4; int n_k_2 = n_k; if (n_k % 2 != 0) n_k_2 = n_k + 2 - n_k % 2; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_2 * c_k_4 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32_2(myRS), n_k_2 / 2); float[] kernelMatrix = new float[n_k_2 * h_k * w_k * c_k_4]; float[] biasArray = new float[n_k_2]; int delta_n = (n_k_2 - n_k) / group; for (int i = 0; i < n_k_2; i++) for (int j = 0; j < c_k_4; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n)) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0; else if (i >= n_k_2 / group) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k_2; i++) { if (((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n)) biasArray[i] = 0; else if (i >= n_k_2 / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript42 = new ScriptC_convRolledInF4OutF2(myRS); myScript42.set_Bias_Blob(biasAllocation); myScript42.set_Kernel_Blob(kernelAllocation); myScript42.set_n_k(n_k_2); myScript42.set_c_k(c_k_4); myScript42.set_h_k(h_k); myScript42.set_w_k(w_k); myScript42.set_pad_x(pad[0]); myScript42.set_pad_y(pad[1]); myScript42.set_stride_x(stride[0]); myScript42.set_stride_y(stride[1]); myScript42.set_group(group); }
Example #27
Source File: Convolution.java From CNNdroid with MIT License | 4 votes |
private void initKernelF4F1(float[][][][] myWeight, float[] myBias) { int n_k = myWeight.length; int c_k = myWeight[0].length; int h_k = myWeight[0][0].length; int w_k = myWeight[0][0][0].length; int c_k_4 = c_k; if (c_k % 4 != 0) c_k_4 = c_k + 4 - c_k % 4; Allocation kernelAllocation; Allocation biasAllocation; Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k * c_k_4 * h_k * w_k / 4); Type biasType = Type.createX(myRS, Element.F32(myRS), n_k); float[] kernelMatrix = new float[n_k * h_k * w_k * c_k_4]; float[] biasArray = new float[n_k]; int delta_n = (n_k - n_k) / group; for (int i = 0; i < n_k; i++) for (int j = 0; j < c_k_4; j++) for (int k = 0; k < h_k; k++) for (int l = 0; l < w_k; l++) { if (j >= c_k || ((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n)) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = 0; else if (i >= n_k / group) kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i - delta_n][j][k][l]; else kernelMatrix[i * h_k * w_k * c_k_4 + k * w_k * c_k_4 + l * c_k_4 + j] = myWeight[i][j][k][l]; } for (int i = 0; i < n_k; i++) { if (((i >= n_k / group - delta_n) && (i < n_k / group)) || (i >= n_k - delta_n)) biasArray[i] = 0; else if (i >= n_k / group) biasArray[i] = myBias[i - delta_n]; else biasArray[i] = myBias[i]; } kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); kernelAllocation.copyFrom(kernelMatrix); biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); biasAllocation.copyFrom(biasArray); myScript41 = new ScriptC_convRolledInF4OutF1(myRS); myScript41.set_Bias_Blob(biasAllocation); myScript41.set_Kernel_Blob(kernelAllocation); myScript41.set_n_k(n_k); myScript41.set_c_k(c_k_4); myScript41.set_h_k(h_k); myScript41.set_w_k(w_k); myScript41.set_pad_x(pad[0]); myScript41.set_pad_y(pad[1]); myScript41.set_stride_x(stride[0]); myScript41.set_stride_y(stride[1]); myScript41.set_group(group); }
Example #28
Source File: Layer.java From rscnn with MIT License | 4 votes |
protected void allocFeatureMapBlock4() { int outNum = outputShape[0]; int outHeight = outputShape[1]; int outWidth = outputShape[2]; int outChannel = outputShape[3]; int outChannelAlign = outChannel; if(outChannelAlign % 4 !=0) { outChannelAlign = outChannel + 4 - (outChannel % 4); } if(featureMapOutput!=null){ FeatureMap old = (FeatureMap)featureMapOutput; if(old.getFeatureMap()!=null){ Allocation out = old.getFeatureMap(); if(out.getBytesSize()==outNum * outHeight * outWidth * outChannelAlign * 4){ old.setN(outNum); old.setH(outHeight); old.setW(outWidth); old.setC(outChannel); return; } else{ out.destroy(); } } } Type outType = Type.createX(renderScript, Element.F32_4(renderScript), outNum * outHeight * outWidth * outChannelAlign / 4); //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT); Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); FeatureMap output = new FeatureMap(); output.setFeatureMap(outAllocation); output.setN(outNum); output.setH(outHeight); output.setW(outWidth); output.setC(outChannel); output.setPad4(true); output.setMatrix2D(false); featureMapOutput = output; }
Example #29
Source File: FullyConnected.java From CNNdroid with MIT License | 4 votes |
private float[][] fullyConnectedLayerInF8OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) { // fully connected layer int h_w = myBias.length; int w_w = myWeight.length / h_w; // Calculate sizes. int n_i, c_i; n_i = inputBlob4.length; c_i = inputBlob4[0].length; int c_i_8 = c_i; if (c_i % 8 != 0) c_i_8 = c_i + 8 - c_i % 8; int n_o = n_i; int c_o = h_w; // Initialize the result. float[][] outputBlob = new float[n_o][c_o]; //initialize Renderscript Type inputType, outType; Allocation frameAllocation; Allocation outAllocation; inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_8 / 4); outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o); frameAllocation = Allocation.createTyped(myRS, inputType); outAllocation = Allocation.createTyped(myRS, outType); myScriptF8.set_c_i(c_i_8); // calculate the result float[] frameMatrix = new float[n_i * c_i_8]; for (int n = 0 ; n < n_i ; n++) for (int i = 0 ; i < c_i_8 ; i++) if (i < c_i) frameMatrix[n * w_w + i] = inputBlob4[n][i]; else frameMatrix[n * w_w + i] = 0; frameAllocation.copyFrom(frameMatrix); myScriptF8.set_In_Blob(frameAllocation); myScriptF8.forEach_root(outAllocation); float[] outMatrix = new float[n_o * c_o]; outAllocation.copyTo(outMatrix); for (int n = 0 ; n < n_i ; n++) for (int c = 0 ; c < c_o ; c++) { outputBlob[n][c] = outMatrix[n * c_o + c]; if (nonLinear) { switch (nonLinearType) { case RectifiedLinearUnit: if (outputBlob[n][c] < 0) outputBlob[n][c] = 0; break; } } } frameAllocation.destroy(); outAllocation.destroy(); inputType.destroy(); outType.destroy(); if (destroy) { myScriptF8.destroy(); myScriptF8 = null; } // return the result return outputBlob; }
Example #30
Source File: FullyConnected.java From CNNdroid with MIT License | 4 votes |
private float[][] fullyConnectedLayerInF4OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) { // fully connected layer int h_w = myBias.length; int w_w = myWeight.length / h_w; // Calculate sizes. int n_i, c_i; n_i = inputBlob4.length; c_i = inputBlob4[0].length; int c_i_4 = c_i; if (c_i % 4 != 0) c_i_4 = c_i + 4 - c_i % 4; int n_o = n_i; int c_o = h_w; // Initialize the result. float[][] outputBlob = new float[n_o][c_o]; //initialize Renderscript Type inputType, outType; Allocation frameAllocation; Allocation outAllocation; inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_4 / 4); outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o); frameAllocation = Allocation.createTyped(myRS, inputType); outAllocation = Allocation.createTyped(myRS, outType); myScriptF4.set_c_i(c_i_4); // calculate the result float[] frameMatrix = new float[n_i * c_i_4]; for (int n = 0 ; n < n_i ; n++) for (int i = 0 ; i < c_i_4 ; i++) if (i < c_i) frameMatrix[n * w_w + i] = inputBlob4[n][i]; else frameMatrix[n * w_w + i] = 0; frameAllocation.copyFrom(frameMatrix); myScriptF4.set_In_Blob(frameAllocation); myScriptF4.forEach_root(outAllocation); float[] outMatrix = new float[n_o * c_o]; outAllocation.copyTo(outMatrix); for (int n = 0 ; n < n_i ; n++) for (int c = 0 ; c < c_o ; c++) { outputBlob[n][c] = outMatrix[n * c_o + c]; if (nonLinear) { switch (nonLinearType) { case RectifiedLinearUnit: if (outputBlob[n][c] < 0) outputBlob[n][c] = 0; break; } } } frameAllocation.destroy(); outAllocation.destroy(); inputType.destroy(); outType.destroy(); if (destroy) { myScriptF4.destroy(); myScriptF4 = null; } // return the result return outputBlob; }