How to use the @tensorflow/tfjs.scalar function in @tensorflow/tfjs

To help you get started, we’ve selected a few @tensorflow/tfjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tensorflow / tfjs-examples / webcam-transfer-learning / index.ts View on Github external
async function predict() {
  ui.isPredicting();
  while (isPredicting) {
    // Capture the frame from the webcam.
    // const img = webcam.capture();
    const img = (await webcamDataset.capture())
                    .expandDims(0)
                    .toFloat()
                    .div(tf.scalar(127))
                    .sub(tf.scalar(1));

    // Make a prediction through mobilenet, getting the internal activation of
    // the mobilenet model, i.e., "embeddings" of the input images.
    const embeddings = truncatedMobileNet.predict(img);

    // Make a prediction through our newly-trained model using the embeddings
    // from mobilenet as input.
    const predictions = model.predict(embeddings);

    // Returns the index with the maximum probability. This number corresponds
    // to the class the model thinks is the most probable given the input.
    const predictedClass = predictions.as1D().argMax();
    img.dispose();

    const classId = (await predictedClass.data())[0];
    predictedClass.dispose();
github tensorflow / tfjs-examples / mobilenet / index.js View on Github external
const logits = tf.tidy(() => {
    // tf.browser.fromPixels() returns a Tensor from an image element.
    const img = tf.browser.fromPixels(imgElement).toFloat();

    const offset = tf.scalar(127.5);
    // Normalize the image from [0, 255] to [-1, 1].
    const normalized = img.sub(offset).div(offset);

    // Reshape to a single-element batch so we can pass it to predict.
    const batched = normalized.reshape([1, IMAGE_SIZE, IMAGE_SIZE, 3]);

    startTime2 = performance.now();
    // Make a prediction through mobilenet.
    return mobilenet.predict(batched);
  });
github ModelDepot / tfjs-yolo-tiny / src / index.js View on Github external
const activation = model.predict(input);

    const [box_xy, box_wh, box_confidence, box_class_probs ] =
      yolo_head(activation, yoloAnchors, numClasses);

    const all_boxes = yolo_boxes_to_corners(box_xy, box_wh);

    let [boxes, scores, classes] = yolo_filter_boxes(
      all_boxes, box_confidence, box_class_probs, filterBoxesThreshold);

    // If all boxes have been filtered out
    if (boxes == null) {
      return null;
    }

    const width = tf.scalar(widthPx);
    const height = tf.scalar(heightPx);

    const image_dims = tf.stack([height, width, height, width]).reshape([1,4]);

    boxes = tf.mul(boxes, image_dims);

    return [boxes, scores, classes];
  });
github thekevinscott / ml-classifier / example / index.js View on Github external
function batchImage(image) {
  // Expand our tensor to have an additional dimension, whose size is 1
  const batchedImage = image.expandDims(0);

  // Turn pixel data into a float between -1 and 1.
  return batchedImage.toFloat().div(tf.scalar(127)).sub(tf.scalar(1));
}
function loadAndProcessImage(image) {
github woudsma / retrain-mobilenet-for-the-web / src / index.js View on Github external
const predict = async (img, model) => {
  const t0 = performance.now()
  const image = tf.fromPixels(img).toFloat()
  const resized = tf.image.resizeBilinear(image, [IMAGE_SIZE, IMAGE_SIZE])
  const offset = tf.scalar(255 / 2)
  const normalized = resized.sub(offset).div(offset)
  const input = normalized.expandDims(0)
  // const output = await tf.tidy(() => model.predict({ input })).data() // MobileNet V1
  const output = await tf.tidy(() => model.predict({ Placeholder: input })).data() // MobileNet V2
  const predictions = labels
    .map((label, index) => ({ label, accuracy: output[index] }))
    .sort((a, b) => b.accuracy - a.accuracy)
  const time = `${(performance.now() - t0).toFixed(1)} ms`
  return { predictions, time }
}
github piximi / application / src / dataset.js View on Github external
return tensorflow.tidy(() => {
      let xs = null;
      // var ys = null;
      let ys2 = null;

      for (let img of imageArray) {
        let imgTensor = tensorflow.browser.fromPixels(img);

        imgTensor = tensorflow.image.resizeBilinear(imgTensor, [224, 224]);
        imgTensor = imgTensor.expandDims(0);
        imgTensor = imgTensor
          .toFloat()
          .div(tensorflow.scalar(127.0))
          .sub(tensorflow.scalar(1.0));
        if (xs == null) {
          xs = imgTensor;
        } else {
          xs = xs.concat(imgTensor, 0);
        }

        if (ys2 == null) {
          ys2 = [img.category];
        } else {
          ys2.push(img.category);
        }
      }
      let labelBatch;
      if (categories.length === 1) {
        labelBatch = tensorflow.tensor1d(ys2, 'int32');
      } else {
github tensorflow / tfjs-converter / tfjs-converter / demo / control_flow / loop_model.js View on Github external
async predict(init, loop, loop2, inc) {
    const dict = {
      'init': tf.scalar(init, 'int32'),
      'times': tf.scalar(loop, 'int32'),
      'times2': tf.scalar(loop2, 'int32'),
      'inc': tf.scalar(inc, 'int32')
    };
    return this.model.executeAsync(dict, OUTPUT_NODE_NAME);
  }
}
github jinglescode / demos / node_backup / backup / index.js View on Github external
return tf.tidy(() => {
    return a.mul(x.pow(tf.scalar(3, 'int32')))
      .add(b.mul(x.square()))
      .add(c.mul(x))
      .add(d);
  });
}
github tensorflow / tfjs-converter / tfjs-converter / demo / mobilenet / mobilenet.js View on Github external
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */

import * as tf from '@tensorflow/tfjs';

import {IMAGENET_CLASSES} from './imagenet_classes';

const GOOGLE_CLOUD_STORAGE_DIR =
    'https://storage.googleapis.com/tfjs-models/savedmodel/';
const MODEL_FILE_URL = 'mobilenet_v2_1.0_224/model.json';
const INPUT_NODE_NAME = 'images';
const OUTPUT_NODE_NAME = 'module_apply_default/MobilenetV2/Logits/output';
const PREPROCESS_DIVISOR = tf.scalar(255 / 2);

export class MobileNet {
  constructor() {}

  async load() {
    this.model = await tf.loadGraphModel(
        GOOGLE_CLOUD_STORAGE_DIR + MODEL_FILE_URL);
  }

  dispose() {
    if (this.model) {
      this.model.dispose();
    }
  }
  /**
   * Infer through MobileNet. This does standard ImageNet pre-processing before
github ml5js / ml5-library / src / Pix2pix / index.js View on Github external
const result = tf.tidy(() => {
      return tf.sub(tf.mul(inputPreproc, tf.scalar(2)), tf.scalar(1));
    });
    inputPreproc.dispose();