[Feature]: Generalize Prediction pipeline for Lightning CLI models#148
Open
aditya0by0 wants to merge 8 commits intodevfrom
Open
[Feature]: Generalize Prediction pipeline for Lightning CLI models#148aditya0by0 wants to merge 8 commits intodevfrom
aditya0by0 wants to merge 8 commits intodevfrom
Conversation
This was referenced Jan 30, 2026
Member
Author
|
Could you confirm our agreed approach for handling
I’m in favor of Option 2 to avoid carrying technical debt in the prediction logic. Does this match your understanding? I'm willing to add this script to the repo, and small readme note for old checkpoints for option 2. import sys
import torch
def add_class_labels_to_checkpoint(input_path, classes_file_path):
with open(classes_file_path, "r") as f:
class_labels = [line.strip() for line in f.readlines()]
assert len(class_labels) > 0, "The classes file is empty."
# 1. Load the checkpoint
checkpoint = torch.load(
input_path, map_location=torch.device("cpu"), weights_only=False
)
if "classification_labels" in checkpoint:
print(
"Warning: 'classification_labels' key already exists in the checkpoint and will be overwritten."
)
# 2. Add your custom key/value pair
checkpoint["classification_labels"] = class_labels
# 3. Save the modified checkpoint
output_path = input_path.replace(".ckpt", "_modified.ckpt")
torch.save(checkpoint, output_path)
print(f"Successfully added classification_labels and saved to {output_path}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python modify_checkpoints.py <input_checkpoint> <classes_file>")
sys.exit(1)
input_ckpt = sys.argv[1]
classes_file = sys.argv[2]
add_class_labels_to_checkpoint(
input_path=input_ckpt, classes_file_path=classes_file
)
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generalize prediction logic
Please merge below PRs after this PR:
Related Discussion
Related bugs rectified in Lightning for the pipeline
Additional changes
Save class labels in checkpoint under the key "classification_labels"
Wrap inference with
torch.inference_mode()to avoid gradient tracking (see Avoid gradient tracking python-chebifier#21)model.eval()in PyTorchtorch.no_grad()andtorch.inference_mode()Use
torch.compilefor faster inference