Some feedback on fast ai library

hi @jeremy

I have been using fastai in the past couple of months after finishing the dl class working on ICIAR2018 challenge. I am in the process of submitting an entry and the fast ai library has been really great both in understanding pytorch and its included functionality. I wanted to give some feedback about the library.

  1. for generating a final image I needed to generate an image by stitching many smaller images. for this I looked for using the learner predict_array method. However unlike predict_with_targs method it doesn’t call the model.eval() method which results in dropout and batch normalization not working correctly

  2. it was useful to have automatic model saving callback to save the models associated with least validation loss, best validation accuracy, etc… so I wanted to suggest adding such a component to the library

class ModelSaver(Callback):

def __init__(self, learner, file_name):
    super().__init__()
    self.learner = learner
    self.file_name = file_name
    self.least_train_loss = None
    self.least_val_loss = None
    self.max_accuracy = None

def on_epoch_end(self, metrics):
    self.last_metrics = metrics
    self.save(self.file_name+ "_last")
    
    if self.least_val_loss is None or metrics[0] < self.least_val_loss:
        self.least_val_loss = metrics[0]
        self.save(self.file_name + "_least_val_loss")
        
    if self.max_accuracy is None or metrics[1] > self.max_accuracy:
        self.max_accuracy = metrics[1]
        self.save(self.file_name + "_max_accuracy")

    if self.least_train_loss is None or self.last_train_loss < self.least_train_loss:
        self.least_train_loss = self.last_train_loss
        self.save(self.file_name+ "_least_train_loss")
    
        
def save(self, filename):
    self.learner.save(filename)
    with open(os.path.join(self.learner.models_path, filename), 'w') as f:
        f.write(str(np.round( [self.last_train_loss] + self.last_metrics, 6)))
        

def on_batch_end(self, loss):
    self.last_train_loss = loss

model_name = "big_model"
callbacks = [ ModelSaver(learn, model_name) ]
learn.fit(lr, 72, cycle_len=1, callbacks = callbacks)

2 Likes