File size: 10,599 Bytes
b1a2536
6f1c3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
OmniCoreX Super Advanced Multi-Modal Data Loader

This module provides robust, scalable, and ultra high-tech utilities for loading,
preprocessing, and batching multi-modal and multi-source knowledge data streams
tailored for OmniCoreX's complex AI architecture.

Features include:

- Flexible support for heterogeneous data sources including text, images, sensor, audio.
- Advanced preprocessing pipelines with caching, augmentation, and normalization.
- Highly efficient synchronized batching for multiple modalities with padding and masking.
- Streaming data integration hooks for real-time AI applications.
- Modular extensibility for new data types and preprocessing strategies.
"""

import os
import random
import json
from typing import Dict, List, Optional, Callable, Tuple, Iterator
from collections import defaultdict
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from PIL import Image
import numpy as np


# -------------------- Preprocessing Functions -------------------- #

def preprocess_text(text: str,
                    tokenizer: Callable[[str], List[int]],
                    max_length: int = 256) -> torch.Tensor:
    """
    Tokenize and pad/truncate text input.

    Args:
        text: Raw input text string.
        tokenizer: Function converting string to list of token IDs.
        max_length: Maximum desired sequence length.

    Returns:
        Tensor of token ids of shape (max_length,)
    """
    token_ids = tokenizer(text)[:max_length]
    pad_length = max_length - len(token_ids)
    padded_ids = token_ids + [0] * pad_length
    return torch.tensor(padded_ids, dtype=torch.long)


def preprocess_image(image_path: str,
                     target_size: Tuple[int, int] = (224, 224),
                     augment: bool = False) -> torch.Tensor:
    """
    Load and preprocess image with resizing, normalization, optional augmentation.

    Args:
        image_path: Path to image file.
        target_size: Desired output image size (width, height).
        augment: Whether to apply augmentation transforms.

    Returns:
        Preprocessed image tensor (3, target_height, target_width)
    """
    transform_list = []
    if augment:
        transform_list.extend([
            transforms.RandomResizedCrop(target_size),
            transforms.RandomHorizontalFlip()
        ])
    else:
        transform_list.append(transforms.Resize(target_size))
        transform_list.append(transforms.CenterCrop(target_size))

    transform_list += [
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ]

    transform = transforms.Compose(transform_list)
    image = Image.open(image_path).convert("RGB")
    return transform(image)


def preprocess_sensor_data(sensor_data: List[float],
                           max_seq_length: int = 100) -> torch.Tensor:
    """
    Normalize and pad sensor time-series data.

    Args:
        sensor_data: Raw 1D sensor readings list.
        max_seq_length: Maximum length for padding/truncation.

    Returns:
        Normalized and padded tensor of shape (max_seq_length,)
    """
    arr = np.array(sensor_data, dtype=np.float32)
    if arr.size == 0:
        arr = np.zeros(max_seq_length, dtype=np.float32)
    else:
        mean = arr.mean() if arr.size > 0 else 0.0
        std = arr.std() if arr.size > 0 else 1.0
        arr = (arr - mean) / (std + 1e-9)
    padded = np.zeros(max_seq_length, dtype=np.float32)
    length = min(max_seq_length, arr.size)
    padded[:length] = arr[:length]
    return torch.tensor(padded)


# -------------------- Dataset Definition -------------------- #

class OmniCoreXMultiModalDataset(Dataset):
    """
    Dataset supporting multi-source, multi-modal knowledge streams integrated seamlessly.

    Expects metadata JSON containing sample entries with modality-specific data references.

    Capable of handling missing data gracefully with sensible fallbacks.
    """

    def __init__(self,
                 metadata_path: str,
                 modalities: List[str],
                 tokenizer: Optional[Callable[[str], List[int]]] = None,
                 text_max_length: int = 256,
                 image_size: Tuple[int, int] = (224, 224),
                 sensor_max_length: int = 100,
                 augmentation: bool = False):
        """
        Initialize the dataset.

        Args:
            metadata_path: Path to JSON file listing dataset samples.
            modalities: Modalities to be loaded (e.g., ['text', 'image', 'sensor']).
            tokenizer: Optional tokenizer callable for text processing.
            text_max_length: Max sequence length for text.
            image_size: Target resolution for images.
            sensor_max_length: Max length for sensor sequences.
            augmentation: Whether to apply augmentation on images.
        """
        super().__init__()
        self.modalities = modalities
        self.tokenizer = tokenizer
        self.text_max_length = text_max_length
        self.image_size = image_size
        self.sensor_max_length = sensor_max_length
        self.augmentation = augmentation

        with open(metadata_path, "r", encoding="utf-8") as f:
            self.metadata = json.load(f)

        if not isinstance(self.metadata, list):
            raise ValueError("Metadata JSON must be a list of samples")

    def __len__(self):
        return len(self.metadata)

    def __getitem__(self, idx):
        sample_meta = self.metadata[idx]
        sample = {}

        if 'text' in self.modalities:
            text = sample_meta.get("text", "")
            if self.tokenizer:
                sample['text'] = preprocess_text(text, self.tokenizer, self.text_max_length)
            else:
                sample['text'] = torch.tensor([], dtype=torch.long)  # empty placeholder

        if 'image' in self.modalities:
            image_path = sample_meta.get("image", "")
            if image_path and os.path.exists(image_path):
                sample['image'] = preprocess_image(image_path, self.image_size, self.augmentation)
            else:
                sample['image'] = torch.zeros(3, self.image_size[1], self.image_size[0])

        if 'sensor' in self.modalities:
            sensor_raw = sample_meta.get("sensor", [])
            sample['sensor'] = preprocess_sensor_data(sensor_raw, self.sensor_max_length)

        # Extend here for additional modalities like audio, video, tabular etc.

        return sample


# -------------------- Collate Function -------------------- #

def multimodal_collate_fn(batch: List[Dict]) -> Dict[str, torch.Tensor]:
    """
    Collates a batch of multi-modal samples into batched tensors with proper stacking.

    Args:
        batch: List of individual modality dictionaries.

    Returns:
        Dict mapping modality to batched tensors.
    """
    collated = defaultdict(list)
    for sample in batch:
        for modality, data in sample.items():
            collated[modality].append(data)

    # Convert lists to batched tensors
    for modality in collated:
        if isinstance(collated[modality][0], torch.Tensor):
            collated[modality] = torch.stack(collated[modality])
        else:
            # For raw types (e.g. strings), keep as list
            collated[modality] = list(collated[modality])

    return dict(collated)


# -------------------- DataLoader Factory -------------------- #

def create_omncorex_dataloader(metadata_path: str,
                              modalities: List[str],
                              tokenizer: Optional[Callable[[str], List[int]]] = None,
                              text_max_length: int = 256,
                              image_size: Tuple[int, int] = (224, 224),
                              sensor_max_length: int = 100,
                              batch_size: int = 16,
                              shuffle: bool = True,
                              num_workers: int = 4,
                              augmentation: bool = False) -> DataLoader:
    """
    Constructs a PyTorch DataLoader over the OmniCoreX multimodal dataset.

    Args:
        metadata_path: Path to dataset metadata JSON.
        modalities: Modalities to load.
        tokenizer: Optional tokenizer callable.
        text_max_length: Max token length for text.
        image_size: Image resize resolution.
        sensor_max_length: Max sequence length for sensor data.
        batch_size: Batch size.
        shuffle: Shuffle dataset if True.
        num_workers: Number of worker subprocesses.
        augmentation: Whether to apply data augmentation (image).

    Returns:
        Configured PyTorch DataLoader instance.
    """
    dataset = OmniCoreXMultiModalDataset(
        metadata_path=metadata_path,
        modalities=modalities,
        tokenizer=tokenizer,
        text_max_length=text_max_length,
        image_size=image_size,
        sensor_max_length=sensor_max_length,
        augmentation=augmentation
    )
    loader = DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=shuffle,
        num_workers=num_workers,
        collate_fn=multimodal_collate_fn,
        pin_memory=True
    )
    return loader


# -------------------- Example Test Run -------------------- #

if __name__ == "__main__":
    import tempfile

    # Dummy tokenizer: simple char-index tokenizer
    def dummy_tokenizer(text):
        return [ord(c) % 100 + 1 for c in text]

    # Create dummy metadata JSON file
    dummy_samples = [
        {
            "text": "OmniCoreX is the ultimate AI brain.",
            "image": "",        # No image file - fallback to zeros
            "sensor": [random.uniform(-1, 1) for _ in range(50)]
        },
        {
            "text": "Testing multi-modal data loader.",
            "sensor": [random.uniform(-0.5, 0.5) for _ in range(80)]
        }
    ]
    with tempfile.NamedTemporaryFile("w", delete=False, suffix=".json") as tmpf:
        json.dump(dummy_samples, tmpf)
        metadata_path = tmpf.name

    dataloader = create_omncorex_dataloader(
        metadata_path=metadata_path,
        modalities=["text", "image", "sensor"],
        tokenizer=dummy_tokenizer,
        batch_size=2,
        augmentation=True,
        shuffle=False,
        num_workers=0
    )

    for batch in dataloader:
        print("Batch data keys:", batch.keys())
        for modality, tensor in batch.items():
            print(f"Modality: {modality}, Tensor shape: {tensor.shape}")
        break