Fix: Prevent NaN errors and optimize tensor loading in LensingDataset#142
Open
kamilansri wants to merge 1 commit intoML4SCI:mainfrom
Open
Fix: Prevent NaN errors and optimize tensor loading in LensingDataset#142kamilansri wants to merge 1 commit intoML4SCI:mainfrom
kamilansri wants to merge 1 commit intoML4SCI:mainfrom
Conversation
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.
This PR addresses a critical mathematical bug in the
LensingDatasetnormalization logic and optimizes how image arrays are loaded into PyTorch tensors. It also fixes fragile file path handling to preventFileNotFoundErrors across different operating systems.Changes Made
1e-8) to the denominator during min-max normalization. This prevents the tensor from filling withNaNs if an image happens to have uniform pixel values (wheremax == min).self.directory + selected_class + '/sim_%d.npy') withos.path.join()for safe, cross-platform path resolution.torch.tensor(np.array([np.load(...)])fortorch.from_numpy(np.load(...)).unsqueeze(0). This avoids allocating an intermediate NumPy array and shares memory directly, speeding up the__getitem__pipeline.%dto f-strings.Context
During training, a single blank or uniformly colored image in the dataset would cause the min-max normalizer to divide by zero, instantly ruining model gradients with
NaNvalues. Furthermore, the dataset loader was previously creating unnecessary memory copies of every image, which creates a bottleneck during data loading. This refactor makes the dataloader safer, faster, and more robust.