File size: 13,921 Bytes
22c1c50 | 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | """
Multimodal Robot Anomaly Detection Dataset Loader
Data Path Structure:
DATA_PATH/
├── train/
│ ├── Annotations/
│ │ ├── Normal_data/
│ │ │ └── {point_name}/
│ │ │ ├── {point_name}_visible_{timestamp}_frame_{frame_id}.jpg
│ │ │ ├── {point_name}_visible_{timestamp}_frame_{frame_id}.json
│ │ │ └── {point_name}_visible_{timestamp}_frame_{frame_id}.txt
│ │ └── Anomaly_data/
│ │ └── {anomaly_name}/
│ │ ├── {anomaly_name}.jpg
│ │ ├── {anomaly_name}.json
│ │ └── {anomaly_name}.txt
│ ├── Other_modalities/
│ │ └── {point_name}/
│ │ ├── {point_name}_visible_{timestamp}.mp4
│ │ ├── {point_name}_infrared_{timestamp}.mp4
│ │ ├── {point_name}_sensor_{timestamp}.txt
│ │ ├── {point_name}_point_cloud_{timestamp}.bag
│ │ └── {point_name}_audio_{timestamp}.wav
│ └── Parameters/
│ ├── Hardware/
│ └── Device_*.json
└── test/
└── (same structure as train)
"""
import os
import json
from pathlib import Path
from dataclasses import dataclass, field
from typing import Optional, List, Dict, Any, Tuple, Literal
from enum import Enum
import torch
from torch.utils.data import Dataset
import cv2
import numpy as np
class DataSplit(Enum):
TRAIN = "train"
TEST = "test"
class DataType(Enum):
NORMAL = "Normal_data"
ANOMALY = "Anomaly_data"
@dataclass
class ImageAnnotation:
"""Image annotation data structure."""
image_path: str
json_path: str
txt_path: str
label: int # 0 for normal, 1 for anomaly
data_type: DataType
point_name: str
frame_id: Optional[str] = None
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class MultimodalData:
"""Multimodal data for a collection point."""
point_name: str
rgb_video_path: Optional[str] = None
infrared_video_path: Optional[str] = None
sensor_data_path: Optional[str] = None
point_cloud_path: Optional[str] = None
audio_path: Optional[str] = None
class MultimodalRobotDataset(Dataset):
"""
Multimodal Robot Anomaly Detection Dataset.
Supports:
- Loading normal and anomaly images with annotations
- Loading multimodal data (RGB, infrared, sensor, point cloud, audio)
- Loading device parameters
"""
def __init__(
self,
root_path: str,
split: DataSplit = DataSplit.TRAIN,
data_type: Optional[DataType] = None, # None means both
transform=None,
load_multimodal: bool = False,
load_parameters: bool = False,
):
"""
Initialize the dataset.
Args:
root_path: Root path to DATA_PATH directory
split: DataSplit.TRAIN or DataSplit.TEST
data_type: Filter by DataType, None means load all
transform: Optional transform to apply to images
load_multimodal: Whether to load multimodal data paths
load_parameters: Whether to load device parameters
"""
self.root_path = Path(root_path)
self.split = split
self.data_type = data_type
self.transform = transform
self.load_multimodal = load_multimodal
self.load_parameters = load_parameters
self.annotations: List[ImageAnnotation] = []
self.multimodal_data: Dict[str, MultimodalData] = {}
self.device_parameters: Dict[str, Any] = {}
self._scan_dataset()
if self.load_parameters:
self._load_parameters()
def _scan_dataset(self):
"""Scan and collect all annotation files."""
split_path = self.root_path / self.split.value
annotation_path = split_path / "Annotations"
data_types = [self.data_type] if self.data_type else [DataType.NORMAL, DataType.ANOMALY]
for dtype in data_types:
dtype_path = annotation_path / dtype.value
if not dtype_path.exists():
continue
for point_dir in dtype_path.iterdir():
if not point_dir.is_dir():
continue
point_name = point_dir.name
# Find all image files
for img_file in point_dir.glob("*.jpg"):
json_file = img_file.with_suffix(".json")
txt_file = img_file.with_suffix(".txt")
# Extract frame_id from filename
frame_id = self._extract_frame_id(img_file.name)
annotation = ImageAnnotation(
image_path=str(img_file),
json_path=str(json_file) if json_file.exists() else "",
txt_path=str(txt_file) if txt_file.exists() else "",
label=0 if dtype == DataType.NORMAL else 1,
data_type=dtype,
point_name=point_name,
frame_id=frame_id,
)
self.annotations.append(annotation)
# Sort by point name for consistent ordering
self.annotations.sort(key=lambda x: (x.data_type.value, x.point_name, x.frame_id or ""))
# Load multimodal data if requested
if self.load_multimodal:
self._scan_multimodal_data()
def _extract_frame_id(self, filename: str) -> Optional[str]:
"""Extract frame ID from filename."""
# Pattern: *_frame_000001.jpg or frame_000001.jpg
if "_frame_" in filename:
parts = filename.replace(".jpg", "").split("_frame_")
return parts[-1] if len(parts) > 1 else None
elif filename.startswith("frame_"):
return filename.replace(".jpg", "").replace("frame_", "")
return None
def _scan_multimodal_data(self):
"""Scan and collect multimodal data paths."""
split_path = self.root_path / self.split.value
multimodal_path = split_path / "Other_modalities"
if not multimodal_path.exists():
return
for point_dir in multimodal_path.iterdir():
if not point_dir.is_dir():
continue
point_name = point_dir.name
mm_data = MultimodalData(point_name=point_name)
for file in point_dir.iterdir():
if "_visible_" in file.name and file.suffix == ".mp4":
mm_data.rgb_video_path = str(file)
elif "_infrared_" in file.name and file.suffix == ".mp4":
mm_data.infrared_video_path = str(file)
elif "_sensor_" in file.name and file.suffix == ".txt":
mm_data.sensor_data_path = str(file)
elif "_point_cloud_" in file.name and file.suffix == ".bag":
mm_data.point_cloud_path = str(file)
elif "_audio_" in file.name and file.suffix == ".wav":
mm_data.audio_path = str(file)
self.multimodal_data[point_name] = mm_data
def _load_parameters(self):
"""Load device parameters."""
split_path = self.root_path / self.split.value
params_path = split_path / "Parameters"
if not params_path.exists():
return
# Load Device_*.json files
for param_file in params_path.glob("*.json"):
device_name = param_file.stem
with open(param_file, 'r', encoding='utf-8') as f:
self.device_parameters[device_name] = json.load(f)
def __len__(self) -> int:
return len(self.annotations)
def __getitem__(self, idx: int) -> Dict[str, Any]:
"""
Get a single sample.
Returns:
Dictionary containing:
- image: RGB image tensor (C, H, W)
- label: 0 for normal, 1 for anomaly
- json_data: annotation from json file
- txt_data: semantic description from txt file
- metadata: additional metadata
"""
ann = self.annotations[idx]
# Load image
image = cv2.imread(ann.image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.transform:
image = self.transform(image)
# Load json annotation
json_data = None
if ann.json_path and os.path.exists(ann.json_path):
with open(ann.json_path, 'r', encoding='utf-8') as f:
json_data = json.load(f)
# Load txt description
txt_data = None
if ann.txt_path and os.path.exists(ann.txt_path):
with open(ann.txt_path, 'r', encoding='utf-8') as f:
txt_data = f.read().strip()
return {
"image": image,
"label": ann.label,
"json_data": json_data,
"txt_data": txt_data,
"metadata": {
"image_path": ann.image_path,
"point_name": ann.point_name,
"frame_id": ann.frame_id,
"data_type": ann.data_type.value,
}
}
def get_multimodal_data(self, point_name: str) -> Optional[MultimodalData]:
"""Get multimodal data for a specific point."""
return self.multimodal_data.get(point_name)
def get_parameter(self, device_name: str) -> Optional[Dict[str, Any]]:
"""Get device parameter by name."""
return self.device_parameters.get(device_name)
def get_stats(self) -> Dict[str, int]:
"""Get dataset statistics."""
normal_count = sum(1 for a in self.annotations if a.data_type == DataType.NORMAL)
anomaly_count = sum(1 for a in self.annotations if a.data_type == DataType.ANOMALY)
return {
"total": len(self.annotations),
"normal": normal_count,
"anomaly": anomaly_count,
"points": len(set(a.point_name for a in self.annotations)),
"multimodal_collections": len(self.multimodal_data),
"device_parameters": len(self.device_parameters),
}
def create_train_test_split(
root_path: str,
transform=None,
load_multimodal: bool = False,
load_parameters: bool = False,
) -> Tuple[MultimodalRobotDataset, MultimodalRobotDataset]:
"""
Create train and test datasets.
Args:
root_path: Root path to DATA_PATH directory
transform: Optional transform to apply to images
load_multimodal: Whether to load multimodal data paths
load_parameters: Whether to load device parameters
Returns:
Tuple of (train_dataset, test_dataset)
"""
train_dataset = MultimodalRobotDataset(
root_path=root_path,
split=DataSplit.TRAIN,
transform=transform,
load_multimodal=load_multimodal,
load_parameters=load_parameters,
)
test_dataset = MultimodalRobotDataset(
root_path=root_path,
split=DataSplit.TEST,
transform=transform,
load_multimodal=load_multimodal,
load_parameters=load_parameters,
)
return train_dataset, test_dataset
# Example usage
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Multimodal Robot Anomaly Dataset Loader")
parser.add_argument("--root", type=str, default="/home/tc/trainData/multimodal_data_process/split1/DATA_PATH",
help="Root path to DATA_PATH directory")
parser.add_argument("--split", type=str, choices=["train", "test", "all"], default="all",
help="Which split to load")
parser.add_argument("--stats", action="store_true", help="Print dataset statistics")
args = parser.parse_args()
if args.split == "all":
train_ds, test_ds = create_train_test_split(
root_path=args.root,
load_multimodal=True,
load_parameters=True,
)
print("=" * 50)
print("TRAIN Dataset Statistics:")
print("=" * 50)
for k, v in train_ds.get_stats().items():
print(f" {k}: {v}")
print("\n" + "=" * 50)
print("TEST Dataset Statistics:")
print("=" * 50)
for k, v in test_ds.get_stats().items():
print(f" {k}: {v}")
else:
split = DataSplit.TRAIN if args.split == "train" else DataSplit.TEST
dataset = MultimodalRobotDataset(
root_path=args.root,
split=split,
load_multimodal=True,
load_parameters=True,
)
print("=" * 50)
print(f"{args.split.upper()} Dataset Statistics:")
print("=" * 50)
for k, v in dataset.get_stats().items():
print(f" {k}: {v}")
# Test loading a sample
if args.stats and args.split == "all":
print("\n" + "=" * 50)
print("Sample Data (first 3):")
print("=" * 50)
for i, sample in enumerate(train_ds):
if i >= 3:
break
print(f"\nSample {i + 1}:")
print(f" Label: {sample['label']} ({'Normal' if sample['label'] == 0 else 'Anomaly'})")
print(f" Point: {sample['metadata']['point_name']}")
print(f" Frame: {sample['metadata']['frame_id']}")
print(f" Image shape: {sample['image'].shape if hasattr(sample['image'], 'shape') else 'N/A'}")
|