sohojoe commited on
Commit
3e4f32c
1 Parent(s): bc10f03

create prototype for is someone is there

Browse files
charles_actor.py CHANGED
@@ -44,6 +44,11 @@ class CharlesActor:
44
  # "hello, how are you today?",
45
  # "hmm, interesting, tell me more about that.",
46
  ]
 
 
 
 
 
47
  print("010")
48
  self._needs_init = True
49
  self._state = "Initialized"
@@ -99,8 +104,12 @@ class CharlesActor:
99
  total_video_frames += 1
100
  skipped_video_frames += (len(video_frames) -1)
101
  image_as_array = video_frames[-1]
102
- last_frame_vector = self._clip_transform.image_to_embeddings(image_as_array)
103
- vector_debug = f"Last frame vector: {last_frame_vector.shape}"
 
 
 
 
104
 
105
  await asyncio.sleep(0.01)
106
  loops+=1
 
44
  # "hello, how are you today?",
45
  # "hmm, interesting, tell me more about that.",
46
  ]
47
+
48
+ print("004")
49
+ print("creating prototypes")
50
+ from prototypes import Prototypes
51
+ self._prototypes = Prototypes()
52
  print("010")
53
  self._needs_init = True
54
  self._state = "Initialized"
 
104
  total_video_frames += 1
105
  skipped_video_frames += (len(video_frames) -1)
106
  image_as_array = video_frames[-1]
107
+ image_vector = self._clip_transform.image_to_embeddings(image_as_array)
108
+ # image_vector = image_vector.unsqueeze(0)
109
+ image_vector = image_vector[0]
110
+ print(f"image_vector.shape: {image_vector.shape}")
111
+ distances, closest_item_key, distance_debug_str = self._prototypes.get_distances(image_vector)
112
+ vector_debug = f"{closest_item_key} {distance_debug_str}"
113
 
114
  await asyncio.sleep(0.01)
115
  loops+=1
clip_transform.py CHANGED
@@ -48,6 +48,13 @@ class CLIPTransform:
48
  image_embeddings = self.model.encode_image(prepro)
49
  image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
50
  return(image_embeddings)
 
 
 
 
 
 
 
51
 
52
  def preprocessed_image_to_emdeddings(self, prepro):
53
  with torch.no_grad():
 
48
  image_embeddings = self.model.encode_image(prepro)
49
  image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
50
  return(image_embeddings)
51
+
52
+ def pil_image_to_embeddings(self, input_im):
53
+ prepro = self.preprocess(input_im).unsqueeze(0).to(self.device)
54
+ with torch.no_grad():
55
+ image_embeddings = self.model.encode_image(prepro)
56
+ image_embeddings /= image_embeddings.norm(dim=-1, keepdim=True)
57
+ return(image_embeddings)
58
 
59
  def preprocessed_image_to_emdeddings(self, prepro):
60
  with torch.no_grad():
