transfer
ðŸ§
Scripts for transfer learning of deep learning models for MRI-based predictions.
The goal is to transfer models such as the 3D-CNNs reported in Hofmann et al. (2022, NeuroImage) to new and smaller datasets and other prediction tasks.
Author: Simon M. Hofmann
Years: 2023
This module is experimental and still in development.
adapt_model
ðŸ§
adapt_model(
model: Sequential,
learning_rate: float = 0.0005,
target_bias: float | None = None,
n_classes: bool | int = False,
) -> Sequential
Adapt a pretrained model to a new dataset and/or task.
This function adapts the output layer of a pretrained model to a new dataset and/or task by replacing the output layer. The model is recompiled with a new learning rate and loss function.
This function is experimental and still in development.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Sequential
|
Pretrained |
required |
learning_rate |
float
|
Learning rate for the optimizer. |
0.0005
|
target_bias |
float | None
|
Model output bias. For classification tasks with this can be left blank [ |
None
|
n_classes |
bool | int
|
Number of classes. For regression tasks set to |
False
|
Returns:
Type | Description |
---|---|
Sequential
|
The adapted model. |
Source code in src/xai4mri/model/transfer.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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
analyse_model_and_data
ðŸ§
analyse_model_and_data(
model: Sequential, data: BaseDataSet, target: str
)
Analyze model and data to estimate training parameters for transfer learning.
This function is not implemented yet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Sequential
|
Pre-trained |
required |
data |
BaseDataSet
|
Dataset, which should be used for model fine-tuning. |
required |
target |
str
|
Variable in the dataset, which is supposed to be predicted. |
required |
Returns:
Type | Description |
---|---|
Recommended number of training epochs, number of training samples, freeze weights, ... |
Source code in src/xai4mri/model/transfer.py
97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
dual_phase_model_training
ðŸ§
dual_phase_model_training(
model: Sequential,
epochs: tuple[int, int],
data: BaseDataSet,
target: str,
model_parent_path: str | Path,
split_dict: dict | None = None,
callbacks: list[Callback] | None = None,
**kwargs
) -> Sequential | None
Train / finetune a model in two phases.
First, train all layers of the model. Then, freeze the first layers, and only finetune the last layers.
This function is experimental and is still in development.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Sequential
|
Compiled |
required |
epochs |
tuple[int, int]
|
Number of training epochs. |
required |
data |
BaseDataSet
|
Dataset for training and evaluation. |
required |
target |
str
|
Variable to be predicted. |
required |
model_parent_path |
str | Path
|
The path to the parent folder of the given model, where the model will be saved. |
required |
split_dict |
dict | None
|
Data split dictionary for training, validation, and test data. |
None
|
callbacks |
list[Callback] | None
|
A list of |
None
|
kwargs |
Additional keyword arguments for |
{}
|
Returns:
Type | Description |
---|---|
Sequential | None
|
trained model |
Source code in src/xai4mri/model/transfer.py
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 304 305 306 307 |
|
mono_phase_model_training
ðŸ§
mono_phase_model_training(
model: Sequential,
epochs: int,
data: BaseDataSet,
target: str,
model_parent_path: str | Path,
split_dict: dict | None = None,
callbacks: list[Callback] | None = None,
**kwargs
) -> Sequential | None
Train / finetune all model weights at once.
This simply trains the model on the provided dataset for the given number of epochs.
When used for transfer learning
This is a naive approach to transfer learning, and can lead to issues such as catastrophic forgetting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Sequential
|
Compiled |
required |
epochs |
int
|
Number of training epochs. |
required |
data |
BaseDataSet
|
Dataset for training and evaluation. This must be a subclass of |
required |
target |
str
|
Variable to be predicted. This must be in the 'study_table |
required |
model_parent_path |
str | Path
|
The path to the parent folder of the given model, where the model will be saved. |
required |
split_dict |
dict | None
|
Data split dictionary for training, validation, and test data. |
None
|
callbacks |
list[Callback] | None
|
A list of |
None
|
kwargs |
Additional keyword arguments for |
{}
|
Returns:
Type | Description |
---|---|
Sequential | None
|
Trained model. |
Source code in src/xai4mri/model/transfer.py
112 113 114 115 116 117 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 |
|