Load that &%$*# Checkpoint!. Tips and Tricks for a Successful... | by Anne Bonner | Towards Data Science

Having trouble loading that checkpoint?
ME TOO.
Being a part of the Udacity Facebook AI PyTorch Challenge has been unbelievably awesome and I’ve learned so much along the way. Having completed the final challenge, I wanted to share the tips and tricks made it possible to successfully submit my checkpoint for the deep learning image classifier project to the Udacity workspace.
Most importantly, follow the directions on your lab! I’m hoping you know this, but I’m putting that here just in case...
I’m assuming you’ve saved your checkpoint if you’re at the point where you’re trying to load it in the lab, but you really need to save your checkpoint because that’s what you’ll be loading in the lab! Torch.save is awfully handy here. As an example, here’s one possible checkpoint:
checkpoint = {'input_size': 2208,              'output_size': 102,              'epochs': epochs,              'batch_size': 64,              'model': models.densenet161(pretrained=True),              'classifier': classifier,              'scheduler': sched,              'optimizer': optimizer.state_dict(),              'state_dict': model.state_dict(),              'class_to_idx': model.class_to_idx             }   torch.save(checkpoint, 'checkpoint.pth')
Just load what you need. You might not want all of the info that I loaded in this checkpoint, but I’m including the one above as an example so that you can see some options. The checkpoint save should be done before you load your checkpoint in the lab.
Make sure you know where your checkpoint is saved so that you can upload that file to the Udacity workspace!
If you’re working in Google Colab and have your Google Drive mounted to your notebook (here’s a great article you can check out if you’re just getting started with Colab and don’t know what I’m talking about), you can move your checkpoint directly into your Google Drive by running:
model_save_name = 'classifier.pth'path = F"/content/gdrive/My Drive/{model_save_name}" torch.save(model.state_dict(), path)
Just make sure you have that file path correct!
*If you decide to save your checkpoint to your Google Drive, you can actually move it from there to Udacity’s workspace by going to your Google Drive, getting the shareable link file, and finding your file ID. You only want the file ID: it will be the long string of letters and numbers after “id=”. Then go to the Udacity lab notebook and run
!pip install gdown==3.6.0
To install gdown. Then run
ckpt_file_id = "YOUR-FILE-ID-GOES-HERE"
(make sure to replace “YOUR-FILE-ID-GOES-HERE” with your actual file ID!)
and finally
!gdown https://drive.google.com/uc?id={ckpt_file_id}
This will download your file and place it in the Udacity lab.
If you aren’t using your Google Drive, when you get to the lab, you’ll want to click the button that says “Upload” and upload your checkpoint file. Make sure it goes all the way to 100% or you might wind up with a corrupt file.
Here’s the important information that you’ll need for your project submission:
You’ll want to load your parameters. I started with
ckpt = torch.load('checkpoint.pth')ckpt.keys()
so that I could take a quick look at my keys. Then I ran my imports.
from collections import OrderedDictimport torchimport torch.nn as nnfrom torchvision import modelsimport osfrom torchvision import datasets, transforms
Here’s one super important detail: if you don’t have already defined image sizes, run this:
image_size = 224# Values you used for normalizing the images. Default here are for# pretrained models from torchvision.norm_mean = [0.485, 0.456, 0.406]norm_std = [0.229, 0.224, 0.225]
After that, you can create a checkpoint load function and use that to load the model.
def load_checkpoint(filepath):    checkpoint = torch.load(filepath, map_location='cpu')    model = checkpoint['model']    model.classifier = checkpoint['classifier']    model.load_state_dict(checkpoint['state_dict'], strict=False)    model.class_to_idx = checkpoint['class_to_idx']    optimizer = checkpoint['optimizer']    epochs = checkpoint['epochs']       for param in model.parameters():        param.requires_grad = False            return model, checkpoint['class_to_idx']model, class_to_idx = load_checkpoint('/home/workspace/checkpoint.pth')
Do you see where it says
map_location='cpu'
and
strict=False
?
You want to include those!
Not kidding. Udacity’s workspace does not have GPU attached, so you want to make sure you specify CPU. You also want to use “strict=False” in order to avoid running into any unnecessary version compatibility issues.
Doublecheck the file path for your classifier. As you can see above, mine was
model, class_to_idx = load_checkpoint('/home/workspace/checkpoint.pth')
You really need to have the right path! If you look at the instructions above the test cell, there’s almost certainly a clear example of the way your file path should read. Udacity really wants you to be successful, so make sure you read the instructions carefully!
Put everything into a single cell! Put every single line of code into the one provided test cell. There were a lot of students who were finally able to successfully submit projects after making that one simple change. Just do it!
Now test that code and get your project in! This project was strictly pass/fail, so if you passed, you’re done! Congratulations!!!
Now go and celebrate!