Refactor CropModel to infer num_classes from checkpoint during loading #966
+99
−31
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.
Fixes #960
This PR makes
num_classes
optional in theCropModel
class and infers it from the checkpoint when loading viaload_from_checkpoint
. This improves usability, especially for users who do not know or remember the original number of classes at the time of inference.Changes Made
num_classes
an optional argument in the CropModel constructor.num_classes
is not provided (None), we defer model/metrics initialization until on_load_checkpoint. Otherwise, we initialize them immediately.on_load_checkpoint
method that readsnum_classes
from the checkpoint if it is stillNone
, then sets up the model and metrics.num_classes
.num_classes
explicitly if they want.forward
without having a valid model raises a clear error.