def progress_formatter(estimator): progress = estimator.get_value('imgs_seen') / self.num_examples if self.is_training: return 'epoch: {:.2f}'.format(progress) if progress > 1: progress = 1 return '{}: {}'.format(self.mode, Percent(progress))
def finalize_info(cls, table): densities = table.get_column('density') count = table.get_column('count_') avg_density = sum(d * c for d, c in zip(densities, count)) / sum(count) footer = [None, ' overall: ', Percent(avg_density), None] table.set_footer(footer) return footer
def eval(self, net, prediction, truth): """ detections: the prediciton of object class annotations: positions of the objects """ # forced dummy test # fakes = [] # for item in mean_aps_inputs: # fakes.append(np.random.randint(10, size=item.shape)) # dummy = self._mean_avg_precisions(*fakes) inputs = [ prediction['test']['box'], prediction['test']['class'], prediction['test']['score'], prediction['test']['count'], truth['rawbox'], truth['rawclass'], truth['count'], ] tensor = tf.py_func(self._mean_avg_precisions, inputs, tf.float32) tensor = tf.reduce_sum(tensor) tensor /= tf.cast(tf.size(tensor), tf.float32) formatter = lambda e: 'mAP accuracy: {}'.format( Percent(e.get_mean('accuracy'))) self.estimator.register(tensor, 'accuracy', formatter=formatter)
def formatter(estimator, name): history = self._formatted_history.setdefault(name, []) value = estimator.get_value(name, 'eval') value = np.sum(value, axis=-1) history.append(sum(value)) accuracy = Percent( sum(history) / (self.session.batch_size * len(history))) return '{}: {}'.format(name, accuracy)
def _train_setup(self, prediction, truth): # formatters accuracy_formatter = lambda e: \ 'accuracy: {}'.format(Percent(e.get_mean('accuracy', 'train'))) # register progress update statistics accuracy = self._accuracy(prediction, truth) self.estimator.register( accuracy, 'accuracy', 'train', formatter=accuracy_formatter)
def _info(self): # FIXME it doesn't make sense to run `gate` once as its density # varies from run to run. gate = util.cast(self.session.run(self.gate), int) density = Percent(util.sum(gate) / util.count(gate)) return self._info_tuple(gate=self.gate.name, density=density, count_=gate.size)
def mask_density(mask): if not mask: return 1, 1 # mask valids = sum(np.sum(m.astype(np.int32)) for m in mask) totals = sum(m.size for m in mask) density = Percent(valids / totals) # active for mm in mask: if mm.ndim == 1: # channel pruning, static mask active = mm break else: flat_masks = (m for mm in mask for m in mm) active = functools.reduce(np.logical_or, flat_masks) active = Percent(np.sum(active) / active.size) return density, active
def _gate_density_formatter(estimator): gates = estimator.get_values('gate.active') if not gates: return 'gate: off' valid = total = 0 for layer, gate in gates.items(): valid += np.sum(gate.astype(np.float32) != 0) total += gate.size return 'gate: {}'.format(Percent(valid / total))
def post_eval(self): stats = {} num_examples = self.session.num_examples num_remaining = num_examples % self.session.batch_size for key in ('top1', 'top5'): history = self.estimator.get_history(key, 'eval') history[-1] = history[-1][:num_remaining] valids = total = 0 for h in history: valids += np.sum(h) total += len(h) stats[key] = Percent(valids / total) self.estimator.flush(key, 'eval') self._formatted_history = {} log.info( ' top1: {}, top5: {} [{} images]' .format(stats['top1'], stats['top5'], num_examples)) return stats
def _gate_loss_formatter(estimator): # gating loss for printing losses = estimator.get_histories('gate.loss') total_losses = None for loss_history in losses.values(): if total_losses is None: total_losses = list(loss_history) else: total_losses = [ a + b for a, b in zip(total_losses, loss_history) ] if total_losses is None: loss_mean = 0 else: loss_mean = np.mean(total_losses) if loss_mean > 0: loss_std = Percent(np.std(total_losses) / loss_mean) else: loss_std = '?%' if math.isnan(loss_mean): log.error( 'Gating loss is NaN. Please check your regularizer weight.') return 'gate.loss: {:.5f}±{}'.format(loss_mean, loss_std)
def _mask_density(mask): if not mask: return 1 valids = sum(np.sum(m.astype(np.int32)) for m in mask) totals = sum(m.size for m in mask) return Percent(valids / totals)
def formatter(estimator): loss_mean, loss_std = estimator.get_mean_std(key) if math.isnan(loss_mean): raise ValueError('Model diverged with a nan-valued loss.') loss_std = '±{}'.format(Percent(loss_std / loss_mean)) return '{}: {:10f}{:5}'.format(name, loss_mean, loss_std)
def _info(self): mask = util.cast(self.session.run(self.mask), int) density = Percent(util.sum(mask) / util.count(mask)) return self._info_tuple( mask=self.mask.name, density=density, count_=mask.size)