本文整理汇总了Python中chainer.reporter.report_scope方法的典型用法代码示例。如果您正苦于以下问题:Python reporter.report_scope方法的具体用法?Python reporter.report_scope怎么用?Python reporter.report_scope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.reporter
的用法示例。
在下文中一共展示了reporter.report_scope方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
iterator = self.get_iterator('main')
all_targets = self.get_all_targets()
for model in all_targets.values():
if hasattr(model, 'train'):
model.train = False
if self.eval_hook:
self.eval_hook(self)
it = copy.copy(iterator)
summary = reporter_module.DictSummary()
for batch in it:
observation = {}
with reporter_module.report_scope(observation):
self.updater.forward(batch)
self.updater.calc_loss()
summary.add(observation)
for model in all_targets.values():
if hasattr(model, 'train'):
model.train = True
return summary.compute_mean()
示例2: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
val_iter = self.get_iterator("main")
target = self.get_target("main")
loss = 0
count = 0
for batch in copy.copy(val_iter):
x, t = convert.concat_examples(batch, device=self.device, padding=(0, -1))
xp = chainer.backends.cuda.get_array_module(x)
state = None
for i in six.moves.range(len(x[0])):
state, loss_batch = target(state, x[:, i], t[:, i])
non_zeros = xp.count_nonzero(x[:, i])
loss += loss_batch.data * non_zeros
count += int(non_zeros)
# report validation loss
observation = {}
with reporter.report_scope(observation):
reporter.report({"loss": float(loss / count)}, target)
return observation
示例3: test_report_key
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def test_report_key(self, metrics_fun, compute_metrics):
repo = chainer.Reporter()
link = Regressor(predictor=DummyPredictor(),
metrics_fun=metrics_fun)
link.compute_metrics = compute_metrics
repo.add_observer('target', link)
with repo:
observation = {}
with reporter.report_scope(observation):
link(self.x, self.t)
# print('observation ', observation)
actual_keys = set(observation.keys())
if compute_metrics:
if metrics_fun is None:
assert set(['target/loss']) == actual_keys
elif isinstance(metrics_fun, dict):
assert set(['target/loss', 'target/user_key']) == actual_keys
elif callable(metrics_fun):
assert set(['target/loss', 'target/metrics']) == actual_keys
else:
raise TypeError()
else:
assert set(['target/loss']) == actual_keys
示例4: test_report_key
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def test_report_key(self, metrics_fun, compute_metrics):
repo = chainer.Reporter()
link = Classifier(predictor=DummyPredictor(),
metrics_fun=metrics_fun)
link.compute_metrics = compute_metrics
repo.add_observer('target', link)
with repo:
observation = {}
with reporter.report_scope(observation):
link(self.x, self.t)
# print('observation ', observation)
actual_keys = set(observation.keys())
if compute_metrics:
if metrics_fun is None:
assert set(['target/loss']) == actual_keys
elif isinstance(metrics_fun, dict):
assert set(['target/loss', 'target/user_key']) == actual_keys
elif callable(metrics_fun):
assert set(['target/loss', 'target/accuracy']) == actual_keys
else:
raise TypeError()
else:
assert set(['target/loss']) == actual_keys
示例5: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
iterator = self._iterators['main']
eval_func = self.eval_func or self._targets['main']
if self.eval_hook:
self.eval_hook(self)
if hasattr(iterator, 'reset'):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
# summary = reporter_module.DictSummary()
summary = collections.defaultdict(list)
for batch in it:
observation = {}
with reporter_module.report_scope(observation):
in_arrays = self.converter(batch, self.device)
with function.no_backprop_mode():
if isinstance(in_arrays, tuple):
eval_func(*in_arrays)
elif isinstance(in_arrays, dict):
eval_func(**in_arrays)
else:
eval_func(in_arrays)
n_data = len(batch)
summary['n'].append(n_data)
# summary.add(observation)
for k, v in observation.items():
summary[k].append(v)
mean = dict()
ns = summary['n']
del summary['n']
for k, vs in summary.items():
mean[k] = sum(v * n for v, n in zip(vs, ns)) / sum(ns)
return mean
# return summary.compute_mean()
示例6: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
bt = time.time()
with chainer.no_backprop_mode():
references = []
hypotheses = []
observation = {}
with reporter.report_scope(observation):
for i in range(0, len(self.test_data), self.batch):
src, trg = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in trg])
src = [chainer.dataset.to_device(self.device, x)
for x in src]
ys = [y.tolist()
for y in self.model.translate(src, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1)
reporter.report({'bleu': bleu}, self.model)
et = time.time()
if self.comm is not None:
# This evaluator is called via chainermn.MultiNodeEvaluator
for i in range(0, self.comm.size):
print('BleuEvaluator::evaluate(): '
'took {:.3f} [s]'.format(et - bt))
sys.stdout.flush()
self.comm.mpi_comm.Barrier()
else:
# This evaluator is called from a conventional
# Chainer exntension
print('BleuEvaluator(single)::evaluate(): '
'took {:.3f} [s]'.format(et - bt))
sys.stdout.flush()
return observation
示例7: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
bt = time.time()
with chainer.no_backprop_mode():
references = []
hypotheses = []
observation = {}
with reporter.report_scope(observation):
for i in range(0, len(self.test_data), self.batch):
src, trg = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in trg])
src = [chainer.dataset.to_device(self.device, x)
for x in src]
if self.comm.rank == 0:
self.model.translate(src, self.max_length)
elif self.comm.rank == 1:
ys = [y.tolist()
for y in self.model.translate(
src, self.max_length)]
hypotheses.extend(ys)
if self.comm.rank == 1:
bleu = bleu_score.corpus_bleu(
references, hypotheses, smoothing_function=bleu_score.
SmoothingFunction().method1)
reporter.report({'bleu': bleu}, self.model)
et = time.time()
if self.comm.rank == 1:
print('BleuEvaluator(single)::evaluate(): '
'took {:.3f} [s]'.format(et - bt))
sys.stdout.flush()
return observation
示例8: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
iterator = self._iterators['main']
target = self._targets['main']
eval_func = self.eval_func or target
if self.eval_hook:
self.eval_hook(self)
it = copy.copy(iterator)
summary = reporter_module.DictSummary()
for _ in range(min(len(iterator.dataset) // iterator.batch_size, self.num_iterations)):
batch = next(it, None)
if batch is None:
break
observation = {}
with reporter_module.report_scope(observation), chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
in_arrays = self.converter(batch, self.device)
if isinstance(in_arrays, tuple):
eval_func(*in_arrays)
elif isinstance(in_arrays, dict):
eval_func(**in_arrays)
else:
eval_func(in_arrays)
summary.add(observation)
return summary.compute_mean()
示例9: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
iterator = self._iterators['main']
target = self._targets['main']
if hasattr(iterator, 'reset'):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
in_values, out_values, rest_values = apply_to_iterator(
target.predict, it)
# delete unused iterators explicitly
del in_values
points, labels, scores = out_values
gt_points, gt_labels = rest_values
result = eval_projected_3d_bbox_single(
points, scores, gt_points,
self.vertex, self.intrinsics, diam=self.diam)
report = result
observation = {}
with reporter.report_scope(observation):
reporter.report(report, target)
return observation
示例10: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
iterator = self._iterators['main']
target = self._targets['main']
if hasattr(iterator, 'reset'):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
in_values, out_values, rest_values = apply_to_iterator(
self.predict_func, it)
# delete unused iterators explicitly
del in_values
pred_labels, pred_scores = out_values
gt_labels, = rest_values
result = eval_multi_label_classification(
pred_labels, pred_scores, gt_labels)
report = {'map': result['map']}
if self.label_names is not None:
for l, label_name in enumerate(self.label_names):
try:
report['ap/{:s}'.format(label_name)] = result['ap'][l]
except IndexError:
report['ap/{:s}'.format(label_name)] = np.nan
observation = {}
with reporter.report_scope(observation):
reporter.report(report, target)
return observation
示例11: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
summary = reporter.DictSummary()
eval_func = self.eval_func or self._targets['main']
observation = {}
with reporter.report_scope(observation):
# we always use the same array for testing, since this is only an example ;)
data = eval_func.net.xp.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], dtype='int32')
eval_func(data=data, label=data)
summary.add(observation)
return summary.compute_mean()
示例12: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
target = self._targets['main']
if self.comm is not None and self.comm.rank != 0:
apply_to_iterator(target.predict, None, comm=self.comm)
return {}
iterator = self._iterators['main']
if hasattr(iterator, 'reset'):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
in_values, out_values, rest_values = apply_to_iterator(
target.predict, it, comm=self.comm)
# delete unused iterators explicitly
del in_values
pred_labels, = out_values
gt_labels, = rest_values
result = eval_semantic_segmentation(pred_labels, gt_labels)
report = {'miou': result['miou'],
'pixel_accuracy': result['pixel_accuracy'],
'mean_class_accuracy': result['mean_class_accuracy']}
if self.label_names is not None:
for l, label_name in enumerate(self.label_names):
try:
report['iou/{:s}'.format(label_name)] = result['iou'][l]
report['class_accuracy/{:s}'.format(label_name)] =\
result['class_accuracy'][l]
except IndexError:
report['iou/{:s}'.format(label_name)] = np.nan
report['class_accuracy/{:s}'.format(label_name)] = np.nan
observation = {}
with reporter.report_scope(observation):
reporter.report(report, target)
return observation
示例13: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
target = self._targets['main']
if self.comm is not None and self.comm.rank != 0:
apply_to_iterator(target.predict, None, comm=self.comm)
return {}
iterator = self._iterators['main']
if hasattr(iterator, 'reset'):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
in_values, out_values, rest_values = apply_to_iterator(
target.predict, it, comm=self.comm)
# delete unused iterators explicitly
del in_values
pred_masks, pred_labels, pred_scores = out_values
gt_masks, gt_labels = rest_values
result = eval_instance_segmentation_voc(
pred_masks, pred_labels, pred_scores,
gt_masks, gt_labels,
iou_thresh=self.iou_thresh,
use_07_metric=self.use_07_metric)
report = {'map': result['map']}
if self.label_names is not None:
for l, label_name in enumerate(self.label_names):
try:
report['ap/{:s}'.format(label_name)] = result['ap'][l]
except IndexError:
report['ap/{:s}'.format(label_name)] = np.nan
observation = {}
with reporter.report_scope(observation):
reporter.report(report, target)
return observation
示例14: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
"""Main evaluate routine for CustomEvaluator."""
iterator = self._iterators["main"]
if self.eval_hook:
self.eval_hook(self)
if hasattr(iterator, "reset"):
iterator.reset()
it = iterator
else:
it = copy.copy(iterator)
summary = reporter_module.DictSummary()
self.model.eval()
with torch.no_grad():
for batch in it:
x = _recursive_to(batch, self.device)
observation = {}
with reporter_module.report_scope(observation):
# read scp files
# x: original json with loaded features
# will be converted to chainer variable later
if self.ngpu == 0:
self.model(*x)
else:
# apex does not support torch.nn.DataParallel
data_parallel(self.model, x, range(self.ngpu))
summary.add(observation)
self.model.train()
return summary.compute_mean()
示例15: evaluate
# 需要导入模块: from chainer import reporter [as 别名]
# 或者: from chainer.reporter import report_scope [as 别名]
def evaluate(self):
"""Evaluate the model."""
val_iter = self.get_iterator("main")
loss = 0
nll = 0
count = 0
self.model.eval()
with torch.no_grad():
for batch in copy.copy(val_iter):
x, t = concat_examples(batch, device=self.device[0], padding=(0, -100))
if self.device[0] == -1:
l, n, c = self.model(x, t)
else:
# apex does not support torch.nn.DataParallel
l, n, c = data_parallel(self.model, (x, t), self.device)
loss += float(l.sum())
nll += float(n.sum())
count += int(c.sum())
self.model.train()
# report validation loss
observation = {}
with reporter.report_scope(observation):
reporter.report({"loss": loss}, self.model.reporter)
reporter.report({"nll": nll}, self.model.reporter)
reporter.report({"count": count}, self.model.reporter)
return observation