File size: 1,059 Bytes
4bf425b
2cf3514
587ab22
 
2cf3514
587ab22
4bf425b
 
2cf3514
4bf425b
 
 
587ab22
4bf425b
587ab22
4bf425b
 
 
 
 
 
 
 
 
 
 
c67d7a6
4bf425b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import argparse
import csv
import numpy as np
import os

from config import MODEL_LIST
from utils import load_model


def main(args):
    caption_txt_path = args.text_path
    f = open(caption_txt_path)
    captions = [sent.strip() for sent in f.readlines()]

    for model_name in MODEL_LIST:
        model, processor = load_model(f"koclip/{model_name}")
        captions_processed = [processor(sent,images=None,return_tensors='jax') for sent in captions]
        vec = [np.asarray(model.get_text_features(**c)) for c in captions_processed]

        with open(os.path.join(args.out_path, f"{model_name}.tsv"), "a+") as f:
            writer = csv.writer(f, delimiter="\t")
            for text, feature in zip(captions, vec):
                writer.writerow([text, ",".join(map(lambda x: str(x), feature))])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--text_path", default="cifar100_captions_kr.txt")
    parser.add_argument("--out_path", default="features/text")
    args = parser.parse_args()
    main(args)