prototypes.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+
4
+ import torch
5
+ from clip_transform import CLIPTransform
6
+ from PIL import Image
7
+
8
+ from torch.nn import functional as F
9
+
10
+ class Prototypes:
11
+ def __init__(self):
12
+ self._clip_transform = CLIPTransform()
13
+ self._prepare_prototypes()
14
+
15
+
16
+ def _prepare_prototypes(self):
17
+ image_embeddings = self.load_images_from_folder('prototypes')
18
+ assert image_embeddings is not None, "no image embeddings found"
19
+ assert len(image_embeddings) > 0, "no image embeddings found"
20
+ person_keys = [key for key in image_embeddings.keys() if key.startswith('person-')]
21
+ no_person_keys = [key for key in image_embeddings.keys() if key.startswith('no_person-')]
22
+ person_keys.sort()
23
+ no_person_keys.sort()
24
+ # create pytorch vector of person embeddings
25
+ person_embeddings = torch.cat([image_embeddings[key] for key in person_keys])
26
+ # create pytorch vector of no_person embeddings
27
+ no_person_embeddings = torch.cat([image_embeddings[key] for key in no_person_keys])
28
+ person_embedding = person_embeddings.mean(dim=0)
29
+ person_embedding /= person_embedding.norm(dim=-1, keepdim=True)
30
+ no_person_embedding = no_person_embeddings.mean(dim=0)
31
+ no_person_embedding /= no_person_embedding.norm(dim=-1, keepdim=True)
32
+
33
+ self.prototype_keys = ["person", "no_person"]
34
+ self.prototypes = torch.stack([person_embedding, no_person_embedding])
35
+
36
+
37
+ def load_images_from_folder(self, folder):
38
+ image_embeddings = {}
39
+ supported_filetypes = ['.jpg','.png','.jpeg']
40
+ for filename in os.listdir(folder):
41
+ if not any([filename.endswith(ft) for ft in supported_filetypes]):
42
+ continue
43
+ image = Image.open(os.path.join(folder,filename))
44
+ embeddings = self._clip_transform.pil_image_to_embeddings(image)
45
+ image_embeddings[filename] = embeddings
46
+ return image_embeddings
47
+
48
+ def get_distances(self, embeddings):
49
+ # case not normalized
50
+ # distances = F.cosine_similarity(embeddings, self.prototypes)
51
+ # case normalized
52
+ distances = embeddings @ self.prototypes.T
53
+ closest_item_idex = distances.argmax().item()
54
+ closest_item_key = self.prototype_keys[closest_item_idex]
55
+ debug_str = ""
56
+ for key, value in zip(self.prototype_keys, distances):
57
+ debug_str += f"{key}: {value.item():.2f}, "
58
+ return distances, closest_item_key, debug_str
59
+
60
+
61
+ if __name__ == "__main__":
62
+ prototypes = Prototypes()
63
+ print ("prototypes:")
64
+ for key, value in zip(prototypes.prototype_keys, prototypes.prototypes):
65
+ print (f"{key}: {len(value)}")
66
+
67
+ embeddings = prototypes.prototypes[0]
68
+ distances, closest_item_key, debug_str = prototypes.get_distances(embeddings)
69
+ print (f"closest_item_key: {closest_item_key}")
70
+ print (f"distances: {debug_str}")
71
+ print ("done")
prototypes/no_person-001.jpg ADDED

Git LFS Details

  • SHA256: 99eeca5a54c32812075b07aca0e5e6f074776b9fce917274fe0d37bdbee31437
  • Pointer size: 130 Bytes
  • Size of remote file: 21 kB
prototypes/no_person-002.jpg ADDED

Git LFS Details

  • SHA256: a760b9a95b9a2efd9aa6332b5db4bc0790686931ff57cbea701b9f53d9222253
  • Pointer size: 129 Bytes
  • Size of remote file: 8.76 kB
prototypes/no_person-003.jpg ADDED

Git LFS Details

  • SHA256: 3a096e67ea9fcada606931e2cf8969c565e55c227ac1c7b322f0063bc21cb39a
  • Pointer size: 130 Bytes
  • Size of remote file: 18.4 kB
prototypes/no_person-004.jpg ADDED

Git LFS Details

  • SHA256: 7c6b72b5fe52a140dda84805082731dc155b6ba8fcace8d606ecead11d323283
  • Pointer size: 130 Bytes
  • Size of remote file: 19.3 kB
prototypes/no_person-005.jpg ADDED

Git LFS Details

  • SHA256: 58c63ee8ef8cda0c717cd088c0977c5c218d39dd76af9c935dda91b789b67a79
  • Pointer size: 130 Bytes
  • Size of remote file: 13.6 kB
prototypes/no_person-006.jpg ADDED

Git LFS Details

  • SHA256: 49f9b0c72d3da45b5d10a6c25b9a098e45a433ba23f2d1d3e541a19a8c6394f3
  • Pointer size: 129 Bytes
  • Size of remote file: 6.09 kB
prototypes/no_person-007.jpg ADDED

Git LFS Details

  • SHA256: 39791c98b14fcff519ef754e5e3f3fe6a35170bd5177c567951d7aae373e8c8a
  • Pointer size: 130 Bytes
  • Size of remote file: 10.3 kB
prototypes/no_person-008.jpg ADDED

Git LFS Details

  • SHA256: 13d9c3657b18aa1fcdc2444858cec46a424fccc0fe70115b21eb3c0b58eed5f4
  • Pointer size: 129 Bytes
  • Size of remote file: 7.72 kB
prototypes/no_person-009.jpg ADDED

Git LFS Details

  • SHA256: 039ef7a1bbe79054e3c658e5576bdff322f1c8511f6ae79ed936377055c51dfb
  • Pointer size: 129 Bytes
  • Size of remote file: 8.87 kB
