|
import sys |
|
from pathlib import Path |
|
sys.path.append(str(Path.cwd())) |
|
from annotation.utils import get_optimal_workers |
|
|
|
import os |
|
import argparse |
|
from typing import List, Dict |
|
from mm_datautils import process_video_frames |
|
from preprocessor import CambrianConfig, CambrianEncoders |
|
import torch |
|
from safetensors.torch import save_file |
|
from collections import defaultdict |
|
import logging |
|
from multiprocessing import cpu_count |
|
from entube_dataset import EnTubeDataset, collate_fn |
|
from torch.utils.data import Dataset, DataLoader |
|
from transformers import BaseImageProcessor |
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
def get_optimal_workers() -> int: |
|
"""Determine the optimal number of workers based on available CPU cores.""" |
|
try: |
|
return max(1, cpu_count() - 1) |
|
except (NotImplementedError, ValueError): |
|
return 1 |
|
|
|
def extract_features(processor: CambrianEncoders, file_path: str, file_name: str) -> Dict[str, torch.Tensor]: |
|
try: |
|
video, image_sizes = process_video_frames(file_path) |
|
image_aux_features_list = processor.prepare_mm_features(images=video, image_sizes=image_sizes) |
|
return { |
|
file_name + '-siglip': image_aux_features_list[0], |
|
file_name + '-dino': image_aux_features_list[1] |
|
} |
|
except Exception as e: |
|
logging.error(f"Error processing {file_path}: {e}") |
|
return {} |
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument( |
|
'--folders', |
|
type=str, |
|
nargs='+', |
|
required=True, |
|
help="List of folder paths to video data" |
|
) |
|
parser.add_argument( |
|
'--output_file', |
|
type = str, |
|
default = 'entube_tensors.safetensors', |
|
help = 'Safetensor file to store embeddings of EnTube dataset by vision encoders' |
|
) |
|
parser.add_argument( |
|
'--config_file', |
|
type = str, |
|
default = 'config.json', |
|
help = 'Path to configuration file of encoders parameters' |
|
) |
|
args = parser.parse_args() |
|
|
|
cambrianConfig = CambrianConfig.from_json_file(args.config_file) |
|
processor = CambrianEncoders(cambrianConfig) |
|
image_processors = [] |
|
if not processor.vision_tower_aux_list[0].is_loaded: |
|
processor.vision_tower_aux_list[0].load_model() |
|
image_processors.append(processor.vision_tower_aux_list[0].image_processor) |
|
|
|
|
|
|
|
|
|
|
|
folder_paths: List[str] = args.folders |
|
data_tensor = dict() |
|
|
|
device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
entube_dataset = EnTubeDataset(folder_paths, image_processors, device) |
|
dataloader = DataLoader( |
|
entube_dataset, |
|
batch_size=4, |
|
collate_fn=collate_fn, |
|
|
|
num_workers=1 |
|
) |
|
|
|
for batch_idx, (videos, image_sizes) in enumerate(dataloader): |
|
print(f"Processing batch {batch_idx + 1}/{len(dataloader)}") |
|
assert isinstance(videos, list), "List of videos features for each processor (vision encoder)" |
|
assert isinstance(videos[0], list) or isinstance(videos[0], torch.Tensor), "List of videos in the batch" |
|
image_aux_features_list = processor.prepare_mm_features(videos, image_sizes) |
|
for i, image_aux_features in enumerate(image_aux_features_list): |
|
print(f"@tcm: In main(): image_aux_features[{i}].shape={image_aux_features.shape}") |
|
break |
|
|
|
|
|
|
|
|