def __call__(self, x, target, bf=False): self.reset() n_step = self.n_step num_lm = x.shape[0] loss_func = 0 if chainer.config.train: r_buf = 0 l, s, b = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) loss_func += self.r_recognize * loss r = xp.where( xp.argmax(y.data, axis=1) == xp.argmax(target, axis=1), 1, 0).reshape((num_lm, 1)).astype(xp.float32) loss_func += F.sum((r - b) * (r - b)) # loss baseline k = self.r * (r - b.data) # calculate r loss_func += F.sum(k * size_p) return loss_func / num_lm else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) loss_func += loss r = xp.where( xp.argmax(y.data, axis=1) == xp.argmax(target, axis=1), 1, 0).reshape((num_lm, 1)).astype(xp.float32) loss_func += F.sum((r - b) * (r - b)) # loss baseline k = self.r * (r - b.data) # calculate r loss_func += F.sum(k * size_p) l = l1 s = s1 b = b1 else: l, s, b1 = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: if bf: self.b_log += xp.sum(b1.data) xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) accuracy = xp.sum(y.data * target) if self.use_gpu: accuracy = chainer.cuda.to_cpu(accuracy) return accuracy else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) l = l1 s = s1
def __call__(self, x, target, mode=0): self.reset() n_step = self.n_step num_lm = x.shape[0] if chainer.config.train: r_buf = 0 l, s, b = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) r_buf += size_p r = xp.where( xp.argmax(y.data, axis=1) == xp.argmax(target, axis=1), 1, 0).reshape((num_lm, 1)).astype(xp.float32) loss += F.sum((r - b) * (r - b)) k = self.r * (r - b.data) loss += F.sum(k * r_buf) return loss / num_lm else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) r_buf += size_p l = l1 s = s1 b = b1 else: l, s, b1 = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) accuracy = xp.sum(y.data * target) if self.use_gpu: accuracy = chainer.cuda.to_cpu(accuracy) return accuracy else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) l = l1 s = s1
def cul_loss(self, y, target, l, lm, num_lm, b): l1, l2 = F.split_axis(l, indices_or_sections=2, axis=1) m1, m2 = F.split_axis(lm, indices_or_sections=2, axis=1) ln_p = ((l1 - m1) * (l1 - m1) + (l2 - m2) * (l2 - m2)) / self.var / 2 accuracy = y * target loss = -F.sum(accuracy) r = xp.where( xp.argmax(y.data, axis=1) == xp.argmax(target.data, axis=1), 1, 0).reshape((num_lm, 1)).astype(xp.float32) loss += F.sum((r - b) * (r - b)) bb = xp.sum(b.data) / num_lm lossm = self.r * (r - bb) loss += F.sum(Variable(lossm) * ln_p) return loss
def __call__(self, x, target, num_lm, batch_size=1, train=1, debug=0, n_step=1): if train == 1: self.reset() r_buf = 0 l, s, b = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) r_buf += size_p r = xp.where( xp.argmax(y.data, axis=1) == xp.argmax(target.data, axis=1), 1, 0).reshape((num_lm, 1)).astype(xp.float32) loss += F.sum((r - b) * (r - b)) k = self.r * (r - b.data) loss += F.sum(k * r_buf) return loss / num_lm else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=1) l1, s1, y, b1 = self.recurrent_forward(xm, lm, sm) loss, size_p = self.cul_loss(y, target, l, s, lm, sm) r_buf += size_p l = l1 s = s1 b = b1 elif train == 0: self.reset() l, s, b1 = self.first_forward(x, num_lm) for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) accuracy = y.data * target.data return xp.sum(accuracy) / num_lm, y.data / n_step, xp.sum( accuracy) / num_lm else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) l = l1 s = s1 elif train == 2: sum_accuracy = 0 ydata = xp.zeros((num_lm, self.num_class)) self.reset() l_list = xp.zeros((n_step, num_lm, 2)) s_list = xp.zeros((n_step, num_lm, 1)) l, s, b1 = self.first_forward(x, num_lm) l_list[0] = l.data s_list[0] = s.data for i in range(n_step): if i + 1 == n_step: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) accuracy = y.data * target.data sum_accuracy += xp.sum(accuracy) ydata += y.data z = np.power(10, s_list - 1) return sum_accuracy / ( num_lm * n_step), ydata / n_step, xp.sum( accuracy, axis=1) / num_lm, l_list, z else: xm, lm, sm = self.make_img(x, l, s, num_lm, random=0) l1, s1, y, b = self.recurrent_forward(xm, lm, sm) accuracy = y.data * target.data sum_accuracy += xp.sum(accuracy) ydata += y.data l = l1 s = s1 l_list[i + 1] = l.data s_list[i + 1] = s.data return False