|
12 | 12 | import os |
13 | 13 | import sys |
14 | 14 |
|
| 15 | +import numpy as np |
15 | 16 | from PIL import Image |
16 | 17 | from src.open_clip import create_model_and_transforms, get_tokenizer |
17 | 18 |
|
@@ -84,30 +85,16 @@ def main(args): |
84 | 85 | root = "./" + args.model_name + args.pretrained |
85 | 86 | os.mkdir(root) |
86 | 87 |
|
87 | | - # file = open(root + "/image.txt", "w+") |
88 | | - # file.write(str(image.asnumpy().tolist())) |
89 | | - # file.close() |
90 | | - # |
91 | | - # file = open(root + "/text.txt", "w+") |
92 | | - # file.write(str(text.asnumpy().tolist())) |
93 | | - # file.close() |
94 | | - |
95 | | - file = open(root + "/image_features.txt", "w+") |
96 | | - file.write(str(image_features.asnumpy().tolist())) |
97 | | - file.close() |
98 | | - |
99 | | - file = open(root + "/text_features.txt", "w+") |
100 | | - file.write(str(text_features.asnumpy().tolist())) |
101 | | - file.close() |
| 88 | + # save as np files. |
| 89 | + np.save(os.path.join(root, "image_features.npy"), image_features.asnumpy()) |
| 90 | + np.save(os.path.join(root, "text_features.npy"), text_features.asnumpy()) |
102 | 91 |
|
103 | 92 | image_features /= image_features.norm(dim=-1, keepdim=True) |
104 | 93 | text_features /= text_features.norm(dim=-1, keepdim=True) |
105 | 94 |
|
106 | 95 | text_probs = ops.softmax(100.0 * image_features @ text_features.T, axis=-1) |
107 | 96 |
|
108 | | - file = open(root + "/text_probs.txt", "w+") |
109 | | - file.write(str(text_probs.asnumpy().tolist())) |
110 | | - file.close() |
| 97 | + np.save(os.path.join(root, "text_probs.npy"), text_probs.asnumpy()) |
111 | 98 |
|
112 | 99 |
|
113 | 100 | if __name__ == "__main__": |
|
0 commit comments