tensorflow-helpers
Version:
Helper functions to use tensorflow in nodejs for transfer learning, image classification, and more
97 lines (96 loc) • 3.6 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImageTensorShape = getImageTensorShape;
exports.calcCropBox = calcCropBox;
exports.cropAndResizeImageTensor = cropAndResizeImageTensor;
const tf = __importStar(require("@tensorflow/tfjs-core"));
const tensor_1 = require("./tensor");
function getImageTensorShape(imageTensor) {
return imageTensor.shape.length === 3
? {
width: imageTensor.shape[1],
height: imageTensor.shape[0],
}
: {
width: imageTensor.shape[2],
height: imageTensor.shape[1],
};
}
/**
* @description calculate center-crop box
* @returns [top,left,bottom,right], values range: 0..1
*/
function calcCropBox(options) {
let { sourceShape, targetShape } = options;
let top = 0;
let left = 0;
let bottom = 1;
let right = 1;
if (sourceShape.width > sourceShape.height ==
targetShape.width > targetShape.height) {
let targetHeightInRatio = (sourceShape.height / sourceShape.width) * targetShape.width;
top =
Math.abs(targetHeightInRatio - targetShape.height) /
targetHeightInRatio /
2;
bottom = 1 - top;
}
else {
let targetWidthInRatio = (sourceShape.width / sourceShape.height) * targetShape.height;
left =
Math.abs(targetWidthInRatio - targetShape.width) / targetWidthInRatio / 2;
right = 1 - left;
}
return [top, left, bottom, right];
}
function cropAndResizeImageTensor(options) {
let { imageTensor, width, height } = options;
let croppedImageTensor = tf.tidy(() => {
let imageShape = getImageTensorShape(imageTensor);
let cropBox = options.aspectRatio != 'center-crop'
? [0, 0, 1, 1]
: calcCropBox({
sourceShape: imageShape,
targetShape: { width, height },
});
const crop = tf.image.cropAndResize(
// Expand image input dimensions to add a batch dimension of size 1.
(0, tensor_1.toTensor4D)(imageTensor), [cropBox], [0], [height, width]);
return crop.div(255);
});
imageTensor.dispose();
return croppedImageTensor;
}
;