Git Product home page Git Product logo

Comments (5)

XinyueZ avatar XinyueZ commented on August 24, 2024 4
class SimilarityCalculator:
    def __init__(self, device):
        self.device = device

    def __call__(self, embeddings):
        raise NotImplementedError

    @staticmethod
    def create_instance(similarity_type, device):
        if similarity_type == SimilarityCalculatorType.COSINE:
            return CosineSimilarity(device)
        elif similarity_type == SimilarityCalculatorType.SOFTMAX:
            return SoftmaxSimilarity(device)
        else:
            raise ValueError(f"Unknown similarity type: {similarity_type}")


class CosineSimilarity(SimilarityCalculator):
    def __init__(self, device):
        super().__init__(device)

    def __call__(self, embeddings):
        preds = cosine_similarity(
            embeddings[ModalityType.VISION].cpu().numpy(),
            embeddings[ModalityType.TEXT].cpu().numpy(),
        )
        preds = torch.from_numpy(preds).to(self.device)
        print(
            preds.shape,
            "\n",
            preds,
            "max: ",
            preds.max(dim=0),
            "min: ",
            preds.min(dim=0),
        )
        return preds


class SoftmaxSimilarity(SimilarityCalculator):
    def __init__(self, device):
        super().__init__(device)

    def __call__(self, embeddings):
        preds = torch.softmax(
            embeddings[ModalityType.VISION] @ embeddings[ModalityType.TEXT].T,
            dim=0,
        )
        print(
            preds.shape,
            "\n",
            preds,
            "sum: ",
            preds.sum(dim=0),
            "max: ",
            preds.max(dim=0),
            "min: ",
            preds.min(dim=0),
        )
        return preds


    def _infer(self, vision, text, threshold=0.5): 
        inputs = {
            ModalityType.VISION: ImageBindClassifier._load_and_transform_vision_data_np(
                vision,
                self.device + ":1" if self.device == "cuda" else self.device,
            ),
            ModalityType.TEXT: ImageBindClassifier._load_and_transform_text(
                text,
                self.bpe_path,
                self.device + ":0" if self.device == "cuda" else self.device,
            ),
        }

        with torch.no_grad():
            embeddings = self.model(inputs)

        preds = self.similarity_calculator(embeddings) # softmax OR from sklearn.metrics.pairwise import cosine_similarity
        return preds

@bakachan19

from imagebind.

DaNious avatar DaNious commented on August 24, 2024 1

I'm also confused about the example codes, but I guess it's ok. It looks like that softmax here is just used to demonstrate the similarity between different modalities provided by ImageBind.

from imagebind.

bakachan19 avatar bakachan19 commented on August 24, 2024 1

Thank you so so much @XinyueZ.

from imagebind.

XinyueZ avatar XinyueZ commented on August 24, 2024

Yeah, agree with @DaNious, tbh, I did the similarity comparison with the cosine_similarity, and also get a proper result.
The example with "softmax" could, maybe I was wrong, people will get confused with the "activation" concept during NN forward, which represents the probability.

from imagebind.

bakachan19 avatar bakachan19 commented on August 24, 2024

@XinyueZ, I am interested in computing the text x image similarity. But I am confused on how to do this.
Could you please share an example code?
Much appreciated.
Thank you!

from imagebind.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.