def save_deconv(self, k, f, d, b): # deconvされた入力層に平均画像を足して画像化 img = ioutil.deprocess(d, self.mean) # 最大値の位置の計算に必要な入力層の領域だけクロッピングして保存 img.crop((b[0], b[2], b[1], b[3])).save( os.path.join(self.deconv_image_dir, "{0:0>4}_{1:0>2}.png".format(f, k)))
def evaluate(self): """override method of extensions.Evaluator.""" 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() max_locs = [] bounds = [] n_processed = 0 filter_idx = 0 pbar = tqdm(total=len(iterator.dataset)) for batch in it: observation = {} with reporter_module.report_scope(observation): in_arrays = self.converter(batch, self.device) if isinstance(in_arrays, tuple): in_vars = tuple( variable.Variable(x, volatile='off') for x in in_arrays) eval_func(*in_vars) elif isinstance(in_arrays, dict): in_vars = { key: variable.Variable(x, volatile='off') for key, x in six.iteritems(in_arrays) } eval_func(**in_vars) else: in_var = variable.Variable(in_arrays, volatile='off') eval_func(in_var) pbar.update(len(batch)) indices = np.arange(filter_idx, filter_idx + len(batch)) % self.n_features # deconv対象の層のVariableを取得 layer_variable = Vutil.get_variable(observation[self.lastname], self.layer_rank) # 最大値の位置の計算に必要な入力層の領域を取得 isfc = Vutil.has_fc_layer(layer_variable) if isfc: batch_bounds = Vutil.get_data_bounds(layer_variable) else: batch_bounds = Vutil.get_max_bounds(layer_variable, indices) topk = np.arange(n_processed, n_processed + len(batch)) // self.n_features input_data = Vutil.get_data_layer(layer_variable).data for k, f, d, b in zip(topk, indices, input_data, batch_bounds): #print(dir(d)) # deconvされた入力層に平均画像を足して画像化 img = ioutil.deprocess(d.get(), self.mean) # 最大値の位置の計算に必要な入力層の領域だけクロッピングして保存 img.crop((b[0], b[2], b[1], b[3])).save( os.path.join(self.patch_image_dir, "{0:0>4}_{1:0>2}.png".format(f, k))) if not isfc: max_locs.extend(Vutil.get_max_locs(layer_variable, indices)) bounds.extend(batch_bounds) filter_idx = (filter_idx + len(batch)) % self.n_features n_processed += len(batch) #self.add_to_confmat(self.confmat, in_vars[1].data, self.getpred(observation[self.lastname])) summary.add(observation) pbar.close() #print(self.confmat) #print(np.diag(self.confmat)) #print(1.0 * np.diag(self.confmat).sum() / self.confmat.sum()) return summary.compute_mean(), max_locs, bounds
def evaluate(self): """override method of extensions.Evaluator.""" 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() max_locs = [] bounds = [] n_processed = 0 filter_idx = 0 pbar = tqdm(total=len(iterator.dataset)) for batch in it: observation = {} with reporter_module.report_scope(observation): in_arrays = self.converter(batch, self.device) if isinstance(in_arrays, tuple): in_vars = tuple(variable.Variable(x, volatile='off') for x in in_arrays) eval_func(*in_vars) elif isinstance(in_arrays, dict): in_vars = {key: variable.Variable(x, volatile='off') for key, x in six.iteritems(in_arrays)} eval_func(**in_vars) else: in_var = variable.Variable(in_arrays, volatile='off') eval_func(in_var) pbar.update(len(batch)) indices = np.arange(filter_idx, filter_idx + len(batch)) % self.n_features # deconv対象の層のVariableを取得 layer_variable = Vutil.get_variable( observation[self.lastname], self.layer_rank) # 最大値の位置の計算に必要な入力層の領域を取得 isfc = Vutil.has_fc_layer(layer_variable) if isfc: batch_bounds = Vutil.get_data_bounds(layer_variable) else: batch_bounds = Vutil.get_max_bounds(layer_variable, indices) topk = np.arange(n_processed, n_processed + len(batch)) // self.n_features input_data = Vutil.get_data_layer(layer_variable).data for k, f, d, b in zip(topk, indices, input_data, batch_bounds): #print(dir(d)) # deconvされた入力層に平均画像を足して画像化 img = ioutil.deprocess(d.get(), self.mean) # 最大値の位置の計算に必要な入力層の領域だけクロッピングして保存 img.crop((b[0], b[2], b[1], b[3])).save( os.path.join(self.patch_image_dir, "{0:0>4}_{1:0>2}.png".format(f, k))) if not isfc: max_locs.extend(Vutil.get_max_locs(layer_variable, indices)) bounds.extend(batch_bounds) filter_idx = (filter_idx + len(batch)) % self.n_features n_processed += len(batch) #self.add_to_confmat(self.confmat, in_vars[1].data, self.getpred(observation[self.lastname])) summary.add(observation) pbar.close() #print(self.confmat) #print(np.diag(self.confmat)) #print(1.0 * np.diag(self.confmat).sum() / self.confmat.sum()) return summary.compute_mean(), max_locs, bounds
def __call__(self, trainer): fn = self.filename.format(trainer) img = ioutil.deprocess( cuda.to_cpu(self.target[self.paramname].W.data)[0], self.mean) img.save(os.path.join(trainer.out, fn)) '''prefix = 'tmp' + fn
def __call__(self, trainer): fn = self.filename.format(trainer) img = ioutil.deprocess(cuda.to_cpu(self.target[self.paramname].W.data)[0], self.mean) img.save(os.path.join(trainer.out, fn)) '''prefix = 'tmp' + fn
def evaluate(self): """override method of extensions.Evaluator.""" 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() max_locs = [] bounds = [] n_processed = 0 for batch in it: observation = {} with reporter_module.report_scope(observation): in_arrays = self.converter(batch, self.device) if isinstance(in_arrays, tuple): in_vars = tuple(variable.Variable(x, volatile='off') for x in in_arrays) eval_func(*in_vars) elif isinstance(in_arrays, dict): in_vars = {key: variable.Variable(x, volatile='off') for key, x in six.iteritems(in_arrays)} eval_func(**in_vars) else: in_var = variable.Variable(in_arrays, volatile='off') eval_func(in_var) # deconv対象の層のVariableを取得 layer_variable = Vutil.get_variable( observation[self.lastname], self.layer_rank) pred = self.getpred(layer_variable) truth = in_vars[1].data xp = cuda.get_array_module(pred) if xp == cupy: pred = pred.get() truth = truth.get() indices = pred.flatten() if self.target == 'pred' else truth.flatten() # 最大値の位置の計算に必要な入力層の領域を取得 isfc = Vutil.has_fc_layer(layer_variable) if isfc: bounds = Vutil.get_data_bounds(layer_variable) else: bounds = Vutil.get_max_bounds(layer_variable, indices) # deconvを実行 deconv_data = self.get_deconv( layer_variable, indices) topk = np.arange(n_processed, n_processed + len(batch)) // self.n_features for i, (t, p, d, b) in enumerate(zip(truth, indices, deconv_data, bounds)): #print(dir(d)) # deconvされた入力層に平均画像を足して画像化 img = ioutil.deprocess(d.get(), self.mean) # 最大値の位置の計算に必要な入力層の領域だけクロッピングして保存 img.crop((b[0], b[2], b[1], b[3])).save( os.path.join(self.deconv_image_dir, "{0:0>6}_{1:0>4}_{2:0>4}.png".format(n_processed + i, t, p))) '''max_locs.extend(self.get_max_locs( observation[self.lastname], self.layer_rank, indices)) bounds.extend(self.get_max_patch_bounds( observation[self.lastname], self.layer_rank, indices))''' n_processed += len(batch) #self.add_to_confmat(self.confmat, in_vars[1].data, self.getpred(observation[self.lastname])) summary.add(observation) #print(self.confmat) #print(np.diag(self.confmat)) #print(1.0 * np.diag(self.confmat).sum() / self.confmat.sum()) return summary.compute_mean(), max_locs, bounds