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: warnings.warn( 'This iterator does not have the reset method. Evaluator ' 'copies the iterator instead of resetting. This behavior is ' 'deprecated. Please implement the reset method.', DeprecationWarning) it = copy.copy(iterator) summary = reporter.DictSummary() for batch in it: observation = {} with reporter.report_scope(observation): in_arrays = convert._call_converter(self.converter, batch, self.device) xp = self.device.xp X, Y = xp.array(in_arrays[0]), xp.array(in_arrays[1]) with function.no_backprop_mode(): eval_func(X, Y) summary.add(observation) return summary.compute_mean()
def predict(self, images, oversample=True): """Computes all the probabilities of given images. Args: images (iterable of PIL.Image or numpy.ndarray): Input images. When you specify a color image as a :class:`numpy.ndarray`, make sure that color order is RGB. oversample (bool): If ``True``, it averages results across center, corners, and mirrors. Otherwise, it uses only the center. Returns: ~chainer.Variable: Output that contains the class probabilities of given images. """ x = concat_examples([prepare(img, size=(256, 256)) for img in images]) if oversample: x = imgproc.oversample(x, crop_dims=(224, 224)) else: x = x[:, :, 16:240, 16:240] # Use no_backprop_mode to reduce memory consumption with function.no_backprop_mode(), chainer.using_config('train', False): x = Variable(self.xp.asarray(x)) y = self(x, layers=['prob'])['prob'] if oversample: n = len(y) // 10 y_shape = y.shape[1:] y = reshape(y, (n, 10) + y_shape) y = sum(y, axis=1) / 10 return y
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() for batch in it: observation = {} with reporter_module.report_scope(observation): in_arrays = self.converter(batch, self.device) with function.no_backprop_mode(): loss, acc = eval_func(**in_arrays) reporter_module.report({ 'val/loss': loss.data, 'val/acc': acc.data }) summary.add(observation) return summary.compute_mean()
def predict_file(voxel_path, out_path, model, channel, box_width, label_name, device): data = sparse.load_npz(voxel_path) data = np.reshape(data.toarray(), [data.shape[0], 14, 30, 30, 30])[:, :channel].astype( np.float32) data_width = data.shape[2] b, e = (data_width - box_width) // 2, (data_width + box_width) // 2 data = data[:, :, b:e, b:e, b:e] batch_size = 16 i = 0 out_data = {} out_pred_score = np.array([]).reshape(0, len(label_name)) while i * batch_size < data.shape[0]: voxel = data[i * batch_size:(i + 1) * batch_size] voxel = Variable(voxel) if device >= 0: voxel.to_gpu() with function.no_backprop_mode(), chainer.using_config('train', False): pred_score = F.sigmoid(model(voxel)) pred_score = chainer.cuda.to_cpu(pred_score.data) out_pred_score = np.vstack([out_pred_score, pred_score]) i += 1 for index, i in enumerate(label_name): out_data.update({i: out_pred_score[:, index]}) np.savez(out_path, **out_data)
def __call__(self, Xim, Xp1, Xp2, Y): Xp1 = self.encode_phrase(Xp1) Xp2 = self.encode_phrase(Xp2) # extract feature map from cnn with function.no_backprop_mode(), chainer.using_config('train', False): features = self._ext_fun(Xim) features = self.bn(features) features_proj = self.project_features(features) features = F.reshape(features, (-1, self.C, self.D)) # get context context1, alpha1 = self.attention_layer(features, features_proj, Xp1) context2, alpha2 = self.attention_layer(features, features_proj, Xp2) h1 = self.fuse_layer(context1, Xp1) h2 = self.fuse_layer(context2, Xp2) h = self.classification_layer(h1, h2) loss = F.sigmoid_cross_entropy(h, Y) precision, recall, fbeta = binary_classification_summary(h, Y) chainer.report({'loss': loss, 'precision': precision, 'recall': recall, 'f1': fbeta}, self) return loss
def evaluate(self): iterator = self._iterators['main'] eval_func = self.eval_func or self._targets['main'] if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: it = copy.copy(iterator) summary = reporter_module.DictSummary() for batch in it: observation = {} kwargs = {} kwargs['train'] = False 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, **kwargs) elif isinstance(in_arrays, dict): eval_func(**in_arrays, **kwargs) else: eval_func(in_arrays, **kwargs) summary.add(observation) observation = summary.compute_mean() return observation
def evaluate(self): val_iter = self.get_iterator('main') model = self.get_target('main') it = copy.copy(val_iter) summary = reporter.DictSummary() res = [] for i, batch in enumerate(it): observation = {} with reporter.report_scope(observation): imgs, pafs, heatmaps, ignore_mask = self.converter( batch, self.device) with function.no_backprop_mode(): x_data = imgs.astype(np.float32).transpose(0, 3, 1, 2) / 256 - 0.5 inferenced_pafs, inferenced_heatmaps = model(x_data) loss, pafs_loss_log, heatmaps_loss_log = compute_loss( inferenced_pafs, inferenced_heatmaps, pafs, heatmaps, ignore_mask) observation['val/loss'] = cuda.to_cpu(loss.data) summary.add(observation) return summary.compute_mean()
def evaluate(self): iterator = self._iterators['main'] eval_func = self._targets['main'] if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: warnings.warn( 'This iterator does not have the reset method. Evaluator ' 'copies the iterator instead of resetting. This behavior is ' 'deprecated. Please implement the reset method.', DeprecationWarning) it = copy.copy(iterator) summary = reporter_module.DictSummary() for x_batch, t_batch in it: observation = {} with reporter_module.report_scope(observation): with function.no_backprop_mode(): eval_func(x_batch, t_batch) summary.add(observation) return summary.compute_mean()
def evaluate(self): iterator = self.get_iterator("main") target = self.get_target("main") if hasattr(iterator, "reset"): iterator.reset() it = iterator else: it = copy.copy(iterator) summary = reporter.DictSummary() for batch in it: observation = {} with reporter.report_scope(observation): with function.no_backprop_mode(): in_arrays, t_arrays = self.converter(batch, self.device) p_arrays = target.predict(in_arrays) _, t_tag_sentences = list( zip(*self.transform_func(in_arrays[0], t_arrays))) _, p_tag_sentences = list( zip(*self.transform_func(in_arrays[0], p_arrays))) fscore = metrics.f1_score(t_tag_sentences, p_tag_sentences) reporter.report({"loss": target(in_arrays, t_arrays)}, target) reporter.report({"fscore": fscore}, target) summary.add(observation) return summary.compute_mean()
def forward(self, inputs): self.retain_inputs(tuple(range(len(inputs)))) with function.no_backprop_mode(),\ chainer.using_config('_will_recompute', True): xs = [variable.Variable(x) for x in inputs] outs = _call_func(self.func, xs) return tuple(out.data for out in outs)
def evaluate(self): val_iter = self.get_iterator('main') model = self.get_target('main') it = copy.copy(val_iter) summary = reporter.DictSummary() res = [] for i, batch in enumerate(it): observation = {} with reporter.report_scope(observation): imgs, pafs, heatmaps, ignore_mask = self.converter(batch, self.device) with function.no_backprop_mode(): x_data = preprocess(imgs) pafs_ys, heatmaps_ys = model(x_data) loss, paf_loss_log, heatmap_loss_log = compute_loss( imgs, pafs_ys, heatmaps_ys, pafs, heatmaps, ignore_mask) observation['val/loss'] = cuda.to_cpu(loss.data) observation['val/paf'] = sum(paf_loss_log) observation['val/heat'] = sum(heatmap_loss_log) summary.add(observation) return summary.compute_mean()
def predict(self, images, oversample=False): """Computes all the probabilities of given images. Args: images (iterable of PIL.Image or numpy.ndarray): Input images. When you specify a color image as a :class:`numpy.ndarray`, make sure that color order is RGB. oversample (bool): If ``True``, it averages results across center, corners, and mirrors. Otherwise, it uses only the center. Returns: ~chainer.Variable: Output that contains the class probabilities of given images. """ #x = concat_examples([prepare(img, size=(256, 256)) for img in images]) #x = concat_examples([prepare(img, size=(224, 224)) for img in images]) # Use no_backprop_mode to reduce memory consumption x = images #print(x) with function.no_backprop_mode(), chainer.using_config('train', False): #x = Variable(x) #x = Variable(self.base.xp.asarray(x)) #print('predicting',x.dtype,x.shape,type(x)) y = self.extract(x, layers=['fc6'])['fc6'] #print('y',y.shape,type(y)) y = self.fc7(y) y = softmax(y) #probability calc return y
def evaluate(self): val_iter = self.get_iterator('main') model = self.get_target('main') it = copy.copy(val_iter) summary = reporter.DictSummary() res = [] for i, batch in enumerate(it): observation = {} with reporter.report_scope(observation): imgs, pafs, heatmaps, ignore_mask = self.converter( batch, self.device) with function.no_backprop_mode(): x_data = preprocess(imgs) pafs_ys, heatmaps_ys = model(x_data) loss, paf_loss_log, heatmap_loss_log = compute_loss( imgs, pafs_ys, heatmaps_ys, pafs, heatmaps, ignore_mask) observation['val/loss'] = cuda.to_cpu(loss.data) observation['val/paf'] = sum(paf_loss_log) observation['val/heat'] = sum(heatmap_loss_log) summary.add(observation) return summary.compute_mean()
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() while True: batch = it.next() 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) summary.add(observation) if it.is_new_epoch: break out = summary.compute_mean() print('#############################################', out) return out
def predict(self, images, oversample=True): """Computes all the probabilities of given images. Args: images (iterable of PIL.Image or numpy.ndarray): Input images. oversample (bool): If ``True``, it averages results across center, corners, and mirrors. Otherwise, it uses only the center. Returns: ~chainer.Variable: Output that contains the class probabilities of given images. """ x = concat_examples([prepare(img, size=(256, 256)) for img in images]) if oversample: x = imgproc.oversample(x, crop_dims=(224, 224)) else: x = x[:, :, 16:240, 16:240] # Use no_backprop_mode to reduce memory consumption with function.no_backprop_mode(): x = Variable(self.xp.asarray(x)) y = self(x, layers=['prob'])['prob'] if oversample: n = y.data.shape[0] // 10 y_shape = y.data.shape[1:] y = reshape(y, (n, 10) + y_shape) y = sum(y, axis=1) / 10 return y
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() for batch in it: observation = {} with reporter_module.report_scope(observation): # read scp files # x: original json with loaded features # will be converted to chainer variable later # batch only has one minibatch utterance, which is specified by batch[0] x = converter_kaldi(batch[0], self.reader) with function.no_backprop_mode(): eval_func(x) delete_feat(x) summary.add(observation) return summary.compute_mean()
def evaluate(self): """Evaluates the model and returns a result dictionary. This method runs the evaluation loop over the validation dataset. It accumulates the reported values to :class:`~chainer.DictSummary` and returns a dictionary whose values are means computed by the summary. Note that this function assumes that the main iterator raises ``StopIteration`` or code in the evaluation loop raises an exception. So, if this assumption is not held, the function could be caught in an infinite loop. Users can override this method to customize the evaluation routine. .. note:: This method encloses :attr:`eval_func` calls with :func:`function.no_backprop_mode` context, so all calculations using :class:`~chainer.FunctionNode`\\s inside :attr:`eval_func` do not make computational graphs. It is for reducing the memory consumption. Returns: dict: Result dictionary. This dictionary is further reported via :func:`~chainer.report` without specifying any observer. """ 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) if self.max_num_iterations is not None: it = self.fixed_num_iterations_iterator(it) summary = reporter_module.DictSummary() 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) summary.add(observation) return self.calculate_mean_of_summary(summary)
def evaluate(self): iterator = self._iterators['main'] target = self._targets['main'] eval_func = self.eval_func 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() count = 0 flag = 0 for batch in it: observation = {} with reporter_module.report_scope(observation): in_arrays = self.converter(batch, self.device) with function.no_backprop_mode(): if not isinstance(in_arrays, tuple): raise TypeError images, labels = in_arrays res = Queue(self.extract_func(target, images)) """ VGGのテストのフロ(ー)チャ(ート) 1. activationsが空になるまで繰り返す 2. pool_indexより、pool_activationsの空き領域にactivations の先頭から入れる。pool_indexが0の場合、pool_labelを 同じlabelsで設定する。 3. pool_activationsの状態をチェック 4. 満タンなら5、そうでなければ1に移動する。 5. eval_funcを実行、pool_activationsを空にし、1へ移動する。 """ # activations_index = 0 while not res.is_full: if self.pool.is_empty: label = labels[res.index] set_remain(self.pool, res) if self.pool.is_full: eval_func(label) self.pool.index = 0 # reset summary.add(observation) # TODO: print_reportで書き直すことは、無理そう # evaluateはiterやepochの概念外。なのでロガーで書こう。 flag += 1 count += len(batch) if flag % 100 == 0: sys.stdout.write("\r{}/{} : {}".format(count, len(it.dataset), summary.compute_mean())) return summary.compute_mean()
def __call__(self, imgs): imgs = self.model.xp.asarray([self.do_transform(img) for img in imgs]) with using_config("train", False), no_backprop_mode(): imgs = Variable(imgs) predictions = self.model(imgs) output = to_cpu(predictions.array) return output
def evaluate(self): """Evaluates the model and returns a result dictionary. This method runs the evaluation loop over the validation dataset. It accumulates the reported values to :class:`~chainer.DictSummary` and returns a dictionary whose values are means computed by the summary. Note that this function assumes that the main iterator raises ``StopIteration`` or code in the evaluation loop raises an exception. So, if this assumption is not held, the function could be caught in an infinite loop. Users can override this method to customize the evaluation routine. .. note:: This method encloses :attr:`eval_func` calls with :func:`function.no_backprop_mode` context, so all calculations using :class:`~chainer.FunctionNode`\\s inside :attr:`eval_func` do not make computational graphs. It is for reducing the memory consumption. Returns: dict: Result dictionary. This dictionary is further reported via :func:`~chainer.report` without specifying any observer. """ 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() 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) summary.add(observation) return summary.compute_mean()
def evaluate(self): iterator = self._iterators['main'] eval_func = self.eval_func or self._targets['main'] if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: it = copy.copy(iterator) summary = reporter_module.DictSummary() pred_labels = [] gt_labels = [] 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) if eval_func.predictions is not None: pred_labels.extend(cuda.to_cpu(eval_func.predictions)) gt_labels.extend(cuda.to_cpu(eval_func.gt)) summary.add(observation) observation = summary.compute_mean() if self.label_names is not None and len(pred_labels) > 0: pred_labels = np.array(pred_labels) gt_labels = np.array(gt_labels) result = eval_semantic_segmentation(pred_labels, gt_labels) report = { 'miou': result['miou'], 'pixel_acc': result['pixel_accuracy'], 'mean_class_acc': result['mean_class_accuracy'] } for l, label_name in enumerate(self.label_names): try: report['iou/{:s}'.format(label_name)] = result['iou'][l] report['class_acc/{:s}'.format( label_name)] = result['class_accuracy'][l] except IndexError: report['iou/{:s}'.format(label_name)] = np.nan report['class_acc/{:s}'.format(label_name)] = np.nan with reporter_module.report_scope(observation): reporter_module.report(report, eval_func) return observation
def predict_core(self, model, batch): in_arrays = self.converter(batch, self.device) with function.no_backprop_mode(): with configuration.using_config('train', False): if isinstance(in_arrays, tuple): y = model(*in_arrays) elif isinstance(in_arrays, dict): y = model(**in_arrays) else: y = model(in_arrays) return _variable_to_array(y, to_cpu=self.to_cpu)
def evaluate(self): # iterator, modelを設定 iterator = self._iterators['main'] model = self._targets['main'] # 別で行う関数があるなら設定 eval_func = self.eval_func or model 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() recall_count = 0 accuracy_count = 0 lcount = 0 for i, batch in enumerate(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) re, ac = self.cm(in_arrays) elif isinstance(in_arrays, dict): eval_func(**in_arrays) re, ac = self.cm(in_arrays) else: eval_func(in_arrays) re, ac = self.cm(in_arrays) recall_count = recall_count + re accuracy_count = accuracy_count + ac summary.add(observation) lcount = i cm_observation = {} cm_observation["cmrecall"] = round(recall_count / (lcount + 1), 3) cm_observation["cmaccuracy"] = round(accuracy_count / (lcount + 1), 3) # print(cm_observation) # print(summary.compute_mean()) summary.add(cm_observation) return summary.compute_mean()
def _plot_figure(trainer): number = 5 fig, axs = plt.subplots(2, number) with function.no_backprop_mode(): for i in range(number): x = Variable(test[i].reshape(1, 1, 64, 64)) y = updater.gen(x) x_data = test[i] y_data = y.data[0] axs[0, i].imshow(x_data.reshape(64, 64), cmap='gray') axs[1, i].imshow(y_data.reshape(64, 64), cmap='gray') plt.savefig( os.path.join(OUT_DIR, ('epoch' + str(updater.epoch) + '.png')))
def evaluate(self, output_file_name): iterator = self.iterator preprocess = self.preprocess target = self.target eval_func = self.eval_func or (lambda x: target(preprocess(x))) device = self.device or chainer.cuda.cupy.cuda.get_device_id() if self.eval_hook: self.eval_hook(self) if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: it = copy.copy(iterator) margin_list = [] for batch in it: in_arrays = self.converter(batch, device) with function.no_backprop_mode(): assert isinstance(in_arrays, tuple) xp = chainer.cuda.get_array_module(*in_arrays) y, t, lipschitz = eval_func((in_arrays + (xp.ones( (1, ), dtype=xp.float32), ))) y = y.data lipschitz = lipschitz.data assert y.size == lipschitz.size assert y.ndim == 2 # calculate Lipschitz-normalized margin margins = chainer.cuda.elementwise( 'T y, T yt, T lipschitz', 'T margin', ''' if (lipschitz == 0.0) { // margin is INF // e.g. y == yt margin = 255.0 * 3.0 * 300 * 300; } else { margin = (yt - y) / lipschitz; if (margin < 0.0) { margin = 0.0; } } ''', 'margin')(y, y[list(range(t.size)), t].reshape(t.size, 1), lipschitz) margins = xp.min(margins, axis=1) margin_list.extend(list(margins.get())) margin_list = np.asarray(margin_list) np.save(output_file_name, margin_list)
def get_predict_value(data, model, gpu): data = data.astype(np.float32) batch_size = 16 i = 0 out_list = [] while i * batch_size < data.shape[0]: voxel = data[i * batch_size:(i + 1) * batch_size] voxel = Variable(voxel) if gpu >= 0: voxel.to_gpu() with no_backprop_mode(), chainer.using_config('train', False): pred_score = F.sigmoid(model(voxel)) pred_score = chainer.cuda.to_cpu(pred_score.data).ravel() out_list.extend(pred_score) i += 1 return np.array(out_list)
def evaluate(self): iterator = self._iterators['main'] self.target = self.get_target('main') it = copy.copy(iterator) summary = reporter.DictSummary() for batch in it: observation = {} with reporter.report_scope(observation): in_arrays = self.converter(batch, self.device) xp = chainer.backend.get_array_module(in_arrays) with function.no_backprop_mode(): y = self.target(in_arrays) self.loss = self.loss_AE(self.target, in_arrays, y) summary.add(observation) return summary.compute_mean()
def _evaluate_local_single(self, iterator): for batch in iterator: in_arrays = convert._call_converter(self.converter, batch, self.device) with function.no_backprop_mode(): if isinstance(in_arrays, tuple): results = self.calc_local(*in_arrays) elif isinstance(in_arrays, dict): results = self.calc_local(**in_arrays) else: results = self.calc_local(in_arrays) if self._progress_hook: self._progress_hook(batch) yield results
def fix_swa_batchnorm(evaluator): # Check batchnorm layer bn_flg = False for l in swa_model.links(): if type(l) == L.normalization.batch_normalization.BatchNormalization: bn_flg = True break # Fix batchnorm's running mean and variance if bn_flg: swa_train_iter.reset() with chainer.using_config('train', True): for batch in swa_train_iter: in_arrays = evaluator.converter(batch, evaluator.device) with function.no_backprop_mode(): swa_model(*in_arrays)
def evaluate(self): iterator = self._iterators['main'] gen = self._targets['gen'] 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() for batch in it: observation = {} with reporter_module.report_scope(observation): xy, xyz, scale = self.converter(batch, self.device) with function.no_backprop_mode(), \ chainer.using_config('train', False): xy_real = xy z_pred = gen(xy_real) mse = F.mean_squared_error(z_pred, xyz[:, :, :, 2::3]) chainer.report({'mse': mse}, gen) xx = gen.xp.power(xyz[:, :, :, 0::3] - xy[:, :, :, 0::2], 2) yy = gen.xp.power(xyz[:, :, :, 1::3] - xy[:, :, :, 1::2], 2) zz1 = gen.xp.power(xyz[:, :, :, 2::3] - z_pred.data, 2) zz2 = gen.xp.power(xyz[:, :, :, 2::3] + z_pred.data, 2) m1 = gen.xp.sqrt(xx + yy + zz1).mean(axis=3)[:, 0] m2 = gen.xp.sqrt(xx + yy + zz2).mean(axis=3)[:, 0] mae = gen.xp.where(m1 < m2, m1, m2) m1 *= scale mae *= scale m1 = gen.xp.mean(m1) mae = gen.xp.mean(mae) chainer.report({'mae1': m1}, gen) chainer.report({'mae2': mae}, gen) summary.add(observation) return summary.compute_mean()
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() for batch in it: observation = {} with reporter_module.report_scope(observation): row_idx, col_idx, val_idx = [], [], [] x = cuda.to_gpu(np.array([i[0] for i in batch])) labels = [l[1] for l in batch] for i in range(len(labels)): l_list = list( set(labels[i]) ) # remove duplicate cateories to avoid double count for y in l_list: row_idx.append(i) col_idx.append(y) val_idx.append(1) m = len(labels) n = self.class_dim t = sp.csr_matrix((val_idx, (row_idx, col_idx)), shape=(m, n), dtype=np.int8).todense() t = cuda.to_gpu(t) with function.no_backprop_mode(): #pdb.set_trace() loss = F.sigmoid_cross_entropy(eval_func(x), t) summary.add( {MyEvaluator.default_name + '/main/loss': loss}) summary.add(observation) return summary.compute_mean()
def evaluate(self): """Evaluates the model and returns a result dictionary. This method runs the evaluation loop over the validation dataset. It accumulates the reported values to :class:`~chainer.DictSummary` and returns a dictionary whose values are means computed by the summary. Users can override this method to customize the evaluation routine. Returns: dict: Result dictionary. This dictionary is further reported via :func:`~chainer.report` without specifying any observer. """ iterator = self._iterators['main'] target = self._targets['main'] eval_func = self.eval_func or target 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() 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) summary.add(observation) return summary.compute_mean()
def evaluate(self): iterator = self._iterators['main'] gen = self._targets['gen'] 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() for batch in it: observation = {} with reporter_module.report_scope(observation): xy_proj, xyz, scale = self.converter(batch, self.device) xy_proj, xyz = xy_proj[:, 0], xyz[:, 0] with function.no_backprop_mode(), \ chainer.using_config('train', False): xy_real = chainer.Variable(xy_proj) z_pred = gen(xy_real) #z_mse = F.mean_squared_error(z_pred, xyz[:, 2::3]) z = z_pred * scale z_real = xyz[:, 2::3] * scale z_mse = F.mean_squared_error(z, z_real) chainer.report({'z_mse': z_mse}, gen) lx = gen.xp.power(xyz[:, 0::3] - xy_proj[:, 0::2], 2) ly = gen.xp.power(xyz[:, 1::3] - xy_proj[:, 1::2], 2) lz = gen.xp.power(xyz[:, 2::3] - z_pred.data, 2) euclidean_distance = gen.xp.sqrt(lx + ly + lz).mean(axis=1) euclidean_distance *= scale[:, 0] euclidean_distance = gen.xp.mean(euclidean_distance) chainer.report({'euclidean_distance': euclidean_distance}, gen) summary.add(observation) return summary.compute_mean()
def get_prediction(model_dir, split, device=None): model_dir = model_dir+'/' if model_dir[-1] != '/' else model_dir setting = json.load(open(model_dir+'settings.json')) image_net = setting['image_net'] phrase_net = setting['phrase_net'] # img_preprocessed = setting['img_preprocessed'] model = setup_model(phrase_net, image_net) chainer.serializers.load_npz(model_dir+'model', model) if device is not None: chainer.cuda.get_device_from_id(device).use() model.to_gpu() wo_image = (image_net is None) test, conv_f = get_dataset(phrase_net, image_net=image_net, split=split, preload=True) test_iter = SerialIterator(test, batch_size=300, repeat=False, shuffle=False) s_i = 0 e_i = 0 pred = np.zeros((len(test),), dtype=np.float32) with function.no_backprop_mode(), chainer.using_config('train', False): for i, batch in enumerate(test_iter): inputs = conv_f(batch, device) score = model.predict(*inputs[:-1]) score.to_cpu() e_i = s_i + len(batch) pred[s_i:e_i] = score.data.ravel() s_i = e_i df = pd.DataFrame({ 'image': test._image_id, 'phrase1': test._phrase1, 'phrase2': test._phrase2, 'ytrue': test._label, 'score': pred, }) return df
def evaluate(self): val_iter = self.get_iterator('main') model = self.get_target('main') it = copy.copy(val_iter) summary = reporter.DictSummary() res = [] for i, batch in enumerate(it): observation = {} with reporter.report_scope(observation): imgs, pafs, heatmaps, ignore_mask = self.converter(batch, self.device) with function.no_backprop_mode(): x_data = imgs.astype(np.float32).transpose(0, 3, 1, 2) / 256 - 0.5 inferenced_pafs, inferenced_heatmaps = model(x_data) loss, pafs_loss_log, heatmaps_loss_log = compute_loss( inferenced_pafs, inferenced_heatmaps, pafs, heatmaps, ignore_mask) observation['val/loss'] = cuda.to_cpu(loss.data) summary.add(observation) return summary.compute_mean()
def forward(self, inputs): with function.no_backprop_mode(): xs = [variable.Variable(x) for x in inputs] outs = self._call_func(xs) return tuple(out.data for out in outs)
def forward(self, inputs): self.retain_inputs(tuple(range(len(inputs)))) with function.no_backprop_mode(): xs = [variable.Variable(x) for x in inputs] outs = _call_func(self.func, xs) return tuple(out.data for out in outs)