def from_config( # type: ignore[override] cls, cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.ORIENTATION_CLASSIFIER", ) -> Dict[str, Any]: node = find_cfg_node(cfg, node_path) head_node_path = node.HEAD_NODE head_node = find_cfg_node(cfg, head_node_path) angle_classes: Sequence[Union[int, float]] = node.ANGLE_CLASSES angle_transform: str = node.ANGLE_TRANSFORM # allow the head class to build its own args, but overwrite some with # our values that we "lift" into the OrientationClassifier CfgNode. head_args: Dict[str, Any] = CLASSIFIER_HEAD_REGISTRY.get( head_node.NAME).from_config(cfg, input_shape=input_shape, node_path=node.HEAD_NODE) head_args["num_classes"] = len(angle_classes) head_args["loss_weight"] = node.LOSS_WEIGHT head_args["loss_key"] = node.LOSS_KEY classifier_head = CLASSIFIER_HEAD_REGISTRY.get( head_node.NAME)(**head_args) # classifier_head = build_classifier_head( # cfg, input_shape, node_path=head_node_path # ) assert isinstance(classifier_head, ClassifierHead) return { "classifier_head": classifier_head, "angle_classes": angle_classes, "angle_transform": angle_transform, "output_key": node.OUTPUT_KEY, }
def from_config( cls, cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.CLASSIFIER_HEAD", *args: Any, **kwargs: Any, ) -> Dict[str, Any]: target_node = find_cfg_node(cfg, node_path) num_classes: int = target_node.NUM_CLASSES pool_method: str = target_node.POOL_METHOD in_features: str = target_node.IN_FEATURES assert ( len(in_features) == 1 ), f"PoolingClassifierHead supports only a single-item IN_FEATURES, got {in_features}" loss_weight: float = target_node.LOSS_WEIGHT loss_key: str = target_node.LOSS_KEY return { "num_classes": num_classes, "pool_method": pool_method, "in_feature": in_features[0], "input_shape": input_shape, "loss_weight": loss_weight, "loss_key": loss_key, }
def from_config( # type: ignore[override] cls, cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.IMAGE_DECODER", ) -> Dict[str, Any]: target_node = find_cfg_node(cfg, node_path) assert len(target_node.IN_FEATURES) == 1 return { "in_feature": target_node.IN_FEATURES[0], "input_shape": input_shape, "layer_sizes": target_node.LAYER_SIZES, "layer_norms": target_node.LAYER_NORMS, "layer_activations": target_node.LAYER_ACTIVATIONS, "output_shape": ( len(cfg.MODEL.PIXEL_MEAN), target_node.OUTPUT_HEIGHT, target_node.OUTPUT_WIDTH, ), "loss_weight": target_node.LOSS_WEIGHT, "loss_key": target_node.LOSS_KEY, "output_key": target_node.OUTPUT_KEY, }
def from_config(cls, cfg: CfgNode, input_shape: ShapeSpec, node_path: str) -> Dict[str, Any]: node = find_cfg_node(cfg, node_path) return { "out_features": node.OUT_FEATURES, "block_specs": node.BLOCK_SPECIFICATIONS, "input_shape": input_shape, }
def build_augmentations_from_cfg( cfg: CfgNode, is_train: bool, node_resolver: Callable[[CfgNode, bool], str] = _default_pipeline_nodes, _extras_default: str = "last", ) -> List[Augmentation]: """ Create a list of :class:`Augmentation`s from a config, using the nodes determined by a node resolver. Returns: list[Augmentation] """ _SEQ_NODENAME: str = "SEQUENCE" node_path: str = node_resolver(cfg, is_train) node: CfgNode = find_cfg_node(cfg, node_path) enabled: bool = node.get("ENABLED", False) extras: str = node.get("EXTRAS", _extras_default) assert extras in ["none", "first", "last"] logger = logging.getLogger(__name__) augs: List[Augmentation] = [] # "enabled" determines whether the custom augmentations are built. if enabled == True: for i, aug_cfg in enumerate(node.get(_SEQ_NODENAME)): try: aug = build_single_augmentation( name=aug_cfg["NAME"], args=aug_cfg["ARGS"], prob=aug_cfg.get("PROB", None), ) except KeyError: logger.error( f"failed to build augmentation: {node_path}.{_SEQ_NODENAME}[{i}]: {aug_cfg}" ) raise augs.append(aug) # "extras" are the things d2 adds by default. # to try to avoid a gotcha for users, "extras" are opt-out with "none" if extras != "none": extra_augs: List[Augmentation] = _build_detectron2_defaults( cfg, is_train) if extras == "first": augs = extra_augs + augs elif extras == "last": augs = augs + extra_augs else: raise ValueError( f'somehow got invalid value for extras: "{extras}"') logger.info( f"Augmentations used in {'training' if is_train else 'testing'}: " + str(augs)) return augs
def from_config(cls, cfg: CfgNode, input_shape: ShapeSpec, node_path: str) -> Dict[str, Any]: node = find_cfg_node(cfg, node_path) return { "input_size": node.INPUT_SIZE, "out_features": node.OUT_FEATURES, "layer_sizes": node.LAYER_SIZES, "layer_norms": node.LAYER_NORMS, "layer_activations": node.LAYER_ACTIVATIONS, "input_shape": input_shape, }
def from_config( # type: ignore[override] cls, cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.UNSUPERVISED_MULTI_OBJECTIVE", ) -> Dict[str, Any]: target_node = find_cfg_node(cfg, node_path) heads: Dict[str, UnsupervisedHead] = {} for head_name, objective_type, head_cfg_path in target_node.OBJECTIVES_LIST: head_module = UNSUPERVISED_HEAD_REGISTRY.get(objective_type) heads[head_name] = head_module(cfg, input_shape, node_path=head_cfg_path) return {"unsupervised_heads": heads}
def from_config( # type: ignore[override] cls, cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.IMAGE_DECODER", ) -> Dict[str, Any]: target_node = find_cfg_node(cfg, node_path) return { "in_features": target_node.IN_FEATURES, "input_shape": input_shape, "output_channels": len(cfg.MODEL.PIXEL_MEAN), "common_stride": target_node.COMMON_STRIDE, "scale_heads_dim": target_node.SCALE_HEADS_DIM, "scale_heads_norm": target_node.SCALE_HEADS_NORM, "predictor_depth": target_node.PREDICTOR_DEPTH, "predictor_dim": target_node.PREDICTOR_DIM, "predictor_norm": target_node.PREDICTOR_NORM, "loss_weight": target_node.LOSS_WEIGHT, "loss_key": target_node.LOSS_KEY, "output_key": target_node.OUTPUT_KEY, }
def build_classifier_head( cfg: CfgNode, input_shape: Dict[str, ShapeSpec], node_path: str = "MODEL.CLASSIFIER_HEAD", ) -> Optional[ClassifierHead]: """ Create a classifier objective from config. Returns: ClassifierHead: a :class:`ClassifierHead` instance. """ node = find_cfg_node(cfg, node_path) name: Optional[str] = node.NAME if name is None: return None classifier_module = CLASSIFIER_HEAD_REGISTRY.get(name)( cfg, input_shape=input_shape, node_path=node_path ) assert isinstance(classifier_module, ClassifierHead) return classifier_module