Skip to content
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

Feat: Extend ptq_evaluate to allow switching between torchvision and timm datsets #946

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/brevitas_examples/imagenet_classification/ptq/ptq_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import warnings

import numpy as np
import timm
import torch
import torch.backends.cudnn as cudnn
import torch.nn.parallel
Expand Down Expand Up @@ -47,10 +48,6 @@ def parse_type(v, default_type):
return default_type(v)


model_names = sorted(
name for name in torchvision.models.__dict__ if name.islower() and not name.startswith("__") and
callable(torchvision.models.__dict__[name]) and not name.startswith("get_"))

parser = argparse.ArgumentParser(description='PyTorch ImageNet PTQ Validation')
parser.add_argument(
'--calibration-dir',
Expand All @@ -75,12 +72,16 @@ def parse_type(v, default_type):
parser.add_argument('--gpu', default=None, type=int, help='GPU id to use (default: None)')
parser.add_argument(
'--calibration-samples', default=1000, type=int, help='Calibration size (default: 1000)')
parser.add_argument(
'--dataset',
Giuseppe5 marked this conversation as resolved.
Show resolved Hide resolved
default='torchvision',
choices=['torchvision', 'timm'],
help='Source of models (default: torchvision)')
parser.add_argument(
'--model-name',
default='resnet18',
metavar='ARCH',
choices=model_names,
help='model architecture: ' + ' | '.join(model_names) + ' (default: resnet18)')
help='model architecture: (default: resnet18)')
parser.add_argument(
'--dtype', default='float', choices=['float', 'bfloat16'], help='Data type to use')
parser.add_argument(
Expand Down Expand Up @@ -351,8 +352,11 @@ def main():
center_crop_shape,
inception_preprocessing=inception_preprocessing)

# Get the model from torchvision
model = get_torchvision_model(args.model_name)
# Get the model from torchvision or timm
if args.dataset == 'torchvision':
model = get_torchvision_model(args.model_name)
else:
model = timm.create_model(args.model_name, pretrained=True)
model = model.to(dtype)

# Preprocess the model for quantization
Expand Down
2 changes: 1 addition & 1 deletion src/brevitas_examples/imagenet_classification/ptq/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def get_model_config(model_name):
config = dict()
# Set-up config parameters
if model_name == 'inception_v3' or model_name == 'googlenet':
if 'inception_v3' in model_name or 'googlenet' in model_name:
config['inception_preprocessing'] = True
else:
config['inception_preprocessing'] = False
Expand Down
Loading