-
Notifications
You must be signed in to change notification settings - Fork 751
NXP backend: Add MobileNetV2 example model and test #12892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
robert-kalmar
merged 3 commits into
pytorch:main
from
nxp-upstream:upstream/main-nxp/EIEX-396-upstream-toy-models-integration
Aug 26, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright 2025 NXP | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import itertools | ||
| from typing import Iterator | ||
|
|
||
| import torch | ||
| import torchvision | ||
|
|
||
| from executorch.examples.models.mobilenet_v2 import MV2Model | ||
| from torch.utils.data import DataLoader | ||
| from torchvision import transforms | ||
|
|
||
|
|
||
| class MobilenetV2(MV2Model): | ||
|
|
||
| def get_calibration_inputs( | ||
| self, batch_size: int = 1 | ||
| ) -> Iterator[tuple[torch.Tensor]]: | ||
| """ | ||
| Returns an iterator for the Imagenette validation dataset, downloading it if necessary. | ||
|
|
||
| Args: | ||
| batch_size (int): The batch size for the iterator. | ||
|
|
||
| Returns: | ||
| iterator: An iterator that yields batches of images from the Imagnetette validation dataset. | ||
| """ | ||
| dataloader = self.get_dataset(batch_size) | ||
|
|
||
| # Return the iterator | ||
| dataloader_iterable = itertools.starmap( | ||
| lambda data, label: (data,), iter(dataloader) | ||
| ) | ||
|
|
||
| # We want approximately 500 samples | ||
| batch_count = 500 // batch_size | ||
| return itertools.islice(dataloader_iterable, batch_count) | ||
|
|
||
| def get_dataset(self, batch_size): | ||
| # Define data transformations | ||
| data_transforms = transforms.Compose( | ||
| [ | ||
| transforms.Resize((224, 224)), | ||
| transforms.ToTensor(), | ||
| transforms.Normalize( | ||
| mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] | ||
| ), # ImageNet stats | ||
| ] | ||
| ) | ||
|
|
||
| dataset = torchvision.datasets.Imagenette( | ||
| root="./data", split="val", transform=data_transforms, download=True | ||
| ) | ||
| dataloader = torch.utils.data.DataLoader( | ||
| dataset, | ||
| batch_size=batch_size, | ||
| shuffle=False, | ||
| num_workers=1, | ||
| ) | ||
| return dataloader | ||
|
|
||
|
|
||
| def gather_samples_per_class_from_dataloader( | ||
| dataloader, num_samples_per_class=10 | ||
| ) -> list[tuple]: | ||
| """ | ||
| Gathers a specified number of samples for each class from a DataLoader. | ||
|
|
||
| Args: | ||
| dataloader (DataLoader): The PyTorch DataLoader object. | ||
| num_samples_per_class (int): The number of samples to gather for each class. Defaults to 10. | ||
|
|
||
| Returns: | ||
| samples: A list of (sample, label) tuples. | ||
| """ | ||
|
|
||
| if not isinstance(dataloader, DataLoader): | ||
| raise TypeError("dataloader must be a torch.utils.data.DataLoader object") | ||
| if not isinstance(num_samples_per_class, int) or num_samples_per_class <= 0: | ||
| raise ValueError("num_samples_per_class must be a positive integer") | ||
|
|
||
| labels = sorted( | ||
| set([label for _, label in dataloader.dataset]) | ||
| ) # Get unique labels from the dataset | ||
| samples_per_label = {label: [] for label in labels} # Initialize dictionary | ||
|
|
||
| for sample, label in dataloader: | ||
| label = label.item() | ||
| if len(samples_per_label[label]) < num_samples_per_class: | ||
| samples_per_label[label].append((sample, label)) | ||
|
|
||
| samples = [] | ||
|
|
||
| for label in labels: | ||
| samples.extend(samples_per_label[label]) | ||
|
|
||
| return samples | ||
|
|
||
|
|
||
| def generate_input_samples_file(): | ||
| model = MobilenetV2() | ||
| dataloader = model.get_dataset(batch_size=1) | ||
| samples = gather_samples_per_class_from_dataloader( | ||
| dataloader, num_samples_per_class=2 | ||
| ) | ||
|
|
||
| torch.save(samples, "calibration_data.pt") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| generate_input_samples_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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.