Model detectron faster rcnn
LossEvalHook
Bases: HookBase
Hook to save the metrics and losses on the validation dataset
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
|
__init__(eval_period, model, data_loader)
Initialization of the class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eval_period |
int)
|
Number of iteration between two losses calculation |
required |
model |
Considered model |
required | |
data_loader |
A dataloader containing the validation data |
required |
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
after_step()
After the training step, check if we are at an iteration where we should calculates the losses. If it is the case, calculate them and save them in the storage.
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
ModelDetectronFasterRcnnObjectDetector
Bases: ModelObjectDetectorMixin
, ModelClass
Faster RCNN model (detectron2) for object detection
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
|
__init__(epochs=99, batch_size=1, validation_split=0.2, lr=0.00025, min_delta_es=0.0, patience=5, restore_best_weights=True, data_augmentation_params=None, rpn_min_overlap=0.3, rpn_max_overlap=0.7, rpn_restrict_num_regions=128, roi_nms_overlap_threshold=0.7, pred_bbox_proba_threshold=0.5, pred_nms_overlap_threshold=0.5, nb_log_write_per_epoch=1, nb_log_display_per_epoch=10, **kwargs)
Initialization of the class (see ModelClass & ModelObjectDetectorMixin for more arguments)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epochs |
float
|
Maximal number of epochs |
99
|
batch_size |
int
|
Number of images in a batch when training |
1
|
validation_split |
float
|
Validation split fraction Only used if there is no validation dataset as input when fitting |
0.2
|
lr |
float
|
Base (because we can use a lr scheduler) learning rate to use |
0.00025
|
min_delta_es |
float
|
Minimal change in losses to be considered an amelioration for early stopping |
0.0
|
patience |
int
|
Early stopping patience. Put to 0 to disable early stopping |
5
|
restore_best_weights |
bool
|
If True, when the training is done, save the model with the best loss on the validation dataset instead of the last model (even if early stopping is disabled) |
True
|
data_augmentation_params |
dict
|
Set of allowed data augmentation |
None
|
rpn_min_overlap |
float
|
Under this threshold a region is classified as background (RPN model) |
0.3
|
rpn_max_overlap |
float
|
Above this threshold a region is classified as object (RPN model) |
0.7
|
rpn_restrict_num_regions |
int
|
Maximal number of regions to keep as target for the RPN |
128
|
roi_nms_overlap_threshold |
float
|
The NMS deletes overlapping ROIs whose IOU is above this threshold |
0.7
|
pred_bbox_proba_threshold |
float
|
Above this threshold (for probabilities), a ROI is considered to be a match |
0.5
|
pred_nms_overlap_threshold |
float
|
When predicting, the NMS deletes overlapping predictions whose IOU is above this threshold |
0.5
|
nb_log_write_per_epoch |
int
|
Number of metrics logs written during one epoch (losses for the train and the valid) |
1
|
nb_log_display_per_epoch |
int
|
Number of metrics logs displayed during one epoch (losses for the train only) |
10
|
Raises: ValueError: If rpn_min_overlap is not in [0, 1] ValueError: If rpn_max_overlap is not in [0, 1] ValueError: If rpn_min_overlap > rpn_max_overlap ValueError: If rpn_restrict_num_regions is not positive ValueError: If roi_nms_overlap_threshold is not in [0, 1] ValueError: If pred_bbox_proba_threshold is not in [0, 1] ValueError: If pred_nms_overlap_threshold is not in [0, 1]
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
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 |
|
fit(df_train, df_valid=None, with_shuffle=True)
Trains the model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df_train |
DataFrame
|
Training dataset with columns file_path & bboxes |
required |
Kwargs: df_valid (pd.DataFrame): Validation dataset with columns file_path & bboxes with_shuffle (boolean): If data must be shuffled before fitting This should be used if the target is not shuffled as the split_validation takes the lines in order. Thus, the validation set might get classes which are not in the train set ... Raises: ValueError: If the same classes are not present when comparing an already trained model and a new dataset
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
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 |
|
predict(df_test, write_images=False, output_dir_image=None, **kwargs)
Predictions on test set - batch size must be equal to 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df_test |
DataFrame
|
Data to predict, with a column 'file_path' |
required |
write_images |
bool
|
If True, we write images with the predicted bboxes |
False
|
output_dir_image |
str
|
Path to which we want to write the predicted images (if write_images is True) |
None
|
Returns:
(list>): list (one entry per image) of list of bboxes
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
reload_from_standalone(**kwargs)
Loads a model from its configuration and the weights of the network - /! Experimental /! -
Kwargs
configuration_path (str): Path to configuration file pth_path (str): Path to pth file
Raises: ValueError: If configuration_path is None ValueError: If pth_path is None FileNotFoundError: If the object configuration_path is not an existing file FileNotFoundError: If the object pth_path is not an existing file
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
save(json_data=None)
Saves the model
Kwargs
json_data (dict): Additional configurations to be saved
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
TrainValJSONWriter
Bases: EventWriter
Write scalars to a json file. It saves scalars as one json per line (instead of a big json) for easy parsing.
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
__init__(json_file, length_epoch, nb_iter_per_epoch, nb_iter_log=20)
Initialization of the class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_file |
str
|
File where we save the results |
required |
length_epoch |
int
|
Number of images in an "epoch" |
required |
nb_iter_per_epoch |
int
|
Number of iterations in an "epoch" |
required |
Kwargs: nb_iter_log (int): Number of iteration between two writes
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
close()
write()
Saves the results
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
TrainValMetricPrinter
Bases: EventWriter
Takes care of displaying the metrics on the train (and also on the val)
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
__init__(cfg, with_valid, length_epoch, nb_iter_per_epoch, nb_iter_log=20)
Initialize the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg |
Model configuration |
required | |
with |
valid (bool
|
If true, also displays the results on the validation dataset |
required |
length_epoch |
int
|
Number of images in an "epoch" |
required |
nb_iter_per_epoch |
int
|
Number of iterations in an "epoch" |
required |
Kwargs: nb_iter_log (int): Number of iteration between two displays
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
write()
Prints the wanted info
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
TrainerRCNN
Bases: DefaultTrainer
We overload the class DefaultTraine in order to: - change when we save the metrics - use the COCOevaluator - do data augmentation - save the validation metrics when training - do early stopping
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
|
__init__(cfg, length_epoch, nb_iter_per_epoch, nb_iter_log_write=20, nb_iter_log_display=20, nb_log_write_per_epoch=1, min_delta_es=0.0, patience=0, restore_best_weights=False)
Initialize the Trainer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg |
Configuration to use |
required | |
length_epoch |
int
|
Number of image in an epoch |
required |
nb_iter_per_epoch |
int
|
Number of iterations in an epoch |
required |
Kwargs: nb_iter_log_write (int): Number of iterations between two log writes (losses on train and validation datasets) nb_iter_log_display (int): Number of iterations between two log displays (losses on train dataset only) nb_log_write_per_epoch (int): Number of metrics logs written during one epoch (losses for the train and the valid) min_delta_es (float): Minimal change in losses to be considered an amelioration for early stopping patience (int): Early stopping patience. Put to 0 to disable early stopping restore_best_weights (bool): If True, when the training is done, save the model with the best loss on the validation dataset instead of the last model (even if early stopping is disabled)
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
after_step()
Function triggered after each step
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
If early stopping has been triggered |
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
build_evaluator(cfg, dataset_name)
classmethod
We redefine the method in order to use the COCOevaluator
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg |
Training configuration |
required | |
dataset_name |
str
|
Name of the dataset |
required |
Returns: Evaluator to use
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
build_hooks()
Build a list of default hooks, including timing, evaluation, checkpointing, lr scheduling, precise BN, writing events.
We rewrite this methos (instead of overloading it) so that we can change when we save metrics From : https://detectron2.readthedocs.io/en/latest/_modules/detectron2/engine/defaults.html#DefaultTrainer
Warning, we deleted the hook on the checkpoint, the early stopping hook takes care of it now !
Returns:
Type | Description |
---|---|
list
|
list[HookBase]: |
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
build_train_loader(cfg)
classmethod
We redefine the method in order to use our own data augmentation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg |
Training configuration |
required |
Returns: train loader to use, with our own data augmentation
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
early_stopping()
Triggers if the condition for early stopping are met. We think in term of epoch. Thus the patience is indeed the number of epochs without amelioration
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
If early stopping has been triggered |
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
train()
Run training.
Raises:
Type | Description |
---|---|
AttributeError
|
No evaluation results obtained during training! |
Returns: OrderedDict of results, if evaluation is enabled. Otherwise None.
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
write_model_final()
If self.restore_best_weights == False, no model is saved during training. Thus we save the final model with the name best.pth
Source code in template_vision/models_training/object_detectors/model_detectron_faster_rcnn.py
data_augmentation_mapper(dataset_dict, horizontal_flip=False, vertical_flip=False, rot_90=False)
Applies the data augmentation on data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_dict |
dict)
|
Data dictionary containing the images on which to do data augmentation |
required |
horizontal_flip |
bool)
|
If True, can do horizontal flip (with 0.5 proba) |
False
|
vertical_flip |
bool)
|
If True, can do vertical flip (with 0.5 proba) |
False
|
rot_90 |
bool)
|
If True, can do a rotation of 0, 90, 180 or 270 degrees (0.25 proba for each) |
False
|
Returns:
Type | Description |
---|---|
dict
|
The dictionary after data augmentation |