TensorFlow
edgemark.models.platforms.TensorFlow.model_generator
main
main(cfg_path=config_file_path, **kwargs)
Generate, train, evaluate, and save models based on the given configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg_path
|
str
|
The path to the configuration file containing the model generation parameters. The configuration file that this path points to should contain the following keys: - model_type (str): A placeholder for the model type. This will be populated by the target model configuration. - time_tag (str): A placeholder for the time tag. This will be populated by the current time. - target_models_dir (str): Path to the directory containing the target models configurations. - datasets_dir (str): Path to the directory containing the datasets. - linkers_dir (str): Path to the directory where the generated models list will be saved. - model_path (str): Path to the model file. - model_save_dir (str): Path to the directory where the generated model will be saved. - data_save_dir (str): Path to the directory where the representative and equality check data will be saved. - TFLM_info_save_path (str): Path to the file where the TFLM info will be saved. - wandb_online (bool): Flag to enable or disable the W&B online mode. - wandb_project_name (str): Name of the W&B project. - train_models (bool): Flag to enable or disable model training. - evaluate_models (bool): Flag to enable or disable model evaluation. - measure_execution_time (bool): Flag to enable or disable the measurement of execution time. - epochs (int): Number of epochs for training the model. If specified, it will override the number of epochs in the model configuration. - n_representative_data (int): Number of samples to be saved for TFLite conversion. - n_eqcheck_data (int): Number of samples to be saved for equivalence check of the model on PC and MCU. |
config_file_path
|
**kwargs
|
dict
|
Keyword arguments to be passed to the configuration file. |
{}
|
Returns:
| Type | Description |
|---|---|
list
|
A list of dictionaries containing the following keys for each target model: - name (str): Name of the target model configuration file. - result (str): Result of the model generation. It can be either "success" or "failed". - error (str): Error message in case of failure. - traceback (str): Traceback in case of failure. |
Source code in edgemark/models/platforms/TensorFlow/model_generator.py
35 36 37 38 39 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
save_models_list
save_models_list(models_list, save_dir)
Saves the list of generated models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models_list
|
list
|
The list of generated models. |
required |
save_dir
|
str
|
The directory where the models list should be saved. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_generator.py
22 23 24 25 26 27 28 29 30 31 32 | |
edgemark.models.platforms.TensorFlow.model_template
This module contains a template class that other models should inherit from.
ModelSupervisorTemplate
This class is a template for TensorFlow models. In order to create a new model, you should inherit from this class and implement its abstract functions.
Source code in edgemark/models/platforms/TensorFlow/model_template.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
__init__
__init__(cfg=None)
Initializes the class. The following attributes should be set in the init function: self.model (tf.keras.Model): The model. self.dataset (DatasetSupervisorTemplate): The dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
dict
|
The configurations of the model. Defaults to None. |
None
|
Source code in edgemark/models/platforms/TensorFlow/model_template.py
22 23 24 25 26 27 28 29 30 31 32 33 | |
compile_model
compile_model(fine_tuning=False)
Compiles the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fine_tuning
|
bool
|
If True, the model will be compiled for fine-tuning. Defaults to False. |
False
|
Source code in edgemark/models/platforms/TensorFlow/model_template.py
50 51 52 53 54 55 56 57 | |
evaluate_model
evaluate_model()
Evaluates the model.
Returns:
| Type | Description |
|---|---|
dict
|
The evaluation metrics. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
78 79 80 81 82 83 84 85 | |
get_FLOPs
get_FLOPs()
Returns the number of FLOPs of the model.
Returns:
| Type | Description |
|---|---|
int
|
The number of FLOPs. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
get_model_info
get_model_info()
Returns the model info that can be anything important, including its configuration.
Returns:
| Type | Description |
|---|---|
dict
|
The model info. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
88 89 90 91 92 93 94 95 | |
get_params_count
get_params_count()
Returns the number of parameters in the model.
Returns:
| Type | Description |
|---|---|
list[int]
|
The total number of parameters, the number of trainable parameters, and the number of non-trainable parameters. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
load_eqcheck_data
staticmethod
load_eqcheck_data(load_dir)
Loads the eqcheck data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_dir
|
str
|
The directory where the eqcheck data is stored. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(data_x, data_y_pred) |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
load_model
load_model(load_dir)
Loads the model in the SavedModel format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_dir
|
str
|
The parent directory where the SavedModel format is stored in. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
257 258 259 260 261 262 263 264 | |
load_representative_data
staticmethod
load_representative_data(load_dir)
Loads the representative data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_dir
|
str
|
The directory where the representative data is stored. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The representative data. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
187 188 189 190 191 192 193 194 195 196 197 198 199 | |
load_weights
load_weights(load_dir)
Loads the model weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_dir
|
str
|
The directory where the model weights are stored. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
293 294 295 296 297 298 299 300 | |
log_model_to_wandb
staticmethod
log_model_to_wandb(model_dir, model_save_name)
Logs the model to W&B.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_dir
|
str
|
The directory where the model is stored. |
required |
model_save_name
|
str
|
The name that will be assigned to the model artifact. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
267 268 269 270 271 272 273 274 275 276 277 278 279 | |
measure_execution_time
measure_execution_time()
Measures the execution time of the model. The process starts by a warm-up phase for 100 iterations, then the execution time is measured for ~10 seconds.
Returns:
| Type | Description |
|---|---|
float
|
The execution time in ms. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
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 | |
save_TFLM_info
save_TFLM_info(save_path)
Saves the information required by the TFLM converter as a YAML file. This is to help TFLM converter in a later stage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_path
|
str
|
The YAML file path where the TFLM info should be saved. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
save_eqcheck_data
save_eqcheck_data(n_samples, save_dir)
Saves the eqcheck data as {"data_x", "data_y_pred"}.
The data_x has shape (samples, input_shape) and data_y_pred has shape (samples, output_shape).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
The number of samples to be saved |
required |
save_dir
|
str
|
The directory where the data should be saved |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
save_model
save_model(save_dir)
Saves the model in two formats: Keras and SavedModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_dir
|
str
|
The directory where the model should be saved in. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
save_model_info
staticmethod
save_model_info(model_info, save_dir)
Saves the model info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_info
|
dict
|
The model info. |
required |
save_dir
|
str
|
The directory where the model info should be saved. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
303 304 305 306 307 308 309 310 311 312 313 314 315 | |
save_representative_data
save_representative_data(n_samples, save_dir)
Saves the representative data with shape (samples, *input_shape).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
The number of samples to be saved. |
required |
save_dir
|
str
|
The directory where the data should be saved. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
173 174 175 176 177 178 179 180 181 182 183 | |
save_weights
save_weights(save_dir)
Saves the model weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_dir
|
str
|
The directory where the model weights should be saved. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
282 283 284 285 286 287 288 289 290 | |
set_configs
set_configs(cfg)
Sets the configs from the given dictionary.
Note: The changed configs won't affect the data, model, or any other loaded attributes. In case you want to change them, you should call the corresponding functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
dict
|
The configurations. |
required |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
36 37 38 39 40 41 42 43 44 45 46 | |
train_model
train_model(fine_tuning=False, tensorboard_log_dir=None, best_weights_dir=None, use_wandb=False)
Trains the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fine_tuning
|
bool
|
If True, the model will be trained for fine-tuning. Defaults to False. |
False
|
tensorboard_log_dir
|
str
|
The directory where the logs should be saved. If None, the logs won't be saved. Defaults to None. |
None
|
best_weights_dir
|
str
|
The directory where the best weights should be saved. If None, the best weights won't be saved. Defaults to None. |
None
|
use_wandb
|
bool
|
If True, the training progress will be logged to W&B. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Optional[History]
|
The training history or None. |
Source code in edgemark/models/platforms/TensorFlow/model_template.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |