Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

outline for loading old sleap config #162

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
120 changes: 119 additions & 1 deletion sleap_nn/config/data_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

from attrs import define, field, validators
from omegaconf import MISSING
from omegaconf import MISSING, MissingMandatoryValue
from typing import Optional, Tuple, Any, List
from loguru import logger

Expand Down Expand Up @@ -185,3 +185,121 @@ class DataConfig:
use_augmentations_train: bool = False
augmentation_config: Optional[AugmentationConfig] = None
skeletons: Optional[dict] = None


def data_mapper(legacy_config: dict) -> DataConfig:
train_labels_path = legacy_config.get("data", {}).get("labels", {}).get("training_labels", None) or MISSING
if train_labels_path is MISSING:
raise MissingMandatoryValue("train_labels_path is missing")
val_labels_path = legacy_config.get("data", {}).get("labels", {}).get("validation_labels", None) or MISSING
if val_labels_path is MISSING:
raise MissingMandatoryValue("val_labels_path is missing")

return DataConfig(
train_labels_path=legacy_config.get("data", {}).get("labels", {}).get("training_labels", MISSING),
val_labels_path=legacy_config.get("data", {}).get("labels", {}).get("validation_labels", MISSING),
test_file_path=legacy_config.get("data", {})
.get("labels", {})
.get("test_labels"),
# provider=legacy_config.get("provider", "LabelsReader"),
# user_instances_only=legacy_config.get("user_instances_only", True),
# data_pipeline_fw=legacy_config.get("data_pipeline_fw", "torch_dataset"),
# np_chunks_path=legacy_config.get("np_chunks_path"),
# litdata_chunks_path=legacy_config.get("litdata_chunks_path"),
# use_existing_chunks=legacy_config.get("use_existing_chunks", False),
# chunk_size=int(legacy_config.get("chunk_size", 100)),
# delete_chunks_after_training=legacy_config.get("delete_chunks_after_training", True),
preprocessing=PreprocessingConfig(
is_rgb=legacy_config.get("data", {})
.get("preprocessing", {})
.get("ensure_rgb", False),
max_height=legacy_config.get("data", {})
.get("preprocessing", {})
.get("target_height"),
max_width=legacy_config.get("data", {})
.get("preprocessing", {})
.get("target_width"),
scale=legacy_config.get("data", {})
.get("preprocessing", {})
.get("input_scaling", 1.0),
crop_hw=legacy_config.get("data", {})
.get("preprocessing", {})
.get("crop_size"),
min_crop_size=legacy_config.get("data", {})
.get("preprocessing", {})
.get("crop_size_detection_padding", 100),
),
# use_augmentations_train=legacy_config.get("use_augmentations_train", False),
augmentation_config=(
AugmentationConfig(
intensity=IntensityConfig(
uniform_noise_min=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("uniform_noise_min_val", 0.0),
uniform_noise_max=min(legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("uniform_noise_max_val", 1.0),
1.0),
uniform_noise_p=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("uniform_noise", 1.0),
gaussian_noise_mean=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("gaussian_noise_mean", 0.0),
gaussian_noise_std=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("gaussian_noise_stddev", 1.0),
gaussian_noise_p=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("gaussian_noise", 1.0),
contrast_min=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("contrast_min_gamma", 0.5),
contrast_max=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("contrast_max_gamma", 2.0),
contrast_p=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("contrast", 1.0),
brightness=(
legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("brightness_min_val", 1.0),
legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("brightness_max_val", 1.0),
),
brightness_p=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("brightness", 1.0),
),
geometric=GeometricConfig(
rotation=legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("rotation_max_angle", 180.0),
scale=(
legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("scale_min", None),
legacy_config.get("optimization", {})
.get("augmentation_config", {})
.get("scale_max", None),
),
# translate_width=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("translate_width", 0.2),
# translate_height=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("translate_height", 0.2),
# affine_p=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("affine_p", 0.0),
# erase_scale_min=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("erase_scale_min", 0.0001),
# erase_scale_max=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("erase_scale_max", 0.01),
# erase_ratio_min=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("erase_ratio_min", 1.0),
# erase_ratio_max=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("erase_ratio_max", 1.0),
# erase_p=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("erase_p", 0.0),
# mixup_lambda=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("mixup_lambda", [0.01, 0.05]),
# mixup_p=legacy_config.get("optimization", {}).get("augmentation_config", {}).get("mixup_p", 0.0),
),
)
# if legacy_config.get("use_augmentations_train", False)
# else None
),
use_augmentations_train=True,
skeletons=legacy_config.get("data", {}).get("labels", {}).get("skeletons"),
)
168 changes: 168 additions & 0 deletions sleap_nn/config/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,171 @@ def validate_pre_trained_weights(self, value):
message = "UNet does not support pre-trained weights."
logger.error(message)
raise ValueError(message)


