-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from fabbarix/feature/save_captions
Save captions with checkpoint
- Loading branch information
Showing
4 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from captionizer import caption_from_path | ||
from pytorch_lightning.callbacks import Callback | ||
|
||
class CaptionSaverCallback(Callback): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def on_save_checkpoint(self, trainer, pl_module, checkpoint): | ||
print('Adding training captions to checkpoint [Dataloader]') | ||
data = trainer.train_dataloader.loaders.sampler.data_source # type: ignore | ||
prompts = set([ | ||
caption_from_path(image_path, data.data_root, data.coarse_class_text, data.placeholder_token) | ||
for image_path in data.image_paths | ||
]) | ||
trained_prompts = (list(prompts)) | ||
checkpoint['trained_captions'] = trained_prompts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/env python3 | ||
import torch | ||
import sys, os | ||
|
||
def main(checkpoint_path): | ||
try: | ||
checkpoint = torch.load(checkpoint_path, map_location="cpu") | ||
if 'trained_captions' in checkpoint: | ||
captions = checkpoint['trained_captions'] | ||
global_step = checkpoint['global_step'] | ||
print(f'Captions in {os.path.basename(checkpoint_path)} [{global_step} Global Steps]:') | ||
for caption in captions: | ||
print(f'\t"{caption}"') | ||
else: | ||
print(f'{checkpoint_path} has no captions saved') | ||
except: | ||
print(f'Failed to extract captions from {checkpoint_path}') | ||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) == 1: | ||
print(f'{sys.argv[0]} <checkpoint>') | ||
main(sys.argv[1]) | ||
|
a1781aa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new captions throws an error after training the first 500 steps. I have used vast.ai and it worked yesterday before this update