Daniel Varga commited on
Commit
9cdc9a1
·
1 Parent(s): 1ce3798

float64->float16 converter, pkl committed

Browse files
Files changed (5) hide show
  1. app.ini +3 -0
  2. app.py +44 -9
  3. convert.py +14 -0
  4. readme.sh +9 -2
  5. sample_fbi_s1e1.f16.pkl +3 -0
app.ini ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [DEFAULT]
2
+ pkl = sample_fbi_s1e1.f16.pkl
3
+ url = https://static.renyi.hu/ai-shared/daniel/sameenergy/
app.py CHANGED
@@ -1,17 +1,54 @@
1
  import sys
 
 
 
 
2
  import gradio as gr
3
  import numpy as np
4
- import pickle
5
- import clip
6
  import torch
 
 
 
 
 
7
 
 
 
 
 
 
 
8
 
9
- pickle_filename, base_url = sys.argv[1:]
10
- # base_url = "https://static.renyi.hu/ai-shared/daniel/sameenergy/"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  data = pickle.load(open(pickle_filename, "rb"))
14
- embeddings = data["embeddings"]
 
 
 
15
  image_features = torch.Tensor(embeddings)
16
  image_features /= image_features.norm(dim=-1, keepdim=True)
17
 
@@ -70,10 +107,8 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
70
  gallery = gr.Gallery(label="Images", show_label=False, elem_id="gallery"
71
  ).style(columns=4, container=False)
72
 
73
- # demo.load(empty_gallery, None, [gallery, state])
74
-
75
  with gr.Row():
76
- selected = gr.Number(placeholder=0, show_label=False)
77
  image_query_button = gr.Button("Show similar")
78
 
79
  text_query_button.click(image_retrieval_from_text, [text], [gallery, state])
@@ -86,4 +121,4 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
86
 
87
 
88
  if __name__ == "__main__":
89
- demo.launch(height=3000)
 
1
  import sys
2
+ import argparse
3
+ import configparser
4
+
5
+ import pickle
6
  import gradio as gr
7
  import numpy as np
 
 
8
  import torch
9
+ import clip
10
+
11
+
12
+ CONFIG_PATH = "app.ini"
13
+
14
 
15
+ def parse_args():
16
+ parser = argparse.ArgumentParser()
17
+ parser.add_argument('--pkl', type=str, help='input pickle produced by create_embedding.py')
18
+ parser.add_argument('--url', type=str, help='the base URL for the images')
19
+ args = parser.parse_args()
20
+ return args
21
 
22
+
23
+ def parse_config_file():
24
+ config = configparser.ConfigParser()
25
+ config.read(CONFIG_PATH)
26
+ config_args = argparse.Namespace(**config['DEFAULT'])
27
+ return config_args
28
+
29
+
30
+ if len(sys.argv) == 1:
31
+ print(f"no command line arguments, using {CONFIG_PATH}")
32
+ args = parse_config_file()
33
+ else:
34
+ print("using command line arguments, ignoring ini file")
35
+ args = parse_args()
36
+
37
+ assert "pkl" in args and args.pkl is not None
38
+ assert "url" in args and args.url is not None
39
+ assert args.url.endswith("/")
40
+
41
+ print("arguments:", args)
42
+
43
+
44
+ pickle_filename, base_url = args.pkl, args.url
45
 
46
 
47
  data = pickle.load(open(pickle_filename, "rb"))
48
+ # the data might be float16 so that the pkl is small,
49
+ # but we use float32 in-memory to avoid numerical issues.
50
+ # tbh i'm not sure there are any such issues.
51
+ embeddings = data["embeddings"].astype(np.float32)
52
  image_features = torch.Tensor(embeddings)
53
  image_features /= image_features.norm(dim=-1, keepdim=True)
54
 
 
107
  gallery = gr.Gallery(label="Images", show_label=False, elem_id="gallery"
108
  ).style(columns=4, container=False)
109
 
 
 
110
  with gr.Row():
111
+ selected = gr.Number(0, show_label=False)
112
  image_query_button = gr.Button("Show similar")
113
 
114
  text_query_button.click(image_retrieval_from_text, [text], [gallery, state])
 
121
 
122
 
123
  if __name__ == "__main__":
124
+ demo.launch()
convert.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import pickle
3
+ import numpy as np
4
+
5
+ filename, = sys.argv[1:]
6
+ data = pickle.load(open(filename, "rb"))
7
+ embeddings = data["embeddings"]
8
+ filenames = data["filenames"]
9
+ embeddings = embeddings.astype(np.float16)
10
+ data["embeddings"] = embeddings
11
+
12
+ output_filename = filename[:-4] + ".f16.pkl"
13
+ with open(output_filename, "wb") as f:
14
+ pickle.dump(data, f)
readme.sh CHANGED
@@ -10,8 +10,15 @@ scp -q -r -P 2820 sample_fbi_s1e1 hexagon.renyi.hu:./ai-shared/daniel/sameenergy
10
  # https://static.renyi.hu/ai-shared/daniel/sameenergy/sample_fbi_s1e1/x_BRIDGE_ADRIATIC/Dobogoko_Esztergom/Videk_ut_Dobogoko_Esztergom_014.jpg
11
 
12
  # run CLIP:
13
- cat sample_fbi_s1e1.txt | python create_embeddings.py sample_fbi_s1e1.pkl no-thumbs
14
  # -> sample_fbi_s1e1.pkl contains embeddings and filenames.
 
15
 
16
  # gradio app:
17
- python app.py sample_fbi_s1e1.pkl https://static.renyi.hu/ai-shared/daniel/sameenergy/
 
 
 
 
 
 
 
10
  # https://static.renyi.hu/ai-shared/daniel/sameenergy/sample_fbi_s1e1/x_BRIDGE_ADRIATIC/Dobogoko_Esztergom/Videk_ut_Dobogoko_Esztergom_014.jpg
11
 
12
  # run CLIP:
13
+ time cat sample_fbi_s1e1.txt | python create_embeddings.py sample_fbi_s1e1.pkl no-thumbs
14
  # -> sample_fbi_s1e1.pkl contains embeddings and filenames.
15
+ # some 12 images/sec on CPU.
16
 
17
  # gradio app:
18
+ python app.py --pkl sample_fbi_s1e1.pkl --url https://static.renyi.hu/ai-shared/daniel/sameenergy/
19
+ # or
20
+ python app.py
21
+ # ...and then it takes these from app.ini
22
+
23
+ python convert.py sample_fbi_s1e1.pkl
24
+ # -> creates sample_fbi_s1e1.f16.pkl dumbed from float64 to float16.
sample_fbi_s1e1.f16.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6ea091f8e382bc09ede33174285d7e1906046549b7c5291860463e001650175f
3
+ size 6570022