ez-frcnn.training
def training.create_model(num_classes):
Creates a Faster R-CNN model pre-trained on COCO and modifies its head for a custom number of classes.
Inputs
num_classes (int): Number of output classes for detection (including background).
Outputs
torchvision.models.detection.FasterRCNN: The modified Faster R-CNN model ready for training or inference.
Source code in library/training.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
def training.load_model_train(model_name, MODEL_DIR, NUM_CLASSES):
Loads a trained model for inference or further training.
Inputs
model_name (str): Filename of the saved model weights. MODEL_DIR (str): Directory where the model weights are stored. NUM_CLASSES (int): Number of output classes the model predicts.
Outputs
torch.nn.Module: The model loaded with trained weights, moved to the appropriate device (CPU or GPU).
Source code in library/training.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
def training.train(train_data_loader, model, optimizer, train_loss_list, train_loss_hist, train_itr, DEVICE):
Performs one epoch of training on the provided model.
Inputs
train_data_loader (DataLoader): Iterable over training dataset batches. model (torch.nn.Module): The model to train. optimizer (torch.optim.Optimizer): Optimizer used for updating model weights. train_loss_list (list): List to store loss values per iteration. train_loss_hist (generator): Generator to track or log loss history. train_itr (int): Current training iteration count. DEVICE (torch.device): Device (CPU or GPU) to perform computations on.
Outputs
train_loss_list (list): Updated list of training loss values.
Source code in library/training.py
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 |
|
def training.validate(valid_data_loader, model, val_loss_list, val_loss_hist, val_itr, DEVICE):
Performs one epoch of validation on the provided model.
Inputs
valid_data_loader (DataLoader): Iterable over validation dataset batches. model (torch.nn.Module): The model to validate. val_loss_list (list): List to store validation loss values per iteration. val_loss_hist (generator): Generator to track or log validation loss history. val_itr (int): Current validation iteration count. DEVICE (torch.device): Device (CPU or GPU) to perform computations on.
Outputs
val_loss_list (list): Updated list of validation loss values.
Source code in library/training.py
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 |
|
def training.train_model(model, train_loader, valid_loader, DEVICE, MODEL_NAME, NUM_EPOCHS, OUT_DIR, PLOT_DIR, SAVE_MODEL_EPOCH, SAVE_PLOTS_EPOCH, tqdm_all, train_loss_mpl):
Train a given PyTorch model with training and validation datasets, periodically saving model checkpoints and loss plots.
Inputs
model (torch.nn.Module): The model to train. train_loader (DataLoader): DataLoader for training dataset. valid_loader (DataLoader): DataLoader for validation dataset. DEVICE (torch.device): Device on which to run training (CPU or GPU). MODEL_NAME (str): Base name for saving model checkpoints. NUM_EPOCHS (int): Total number of epochs for training. OUT_DIR (str): Directory to save model checkpoints. PLOT_DIR (str): Directory to save training/validation loss plots. SAVE_MODEL_EPOCH (int): Frequency (in epochs) to save the model. SAVE_PLOTS_EPOCH (int): Frequency (in epochs) to save the loss plots. tqdm_all (iterable): Iterable (e.g., tqdm wrapper) for epoch iteration. train_loss_mpl (Panel object): Matplotlib figure holder for live loss plot updates.
Output
list: A list containing two elements: - train_loss_list: Loss values for all training iterations. - val_loss_list: Loss values for all validation iterations.
Source code in library/training.py
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 |
|
def training.train_model_no_val(model, train_loader, valid_loader, DEVICE, MODEL_NAME, NUM_EPOCHS, OUT_DIR, PLOT_DIR, SAVE_MODEL_EPOCH, SAVE_PLOTS_EPOCH):
Train a PyTorch model using only the training dataset, without validation.
Inputs
model (torch.nn.Module): The model to train. train_loader (DataLoader): DataLoader for training dataset. valid_loader (DataLoader): DataLoader for validation dataset (unused). DEVICE (torch.device): Device for training (CPU or GPU). MODEL_NAME (str): Base filename for saving model checkpoints. NUM_EPOCHS (int): Number of training epochs. OUT_DIR (str): Directory to save model checkpoints. PLOT_DIR (str): Directory to save training loss plots. SAVE_MODEL_EPOCH (int): Frequency (in epochs) to save the model. SAVE_PLOTS_EPOCH (int): Frequency (in epochs) to save training loss plots.
Output
list: A list containing two elements: - train_loss_list: Loss values for all training iterations. - val_loss_list: Empty list (validation losses not tracked).
Source code in library/training.py
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 |
|
class Averager:
Utility class to compute and maintain the running average of numeric values.
Methods:
Name | Description |
---|---|
send |
Add a new value to the running total and increment count. |
value |
Returns the current average of all values received. |
reset |
Resets the total and count to start a new average calculation. |
Source code in library/training.py
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 |
|