train_model
Train and test the neural network.
FoldMetrics
Bases: BaseModel
Metrics for each fold.
Parameters
n_epochs
:int
How many epochs were trained.iou_thresholds
:List[float]
Which IoU thresholds were used to compute the models performance metrics.avg_train_loss
:List[float]
The average training loss for each epoch.avg_val_loss
:List[float]
The average validation loss for each epoch.metrics
:List[List[PerformanceMetrics]]
The performance metrics for each epoch for each IoU threshold.
Source code in chirpdetector/train_model.py
PerformanceMetrics
Bases: BaseModel
Performance metrics for object detection models.
Parameters
classes
:List[int]
The classes.precision
:List[List[float]]
The precision per class per target (bbox).recall
:List[List[float]]
The recall per class per target (bbox).f1
:List[List[float]]
The f1 score per class per target (bbox).scores
:List[List[float]]
The scores per class per target (bbox).average_precision
:List[float]
The average precision per class.mean_avg_prec
:float
The mean average precision.
Source code in chirpdetector/train_model.py
bbox_dict_to_list(predicted_bboxes, groundtruth_bboxes)
Convert the predicted and groundtruth bboxes to lists.
Format will be as follows: [img_idx, label, score, x1, y1, x2, y2]
For ground truth the score will always be 1.
Parameters
predicted_bboxes
:List[dict]
The predicted bboxes.groundtruth_bboxes
:List[dict]
The groundtruth bboxes.
Returns
pred_boxes
:List
The predicted bboxes.true_boxes
:List
The groundtruth bboxes.
Source code in chirpdetector/train_model.py
collapse_all_dims(arr)
Collapse all dimensions of an array.
Parameters
np.ndarray
:np.ndarray
The array to collapse.
Returns
np.ndarray
The collapsed array.
Source code in chirpdetector/train_model.py
intersection_over_union(boxes_preds, boxes_labels, box_format='corners')
Calculate intersection over union.
Adapted from: https://github.com/aladdinpersson/Machine-Learning-Collection/blob/master /ML/Pytorch/object_detection/metrics/iou.py
Parameters
boxes_preds (tensor): Predictions of Bounding Boxes (BATCH_SIZE, 4)
boxes_labels (tensor): Correct Labels of Boxes (BATCH_SIZE, 4)
box_format (str): midpoint/corners, if boxes (x,y,w,h) or (x1,y1,x2,y2)
Returns
tensor: Intersection over union for all examples
Source code in chirpdetector/train_model.py
mean_average_precision(pred_boxes, true_boxes, iou_threshold=0.5, box_format='corners', num_classes=1)
Calculate mean average precision and metrics used in it.
Adapted from: https://github.com/aladdinpersson/Machine-Learning-Collection /blob/master/ML/Pytorch/object_detection/metrics/mean_avg_precision.py
Parameters
pred_boxes
:list
list of lists containing all bboxes with each bboxes specified as [train_idx, class_prediction, prob_score, x1, y1, x2, y2]true_boxes
:list
Similar as pred_boxes except all the correct ones. Score is set to 1.iou_threshold
:float
IOU threshold where predicted bboxes is correct. See intersection_over_union function.box_format
:str
"midpoint" or "corners" used to specify bboxes Midpoint is YOLO format: [x, y, width, height] and corners is e.g. COCO format: [x1, y1, x2, y2]. This model outputs "corners" format.num_classes
:int
number of classes
Returns
PerformanceMetrics
The performance metrics.
Source code in chirpdetector/train_model.py
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 |
|
plot_epochs(epoch_train_loss, epoch_val_loss, epoch_avg_train_loss, epoch_avg_val_loss, path)
Plot the loss for each epoch.
Parameters
epoch_train_loss
:list
The training loss for each epoch.epoch_val_loss
:list
The validation loss for each epoch.epoch_avg_train_loss
:list
The average training loss for each epoch.epoch_avg_val_loss
:list
The average validation loss for each epoch.path
:pathlib.Path
The path to save the plot to.
Returns
None
Source code in chirpdetector/train_model.py
plot_folds(fold_avg_train_loss, fold_avg_val_loss, path)
Plot the loss for each fold.
Parameters
fold_avg_train_loss
:list
The average training loss for each fold.fold_avg_val_loss
:list
The average validation loss for each fold.path
:pathlib.Path
The path to save the plot to.
Returns
None
Source code in chirpdetector/train_model.py
save_model(epoch, model, optimizer, path)
Save the model state dict.
Parameters
epoch
:int
The current epoch.model
:torch.nn.Module
The model to save.optimizer
:torch.optim.Optimizer
The optimizer to save.path
:pathlib.Path
The path to save the model to.
Returns
None
Source code in chirpdetector/train_model.py
train(config, mode='pretrain')
Train the model.
Parameters
config
:Config
The config file.mode
:str
The mode to train in. Eitherpretrain
orfinetune
.
Returns
None
Source code in chirpdetector/train_model.py
699 700 701 702 703 704 705 706 707 708 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 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 |
|
train_cli(config_path, mode)
Train the model from the command line.
Parameters
config_path
:pathlib.Path
The path to the config file.mode
:str
The mode to train in. Eitherpretrain
orfinetune
.
Returns
None
Source code in chirpdetector/train_model.py
train_epoch(dataloader, device, model, optimizer)
Train the model for one epoch.
Parameters
dataloader
:DataLoader
The dataloader for the training data.device
:torch.device
The device to train on.model
:torch.nn.Module
The model to train.optimizer
:torch.optim.Optimizer
The optimizer to use.
Returns
train_loss
:List
The training loss for each batch.
Source code in chirpdetector/train_model.py
val_epoch(dataloader, device, model)
Validate the model for one epoch.
Parameters
dataloader
:DataLoader
The dataloader for the validation data.device
:torch.device
The device to train on.model
:torch.nn.Module
The model to train.
Returns
loss_dict
:dict
The loss dictionary.