Skip to content

Commit

Permalink
Revert "fixed image_size and training loss, accuracy for inception mo…
Browse files Browse the repository at this point in the history
…dels"

This reverts commit fbbd351.
  • Loading branch information
soumendukrg committed Nov 14, 2019
1 parent fbbd351 commit ed895e6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 723 deletions.
29 changes: 12 additions & 17 deletions distiller/apputils/data_loaders.py
Expand Up @@ -58,20 +58,20 @@ def classification_get_input_shape(dataset):
raise ValueError("dataset %s is not supported" % dataset)


def __dataset_factory(dataset, is_inception):
def __dataset_factory(dataset):
return {'cifar10': cifar10_get_datasets,
'mnist': mnist_get_datasets,
'imagenet': imagenet_get_datasets}.get(dataset, None)


def load_data(dataset, data_dir, batch_size, workers, validation_split=0.1, deterministic=False,
effective_train_size=1., effective_valid_size=1., effective_test_size=1.,
fixed_subset=False, sequential=False, is_inception=False):
fixed_subset=False, sequential=False):
"""Load a dataset.
Args:
dataset: a string with the name of the dataset to load (cifar10/imagenet)
data_dir: the directory where the datset resides
data_dir: the directory where the dataset resides
batch_size: the batch size
workers: the number of worker threads to use for loading the data
validation_split: portion of training dataset to set aside for validation
Expand All @@ -86,8 +86,8 @@ def load_data(dataset, data_dir, batch_size, workers, validation_split=0.1, dete
"""
if dataset not in DATASETS_NAMES:
raise ValueError('load_data does not support dataset %s" % dataset')
datasets_fn = __dataset_factory(dataset, is_inception)
return get_data_loaders(datasets_fn, data_dir, batch_size, workers, is_inception,
datasets_fn = __dataset_factory(dataset)
return get_data_loaders(datasets_fn, data_dir, batch_size, workers,
validation_split=validation_split,
deterministic=deterministic,
effective_train_size=effective_train_size,
Expand Down Expand Up @@ -155,23 +155,17 @@ def cifar10_get_datasets(data_dir):
return train_dataset, test_dataset



def imagenet_get_datasets(data_dir, is_inception):
def imagenet_get_datasets(data_dir):
"""
Load the ImageNet dataset.
"""
# Inception Network accepts image of size 3, 299, 299
if is_inception == True:
resize, crop = 336, 299
else:
resize, crop = 256, 224
train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'val')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])

train_transform = transforms.Compose([
transforms.RandomResizedCrop(crop),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
Expand All @@ -180,8 +174,8 @@ def imagenet_get_datasets(data_dir, is_inception):
train_dataset = datasets.ImageFolder(train_dir, train_transform)

test_transform = transforms.Compose([
transforms.Resize(resize),
transforms.CenterCrop(crop),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])
Expand All @@ -190,6 +184,7 @@ def imagenet_get_datasets(data_dir, is_inception):

return train_dataset, test_dataset


def __image_size(dataset):
# un-squeeze is used here to add the batch dimension (value=1), which is missing
return dataset[0][0].unsqueeze(0).size()
Expand Down Expand Up @@ -266,10 +261,10 @@ def _get_sampler(data_source, effective_size, fixed_subset=False, sequential=Fal
return SwitchingSubsetRandomSampler(data_source, effective_size)


def get_data_loaders(datasets_fn, data_dir, batch_size, num_workers, is_inception, validation_split=0.1, deterministic=False,
def get_data_loaders(datasets_fn, data_dir, batch_size, num_workers, validation_split=0.1, deterministic=False,
effective_train_size=1., effective_valid_size=1., effective_test_size=1., fixed_subset=False,
sequential=False):
train_dataset, test_dataset = datasets_fn(data_dir, is_inception) # load data according to model type
train_dataset, test_dataset = datasets_fn(data_dir)

worker_init_fn = None
if deterministic:
Expand Down
19 changes: 3 additions & 16 deletions distiller/apputils/image_classifier.py
Expand Up @@ -470,16 +470,11 @@ def save_collectors_data(collectors, directory):


def load_data(args, fixed_subset=False, sequential=False, load_train=True, load_val=True, load_test=True):
# create flag to indicate if model is inception to use proper image size
if args.arch in ['inception_v3, inceptionv3, inceptionv4', 'inceptionresnetv2']:
is_inception = True
else:
is_inception = False
train_loader, val_loader, test_loader, _ = apputils.load_data(args.dataset,
os.path.expanduser(args.data), args.batch_size,
args.workers, args.validation_split, args.deterministic,
args.effective_train_size, args.effective_valid_size, args.effective_test_size,
fixed_subset, sequential, is_inception)
fixed_subset, sequential)
msglogger.info('Dataset sizes:\n\ttraining=%d\n\tvalidation=%d\n\ttest=%d',
len(train_loader.sampler), len(val_loader.sampler), len(test_loader.sampler))

Expand Down Expand Up @@ -580,17 +575,9 @@ def _log_training_progress():
output = args.kd_policy.forward(inputs)

if not early_exit_mode(args):
# For inception, we need to add losses for both classifier outputs
if isinstance(output, tuple):
loss = sum((criterion(o,target) for o in output))
else:
loss = criterion(output, target)
loss = criterion(output, target)
# Measure accuracy
# For inception, we only consider accuracy of main classifier
if isinstance(output, tuple):
classerr.add(output[0].detach(), target)
else:
classerr.add(output.detach(), target)
classerr.add(output.detach(), target)
acc_stats.append([classerr.value(1), classerr.value(5)])
else:
# Measure accuracy and record loss
Expand Down

0 comments on commit ed895e6

Please sign in to comment.