Segmentation DAG#
Model#
- class archai.discrete_search.search_spaces.cv.segmentation_dag.model.SegmentationDagModel(graph: List[Dict[str, Any]], channels_per_scale: Dict[str, Any], post_upsample_layers: int | None = 1, stem_stride: int | None = 2, img_size: Tuple[int, int] | None = (256, 256), nb_classes: int | None = 19)[source]#
Model defined by a directed acyclic graph (DAG) of operations.
- forward(x: Tensor) Tensor [source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- validate_forward(x: Tensor) Tensor [source]#
Checks if the constructed model is working as expected.
- Parameters:
x – Input tensor.
- Returns:
Output tensor.
- classmethod from_file(config_file: str | Path, img_size: Tuple[int, int] | None = 256, nb_classes: int | None = 19) SegmentationDagModel [source]#
Creates a SegmentationArchaiModel from a YAML config file.
- Parameters:
config_file – Path to the YAML config file, following the format: >>> post_upsample_layers: 2 >>> channels_per_scale: >>> 1: 32 >>> 2: 64 >>> architecture: >>> - name: input >>> scale: 1 >>> op: conv3x3 >>> inputs: null >>> - name: node0 >>> scale: 2 >>> op: conv5x5 >>> inputs: [input] >>> - name: output >>> scale: 4 >>> op: conv3x3 >>> inputs: [node0, node1]
img_size – The size of the input image.
nb_classes – The number of classes in the dataset.
- Returns:
A SegmentationArchaiModel instance.
- to_config() Dict[str, Any] [source]#
Converts the model to a configuration dictionary.
- Returns:
A configuration dictionary.
- to_file(path: str) None [source]#
Saves the model to a YAML config file.
- Parameters:
path – Path to the YAML config file.
- training: bool#
Operators#
- class archai.discrete_search.search_spaces.cv.segmentation_dag.ops.NormalConvBlock(in_channels: int, out_channels: int, kernel_size: int | None = 3, stride: int | None = 1, padding: int | None = 1, bias: bool | None = True, **kwargs)[source]#
Normal Convolutional Block with BatchNorm and ReLU.
- forward(x: Tensor)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- class archai.discrete_search.search_spaces.cv.segmentation_dag.ops.SeparableConvBlock(in_channels: int, out_channels: int, kernel_size: int | None = 3, stride: int | None = 1, padding: int | None = 1, expand_ratio: float | None = 1.0, id_skip: bool | None = False, bias: bool | None = True)[source]#
Separable Convolutional Block with BatchNorm and ReLU.
- forward(x: Tensor) Tensor [source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
- class archai.discrete_search.search_spaces.cv.segmentation_dag.ops.Block(in_ch: int, out_ch: int, in_scale: int, out_scale: int, op_name: str)[source]#
Block of operations.
- forward(input: Tensor) Tensor [source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool#
Search Space#
- class archai.discrete_search.search_spaces.cv.segmentation_dag.search_space.SegmentationDagSearchSpace(nb_classes: int, img_size: Tuple[int, int], min_mac: int | None = 0, max_mac: int | None = 9223372036854775807, min_layers: int | None = 1, max_layers: int | None = 12, max_downsample_factor: int | None = 16, skip_connections: bool | None = True, max_skip_connection_length: int | None = 3, max_scale_delta: int | None = 1, max_post_upsample_layers: int | None = 3, min_base_channels: int | None = 8, max_base_channels: int | None = 48, base_channels_binwidth: int | None = 8, min_delta_channels: int | None = 8, max_delta_channels: int | None = 48, delta_channels_binwidth: int | None = 8, downsample_prob_ratio: float | None = 1.5, op_subset: str | None = None, mult_delta: bool | None = False, seed: int | None = 1)[source]#
Search space for segmentation DAGs.
- is_valid_model(model: Module) Tuple[bool, int] [source]#
Check if a model is valid and falls inside of the specified MAdds range.
- Parameters:
model – Model to check.
- Returns:
Tuple of (is_valid, MAdds).
- load_from_graph(graph: List[Dict[str, Any]], channels_per_scale: Dict[str, Any], post_upsample_layers: int | None = 1) ArchaiModel [source]#
Create a SegmentationDagModel from a DAG.
- Parameters:
graph – DAG graph.
channels_per_scale – Number of channels per scale.
post_upsample_layers – Number of post upsample layers.
- Returns:
Archai-based model.
- random_neighbor(param_values: List[int], current_value: int) int [source]#
Sample a random neighbor from an element of a list.
- Parameters:
param_values – List of values.
current_value – Current value.
- Returns:
Random neighbor.
- rename_dag_node_list(node_list: List[Dict[str, Any]], prefix: str | None = '', rename_input_output: bool | None = True, add_input_output: bool | None = False) List[Dict[str, Any]] [source]#
Rename a list of nodes from a DAG.
- Parameters:
node_list – List of nodes.
prefix – Prefix to add to the name of each node.
rename_input_output – Whether to rename the input and output nodes.
add_input_output – Whether to add input and output nodes.
- Returns:
Renamed list of nodes.
- save_arch(model: ArchaiModel, path: str) None [source]#
Save an architecture to a file without saving the weights.
- Parameters:
model – Model’s architecture to save.
file_path – File path to save the architecture.
- save_model_weights(model: ArchaiModel, path: str) None [source]#
Save the weights of a model.
- Parameters:
model – Model to save the weights.
file_path – File path to save the weights.
- load_arch(path: str) ArchaiModel [source]#
Load from a file an architecture that was saved using SearchSpace.save_arch().
- Parameters:
file_path – File path to load the architecture.
- Returns:
Loaded model.
- load_model_weights(model: ArchaiModel, path: str) None [source]#
Load the weights (created with SearchSpace.save_model_weights()) into a model of the same architecture.
- Parameters:
model – Model to load the weights.
file_path – File path to load the weights.
- random_sample() ArchaiModel [source]#
Randomly sample an architecture from the search spaces.
- Returns:
Sampled architecture.
- mutate(base_model: ArchaiModel, patience: int | None = 5) ArchaiModel [source]#
Mutate an architecture from the search space.
This method should not alter the base model architecture directly, only generate a new one.
- Parameters:
arch – Base model.
- Returns:
Mutated model.
- crossover(model_list: List[ArchaiModel], patience: int | None = 30) ArchaiModel | None [source]#
Combine a list of architectures into a new one.
- Parameters:
arch_list – List of architectures.
- Returns:
Resulting model.