new scripts

This commit is contained in:
2023-07-06 16:35:36 +02:00
parent 639af22b48
commit 2d50eb40da
4 changed files with 264 additions and 2 deletions
+6 -2
View File
@@ -33,12 +33,16 @@ class CheckpointSaver:
def load_model(model: torch.nn.Module, checkpoint, optimizer: torch.nn.Module=None, scaler: torch.nn.Module=None, iteration=''):
# list of checkpoint files in the folder checkpoints
checkpoints = [os.path.join(checkpoint, f) for f in os.listdir(checkpoint) if f.endswith(f'{iteration}.pt')]
if iteration == '':
checkpoints = [os.path.join(checkpoint, f) for f in os.listdir(checkpoint) if f.endswith('.pt')]
else:
checkpoints = [os.path.join(checkpoint, f) for f in os.listdir(checkpoint) if f.endswith(f'iteration_{iteration}.pt')]
assert len(checkpoints) == 1
iterations = np.asarray([int(f.split('_')[-1].split('.')[0]) for f in checkpoints])
last_iteration = np.argmax(iterations)
checkpoint = torch.load(checkpoints[last_iteration])
iteration = iterations[last_iteration]
model.load_state_dict(checkpoint['model'])
model.load_state_dict(checkpoint['model'], strict=False)
if optimizer is not None: optimizer.load_state_dict(checkpoint['optimizer'].state_dict())
if scaler is not None and checkpoint['scaler'] is not None: scaler.load_state_dict(checkpoint['scaler'])
print(f'Loaded model from {checkpoints[last_iteration]} at iteration {iteration}')