| from dataclasses import dataclass, field | |
| from typing import Dict, List, Optional, Any | |
| from omegaconf import OmegaConf | |
| import yaml | |
| import os | |
| from pathlib import Path | |
| class DataConfig: | |
| vqa_dir: str = "data/vqa" | |
| coco_dir: str = "data/coco" | |
| gqa_dir: str = "data/gqa" | |
| clevr_dir: str = "data/clevr" | |
| image_size: int = 224 | |
| max_question_length: int = 20 | |
| max_answer_length: int = 10 | |
| vocab_size: int = 10000 | |
| batch_size: int = 32 | |
| num_workers: int = 4 | |
| pin_memory: bool = True | |
| use_augmentation: bool = True | |
| brightness_range: float = 0.2 | |
| contrast_range: float = 0.2 | |
| saturation_range: float = 0.2 | |
| rotation_range: int = 10 | |
| class ModelConfig: | |
| model_type: str = "hybrid_vqa" | |
| visual_encoder: str = "resnet50" | |
| visual_dim: int = 2048 | |
| visual_layers_to_freeze: List[int] = field(default_factory=lambda: []) | |
| text_encoder: str = "bert" | |
| text_dim: int = 768 | |
| text_max_length: int = 512 | |
| nmn_modules: List[str] = field( | |
| default_factory=lambda: [ | |
| "find", | |
| "filter", | |
| "count", | |
| "compare", | |
| "relate", | |
| "answer", | |
| ] | |
| ) | |
| nmn_hidden_dim: int = 512 | |
| scene_graph_max_objects: int = 50 | |
| scene_graph_max_relations: int = 100 | |
| gnn_layers: int = 3 | |
| gnn_hidden_dim: int = 512 | |
| attention_heads: int = 8 | |
| attention_dropout: float = 0.1 | |
| program_vocab_size: int = 100 | |
| program_max_length: int = 20 | |
| class TrainingConfig: | |
| learning_rate: float = 1e-4 | |
| batch_size: int = 32 | |
| num_epochs: int = 100 | |
| gradient_clip_val: float = 1.0 | |
| optimizer: str = "adamw" | |
| weight_decay: float = 1e-4 | |
| betas: List[float] = field(default_factory=lambda: [0.9, 0.999]) | |
| scheduler: str = "cosine" | |
| warmup_steps: int = 1000 | |
| cosine_min_lr: float = 1e-7 | |
| dropout: float = 0.1 | |
| label_smoothing: float = 0.1 | |
| use_amp: bool = True | |
| precision: int = 16 | |
| checkpoint_frequency: int = 5 | |
| num_checkpoints_to_keep: int = 3 | |
| class EvaluationConfig: | |
| metrics: List[str] = field( | |
| default_factory=lambda: ["accuracy", "vqa_score", "bleu", "rouge", "f1"] | |
| ) | |
| eval_splits: List[str] = field(default_factory=lambda: ["val", "test"]) | |
| confidence_threshold: float = 0.0 | |
| save_attention_maps: bool = True | |
| save_reasoning_traces: bool = True | |
| visualize_scene_graphs: bool = True | |
| class LoggingConfig: | |
| use_wandb: bool = True | |
| project_name: str = "neuro_symbolic_vqa" | |
| log_every_n_steps: int = 50 | |
| log_images_frequency: int = 500 | |
| visualize_predictions: bool = True | |
| max_images_to_log: int = 16 | |
| class VQAConfig: | |
| data: DataConfig = field(default_factory=DataConfig) | |
| model: ModelConfig = field(default_factory=ModelConfig) | |
| training: TrainingConfig = field(default_factory=TrainingConfig) | |
| evaluation: EvaluationConfig = field(default_factory=EvaluationConfig) | |
| logging: LoggingConfig = field(default_factory=LoggingConfig) | |
| seed: int = 42 | |
| device: str = "auto" | |
| num_deterministic: bool = False | |
| output_dir: str = "outputs" | |
| checkpoint_dir: str = "checkpoints" | |
| log_dir: str = "logs" | |
| cache_dir: str = "cache" | |
| def load_config(config_path: str) -> VQAConfig: | |
| if not os.path.exists(config_path): | |
| raise FileNotFoundError(f"Configuration file not found: {config_path}") | |
| with open(config_path, "r") as f: | |
| config_dict = yaml.safe_load(f) | |
| return OmegaConf.to_object(OmegaConf.create(config_dict)) | |
| def save_config(config: VQAConfig, output_path: str): | |
| config_dict = OmegaConf.to_container(OmegaConf.create(config), resolve=True) | |
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | |
| with open(output_path, "w") as f: | |
| yaml.dump(config_dict, f, default_flow_style=False) | |
| def create_default_config(output_path: str): | |
| config = VQAConfig() | |
| save_config(config, output_path) | |
| print(f"Default configuration saved to: {output_path}") | |
| def get_device(device_str: str): | |
| if device_str == "auto": | |
| if torch.cuda.is_available(): | |
| return torch.device("cuda") | |
| elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): | |
| return torch.device("mps") | |
| else: | |
| return torch.device("cpu") | |
| else: | |
| return torch.device(device_str) | |
| if __name__ == "__main__": | |
| config_path = "config/default_config.yaml" | |
| create_default_config(config_path) | |
| config = load_config(config_path) | |
| print("Configuration loaded successfully!") | |
| print(f"Model type: {config.model.model_type}") | |
| print(f"Visual encoder: {config.model.visual_encoder}") | |
| print(f"Text encoder: {config.model.text_encoder}") | |
Xet Storage Details
- Size:
- 4.8 kB
- Xet hash:
- 2af1b1e5d266851edb06f923a440ecdaf69016e01d852e5871e4739b4d0e9884
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.