Skip to content

Instantly share code, notes, and snippets.

@ulat
Created June 9, 2020 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulat/5658a863b578d2617654dabddd9dd1a7 to your computer and use it in GitHub Desktop.
Save ulat/5658a863b578d2617654dabddd9dd1a7 to your computer and use it in GitHub Desktop.
I have extended the `SaveModelCallback` so that I can copy my saved models from colab onto gdrive automatically.
class SaveAndPersistModel(SaveModelCallback):
def __init__(self , monitor='valid_loss', comp=None, min_delta=0., fname='model', every_epoch=False, add_save=None, with_opt=False,
persistence_path:str='./drive/My\ Drive/'):
super().__init__(monitor, comp, min_delta, fname, every_epoch, add_save, with_opt)
self.persistence_path = persistence_path
def after_epoch(self):
super().after_epoch()
#print(str(self.path/f"{self.fname}.tar.gz"))
#print(str(self.path/self.model_dir/f"{self.fname}.pth"))
#print(self.persistence_path)
subprocess.call(["tar", "-czvf", str(self.path/f"{self.fname}.tar.gz"),
str(self.path/self.model_dir/f"{self.fname}.pth")])
subprocess.call(["cp", self.path/f"{self.fname}.tar.gz", self.persistence_path])
@ulat
Copy link
Author

ulat commented Jun 9, 2020

Caveat: My gdrive will be mount at ./drive/My\ Drive/language_model. I had not managed to work with the path, containing a blank. Therefore I have created a symbolic link which worked fine!

subprocess.call(["ln", "-s", "./drive/My\ Drive/language_model", "model_upload"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment