Commit ·
ade4ac9
1
Parent(s): ab10ce8
Added timeit loggings
Browse files- handler.py +15 -15
handler.py
CHANGED
|
@@ -103,31 +103,31 @@ class EndpointHandler:
|
|
| 103 |
def embed_frames_with_xclip_processing(self, frames):
|
| 104 |
# Initialize an empty list to store the frame embeddings
|
| 105 |
|
| 106 |
-
#self.logger.info("Preprocessing frames.")
|
| 107 |
frame_preprocessed = self.preprocess_frames(frames)
|
| 108 |
|
| 109 |
# Pass the preprocessed frame through the model to get the frame embeddings
|
| 110 |
-
#self.logger.info("Getting video features.")
|
| 111 |
frame_embedding = self.model.get_video_features(**frame_preprocessed)
|
| 112 |
|
| 113 |
# Check the shape of the tensor
|
| 114 |
-
#self.logger.info(f"Shape of the batch_emb tensor: {frame_embedding.shape}")
|
| 115 |
|
| 116 |
# Normalize the embeddings if it's a 2D tensor
|
| 117 |
if frame_embedding.dim() == 2:
|
| 118 |
-
#self.logger.info("Normalizing embeddings")
|
| 119 |
batch_emb = torch.nn.functional.normalize(frame_embedding, p=2, dim=1)
|
| 120 |
else:
|
| 121 |
-
#self.logger.info("Skipping normalization due to tensor shape")
|
| 122 |
batch_emb = frame_embedding.squeeze(0)
|
| 123 |
|
| 124 |
-
#self.logger.info("Converting into numpy array")
|
| 125 |
batch_emb = batch_emb.cpu().detach().numpy()
|
| 126 |
|
| 127 |
-
#self.logger.info("Converting to list")
|
| 128 |
batch_emb = batch_emb.tolist()
|
| 129 |
|
| 130 |
-
#self.logger.info("Returning batch_emb list")
|
| 131 |
return batch_emb
|
| 132 |
|
| 133 |
def process_video(self, video_url, video_metadata):
|
|
@@ -136,9 +136,9 @@ class EndpointHandler:
|
|
| 136 |
download_start_time = timeit.default_timer()
|
| 137 |
video_bytes, video_headers = self.download_video_as_bytes(video_url)
|
| 138 |
download_end_time = timeit.default_timer()
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
self.logger.info("Extracting frames.")
|
| 143 |
processing_start_time = timeit.default_timer()
|
| 144 |
frames = self.extract_evenly_spaced_frames_from_bytes(
|
|
@@ -146,15 +146,15 @@ class EndpointHandler:
|
|
| 146 |
)
|
| 147 |
processing_end_time = timeit.default_timer()
|
| 148 |
self.logger.info(
|
| 149 |
-
|
| 150 |
-
|
| 151 |
self.logger.info("Embedding frames with Xclip.")
|
| 152 |
embedding_start_time = timeit.default_timer()
|
| 153 |
frame_embeddings = self.embed_frames_with_xclip_processing(frames)
|
| 154 |
embedding_end_time = timeit.default_timer()
|
| 155 |
self.logger.info(
|
| 156 |
-
|
| 157 |
-
|
| 158 |
video_metadata["url"] = video_url
|
| 159 |
self.logger.info("Returning embeddings and metadata.")
|
| 160 |
return frame_embeddings, video_metadata
|
|
|
|
| 103 |
def embed_frames_with_xclip_processing(self, frames):
|
| 104 |
# Initialize an empty list to store the frame embeddings
|
| 105 |
|
| 106 |
+
# self.logger.info("Preprocessing frames.")
|
| 107 |
frame_preprocessed = self.preprocess_frames(frames)
|
| 108 |
|
| 109 |
# Pass the preprocessed frame through the model to get the frame embeddings
|
| 110 |
+
# self.logger.info("Getting video features.")
|
| 111 |
frame_embedding = self.model.get_video_features(**frame_preprocessed)
|
| 112 |
|
| 113 |
# Check the shape of the tensor
|
| 114 |
+
# self.logger.info(f"Shape of the batch_emb tensor: {frame_embedding.shape}")
|
| 115 |
|
| 116 |
# Normalize the embeddings if it's a 2D tensor
|
| 117 |
if frame_embedding.dim() == 2:
|
| 118 |
+
# self.logger.info("Normalizing embeddings")
|
| 119 |
batch_emb = torch.nn.functional.normalize(frame_embedding, p=2, dim=1)
|
| 120 |
else:
|
| 121 |
+
# self.logger.info("Skipping normalization due to tensor shape")
|
| 122 |
batch_emb = frame_embedding.squeeze(0)
|
| 123 |
|
| 124 |
+
# self.logger.info("Converting into numpy array")
|
| 125 |
batch_emb = batch_emb.cpu().detach().numpy()
|
| 126 |
|
| 127 |
+
# self.logger.info("Converting to list")
|
| 128 |
batch_emb = batch_emb.tolist()
|
| 129 |
|
| 130 |
+
# self.logger.info("Returning batch_emb list")
|
| 131 |
return batch_emb
|
| 132 |
|
| 133 |
def process_video(self, video_url, video_metadata):
|
|
|
|
| 136 |
download_start_time = timeit.default_timer()
|
| 137 |
video_bytes, video_headers = self.download_video_as_bytes(video_url)
|
| 138 |
download_end_time = timeit.default_timer()
|
| 139 |
+
self.logger.info(
|
| 140 |
+
f"Video downloading took {download_end_time - download_start_time} seconds"
|
| 141 |
+
)
|
| 142 |
self.logger.info("Extracting frames.")
|
| 143 |
processing_start_time = timeit.default_timer()
|
| 144 |
frames = self.extract_evenly_spaced_frames_from_bytes(
|
|
|
|
| 146 |
)
|
| 147 |
processing_end_time = timeit.default_timer()
|
| 148 |
self.logger.info(
|
| 149 |
+
f"Extracting video frames took {processing_end_time - processing_start_time} seconds"
|
| 150 |
+
)
|
| 151 |
self.logger.info("Embedding frames with Xclip.")
|
| 152 |
embedding_start_time = timeit.default_timer()
|
| 153 |
frame_embeddings = self.embed_frames_with_xclip_processing(frames)
|
| 154 |
embedding_end_time = timeit.default_timer()
|
| 155 |
self.logger.info(
|
| 156 |
+
f"Embedding calculation took {embedding_end_time - embedding_start_time} seconds"
|
| 157 |
+
)
|
| 158 |
video_metadata["url"] = video_url
|
| 159 |
self.logger.info("Returning embeddings and metadata.")
|
| 160 |
return frame_embeddings, video_metadata
|