variants
最后更新
luxonis_train.variants节点和模型的 variant 参数背后的机制。变体是构造函数参数的命名集合。一个类在其 VariantBase.get_variants 中声明其变体。VariantMeta 在调用创建实例时应用选定的集合。因此,配置通过一个词而不是参数列表来选择大小。类
VariantBase
NotImplementedError,就像 BaseNode 所做的那样。方法
get_variants
Python
1def get_variants() -> tuple[str, dict[str, Kwargs]]:__init__。默认变体名称必须是字典的键。实现必须在每次调用时返回新的字典,因为 VariantMeta 会删除调用替换的键。返回tuple[str, dict[str, Kwargs]]: 默认变体名称和带有其构造函数参数的变体。
NotImplementedError: 当类没有变体时。
VariantMeta
variant。该参数不会到达 __init__。它的值选择元类如何构建实例:None、""或"none":元类使用其他参数调用__init__。"default":元类从get_variants中选择默认变体。当get_variants引发NotImplementedError时,元类会记录警告并使用其他参数调用__init__。- 任何其他名称:元类选择该名称的变体。
_variant 中。然后它使用变体的参数和其他参数调用 __init__。调用的关键字参数替换同名的变体参数,元类会为此记录一条信息消息。如果没有选定的变体,_variant 会保留其类默认值 None。在 __init__ 之后,如果类定义了 __post_init__ 方法,元类会调用实例的 __post_init__ 方法。类注册来自 luxonis_ml 的 AutoRegisterMeta 基类。示例
Pycon
1>>> from luxonis_train.variants import VariantBase
2>>> class Block(VariantBase, register=False):
3... def __init__(self, width: int = 1):
4... self.width = width
5...
6... @staticmethod
7... def get_variants():
8... return "n", {"n": {"width": 8}, "s": {"width": 16}}
9>>> Block(variant="default").width
108
11>>> Block().width
121函数
add_variant_aliases
Python
1def add_variant_aliases(variants: dict[str, Kwargs], aliases: dict[str, Collection[str]] | Literal['yolo'] = 'yolo') -> dict[str, Kwargs]:aliases 中 variants 包含的每个变体名称,该函数会为每个别名添加一个条目。别名条目与变体的条目是相同的字典对象,而不是副本。别名会替换同名的条目。该函数会跳过 variants 中不包含的名称。示例
Pycon
1>>> add_variant_aliases({"n": {"width": 8}, "l": {"width": 64}})
2{'n': {'width': 8}, 'l': {'width': 64},
3 'nano': {'width': 8}, 'large': {'width': 64}}
4>>> add_variant_aliases(
5... {"a": {"x": 1}}, {"a": ["alpha"], "b": ["beta"]}
6... )
7{'a': {'x': 1}, 'alpha': {'x': 1}}variants(dict[str, Kwargs]): 变体,按名称键入。该函数会将别名添加到此字典中。aliases(dict[str, Collection[str]] | Literal['yolo']): 每个变体名称映射到其别名。"yolo"将"tiny"、"nano"、"small"、"medium"和"large"映射到它们的第一个字母,并将每个第一个字母映射回其全名。
dict[str, Kwargs]:variants字典本身,带有别名。