ModelManager
automil.model.ModelManager provides model management utilities and is responsible for instantiating MIL models and
validating hyperparameters against model-specific contraints and limits
ModelManager
Manages the instantiation and configuration of MIL models.
This class provides
- An interface for creating automil supported MIL models
- Model-specific hyperparameter validation and adjustments should they be outside of recommended model-limits
- dummy input generation for debugging and validation
Source code in automil/model.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
model_class
property
model_class: type[Module]
Corresponding python class implementing the model.
Returns:
| Type | Description |
|---|---|
type[Module]
|
type[nn.Module]: Model class. |
slideflow_name
property
slideflow_name: str
Slideflow-internal identifier for the managed model.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Slideflow model name. |
compare_models
classmethod
compare_models() -> str
Generates a comparison table for all available models
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A comparison table as string |
Source code in automil/model.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
create_dummy_input
create_dummy_input(
batch_size: int, tiles_per_bag: int, input_dim: int
) -> tuple
Creates an appropriate dummy input for the model
Dummy input tensors can be used for a variety of tasks. Primarily they are used
to perform dry runs, for example to measure the memory reservation of a model instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of samples in batch |
required |
tiles_per_bag
|
int
|
Number of tiles per bag |
required |
input_dim
|
int
|
Feature dimension |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of tensors to pass to model forward() |
Source code in automil/model.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
create_model
create_model(
input_dim: int = 1024, num_classes: int = 2, **kwargs
) -> nn.Module
Instantiates the model with validated hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Feature dimensions. Defaults to 1024. |
1024
|
num_classes
|
int
|
Number of classes. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
Module
|
nn.Module: Instantiated model |
Source code in automil/model.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
validate_hyperparameters
validate_hyperparameters(
lr: float, batch_size: int, max_tiles_per_bag: int
) -> dict[str, float | int]
Validates a set of hyperparameters against model-specific constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Learning rate. |
required |
batch_size
|
int
|
Batch size. |
required |
max_tiles_per_bag
|
int
|
Maximum tiles per bag. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float | int]
|
dict[str, float | int]: Suggested parameter adjustments for out-of-range values. |
Source code in automil/model.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
Helpers
create_model_instance
create_model_instance(
model_type: ModelType, input_dim: int, n_out: int = 2
) -> nn.Module
Safely creates a model instance with the correct parameters.
This method instantiates a model corresponding to the provided
:class:ModelType with the specified input and output dimensions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_type
|
ModelType
|
The ModelType enum |
required |
input_dim
|
int
|
Input feature dimension |
required |
n_out
|
int
|
Number of output classes |
2
|
Returns:
| Type | Description |
|---|---|
Module
|
Instantiated model |
Source code in automil/model.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |