12 imgdir=
'Objects365/images/val'
13 txtdir=
'Objects365/labels/val'
15 txt_embeds = np.ndarray((numtexts, 512), np.float32)
17 images = np.ndarray((numimages, 3, height, width), np.float32)
18 txt = np.ndarray((numimages, nc, 512), np.float32)
20 txt0 = np.ndarray((numimages, 4, 32, nc), np.float32)
21 txt1 = np.ndarray((numimages, 2, 32, nc), np.float32)
22 txt2 = np.ndarray((numimages, 4, 32, nc), np.float32)
23 txt3 = np.ndarray((numimages, 8, 32, nc), np.float32)
24 txt4 = np.ndarray((numimages, nc, 512), np.float32)
29 print(
"Loading all text embeddings...")
31 for i
in range(numtexts):
32 fname = f
"text_vec_{i}.npy"
33 if os.path.isfile(fname):
35 e /= np.linalg.norm(e[0,:])
36 txt_embeds[idx, :] = e
39 print(f
"Cannot load {fname}")
43 print(
"Processing images...")
44 allimages = list(filter(
lambda f: f.endswith(
'.jpg'), os.listdir(imgdir)))
45 selected_images = random.sample(allimages, numimages)
48 session = ort.InferenceSession(f
"yolov8s-jevois-{width}x{height}-{nc}c-txt.onnx")
52 for imgname
in selected_images:
53 fname = os.path.join(imgdir, imgname)
54 if not os.path.isfile(fname):
55 print(f
"Cannot load {fname}")
58 image = Image.open(fname).resize((width, height));
59 arr = np.array(image).astype(np.float32)
60 arr = (arr - 0.0) / 255.0
61 arr = np.transpose(arr, (2, 0, 1))
62 images[idx, :, :, :] = arr
65 labelpath = os.path.join(txtdir, imgname.replace(
'.jpg',
'.txt'))
66 x = subprocess.run(
"cat " + str(labelpath) +
" | awk '{ print $1 }' | sort -n | uniq",
67 shell=
True, capture_output=
True, text=
True)
68 categs_str = x.stdout.splitlines()
69 categs = [int(c)
for c
in categs_str]
71 print(f
"categs in {labelpath}: {categs}")
73 random.shuffle(categs)
74 extra = list(range(numtexts))
79 print(f
"selected categs in {labelpath}: {categs}")
82 txt[idx, i, :] = txt_embeds[categs[i]]
85 txtin = txt[idx, :, :]
86 txtin = np.expand_dims(txtin, axis=0)
88 outs = session.run(
None, {
"txt_feats": txtin })
89 txt0[idx, :, :, :] = outs[0]
90 txt1[idx, :, :, :] = outs[1]
91 txt2[idx, :, :, :] = outs[2]
92 txt3[idx, :, :, :] = outs[3]
93 txt4[idx, :, :] = outs[4]
97 with open(f
"objects365-images-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, images)
98 with open(f
"objects365-texts-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt)
99 with open(f
"objects365-txt0-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt0)
100 with open(f
"objects365-txt1-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt1)
101 with open(f
"objects365-txt2-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt2)
102 with open(f
"objects365-txt3-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt3)
103 with open(f
"objects365-txt4-{width}x{height}-{nc}.npy",
'wb')
as f: np.save(f, txt4)