[DEBUG] Number of heads instantiated: 2
[DEBUG] Head 0 type: <class 'xview3.centernet.models.heads.decoupled_head.DecoupledHeadGroupNormLateShuffle'>
[DEBUG] Head 1 type: <class 'xview3.centernet.models.heads.yolo_head.YOLOHead'>
[2024-10-05 18:28:29,285][timm.models._builder][INFO] - Loading pretrained weights from Hugging Face hub (timm/efficientnetv2_rw_s.ra2_in1k)
[2024-10-05 18:28:29,733][timm.models._hub][INFO] - [timm/efficientnetv2_rw_s.ra2_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
[2024-10-05 18:28:29,831][timm.models._builder][WARNING] - Unexpected keys (bn2.bias, bn2.num_batches_tracked, bn2.running_mean, bn2.running_var, bn2.weight, classifier.bias, classifier.weight, conv_head.weight) found while loading pretrained weights. This may be expected if model is being adapted.
[2024-10-05 18:28:29,832][xview3.centernet.models.centernet_unet][INFO] - Adjusted encoder's first conv layer to have 2 input channels.
[2024-10-05 18:28:30,082][timm.models._builder][INFO] - Loading pretrained weights from Hugging Face hub (timm/efficientnetv2_rw_s.ra2_in1k)
[2024-10-05 18:28:30,209][timm.models._hub][INFO] - [timm/efficientnetv2_rw_s.ra2_in1k] Safe alternative available for 'pytorch_model.bin' (as 'model.safetensors'). Loading weights using safetensors.
[2024-10-05 18:28:30,216][timm.models._builder][INFO] - Converted input conv conv_stem pretrained weights from 3 to 2 channel(s)
[2024-10-05 18:28:30,229][timm.models._builder][WARNING] - Unexpected keys (bn2.bias, bn2.num_batches_tracked, bn2.running_mean, bn2.running_var, bn2.weight, classifier.bias, classifier.weight, conv_head.weight) found while loading pretrained weights. This may be expected if model is being adapted.
[2024-10-05 18:28:30,230][xview3.centernet.models.centernet_unet][INFO] - Adjusted encoder's first conv layer to have 2 input channels.
[W 2024-10-05 18:28:30,708] Trial 0 failed with parameters: {'num_layers': 5, 'num_filters': 64, 'kernel_size': 5, 'activation': 'relu', 'dropout_rate': 0.4239230831361559, 'learning_rate': 0.00012866589470813123, 'optimizer': 'Adam'} because of the following error: AssertionError('Invalid window_size: (4, 4)').
Traceback (most recent call last):
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/nas.py", line 240, in
lambda trial: self.evaluate_architecture(trial, config, train_loader, val_loader),
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 388, in evaluate_architecture
model = self.nas.create_model(config, trial) # Ensure create_model is accessible
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/nas.py", line 110, in create_model
model = CenterNetUNetModel.from_config(updated_config)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/centernet_unet.py", line 659, in from_config
model = cls(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/centernet_unet.py", line 173, in init
self.transformer_encoder = SwinTransformerEncoder(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/transformer_classifier.py", line 82, in init
self.swin = DebugSwinTransformer(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/transformer_classifier.py", line 12, in init
assert isinstance(window_size, int) and window_size > 0, f"Invalid window_size: {window_size}"
AssertionError: Invalid window_size: (4, 4)
[W 2024-10-05 18:28:30,712] Trial 0 failed with value None.
Error executing job with overrides: []
Traceback (most recent call last):
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 821, in
main()
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/main.py", line 94, in decorated_main
_run_hydra(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/utils.py", line 394, in _run_hydra
_run_app(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/utils.py", line 457, in _run_app
run_and_report(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/utils.py", line 223, in run_and_report
raise ex
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/utils.py", line 220, in run_and_report
return func()
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/utils.py", line 458, in
lambda: hydra.run(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/_internal/hydra.py", line 132, in run
_ = ret.return_value
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/core/utils.py", line 260, in return_value
raise self._return_value
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/hydra/core/utils.py", line 186, in run_job
ret.return_value = task_function(task_cfg)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 817, in main
pipeline.train()
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 535, in train
self.model = self.build_model(config, train_loader, valid_loader)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 340, in build_model
best_trial = self.nas.optimize(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/nas.py", line 239, in optimize
study.optimize(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/study.py", line 475, in optimize
_optimize(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/_optimize.py", line 63, in _optimize
_optimize_sequential(
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential
frozen_trial = _run_trial(study, func, catch)
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/_optimize.py", line 248, in _run_trial
raise func_err
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/optuna/study/_optimize.py", line 197, in _run_trial
value_or_values = func(trial)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/nas.py", line 240, in
lambda trial: self.evaluate_architecture(trial, config, train_loader, val_loader),
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/train_multilabel3.py", line 388, in evaluate_architecture
model = self.nas.create_model(config, trial) # Ensure create_model is accessible
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/nas.py", line 110, in create_model
model = CenterNetUNetModel.from_config(updated_config)
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/centernet_unet.py", line 659, in from_config
model = cls(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/centernet_unet.py", line 173, in init
self.transformer_encoder = SwinTransformerEncoder(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/transformer_classifier.py", line 82, in init
self.swin = DebugSwinTransformer(
File "/Volumes/Crucial/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/models/transformer_classifier.py", line 12, in init
assert isinstance(window_size, int) and window_size > 0, f"Invalid window_size: {window_size}"
AssertionError: Invalid window_size: (4, 4)
(sam3) samerkharboush@Samers-MBP SAR-MPS-OptunaNasFbnYoloTransformers %
It looks like you're encountering an AssertionError related to the window_size parameter in your DebugSwinTransformer. This error is preventing your Optuna trials from running successfully. Let's break down the issue and provide a comprehensive solution to resolve it while ensuring that your project structure and functionality remain intact.
Issue Summary
-
Error Message:
AssertionError: Invalid window_size: (4, 4)
-
Cause:
The DebugSwinTransformer class in transformer_classifier.py expects window_size to be an integer. However, your configuration is passing a tuple (4, 4) as the window_size.
-
Relevant Config Snippet:
transformer:
window_size:
- 4
- 4
Understanding the Root Cause
-
SwinTransformer Expectations:
- The standard Swin Transformer typically expects
window_size to be a tuple or list indicating the size of the window in each dimension (e.g., (7, 7)).
-
Current Assertion:
Solution Steps
1. Modify the Assertion in DebugSwinTransformer
Update the assertion in transformer_classifier.py to accept both integers and tuples/lists. This change will align the DebugSwinTransformer with the standard Swin Transformer expectations.
Modified transformer_classifier.py:
# transformer_classifier.py
class DebugSwinTransformer(nn.Module):
def __init__(self, window_size, ...):
# Updated assertion to handle both int and tuple
if isinstance(window_size, int):
assert window_size > 0, f"Invalid window_size: {window_size}"
elif isinstance(window_size, (tuple, list)) and len(window_size) == 2:
assert all(isinstance(w, int) and w > 0 for w in window_size), f"Invalid window_size: {window_size}"
else:
raise AssertionError(f"Invalid window_size: {window_size}")
# Rest of your initialization code
...
2. Verify and Update Configuration if Necessary
Ensure that your configuration aligns with the updated assertion. Since Swin Transformer typically uses a tuple for window_size, your current configuration seems appropriate. However, verify other parts of the configuration to ensure consistency.
Config Verification:
transformer:
enabled: true
embed_dim: 96
depths:
- 2
- 2
- 6
- 2
num_heads:
- 3
- 6
- 12
- 24
window_size:
- 4
- 4 # This will be interpreted as (4, 4)
mlp_ratio: 4.0
qkv_bias: true
qk_scale: null
drop_rate: 0.0
attn_drop_rate: 0.0
drop_path_rate: 0.1
norm_layer: torch.nn.LayerNorm
use_dropout: false
use_custom_pos_embed: false
If window_size should indeed be a tuple (4, 4), no further changes are needed in the configuration. If you intended window_size to be a single integer, adjust the config accordingly.
3. Implement Robustness in DebugSwinTransformer
To further enhance the robustness of your DebugSwinTransformer, consider allowing flexibility in the window_size parameter:
# transformer_classifier.py
class DebugSwinTransformer(nn.Module):
def __init__(self, window_size, ...):
super(DebugSwinTransformer, self).__init__()
# Enhanced assertion
if isinstance(window_size, int):
assert window_size > 0, f"Invalid window_size: {window_size}"
self.window_size = (window_size, window_size)
elif isinstance(window_size, (tuple, list)):
assert len(window_size) == 2, f"window_size must have two dimensions, got {len(window_size)}"
assert all(isinstance(w, int) and w > 0 for w in window_size), f"Invalid window_size elements: {window_size}"
self.window_size = tuple(window_size)
else:
raise AssertionError(f"Invalid window_size type: {type(window_size)}. Expected int or tuple/list of two ints.")
# Initialize Swin Transformer with the validated window_size
self.swin = SwinTransformer(..., window_size=self.window_size, ...)
# Rest of your initialization code
...
4. Re-run Optuna Trials
After making the above changes, re-run your Optuna trials to ensure that the issue is resolved.
5. Additional Recommendations
-
Logging: Enhance logging to capture the configurations being passed to different components. This can aid in debugging similar issues in the future.
-
Configuration Validation: Implement a configuration validation step before model training to catch such discrepancies early.
-
Unit Tests: Add unit tests for the DebugSwinTransformer to ensure it handles various window_size inputs correctly.
Final Thoughts
By updating the assertion in your DebugSwinTransformer to accommodate both integers and tuples/lists, you align your custom transformer with standard practices and resolve the current error. Ensure that all related components and configurations are consistent with this change to maintain the integrity of your project.
If you encounter further issues or need assistance with other parts of your project, feel free to ask!