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

Fix OFA valid/test #50

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Changes from all commits
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
63 changes: 27 additions & 36 deletions nnabla_nas/runner/searcher/ofa.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,7 @@ def run(self):

# Test for init parameters
if self.hparams['task'] != 'fullnet':
OFAResize.IS_TRAINING = False
for genotype in self.hparams['valid_genotypes']:
for img_size in self.hparams['valid_image_size_list']:
self.monitor.reset()
OFAResize.ACTIVE_SIZE = img_size
self.model.set_valid_arch(genotype)
self.reset_running_statistics()
for i in tqdm(range(self.one_epoch_test), desc='Test for init parameters'):
self.update_graph('test')
self.valid_on_batch(is_test=True)
clear_memory_cache()
self.monitor.info(f'img_size={img_size}, genotype={genotype} \n')
self.callback_on_epoch_end(is_test=True)

self.loss.zero()
for k in self.metrics:
self.metrics[k].zero()
self.valid_genotypes(mode='test')

# training
for self.cur_epoch in range(self.cur_epoch, self.hparams['epoch']):
Expand All @@ -106,25 +90,8 @@ def run(self):
self.monitor.display(i, key=train_keys)
clear_memory_cache()
if self.cur_epoch % self.hparams["validation_frequency"] == 0:
OFAResize.IS_TRAINING = False
for genotype in self.hparams['valid_genotypes']:
for img_size in self.hparams['valid_image_size_list']:
self.monitor.reset()
OFAResize.ACTIVE_SIZE = img_size
self.model.set_valid_arch(genotype)
self.reset_running_statistics()
for i in tqdm(range(self.one_epoch_valid),
desc=f'Valid [{self.cur_epoch}/{self.hparams["epoch"]}]'):
self.update_graph('valid')
self.valid_on_batch(is_test=False)
clear_memory_cache()
self.monitor.info(f'img_size={img_size}, genotype={genotype} \n')
self.callback_on_epoch_end(is_test=False)
self.monitor.write(self.cur_epoch)

self.loss.zero()
for k in self.metrics:
self.metrics[k].zero()
self.valid_genotypes(mode='valid')

return self

def callback_on_start(self):
Expand Down Expand Up @@ -205,6 +172,30 @@ def valid_on_batch(self, is_test=False):
[self.loss] + list(self.metrics.values()), division=True, inplace=False)
self.event.add_default_stream_event()

def valid_genotypes(self, mode='valid'):
assert mode in ['valid', 'test']
is_test = True if mode == 'test' else False

OFAResize.IS_TRAINING = False
for genotype in self.hparams['valid_genotypes']:
for img_size in self.hparams['valid_image_size_list']:
self.monitor.reset()
OFAResize.ACTIVE_SIZE = img_size
self.model.set_valid_arch(genotype)
self.reset_running_statistics()
for _ in tqdm(range(self.one_epoch_valid if mode == 'valid' else self.one_epoch_test),
desc=f'{mode} [{self.cur_epoch}/{self.hparams["epoch"]}]'):
self.update_graph(mode)
self.valid_on_batch(is_test=is_test)
clear_memory_cache()
self.monitor.info(f'img_size={img_size}, genotype={genotype} \n')
self.callback_on_epoch_end(is_test=is_test)
self.monitor.write(self.cur_epoch)

self.loss.zero()
for k in self.metrics:
self.metrics[k].zero()

def callback_on_epoch_end(self, epoch=None, is_test=False, info=None):
if is_test:
num_of_samples = self.one_epoch_test * self.accum_test * self.mbs_test
Expand Down