prototypes/no_person-010.jpg ADDED

Git LFS Details

  • SHA256: a6b75df2cd6fcb18c3433d41d0161c23176046d09e7111a5f85cf278f4c4c6cc
  • Pointer size: 129 Bytes
  • Size of remote file: 9.19 kB
prototypes/no_person-011.jpg ADDED

Git LFS Details

  • SHA256: cb1c79654bb16fc767cb65917fcd7d753896e62e9f99a4c9743b1cf1c876d70d
  • Pointer size: 129 Bytes
  • Size of remote file: 9.7 kB
prototypes/no_person-012.jpg ADDED

Git LFS Details

  • SHA256: 0b1515d7e1bb1d7b890587517bfb004a8607aea96ef81f3a0cc2c47c6444310e
  • Pointer size: 130 Bytes
  • Size of remote file: 16 kB
prototypes/no_person-013.jpg ADDED

Git LFS Details

  • SHA256: 9b53d70cff122a1fa7ac644e24eaf752551e3b2e614cb9d3f2db5aac6b0fe566
  • Pointer size: 129 Bytes
  • Size of remote file: 9.63 kB
prototypes/no_person-014.jpg ADDED

Git LFS Details

  • SHA256: ab90850ae52e907625bb79856e33c6f778001a5b8ba72cdf4eb08aeac695cf38
  • Pointer size: 129 Bytes
  • Size of remote file: 9.11 kB
prototypes/no_person-015.jpg ADDED

Git LFS Details

  • SHA256: 2f06b61ef731f83e53a27e911b8714fb37f16cdec4204ccae0a6999874e8ce8b
  • Pointer size: 129 Bytes
  • Size of remote file: 7.55 kB
prototypes/no_person-016.jpg ADDED

Git LFS Details

  • SHA256: ec78a6ca0eef451f98a1312b3f5b6f5714185fb670119d93c838807a5c3a3ddf
  • Pointer size: 130 Bytes
  • Size of remote file: 19.6 kB
prototypes/no_person-017.jpg ADDED

Git LFS Details

  • SHA256: d7be10b95195ccdfbe030c9a7a4d628d4983e7a6b0aef0b75d5632e24493c1d6
  • Pointer size: 129 Bytes
  • Size of remote file: 8.06 kB
prototypes/no_person-018.jpg ADDED

Git LFS Details

  • SHA256: 05728496f9fc9ccbec130bd3fa422cc2602e1efccc5d01c4ff18dada19765669
  • Pointer size: 130 Bytes
  • Size of remote file: 13.4 kB
prototypes/no_person-019.jpg ADDED

Git LFS Details

  • SHA256: a11b8ed175c80be1b04bd0c85ede5e6b2440af35d22f96a0f1923474422e1e85
  • Pointer size: 129 Bytes
  • Size of remote file: 5.13 kB
prototypes/no_person-020.jpg ADDED

Git LFS Details

  • SHA256: 47391386b25691ed31b45d5202c1699ac44c786d16041de987d12c4b1b6381bc
  • Pointer size: 129 Bytes
  • Size of remote file: 4.9 kB
prototypes/person-001.jpg ADDED

Git LFS Details

  • SHA256: 502647fd2ce8fdf8b22d451c8642e19c0a969c4f120daa1e56687af100b9efe5
  • Pointer size: 130 Bytes
  • Size of remote file: 21.8 kB
prototypes/person-002.jpg ADDED

Git LFS Details

  • SHA256: 8c2ac0d3ff0178e9ebaed5c399cea0dc5d7c10d98f6562f67f8af2d4f184e2d2
  • Pointer size: 130 Bytes
  • Size of remote file: 15.1 kB
prototypes/person-003.jpg ADDED

Git LFS Details

  • SHA256: bf0c6af1975cd76655114be95fd5dc8b45103869868ae2202945fd1d886d43f2
  • Pointer size: 130 Bytes
  • Size of remote file: 23 kB
prototypes/person-004.jpg ADDED

Git LFS Details

  • SHA256: 7e3dc2906708413642fad6ccc582253e1c2709e9fce7f337a03de4e41f9f684b
  • Pointer size: 130 Bytes
  • Size of remote file: 23.8 kB
prototypes/person-005.jpg ADDED

