misc.py
def train(n_epochs, loaders, model, optimizer, criterion, device, path_model, fivecrop = None, lr_scheduler = None):
    """Trains, validates, and saves the model and other data in a file"""
    # initialize tracker for minimum validation loss
    valid_loss_min = np.Inf 
    train_loss = []
    valid_loss = []
    path_state_dict = f"./temp/temp_state_dict_{str(int(np.abs(np.random.randn()) * 1e12))}.pt"
    # Time everything
    time_start = time.time()
    for epoch in range(1, n_epochs+1):
        time_start_epoch = time.time()    
        # Train this epoch
        model, train_loss_epoch = train_epoch(model,loaders["train"],optimizer,criterion,device)
        train_loss.append(train_loss_epoch)   
        # Validate this epoch
        valid_loss_epoch = valid_epoch(model,loaders["valid"],criterion,device,fivecrop)
        # Call the learning rate scheduler if we have one
        if lr_scheduler is not None:
            lr_scheduler.step(valid_loss_epoch)
        valid_loss.append(valid_loss_epoch)  
        # Save if validation loss is the lowest so far
        if valid_loss_epoch <= valid_loss_min:
            torch.save(model.state_dict(), path_state_dict)
            valid_loss_min = valid_loss_epoch 
        # Print epoch statistics
        print('Epoch {} done in {:.2f} seconds. \tTraining Loss: {:.3f} \tValidation Loss: {:.3f}'.format(
            epoch,             
            time.time() - time_start_epoch,
            train_loss_epoch,
            valid_loss_epoch
            ))   
    # Show final statistics    
    print(f"{n_epochs} epochs ready in {(time.time() - time_start):.3f} seconds. Minimum validation loss: {valid_loss_min:.3f}")
    # Load best config
    model.load_state_dict(torch.load(path_state_dict))
    # Save everything to a file
    model_data = {"model": model, "train_loss": train_loss, "valid_loss": valid_loss}
    torch.save(model_data,path_model)
    # Remove the temporary file
    os.remove(path_state_dict)
Trains, validates, and saves the model and other data in a file
Trains, validates, and saves the model and other data in a file
Trains validates and saves the model and other data in a file
def train(n_epochs, loaders, model, optimizer, criterion, device, path_model, fivecrop = None, lr_scheduler = None):
    valid_loss_min = np.Inf 
    train_loss = []
    valid_loss = []
    path_state_dict = f"./temp/temp_state_dict_{str(int(np.abs(np.random.randn()) * 1e12))}.pt"
    time_start = time.time()
    for epoch in range(1, n_epochs+1):
        time_start_epoch = time.time()    
        model, train_loss_epoch = train_epoch(model,loaders["train"],optimizer,criterion,device)
        train_loss.append(train_loss_epoch)   
        valid_loss_epoch = valid_epoch(model,loaders["valid"],criterion,device,fivecrop)
        if lr_scheduler is not None:
            lr_scheduler.step(valid_loss_epoch)
        valid_loss.append(valid_loss_epoch)  
        if valid_loss_epoch <= valid_loss_min:
            torch.save(model.state_dict(), path_state_dict)
            valid_loss_min = valid_loss_epoch 
        print('Epoch {} done in {:.2f} seconds. \tTraining Loss: {:.3f} \tValidation Loss: {:.3f}'.format(
            epoch,             
            time.time() - time_start_epoch,
            train_loss_epoch,
            valid_loss_epoch
            ))   
    print(f"{n_epochs} epochs ready in {(time.time() - time_start):.3f} seconds. Minimum validation loss: {valid_loss_min:.3f}")
    model.load_state_dict(torch.load(path_state_dict))
    model_data = {"model": model, "train_loss": train_loss, "valid_loss": valid_loss}
    torch.save(model_data,path_model)
    os.remove(path_state_dict)
def train ( n_epochs , loaders , model , optimizer , criterion , device , path_model , fivecrop = None , lr_scheduler = None ) : valid_loss_min = np . Inf train_loss = [ ] valid_loss = [ ] path_state_dict = f"./temp/temp_state_dict_{str(int(np.abs(np.random.randn()) * 1e12))}.pt" time_start = time . time ( ) for epoch in range ( 1 , n_epochs + 1 ) : time_start_epoch = time . time ( ) model , train_loss_epoch = train_epoch ( model , loaders [ "train" ] , optimizer , criterion , device ) train_loss . append ( train_loss_epoch ) valid_loss_epoch = valid_epoch ( model , loaders [ "valid" ] , criterion , device , fivecrop ) if lr_scheduler is not None : lr_scheduler . step ( valid_loss_epoch ) valid_loss . append ( valid_loss_epoch ) if valid_loss_epoch <= valid_loss_min : torch . save ( model . state_dict ( ) , path_state_dict ) valid_loss_min = valid_loss_epoch print ( 'Epoch {} done in {:.2f} seconds. \tTraining Loss: {:.3f} \tValidation Loss: {:.3f}' . format ( epoch , time . time ( ) - time_start_epoch , train_loss_epoch , valid_loss_epoch ) ) print ( f"{n_epochs} epochs ready in {(time.time() - time_start):.3f} seconds. Minimum validation loss: {valid_loss_min:.3f}" ) model . load_state_dict ( torch . load ( path_state_dict ) ) model_data = { "model" : model , "train_loss" : train_loss , "valid_loss" : valid_loss } torch . save ( model_data , path_model ) os . remove ( path_state_dict )
Trains, validates, and saves the model and other data in a file
Trains validates and saves the model and other data in a file
"""Trains, validates, and saves the model and other data in a file""" # initialize tracker for minimum validation loss # Time everything # Train this epoch # Validate this epoch # Call the learning rate scheduler if we have one # Save if validation loss is the lowest so far # Print epoch statistics # Show final statistics     # Load best config # Save everything to a file # Remove the temporary file
n_epochs
loaders
model
optimizer
criterion
device
path_model
fivecrop
lr_scheduler