(args: {
inputs: TransformInputs,
attrs: TransformAttrs,
backend: MathBackendCPU
})
| 20 | import {MathBackendCPU} from '../backend_cpu'; |
| 21 | |
| 22 | export function transform(args: { |
| 23 | inputs: TransformInputs, |
| 24 | attrs: TransformAttrs, |
| 25 | backend: MathBackendCPU |
| 26 | }): TensorInfo { |
| 27 | const {inputs, attrs, backend} = args; |
| 28 | const {image, transforms} = inputs; |
| 29 | const {interpolation, fillMode, fillValue, outputShape} = attrs; |
| 30 | |
| 31 | const [batch, imageHeight, imageWidth, numChannels] = image.shape; |
| 32 | const [outHeight, outWidth] = |
| 33 | outputShape != null ? outputShape : [imageHeight, imageWidth]; |
| 34 | const outShape = [batch, outHeight, outWidth, numChannels]; |
| 35 | |
| 36 | const inStrides = util.computeStrides(image.shape); |
| 37 | const batchInStride = inStrides[0]; |
| 38 | const rowInStride = inStrides[1]; |
| 39 | const colInStride = inStrides[2]; |
| 40 | |
| 41 | const outStrides = util.computeStrides(outShape); |
| 42 | const batchOutStride = outStrides[0]; |
| 43 | const rowOutStride = outStrides[1]; |
| 44 | const colOutStride = outStrides[2]; |
| 45 | |
| 46 | const outVals = util.getTypedArrayFromDType( |
| 47 | image.dtype as NumericDataType, util.sizeFromShape(outShape)); |
| 48 | |
| 49 | outVals.fill(fillValue); |
| 50 | |
| 51 | const imageVals = backend.data.get(image.dataId).values as TypedArray; |
| 52 | const transformVals = |
| 53 | backend.data.get(transforms.dataId).values as TypedArray; |
| 54 | |
| 55 | // Ref TF implementation: |
| 56 | // https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/image/image_ops.h |
| 57 | for (let b = 0; b < batch; ++b) { |
| 58 | const transform = transforms.shape[0] === 1 ? |
| 59 | transformVals : |
| 60 | transformVals.subarray(b * 8, b * 8 + 8); |
| 61 | |
| 62 | for (let outY = 0; outY < outHeight; ++outY) { |
| 63 | for (let outX = 0; outX < outWidth; ++outX) { |
| 64 | for (let channel = 0; channel < numChannels; ++channel) { |
| 65 | let val; |
| 66 | |
| 67 | const projection = transform[6] * outX + transform[7] * outY + 1; |
| 68 | |
| 69 | if (projection === 0) { |
| 70 | // Return the fill value for infinite coordinates, |
| 71 | // which are outside the input image |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | const inX = |
| 76 | (transform[0] * outX + transform[1] * outY + transform[2]) / |
| 77 | projection; |
| 78 | const inY = |
| 79 | (transform[3] * outX + transform[4] * outY + transform[5]) / |
nothing calls this directly
no test coverage detected
searching dependent graphs…