Skip to content

Consider using tf.stack to simplify aggregate features #2

Description

@HyperCrowd

When aggregating heatmaps, use tf.stack to get the features of that grouping. Example:

const tf = require('@tensorflow/tfjs-node-gpu');
const { loadImage } = require('canvas');

async function runImageSimilarity() {
  // Load the pre-trained model
  const model = await tf.loadLayersModel('file://path/to/model/model.json');

  // Load grayscale images
  const imagePaths = ['image1.jpg', 'image2.jpg']; // Replace with your image paths
  const images = [];
  for (const path of imagePaths) {
    const img = await loadImage(path);
    const imgTensor = tf.browser.fromPixels(img, 1).toFloat();
    images.push(imgTensor);
  }

  // Convert images to a single tensor
  const inputTensor = tf.stack(images);

  // Extract features from the images
  const features = model.predict(inputTensor);

  // Calculate similarities between images
  const similarityThreshold = 0.8;
  for (let i = 0; i < features.shape[0]; i++) {
    for (let j = i + 1; j < features.shape[0]; j++) {
      const feature1 = features.slice([i, 0], [1, features.shape[1]]);
      const feature2 = features.slice([j, 0], [1, features.shape[1]]);

      const similarityScore = tf.linalg.norm(feature1.sub(feature2));
      if (similarityScore < similarityThreshold) {
        console.log(`Images ${i} and ${j} are similar with a similarity score of ${similarityScore.arraySync()}.`);
      }
    }
  }
}

runImageSimilarity();

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions