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

ENH: Determine network kwargs dynamically in vak.models.get #680

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/vak/models/get.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Function that gets an instance of a model,
given its name and a configuration as a dict."""
from __future__ import annotations
import inspect
from typing import Callable

from . import registry
Expand Down Expand Up @@ -52,20 +53,29 @@ def get(name: str,
f"Valid model names are: {registry.MODEL_NAMES}"
) from e

# still need to special case model logic here
if name in ('TweetyNet', 'TeenyTweetyNet', 'ED_TCN'):
num_input_channels = input_shape[-3]
num_freqbins = input_shape[-2]
config["network"].update(
num_classes=num_classes,
num_input_channels=num_input_channels,
num_freqbins=num_freqbins
)
else:
model_names = list(all_models_dict.keys())
raise ValueError(
f"Invalid model name: '{name}'.\nValid model names are: {model_names}"
model_family = registry.MODEL_FAMILY_FROM_NAME[name]

if model_family == 'FrameClassificationModel':
# still need to special case model logic here
net_init_params = list(
inspect.signature(
model_class.definition.network.__init__
).parameters.keys()
)
if ('num_input_channels' in net_init_params) and ('num_freqbins' in net_init_params):
num_input_channels = input_shape[-3]
num_freqbins = input_shape[-2]
config["network"].update(
num_classes=num_classes,
num_input_channels=num_input_channels,
num_freqbins=num_freqbins
)
else:
raise ValueError(
f"Detected that model with name '{name}' was family '{model_family}', but "
f"unable to determine network init arguments for model. Currently all models "
f"in this family must have networks with parameters ``num_input_channels`` and ``num_freqbins``"
)

model = model_class.from_config(config=config, labelmap=labelmap, post_tfm=post_tfm)

Expand Down
Loading