Git LFS Details

  • SHA256: c3881846d4b3de9cd9698546cbe206ec5ecba1e38cd3e6c1d300b35161384bd6
  • Pointer size: 130 Bytes
  • Size of remote file: 20.5 kB
prototypes/person-006.jpg ADDED

Git LFS Details

  • SHA256: 35fea1b78436ddb0816dc9f69d407b18b78fcffce56edc371a71a39041381440
  • Pointer size: 129 Bytes
  • Size of remote file: 9.38 kB
prototypes/person-007.jpg ADDED

Git LFS Details

  • SHA256: e923183b9400878e2ab81f6f264e64e5d792959b9ab8cec4c0042e7f5b8723b7
  • Pointer size: 130 Bytes
  • Size of remote file: 11.1 kB
prototypes/person-008.jpg ADDED

Git LFS Details

  • SHA256: f3dae323cbb170f987e00325b254158f04e065d473a3335cf70b579c9b8c4023
  • Pointer size: 130 Bytes
  • Size of remote file: 10.2 kB
prototypes/person-009.jpg ADDED

Git LFS Details

  • SHA256: bc5c0e3cd64e555af03bdd44a0f488a5f3b8ab5cdc05271366be473a5f34f754
  • Pointer size: 129 Bytes
  • Size of remote file: 9.8 kB
prototypes/person-010.jpg ADDED

Git LFS Details

  • SHA256: 332e10b368c4907da98a55bb49f142e67dfbc2971af0476d2abd7edfc5889d8b
  • Pointer size: 130 Bytes
  • Size of remote file: 10.1 kB
prototypes/person-011.jpg ADDED

Git LFS Details

  • SHA256: d05e98b7cbbeb8a999dae95ba8514e023de9d5808406928626230f1555188e16
  • Pointer size: 130 Bytes
  • Size of remote file: 10.5 kB
prototypes/person-012.jpg ADDED

Git LFS Details

  • SHA256: c1626db7731c0556f1d6d503980b2d645fbece7ba23e0a508f52af3c84d93e18
  • Pointer size: 130 Bytes
  • Size of remote file: 26.2 kB
prototypes/person-013.jpg ADDED

Git LFS Details

  • SHA256: 109b3982845bc1dce942a4c96ee319281ecde51b9b4e36183e09d88278dd4216
  • Pointer size: 130 Bytes
  • Size of remote file: 16.7 kB
prototypes/person-014.jpg ADDED

Git LFS Details

  • SHA256: fada380c7a223abe571c98b0b284c65ca7f055d549612d9b239e5a2154da6af0
  • Pointer size: 130 Bytes
  • Size of remote file: 15.4 kB
prototypes/person-015.jpg ADDED

Git LFS Details

  • SHA256: 990af66a3e229d7892743779f7343dc15e24b51a4804363e8129e5e2f8f6f7cb
  • Pointer size: 130 Bytes
  • Size of remote file: 13.5 kB
prototypes/person-016.jpg ADDED

Git LFS Details

  • SHA256: c6ffb03a9e6ed6d1828cf0cc69c341fb912e7091dc6e8c1fc2f1b19301aff62a
  • Pointer size: 130 Bytes
  • Size of remote file: 23.2 kB
prototypes/person-017.jpg ADDED

Git LFS Details

  • SHA256: e9a5240e272893771f25df22f80117a0aabfdfe3564a2e42c04703d990c2a802
  • Pointer size: 130 Bytes
  • Size of remote file: 14.7 kB
prototypes/person-018.jpg ADDED

Git LFS Details

  • SHA256: 85384d8c05e1712db05d584a36977dc90adbf9b27feed5f81c6ef86de5272a04
  • Pointer size: 130 Bytes
  • Size of remote file: 15.7 kB
prototypes/person-019.jpg ADDED

Git LFS Details

  • SHA256: ba4bf538b25a9d30a17e48abf5fc64300f304dd92ade25fad8f64634c468bd90
  • Pointer size: 130 Bytes
  • Size of remote file: 10.8 kB
prototypes/person-020.jpg ADDED

Git LFS Details

  • SHA256: 2eaa06b6b4f8fdf122d8b55a287ace3f0a8ce1b8da21efaa66f6d1131f8065f0
  • Pointer size: 130 Bytes
  • Size of remote file: 11.5 kB