def report_scores(self, y, t): with chainer.no_backprop_mode(): if self.nested_label: dice = mean_dice_coefficient( dice_coefficient(y[:, 0:2, ...], t[:, 0, ...])) for i in range(1, t.shape[1]): dices = dice_coefficient(y[:, 2 * i:2 * (i + 1), ...], t[:, i, ...]) dice = F.concat((dice, mean_dice_coefficient(dices)), axis=0) else: dice = dice_coefficient(y, t) mean_dice = mean_dice_coefficient(dice) if self.nested_label: b, c, h, w, d = t.shape y = F.reshape(y, (b, 2, h * c, w, d)) t = F.reshape(t, (b, h * c, w, d)) accuracy = F.accuracy(y, t) reporter.report({'acc': accuracy, 'mean_dc': mean_dice}) xp = cuda.get_array_module(y) for i in range(len(dice)): if not xp.isnan(dice.data[i]): reporter.report({'dc_{}'.format(i): dice[i]})
def compute_dice_coef(self, y, t): if self.nested_label: dice = mean_dice_coefficient(dice_coefficient(y[:, 0:2, ...], t[:, 0, ...])) for i in range(1, t.shape[1]): dices = dice_coefficient(y[:, 2*i:2*(i+1), ...], t[:, i, ...]) dice = F.concat((dice, mean_dice_coefficient(dices)), axis=0) else: dice = dice_coefficient(y, t, is_brats=self.is_brats) return dice
def evaluate(self): summary = reporter_module.DictSummary() iterator = self._iterators['main'] enc = self._targets['enc'] dec = self._targets['dec'] bound = self._targets['bound'] reporter = Reporter() observer = object() reporter.add_observer(self.default_name, observer) if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: it = copy.copy(iterator) for batch in it: x, t, te = self.converter(batch, self.device) with chainer.no_backprop_mode(), chainer.using_config( 'train', False): if self.dataset == 'msd_bound': h, w, d = x.shape[2:] hc, wc, dc = self.crop_size if self.nested_label: y = cupy.zeros((1, 2 * (self.nb_labels - 1), h, w, d), dtype='float32') else: y = cupy.zeros((1, self.nb_labels, h, w, d), dtype='float32') s = 128 # stride ker = 256 # kernel size dker = dc # kernel size for depth ds = dker * 0.5 # stride for depth dsteps = int(math.floor((d - dker) / ds) + 1) steps = round((h - ker) / s + 1) for i in range(steps): for j in range(steps): for k in range(dsteps): xx = x[:, :, s * i:ker + s * i, s * j:ker + s * j, ds * k:dker + ds * k] hhs = enc(xx) yye, bbs = bound(hhs) yy = dec(hhs, bbs) y[:, :, s * i:ker + s * i, s * j:ker + s * j, ds * k:dker + ds * k] += yy.data # for the bottom depth part of the image xx = x[:, :, s * i:ker + s * i, s * j:ker + s * j, -dker:] hhs = enc(xx) yye, bbs = bound(hhs) yy = dec(hhs, bbs) y[:, :, s * i:ker + s * i, s * j:ker + s * j, -dker:] += yy.data else: hs = enc(x) ye, bs = bound(hs) y = dec(hs, bs) seg_loss = self.compute_loss(y, t) accuracy = self.compute_accuracy(y, t) dice = self.compute_dice_coef(y, t) mean_dice = mean_dice_coefficient(dice) weighted_loss = seg_loss observation = {} with reporter.scope(observation): reporter.report( { 'loss/seg': seg_loss, 'loss/total': weighted_loss, 'acc': accuracy, 'mean_dc': mean_dice }, observer) xp = cuda.get_array_module(y) for i in range(len(dice)): if not xp.isnan(dice.data[i]): reporter.report({'dc_{}'.format(i): dice[i]}, observer) summary.add(observation) return summary.compute_mean()
def evaluate(self): summary = reporter_module.DictSummary() iterator = self._iterators['main'] enc = self._targets['enc'] dec = self._targets['dec'] reporter = Reporter() observer = object() reporter.add_observer(self.default_name, observer) if hasattr(iterator, 'reset'): iterator.reset() it = iterator else: it = copy.copy(iterator) for batch in it: x, t = self.converter(batch, self.device) with chainer.no_backprop_mode(), chainer.using_config('train', False): if self.dataset == 'msd_bound': # evaluation method for BRATS dataset only h, w, d = x.shape[2:] hc, wc, dc = self.crop_size if self.nested_label: y = cupy.zeros((1, 2*(self.nb_labels-1), h, w, d), dtype='float32') else: y = cupy.zeros((1, self.nb_labels, h, w, d), dtype='float32') hker = hc # kernel size hs = int(0.5*hker) # stride wker = wc wc = int(0.5*wker) dker = dc # kernel size for depth for i in range(2): for j in range(2): for k in range(2): xx = x[:, :, -i*hker:min(hker*(i+1), h), -j*wker:min(wker*(j+1), w), -k*dker:min(dker*(k+1), d)] hs = enc(xx) yy = dec(hs) y[:, :, -i*hker:min(hker*(i+1), h), -j*wker:min(wker*(j+1), w), -k*dker:min(dker*(k+1), d)] += yy.data else: hs = enc(x) y = dec(hs) seg_loss = self.compute_loss(y, t) accuracy = self.compute_accuracy(y, t) dice = self.compute_dice_coef(y, t) mean_dice = mean_dice_coefficient(dice) observation = {} with reporter.scope(observation): reporter.report({ 'loss/seg': seg_loss, 'acc': accuracy, 'mean_dc': mean_dice }, observer) xp = cuda.get_array_module(y) for i in range(len(dice)): if not xp.isnan(dice.data[i]): reporter.report({'dc_{}'.format(i): dice[i]}, observer) summary.add(observation) return summary.compute_mean()