Saver

class tinder.saver.Saver(weight_dir, exp_name)[source]

A helper class to save and load your model.

Example:

saver = Saver('/data/weights/', 'resnet152-cosine')
saver.load_latest(alexnet, opt)  # resume from the latest
for epoch in range(100):
    ..
    saver.save(alexnet, opt, epoch=epoch, score=acc)

# inference
saver.load_best(alexnet, opt=None) # no need for optimizer

The batch dimension is implicit. The above code is the same as tensor.view(tensor.size(0), 3, -1, 256).

Parameters
  • weight_dir (str) – directory for your weights

  • exp_name (str) – name of your experiment (e.g. resnet152-cosine)

load(model_dict: dict, epoch: int) → bool[source]

Load the model.

It is recommended to use load_latest or load_best instead.

Parameters
  • model_dict (dict) – see save()

  • epoch (int) – epoch to load

load_best(dic: dict) → bool[source]

Load the best model.

Parameters

dic (dict) – see save()

Returns

SimpleNamespace

load_latest(dic: dict) → bool[source]

Load the latest model.

Parameters

dic (dict) – see save()

Returns

the epoch of the loaded model. -1 if no model exists.

Return type

int

save(dic: dict, epoch: int, score: float = None)[source]

Save the model.

score is used to choose the best model. An example for score is validation accuracy.

Example:

model = {
    'net':net,
    'opt':opt,
    'scheduler':cosine_annealing,
    'lr': 0.01
}

saver = Saver()
saver.save(model, epoch=3, score=val_acc)
saver.save(model, epoch=4, score=val_acc)
meta = saver.load_latest(model)
print(meta.lr)
print(meta.epoch)
Parameters
  • dic (dict) – the values are objects with state_dict and load_state_dict

  • epoch (int) – number of epochs completed

  • score (float, optional) – Defaults to None