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

Added option to optionally compute channel statistics #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/rastervision/common/data/generators.py
Expand Up @@ -81,18 +81,23 @@ def __init__(self, options):

self.train_probs = self.compute_train_probs()

# If a dataset's normalized parameters have already been
# calculated, load its json file. Otherwise, calculate parameters
# with a small batch.
channel_stats_path = join(self.dataset_path,
self.name + '_channel_stats.json')
if exists(channel_stats_path):
with open(channel_stats_path) as channel_stats_file:
channel_stats = json.load(channel_stats_file)
self.channel_stats = (np.array(channel_stats['means']),
np.array(channel_stats['stds']))
# We may be using this generator for tasks that don't require
# channel stats; if so, avoid loading or computing them
if options.requires_channel_stats:
# If a dataset's normalized parameters have already been
# calculated, load its json file. Otherwise, calculate parameters
# with a small batch.
channel_stats_path = join(self.dataset_path,
self.name + '_channel_stats.json')
if exists(channel_stats_path):
with open(channel_stats_path) as channel_stats_file:
channel_stats = json.load(channel_stats_file)
self.channel_stats = (np.array(channel_stats['means']),
np.array(channel_stats['stds']))
else:
self.channel_stats = self.compute_channel_stats(100, False)
else:
self.channel_stats = self.compute_channel_stats(100, False)
self.channel_stats = None

def compute_train_probs(self):
return None
Expand Down Expand Up @@ -215,12 +220,16 @@ def make_gen():
yield batch, batch_file_inds

def normalize(self, batch_x):
if not self.channel_stats:
raise Exception("channel_stats is undefined; this task requires channel_stats")
means, stds = self.channel_stats
batch_x = batch_x - means[np.newaxis, np.newaxis, np.newaxis, :]
batch_x = batch_x / stds[np.newaxis, np.newaxis, np.newaxis, :]
return batch_x

def unnormalize(self, batch_x):
if not self.channel_stats:
raise Exception("channel_stats is undefined; this task requires channel_stats")
means, stds = self.channel_stats
nb_dims = len(batch_x.shape)
if nb_dims == 3:
Expand Down
4 changes: 4 additions & 0 deletions src/rastervision/common/options.py
Expand Up @@ -58,3 +58,7 @@ def __init__(self, options):
raise ValueError(
'{} are not valid augment_methods'.format(
str(invalid_augment_methods)))

# Option for determining whether or not to compute or load channel stats;
# if channel stats for normalization of images are not needed, can set this to false.
self.requires_channel_stats = options.get('requires_channel_stats', True)