Esempio n. 1
0
    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
Esempio n. 2
0
 def use_model(self, x, t):
     self.reset()
     num_lm = x.shape[0]
     n_step = self.n_step
     s_list = xp.empty((n_step, num_lm, 1))
     l_list = xp.empty((n_step, num_lm, 2))
     x_list = xp.empty((n_step, num_lm, 3, self.gsize, self.gsize))
     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)
             s_list[i] = sm.data
             l_list[i] = lm.data
             x_list[i] = xm.data
             accuracy = y.data * t
             s_list = xp.power(10, s_list - 1)
             return xp.sum(accuracy, axis=1), l_list, s_list, x_list
         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
         s_list[i] = sm.data
         l_list[i] = lm.data
         x_list[i] = xm.data
     return
Esempio n. 3
0
 def s1_determin(self, x, t, l1, s1):
     self.reset()
     num_lm = x.data.shape[0]
     s1 = xp.log10(s1 + 0.001) + 1
     self.first_forward(x, num_lm)
     xm, lm, sm = self.make_img(x, Variable(l1), Variable(s1), num_lm, random=0)
     l1, s1, y, b = self.recurrent_forward(xm, lm, sm)
     accuracy = y.data * t.data
     return xp.sum(accuracy)
Esempio n. 4
0
 def s2_determin(self, x, t, l_list, s_list):
     self.reset()
     num_lm = x.data.shape[0]
     s_list = xp.log10(s_list + 0.001) + 1
     self.first_forward(x, num_lm)
     xm, lm, sm = self.make_img(x, Variable(l_list[0]), Variable(s_list[0]), num_lm, random=0)
     self.recurrent_forward(xm, lm, sm)
     xm, lm, sm = self.make_img(x, Variable(l_list[1]), Variable(s_list[1]), num_lm, random=0)
     l1, s1, y, b = self.recurrent_forward(xm, lm, sm)
     accuracy = y.data * t.data
     class_l = xp.argmax(y.data, axis=1)
     return xp.sum(accuracy), class_l
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
 def use_model(self, x, t):
     self.reset()
     num_lm = x.shape[0]
     n_step = self.n_step
     s_list = xp.ones((n_step, num_lm, 1)) * (self.gsize / self.img_size)
     l_list = xp.empty((n_step, num_lm, 2))
     l, b1 = self.first_forward(x, num_lm)
     for i in range(n_step):
         if i + 1 == n_step:
             xm, lm = self.make_img(x, l, num_lm, random=0)
             l1, y, b = self.recurrent_forward(xm, lm)
             l_list[i] = l1.data
             accuracy = y.data * t
             return xp.sum(accuracy, axis=1), l_list, s_list
         else:
             xm, lm = self.make_img(x, l, num_lm, random=0)
             l1, y, b = self.recurrent_forward(xm, lm)
         l = l1
         l_list[i] = l.data
     return
Esempio n. 8
0
    def __call__(self,
                 x,
                 target,
                 num_lm,
                 batch_size=1,
                 train=1,
                 debug=0,
                 n_step=1):

        if train == 1:
            self.reset()
            l, b1 = self.first_forward(x, num_lm)
            for i in range(n_step):
                if i + 1 == n_step:
                    xm, lm = self.make_img(x, l, num_lm, random=1)
                    l1, y, b = self.recurrent_forward(xm, lm)

                    loss = self.cul_loss(y, target, l, lm, num_lm, b1)
                    return loss / num_lm
                else:
                    xm, lm = self.make_img(x, l, num_lm, random=0)
                    l1, y, b = self.recurrent_forward(xm, lm)
                l = l1
                b1 = b

        elif train == 0:
            sum_accuracy = 0
            ydata = xp.zeros((num_lm, self.num_class))
            self.reset()
            l, b1 = self.first_forward(x, num_lm)
            for i in range(n_step):
                if i + 1 == n_step:
                    xm, lm = self.make_img(x, l, num_lm, random=0)
                    l1, y, b = self.recurrent_forward(xm, lm)

                    accuracy = y.data * target.data
                    sum_accuracy += xp.sum(accuracy)
                    ydata += y.data

                    return sum_accuracy / (
                        num_lm *
                        n_step), ydata / n_step, xp.sum(accuracy) / num_lm
                else:
                    xm, lm = self.make_img(x, l, num_lm, random=0)
                    l1, y, b = self.recurrent_forward(xm, lm)
                    accuracy = y.data * target.data
                    sum_accuracy += xp.sum(accuracy)
                    ydata += y.data
                l = l1
        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, b1 = self.first_forward(x, num_lm)
            l_list[0] = l.data
            s_list[0] = self.gsize / self.img_size
            for i in range(n_step):
                if i + 1 == n_step:
                    xm, lm = self.make_img(x, l, num_lm, random=0)
                    l1, y, b = self.recurrent_forward(xm, lm)

                    accuracy = y.data * target.data
                    sum_accuracy += xp.sum(accuracy)
                    ydata += y.data
                    return sum_accuracy / (
                        num_lm * n_step), ydata / n_step, xp.sum(
                            accuracy) / num_lm, l_list, s_list
                else:
                    xm, lm = self.make_img(x, l, num_lm, random=0)
                    l1, y, b = self.recurrent_forward(xm, lm)
                    accuracy = y.data * target.data
                    sum_accuracy += xp.sum(accuracy)
                    ydata += y.data
                l = l1
                l_list[i + 1] = l.data
                s_list[i + 1] = self.gsize / self.img_size

            return False
Esempio n. 9
0
    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