File size: 20,166 Bytes
bf7f823 3fe3838 bf7f823 3fe3838 bf7f823 |
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# =================================================================================== #
# ImageNet CLIP Feature Extraction - Download-First Strategy
# Author:AbstractPhil
#
# Description: Should sufficiently handle preparing imagenet from a repo of choice.
# Formatted for colab - uses userdata to set HF_TOKEN with userdata.get('HF_TOKEN')
# Should run as-is without hassle, but it's a little time consuming.
#
# License: MIT
# =================================================================================== #
import os, json, datetime, time
from pathlib import Path
from typing import Dict, List, Union, Optional, Generator
import torch
import torch.nn.functional as F
from datasets import Dataset, DatasetDict, Features, Value, Sequence
from transformers import CLIPModel
from huggingface_hub import HfApi, HfFolder, create_repo
from google.colab import userdata
# Set your HF_TOKEN here.
HF_TOKEN = userdata.get('HF_TOKEN') # set to os.environ or whatever you want to use.
os.environ["HF_TOKEN"] = HF_TOKEN
import torchvision.transforms.functional as TF
from torch.utils.data import DataLoader
# Configuration for ImageNet-scale processing
CONFIG = {
"device": "cuda" if torch.cuda.is_available() else "cpu",
"batch_size": 256, # A100 can handle much larger batches
"generator_chunk_size": 5000, # Process and yield in chunks
"prefetch_factor": 16, # DataLoader prefetch
"persistent_workers": True, # Keep workers alive
"num_workers": 2, # Parallel data loading
"image_size": 224,
"vector_dim": 768,
"normalize_on_gpu": True,
"clip_mean": (0.48145466, 0.4578275, 0.40821073),
"clip_std": (0.26862954, 0.26130258, 0.27577711),
# Memory management for ImageNet scale
"max_memory_gb": 64, # Adjust based on available RAM
"memory_cleanup_interval": 10000, # Clean memory every N images
# Output configuration
"upload_to_hub": False, # set to true if you wish to upload to your repo
"repo_id": "", #"AbstractPhil/imagenet-clip-features", # change this to your HF repo, you can't upload to mine.
"generator_version": "2.0.0", # Must be x.y.z format
# Download-first strategy (optimized for multiple models)
"download_first": True, # Download entire dataset before processing
"cache_dir": "./imagenet_cache", # Where to cache downloaded data
"keep_dataset_in_memory": False, # False to save RAM
"imagenet_repo": "benjamin-paine/imagenet-1k-256x256",
}
# Extended list of CLIP models to process
CLIP_MODELS = [
# OpenAI CLIP models
#{"repo_id": "openai/clip-vit-base-patch32", "short_name": "clip_vit_b32", "dim": 512},
# {"repo_id": "openai/clip-vit-base-patch16", "short_name": "clip_vit_b16", "dim": 512},
#{"repo_id": "laion/CLIP-ViT-B-32-laion2B-s34B-b79K", "short_name": "clip_vit_laion_b32", "dim": 512},
#{"repo_id": "openai/clip-vit-large-patch14", "short_name": "clip_vit_l14", "dim": 768},
#{"repo_id": "openai/clip-vit-large-patch14-336", "short_name": "clip_vit_l14_336", "dim": 768},
# LAION CLIP models (if you want to add them)
{"repo_id": "laion/CLIP-ViT-H-14-laion2B-s32B-b79K", "short_name": "clip_vit_laion_h14", "dim": 1024},
#{"repo_id": "laion/CLIP-ViT-g-14-laion2B-s12B-b42K", "short_name": "clip_vit_laion_g14", "dim": 1024},
# {"repo_id": "laion/CLIP-ViT-bigG-14-laion2B-39B-b160k", "short_name": "clip_vit_laion_bigg14", "dim": 1280},
# You can add more models here
]
TARGET_SPLITS = ["train", "validation", "test"]
class ImageNetClipFeatureExtractor:
"""
Production-ready CLIP feature extractor optimized for processing multiple models.
Uses download-first strategy for maximum throughput.
"""
def __init__(self, config: dict):
self.cfg = config
self.device = torch.device(config["device"])
self._setup_preprocessing()
self.hf_token = os.environ.get("HF_TOKEN") or userdata.get('HF_TOKEN')
self.datasets_cache = {} # Cache loaded datasets
def _setup_preprocessing(self):
self._mean = torch.tensor(self.cfg["clip_mean"]).view(1, 3, 1, 1)
self._std = torch.tensor(self.cfg["clip_std"]).view(1, 3, 1, 1)
def _download_datasets(self):
"""
Pre-download all datasets once before processing any models.
This is called once and datasets are reused for all models.
"""
from datasets import load_dataset
print("=" * 60)
print("π₯ DOWNLOADING IMAGENET DATASET")
print("=" * 60)
for split in TARGET_SPLITS:
if split not in self.datasets_cache:
print(f"\n[β¬] Downloading {split} split to {self.cfg['cache_dir']}...")
start_time = time.time()
dataset = load_dataset(
imagenet_repo, #small tweak to allow setting your own imagenet target repo.
split=split,
cache_dir=self.cfg["cache_dir"],
keep_in_memory=self.cfg["keep_dataset_in_memory"],
num_proc=None # Disable the progress bar noise
)
download_time = time.time() - start_time
print(f"[β
] Downloaded {len(dataset)} {split} images in {download_time/60:.1f} minutes")
if download_time > 0:
print(f"[π] Download speed: {len(dataset)/download_time:.1f} images/sec")
self.datasets_cache[split] = dataset
print("\n[β
] All datasets downloaded and cached!")
print("=" * 60)
def _gpu_preprocess(self, images: torch.Tensor) -> torch.Tensor:
"""Memory-efficient GPU preprocessing."""
if images.dtype != torch.float32:
images = images.float()
# Handle both 0-1 and 0-255 ranges
if images.max() > 1.5:
images = images / 255.0
# Resize if needed
if images.shape[-1] != self.cfg["image_size"]:
images = F.interpolate(
images,
size=(self.cfg["image_size"], self.cfg["image_size"]),
mode="bilinear",
align_corners=False
)
# Normalize
if self.cfg["normalize_on_gpu"]:
mean = self._mean.to(images.device, dtype=images.dtype)
std = self._std.to(images.device, dtype=images.dtype)
images = (images - mean) / std
return images
def _collate_fn(self, batch):
"""Custom collate function for DataLoader."""
import hashlib
images = []
labels = []
image_ids = []
for item in batch:
image = item['image']
if image.mode != 'RGB':
image = image.convert('RGB')
# Convert to tensor [3, H, W]
image_tensor = TF.to_tensor(image)
# Generate SHA256 hash of the image
image_bytes = image.tobytes()
sha256_hash = hashlib.sha256(image_bytes).hexdigest()
images.append(image_tensor)
labels.append(item.get('label', -1))
image_ids.append(sha256_hash)
return {
'images': torch.stack(images),
'labels': labels,
'image_ids': image_ids
}
def _imagenet_generator_optimized(self, split: str, model_id: str) -> Generator[Dict, None, None]:
"""
Optimized generator using pre-downloaded data and DataLoader for parallel loading.
"""
# Use cached dataset
dataset = self.datasets_cache[split]
# Create DataLoader for efficient parallel loading
dataloader = DataLoader(
dataset,
batch_size=self.cfg["batch_size"],
shuffle=False, # Keep order for reproducibility
num_workers=self.cfg["num_workers"],
prefetch_factor=self.cfg["prefetch_factor"],
persistent_workers=self.cfg["persistent_workers"],
collate_fn=self._collate_fn,
pin_memory=True # Faster GPU transfer
)
# Load CLIP model
print(f"\n[π€] Loading {model_id}")
model = CLIPModel.from_pretrained(model_id).to(self.device)
model.eval()
# Setup for chunked processing
chunk_buffer = []
timestamp = datetime.datetime.now(datetime.timezone.utc)
images_processed = 0
start_time = time.time()
last_print_time = start_time
print_interval = 10 # Print progress every 10 seconds
try:
with torch.no_grad():
for batch_idx, batch in enumerate(dataloader):
# Move batch to GPU
image_batch = batch['images'].to(self.device, non_blocking=True)
labels = batch['labels']
image_ids = batch['image_ids']
# Preprocess on GPU
image_batch = self._gpu_preprocess(image_batch)
# Extract features
features = model.get_image_features(pixel_values=image_batch)
features = features / features.norm(dim=-1, keepdim=True)
# Create records
for img_id, label, feature_vec in zip(image_ids, labels, features):
chunk_buffer.append({
"image_id": img_id, # Now using SHA256 hash
"label": int(label),
"clip_model": model_id,
"clip_features": feature_vec.detach().cpu().float().numpy().tolist(),
"vector_dim": features.shape[-1],
"timestamp": timestamp,
})
images_processed += len(image_ids)
# Print progress at regular time intervals
current_time = time.time()
if current_time - last_print_time >= print_interval:
elapsed = current_time - start_time
speed = images_processed / elapsed
eta = (len(dataset) - images_processed) / speed
print(f"[β‘] Progress: {images_processed}/{len(dataset)} "
f"({100*images_processed/len(dataset):.1f}%) | "
f"Speed: {speed:.1f} img/sec | "
f"ETA: {eta/60:.1f} min")
last_print_time = current_time
# Yield chunk when it reaches configured size
if len(chunk_buffer) >= self.cfg["generator_chunk_size"]:
elapsed = time.time() - start_time
speed = images_processed / elapsed
print(f"[π¦] Yielding chunk of {len(chunk_buffer)} features | "
f"Progress: {images_processed}/{len(dataset)} "
f"({100*images_processed/len(dataset):.1f}%)")
yield from chunk_buffer
chunk_buffer = []
# Memory cleanup at configured interval
if images_processed % self.cfg["memory_cleanup_interval"] == 0:
torch.cuda.empty_cache()
# Yield remaining chunk buffer
if chunk_buffer:
print(f"[π¦] Final chunk of {len(chunk_buffer)} features")
yield from chunk_buffer
# Final stats
total_time = time.time() - start_time
print(f"\n[β
] Processed {images_processed} images in {total_time/60:.1f} minutes")
print(f"[π] Average speed: {images_processed/total_time:.1f} images/sec")
finally:
del model
torch.cuda.empty_cache()
def extract_and_upload(self, model_config: dict, split: str = "train"):
"""
Extract features using optimized generator and upload to HuggingFace.
Returns the dataset if upload fails for retry purposes.
"""
model_id = model_config["repo_id"]
short_name = model_config["short_name"]
print("\n" + "=" * 60)
print(f"βοΈ PROCESSING: {short_name} - {split}")
print("=" * 60)
# Define dataset features
features = Features({
"image_id": Value("string"),
"label": Value("int32"),
"clip_model": Value("string"),
"clip_features": Sequence(Value("float32")),
"vector_dim": Value("int32"),
"timestamp": Value("timestamp[ns]"),
})
# Suppress the "Generating split" progress bar
import sys
import io
old_stderr = sys.stderr
sys.stderr = io.StringIO()
try:
# Create dataset from generator
dataset = Dataset.from_generator(
lambda: self._imagenet_generator_optimized(split, model_id),
features=features,
writer_batch_size=self.cfg["generator_chunk_size"],
split=split
)
except Exception as e:
raise Exception(e)
#finally:
# # Restore stderr
# sys.stderr = old_stderr
# return
# Add metadata
dataset.info.description = f"CLIP features for ImageNet-1k 256x256 {split} using {model_id}"
dataset.info.version = self.cfg["generator_version"]
# Save to disk before upload (safety backup)
temp_path = f"./temp_dataset_{short_name}_{split}"
print(f"[πΎ] Saving dataset to {temp_path} for safety...")
dataset.save_to_disk(temp_path)
# Upload to HuggingFace
split_name = f"{short_name}_{split}"
print(f"\n[π€] Uploading {split_name} to {self.cfg['repo_id']}")
try:
dataset.push_to_hub(
self.cfg["repo_id"],
split=split_name,
token=self.hf_token,
commit_message=f"Add {split_name} CLIP features",
max_shard_size="500MB"
)
print(f"[β
] Successfully uploaded {split_name}")
# Clean up temp file on success
import shutil
shutil.rmtree(temp_path, ignore_errors=True)
return None
except Exception as e:
print(f"[β] Upload failed for {split_name}: {e}")
print(f"[π‘] Dataset saved at {temp_path} - you can retry upload with:")
print(f" from datasets import load_from_disk")
print(f" dataset = load_from_disk('{temp_path}')")
print(f" dataset.push_to_hub('{self.cfg['repo_id']}', split='{split_name}', ...)")
return dataset # Return dataset for potential retry
def extract_all_models(self, models_to_process=None):
"""
Extract features for all models and splits.
Args:
models_to_process: List of model configs to process (default: all)
"""
# Ensure repo exists
if self.hf_token:
try:
create_repo(self.cfg["repo_id"], repo_type="dataset", exist_ok=True, token=self.hf_token)
print(f"[β
] Repository ready: {self.cfg['repo_id']}")
except Exception as e:
print(f"[β οΈ] Repo creation warning: {e}")
# Download all data first (once for all models)
self._download_datasets()
# Process specified models or all
models = models_to_process or CLIP_MODELS
total_combinations = len(models) * 2 # train + validation
print("\n" + "=" * 60)
print(f"π PROCESSING PLAN: {len(models)} models Γ 2 splits = {total_combinations} tasks")
print("=" * 60)
# Keep track of failed uploads for retry
failed_uploads = []
for i, model_config in enumerate(models, 1):
print(f"\n[{i}/{len(models)}] Model: {model_config['short_name']}")
for split in TARGET_SPLITS: #"train", "test"]:
try:
dataset = self.extract_and_upload(model_config, split)
if dataset is not None:
# Upload failed but we have the dataset
failed_uploads.append({
'model': model_config['short_name'],
'split': split,
'dataset': dataset,
'path': f"./temp_dataset_{model_config['short_name']}_{split}"
})
except Exception as e:
print(f"[β] Failed {model_config['short_name']} {split}: {e}")
continue
# Cleanup between models
torch.cuda.empty_cache()
print("\n" + "=" * 60)
if failed_uploads:
print(f"β οΈ PROCESSING COMPLETE WITH {len(failed_uploads)} FAILED UPLOADS")
print("\nFailed uploads saved to disk:")
for failure in failed_uploads:
print(f" - {failure['model']}_{failure['split']}: {failure['path']}")
print("\nYou can retry these uploads after fixing the issue.")
else:
print("π ALL PROCESSING COMPLETE!")
print("=" * 60)
return failed_uploads # Return list of failed uploads for retry
# ============================================================
# Utility Functions
# ============================================================
def estimate_processing_time(num_models=len(CLIP_MODELS)):
"""
Estimate total processing time for all models.
"""
print("=" * 60)
print("β±οΈ TIME ESTIMATES")
print("=" * 60)
# Dataset sizes
train_size = 1_281_167
val_size = 50_000
total_images = train_size + val_size
# Time estimates
download_time_min = 60 # minutes
download_time_max = 120
# Processing speeds (images/sec)
speed_min = 800
speed_max = 1200
print(f"\nπ Dataset sizes:")
print(f" - Train: {train_size:,} images")
print(f" - Validation: {val_size:,} images")
print(f" - Total per model: {total_images:,} images")
print(f"\n⬠Download time (one-time):")
print(f" - Estimated: {download_time_min}-{download_time_max} minutes")
print(f"\nπ Processing speed:")
print(f" - Expected: {speed_min}-{speed_max} images/sec")
# Per model
time_per_model_min = total_images / speed_max / 60
time_per_model_max = total_images / speed_min / 60
print(f"\nβ±οΈ Per model:")
print(f" - Processing time: {time_per_model_min:.1f}-{time_per_model_max:.1f} minutes")
# Total
total_min = download_time_min + (num_models * time_per_model_min)
total_max = download_time_max + (num_models * time_per_model_max)
print(f"\nπ― Total for {num_models} models:")
print(f" - Total time: {total_min:.1f}-{total_max:.1f} minutes")
print(f" - Or: {total_min/60:.1f}-{total_max/60:.1f} hours")
print("\nπ‘ Tips:")
print(" - Processing is GPU-bound, so better GPUs = faster")
print(" - A100/H100 can use batch_size=1024+ for more speed")
print(" - Multiple GPUs can process different models in parallel")
print("=" * 60)
# ============================================================
# Main Execution
# ============================================================
"""
Main execution for multi-model ImageNet CLIP feature extraction.
"""
# Show time estimates
estimate_processing_time()
# Confirm settings
print(f"\nπ§ Current configuration:")
print(f" - Batch size: {CONFIG['batch_size']}")
print(f" - Chunk size: {CONFIG['generator_chunk_size']}")
print(f" - Workers: {CONFIG['num_workers']}")
print(f" - Models to process: {len(CLIP_MODELS)}")
# Option to process subset of models
# For testing, you might want to start with just one:
# test_models = CLIP_MODELS[:1] # Just first model
# extractor.extract_all_models(models_to_process=test_models)
# Run extraction
extractor = ImageNetClipFeatureExtractor(CONFIG)
extractor.extract_all_models() # Process all models |