TensorFlow.js Models

repository·master·Indexed 12 days ago

https://github.com/tensorflow/tfjs-models

A collection of pre-trained TensorFlow.js models for tasks such as image classification, object detection, pose estimation, and natural language processing. Includes specialized packages and documentation for body segmentation (via MediaPipe SelfieSegmentation and BodyPix) and face detection.

Tokens
89.9K
Snippets
334
Records
431
Agent score
96%

What's inside TensorFlow.js Models

  1. Overview of TensorFlow.js Pre-trained Models

    master

    This repository provides a collection of pre-trained models ported to TensorFlow.js. These models are available via NPM and unpkg, allowing them to be used out-of-the-box in any web project.

    Key characteristics:

    • Ease of Use: Most models are designed to hide low-level tensor operations, making them accessible to developers who are not machine learning experts.
    • Versatility: Models can be used directly for inference or integrated into transfer learning workflows using TensorFlow.js.
    • Extensibility: Models serve as building blocks for more complex applications.
  2. Overview of Speech Command Recognizer

    master

    The Speech Command Recognizer is a JavaScript module built on TensorFlow.js that enables the recognition of spoken commands consisting of simple, isolated English words. It utilizes the WebAudio API for audio input and leverages WebGL GPU acceleration for inference and transfer learning directly in the browser.

    Default Vocabulary:

    • Digits: zero through nine
    • Directions: up, down, left, right
    • Actions: go, stop
    • Affirmations/Negations: yes, no
    • Special categories: unknown word, background noise
  3. PoseNet Demo capabilities

    master

    The PoseNet demos provide two primary use cases for pose estimation:

    • Camera Demo: Estimates poses in real-time using a webcam video stream.
    • Coco Images Demo: Estimates poses in static images from the COCO dataset. This demo also demonstrates the differences between single-person and multi-person pose detection algorithms.
  4. Explore face-landmarks-detection demos

    master

    There are two primary ways to interact with the face-landmarks-detection models via web demos:

    • Live Camera Demo: Uses your device's camera for real-time face tracking. It is compatible with laptops, iPhones, and Android phones. You can switch between different runtimes to compare performance.
    • Upload a Video Demo: Allows you to upload an .mp4 video file. After processing, the demo automatically downloads the video with the detected face landmarks applied.
  5. Supported Pose Detection Models

    master

    The pose-detection package provides three primary model options:

    ModelKeypointsDescription
    MoveNet17 (COCO)Ultra fast and accurate; ideal for 50+ fps on modern devices.
    BlazePose33 (MediaPipe)Detects 33 keypoints (including face, hands, and feet) plus 3D coordinates and segmentation masks.
    PoseNet17 (COCO)Supports multi-pose estimation (detecting multiple people in one image).
  6. Explore depth-estimation model demos

    master

    The depth-estimation models power several interactive demos that showcase different capabilities:

    • 3D Photo Demo: Upload a portrait image to create a 3D photo animation. Works on laptops, iPhones, and Android phones. View 3D Photo Demo

    • Relighting Demo: Uses a live camera stream to allow placing light sources or point lights to change the scene's lighting. Works on laptops, iPhones, and Android phones. View Relighting Demo

    • Depth Map Demo: Uses a live camera stream to visualize depth in real-time. Works on laptops, iPhones, and Android phones. View Depth Map Demo

  7. Use BodyPix for person and body part segmentation

    master

    BodyPix is a TensorFlow.js model used for person segmentation in the browser. It can identify pixels belonging to a person and can further segment specific body parts.

    Note: This README is for the archived 1.0 version. Version 2.0 introduced multi-person support, a new ResNet model, and a new API. For the latest features, refer to the current documentation.

    To use BodyPix, you follow a two-step process:

    1. Loading the model: Use the provided loading function to initialize the model.
    2. Making a prediction: Pass an image element (or tensor) to the model to receive segmentation data.

    Available segmentation types:

    • Person segmentation: Identifies which pixels belong to a person.
    • Person body part segmentation: Identifies specific body parts (e.g., arms, legs, face) within the person segmentation.
  8. Overview of BodyPix segmentation capabilities

    master

    BodyPix is a model for real-time person and body part segmentation in the browser.

    Key capabilities:

    • Person Segmentation: Segments an image into pixels that are part of a person and pixels that are not.
    • Body Part Segmentation: Segments pixels into twenty-four distinct body parts.

    Usage Note: The model is optimized for a single person centered in the input image or video. To segment multiple people, you must combine BodyPix with a person detector by cropping boxes for each detected person and running segmentation on those individual crops.

  9. How the KNN Classifier works

    master

    Unlike standard TensorFlow.js models that provide pre-trained weights, the KNN Classifier is a utility for constructing a K-Nearest Neighbors model using activations (tensors) from another model or any other tensors you associate with a label.

    The workflow is typically:

    1. Initialize a classifier using knnClassifier.create().
    2. Extract features from an input (e.g., using a model like MobileNet) to get a tensor of activations.
    3. Train by adding these activations as examples with a specific label using addExample().
    4. Predict by passing new activations to predictClass().
    const classifier = knnClassifier.create();
    // ... extract logits from another model ...
    classifier.addExample(logits, label);
    // ... predict ...
    const result = await classifier.predictClass(newLogits);
  10. Understand the Pose detection output format

    master

    The estimatePoses method returns an array of pose objects. Each object contains:

    • score: A confidence score for the entire pose (0 to 1).
    • keypoints: An array of detected body parts. Each keypoint includes x, y (pixel coordinates), score (confidence), and name (e.g., 'nose').
    • keypoints3D (BlazePose only): 3D coordinates where x, y, and z represent absolute distance in meters in a 2x2x2 meter cubic space (range -1 to 1). The hip center is at (0, 0, 0).
    • segmentation (BlazePose only): Provides a mask and a maskValueToLabel function.

    Keypoint Normalization

    To convert pixel-based x and y coordinates to a normalized [0, 1] range, use: poseDetection.calculators.keypointsToNormalizedKeypoints(keypoints, imageSize).

    // Example output structure
    [
      {
        score: 0.8,
        keypoints: [
          {x: 230, y: 220, score: 0.99, name: "nose"},
          {x: 212, y: 190, score: 0.91, name: "left_eye"}
        ],
        keypoints3D: [
          {x: 0.65, y: 0.11, z: 0.05, score: 0.99, name: "nose"}
        ],
        segmentation: {
          maskValueToLabel: (maskValue) => { return 'person' },
          mask: { /* methods like toCanvasImageSource, toImageData, toTensor */ }
        }
      }
    ]
  11. How trackers work in pose detection

    master

    In pose detection, a tracker is an algorithm that runs downstream of a detector to maintain object identity across consecutive video frames. While a detector locates objects in a single frame, it does not associate them with objects from previous frames.

    Key Concepts:

    • Tracks: The history of objects maintained by the tracker.
    • Linking: The process of associating incoming detections with existing tracks, spawning new tracks for unmatched detections, or killing old tracks that no longer have associated detections.
    • Similarity Scoring: To match detections to tracks, the tracker calculates a similarity score. Common methods include:
      • Intersection-over-Union (IoU): Used when detections are bounding boxes.
      • Object Keypoint Similarity: Used when detections contain keypoints (poses).
    • Matching Algorithm: An optimization procedure that uses the similarity matrix to decide which specific detection belongs to which specific track.

    Workflow Example:

    1. Detector identifies objects in frame t.
    2. Tracker compares these detections against tracks from frame t-1 using a similarity function.
    3. A matching algorithm links high-scoring detections to existing tracks.
    4. Unmatched detections create new tracks; unmatched tracks are eventually deleted.