def model_mapper(legacy_config: dict) -> ModelConfig:
return ModelConfig(
# init_weights=legacy_config.get("init_weights", "default"),
# pre_trained_weights not in old config
# pretrained_backbone_weights=legacy_config.get("PretrainedEncoderConfig")?? # i think its different
# pretrained_head_weights not in old config
backbone_config=BackboneConfig(
unet=(
UNetConfig(
# in_channels=legacy_config.get("backbone", {}).get("in_channels", 1),
# kernel_size=legacy_config.get("backbone", {}).get("kernel_size", 3),
filters=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("filters", 32),
filters_rate=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("filters_rate", 1.5),
max_stride=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("max_stride", 16),
stem_stride=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("stem_stride", 16),
middle_block=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("middle_block", True),
up_interpolate=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("up_interpolate", True),
stacks=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("stacks", 1),
# convs_per_block=2,
output_stride=legacy_config.get("model", {})
.get("backbone", {})
.get("unet", {})
.get("output_stride", 1),
)
if legacy_config.get("model", {}).get("backbone", {}).get("unet")
else None
),
# convnext not in old config
# swint not in old config
),
head_configs=HeadConfig(
single_instance=(
(
SingleInstanceConfig(
confmaps=SingleInstanceConfMapsConfig(
part_names=legacy_config.get("model", {})
.get("heads", {})
.get("single_instance", {})
.get("part_names"),
sigma=legacy_config.get("model", {})
.get("heads", {})
.get("single_instance", {})
.get("sigma", 5.0),
output_stride=legacy_config.get("model", {})
.get("heads", {})
.get("single_instance", {})
.get("output_stride", 1),
)
)
)
if legacy_config.get("model", {})
.get("heads", {})
.get("single_instance")
else None
),
centroid=(
CentroidConfig(
confmaps=CentroidConfMapsConfig(
anchor_part=legacy_config.get("model", {})
.get("heads", {})
.get("centroid", {})
.get("anchor_part"),
sigma=legacy_config.get("model", {})
.get("heads", {})
.get("centroid", {})
.get("sigma", 5.0),
output_stride=legacy_config.get("model", {})
.get("heads", {})
.get("centroid", {})
.get("output_stride", 1),
)
)
if legacy_config.get("model", {}).get("heads", {}).get("centroid")
else None
),
centered_instance=(
CenteredInstanceConfig(
confmaps=CentroidConfMapsConfig(
anchor_part=legacy_config.get("model", {})
.get("heads", {})
.get("centered_instance", {})
.get("anchor_part"),
sigma=legacy_config.get("model", {})
.get("heads", {})
.get("centered_instance", {})
.get("sigma", 5.0),
output_stride=legacy_config.get("model", {})
.get("heads", {})
.get("centered_instance", {})
.get("output_stride", 1),
part_names=legacy_config.get("model", {})
.get("heads", {})
.get("centered_instance", {})
.get("part_names", None),
)
)
if legacy_config.get("model", {})
.get("heads", {})
.get("centered_instance")
else None
),
bottomup=(
BottomUpConfig(
confmaps=BottomUpConfMapsConfig(
loss_weight=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("loss_weight", None),
sigma=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("sigma", 5.0),
output_stride=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("output_stride", 1),
part_names=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("part_names", None),
),
pafs=PAFConfig(
edges=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("edges", None),
sigma=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("sigma", 15.0),
output_stride=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("output_stride", 1),
loss_weight=legacy_config.get("model", {})
.get("heads", {})
.get("multi_instance", {})
.get("loss_weight", None),
),
)
if legacy_config.get("model", {}).get("heads", {}).get("multi_instance")
else None
),
),
)
Loading
Loading