Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tfjs-backend-wasm/src/kernels/Cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ interface CastAttrs extends NamedAttrMap {
dtype: DataType;
}

function cast(
args: {inputs: CastInputs, attrs: CastAttrs, backend: BackendWasm}) {
export function cast(
args: {inputs: CastInputs, attrs: CastAttrs, backend: BackendWasm}):
TensorInfo {
const {inputs: {x}, attrs: {dtype}, backend} = args;
const out = backend.makeOutput(x.shape, dtype);
const inVals = backend.typedArrayFromHeap(x);
Expand Down
18 changes: 16 additions & 2 deletions tfjs-backend-wasm/src/kernels/CropAndResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import {NamedAttrMap, NamedTensorInfoMap, registerKernel, TensorInfo} from '@tensorflow/tfjs-core';

import {BackendWasm} from '../backend_wasm';
import {cast} from './Cast';

interface CropAndResizeInputs extends NamedTensorInfoMap {
images: TensorInfo;
Expand Down Expand Up @@ -71,11 +72,19 @@ function cropAndResize(args: {
const [cropHeight, cropWidth] = cropSize as [number, number];
const outShape = [numBoxes, cropHeight, cropWidth, images.shape[3]];

const imagesId = backend.dataIdMap.get(images.dataId).id;
let imagesData = backend.dataIdMap.get(images.dataId);
let castedData;
if (images.dtype !== 'float32') {
castedData =
cast({backend, inputs: {x: images}, attrs: {dtype: 'float32'}});
imagesData = backend.dataIdMap.get(castedData.dataId);
}

const imagesId = imagesData.id;
const boxesId = backend.dataIdMap.get(boxes.dataId).id;
const boxIndId = backend.dataIdMap.get(boxInd.dataId).id;

const out = backend.makeOutput(outShape, images.dtype);
const out = backend.makeOutput(outShape, 'float32');
const outId = backend.dataIdMap.get(out.dataId).id;

const imagesShapeBytes = new Uint8Array(new Int32Array(images.shape).buffer);
Expand All @@ -85,6 +94,11 @@ function cropAndResize(args: {
cropWidth,
InterpolationMethod[method as {} as keyof typeof InterpolationMethod],
extrapolationValue, outId);

if (castedData != null) {
backend.disposeData(castedData.dataId);
}

return out;
}

Expand Down
21 changes: 17 additions & 4 deletions tfjs-backend-wasm/src/kernels/ResizeBilinear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {NamedAttrMap, NamedTensorInfoMap, registerKernel, TensorInfo, util} from

import {BackendWasm} from '../backend_wasm';

import {cast} from './Cast';

interface ResizeBilinearInputs extends NamedTensorInfoMap {
x: TensorInfo;
}
Expand Down Expand Up @@ -48,7 +50,7 @@ function setup(backend: BackendWasm): void {
]);
}

function cropAndResize(args: {
function resizeBilinear(args: {
backend: BackendWasm,
inputs: ResizeBilinearInputs,
attrs: ResizeBilinearAttrs
Expand All @@ -60,9 +62,15 @@ function cropAndResize(args: {
const [batch, oldHeight, oldWidth, numChannels] = x.shape;
const outShape = [batch, newHeight, newWidth, numChannels];

const xId = backend.dataIdMap.get(x.dataId).id;
let xData = backend.dataIdMap.get(x.dataId);
let castedData;
if (xData.dtype !== 'float32') {
castedData = cast({backend, inputs: {x}, attrs: {dtype: 'float32'}});
xData = backend.dataIdMap.get(castedData.dataId);
}
const xId = xData.id;

const out = backend.makeOutput(outShape, x.dtype);
const out = backend.makeOutput(outShape, 'float32');
if (util.sizeFromShape(x.shape) === 0) {
return out;
}
Expand All @@ -71,12 +79,17 @@ function cropAndResize(args: {
wasmResizeBilinear(
xId, batch, oldHeight, oldWidth, numChannels, newHeight, newWidth,
alignCorners ? 1 : 0, outId);

if (castedData != null) {
backend.disposeData(castedData.dataId);
}

return out;
}

registerKernel({
kernelName: 'ResizeBilinear',
backendName: 'wasm',
setupFunc: setup,
kernelFunc: cropAndResize
kernelFunc: resizeBilinear
});
4 changes: 2 additions & 2 deletions tfjs-core/src/backends/cpu/backend_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3561,8 +3561,8 @@ export class MathBackendCPU extends KernelBackend {
const numBoxes = boxes.shape[0];

const [cropHeight, cropWidth] = cropSize;
const output = ops.buffer(
[numBoxes, cropHeight, cropWidth, numChannels], images.dtype);
const output =
ops.buffer([numBoxes, cropHeight, cropWidth, numChannels], 'float32');

const boxVals = this.readSync(boxes.dataId) as TypedArray;
const boxIndVals = this.readSync(boxIndex.dataId) as TypedArray;
Expand Down
4 changes: 2 additions & 2 deletions tfjs-core/src/backends/webgl/backend_webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2195,7 +2195,7 @@ export class MathBackendWebGL extends KernelBackend {
new ResizeBilinearPackedProgram(
x.shape, newHeight, newWidth, alignCorners) :
new ResizeBilinearProgram(x.shape, newHeight, newWidth, alignCorners);
return this.compileAndRun(program, [x]);
return this.compileAndRun(program, [x], 'float32');
}

resizeBilinearBackprop(dy: Tensor4D, x: Tensor4D, alignCorners: boolean):
Expand Down Expand Up @@ -2260,7 +2260,7 @@ export class MathBackendWebGL extends KernelBackend {
extrapolationValue: number): Tensor4D {
const program = new CropAndResizeProgram(
image.shape, boxes.shape, cropSize, method, extrapolationValue);
return this.compileAndRun(program, [image, boxes, boxIndex]);
return this.compileAndRun(program, [image, boxes, boxIndex], 'float32');
}

depthToSpace(x: Tensor4D, blockSize: number, dataFormat: 'NHWC'|'NCHW'):
Expand Down
2 changes: 1 addition & 1 deletion tfjs-core/src/ops/image_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function cropAndResize_(
method?: 'bilinear'|'nearest',
extrapolationValue?: number,
): Tensor4D {
const $image = convertToTensor(image, 'image', 'cropAndResize', 'float32');
const $image = convertToTensor(image, 'image', 'cropAndResize');
const $boxes = convertToTensor(boxes, 'boxes', 'cropAndResize', 'float32');
const $boxInd = convertToTensor(boxInd, 'boxInd', 'cropAndResize', 'int32');
method = method || 'bilinear';
Expand Down
Loading