Post 002 - The wise seek to contain power (Save and Reload trained model)
When I ran some fast.ai Jupyter notebooks, I had to spend a long time fine-tuning deep learning models. At some point, I might train an awesome model probably. However, if I accidentally closed the notebook or the notebook was shut down at this time, I had to re-train these models, which might waste a lot of time. Thus, I was thinking about a way to save these well-performance models. At the same time, I can reload these models whenever I need to. I found that notebook (02-saving-a-basic-fastai-model.ipynb) mentioned a way to export the trained model.
After finishing the model training,
dls = ImageDataLoaders.from_name_func('.',
get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat,
item_tfms=Resize(192))
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
export it by using learn.export() as follows.
learn.export('model.pkl')
However, this notebook didn’t mention anything about reloading my model back. After searching, I found a new way to save and reload my model. Save my model by using learn.save().
from fastai.losses import CrossEntropyLossFlat
learn = vision_learner(dls, resnet34, metrics=accuracy, loss_func=CrossEntropyLossFlat())
learn.fine_tune(3)
# Save the latest trained model
learn.save('trained_model')
Reload my model by using learn.load(). However, it is worth mentioning that I have to reload the training set to run the dls and vision_learner() before I reload the model. This method worked successfully and saved lots of time during the development of the program.
from fastai.vision.all import *
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(path)
learn = vision_learner(dls, resnet34, metrics=accuracy, loss_func=CrossEntropyLossFlat())
# Reload the latest trained model
learn.load('Q2_trained_model')