Code Monkey home page Code Monkey logo

Comments (2)

dqshuai avatar dqshuai commented on August 15, 2024 2

this is my way to generate the embeddings.

with open(os.path.join(os.path.join(root,'CUB_200_2011'),'images.txt'),'r') as f:
    for line in f:
        image_id,file_name = line.split()
        text_file = file_name.replace('.jpg','.txt')
        text_file = text_root + text_file
        text_list = []
        with open(text_file,'r') as f_text:
            for line in f_text:
                line = line.encode(encoding='UTF-8',errors='strict')
                line = line.replace(b'\xef\xbf\xbd\xef\xbf\xbd',b' ')
                line = line.decode('UTF-8','strict')
                text_list.append(line) 
        inputs = tokenizer(text_list, return_tensors="pt",padding="max_length",truncation=True, max_length=32)
        outputs = model(**inputs)
        embedding_mean = outputs[1].mean(dim=0).reshape(1,-1).detach().numpy()
        embedding_full = outputs[1].detach().numpy()
        embedding_words = outputs[0].detach().numpy()
        data_dict = {
            'embedding_mean':embedding_mean,
            'embedding_full':embedding_full,
            'embedding_words':embedding_words,
        }
        class_name,image_name = file_name.split('/')
        if not os.path.exists(os.path.join(embedding_root,class_name)):
            os.makedirs(os.path.join(embedding_root,class_name))
        embedding_file_path = os.path.join(os.path.join(embedding_root,class_name),image_name.replace('.jpg','.pickle'))
        with open(embedding_file_path,'wb') as f_write:
            pickle.dump(data_dict,f_write)

Moreover,bert_embedding is embeding of cub which is generated. Hope it helps you!

from metaformer.

BakingBrains avatar BakingBrains commented on August 15, 2024

Thanks a lot @dqshuai. Awesome work.
This solved my doubt.

Thank You

from metaformer.

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.