import numpy as np
import unittest
from unittest.mock import MagicMock

import chainer
from chainer import testing
from chainer.testing import attr
from chainercv.links import PixelwiseSigmoidClassifier
from chainercv.links import PixelwiseSoftmaxClassifier
from chainercv.links import PixelwiseSoftmaxWithWeightClassifier
from chainercv.links.loss import semantic_segmentation_loss as ssloss

class DummyCNN(chainer.Chain):

    def __init__(self, n_class):
        super(DummyCNN, self).__init__()
        self.n_class = n_class

    def __call__(self, x):
        n, _, h, w = x.shape
        y = self.xp.random.rand(n, self.n_class, h, w).astype(np.float32)
        return chainer.Variable(y)
        

@testing.parameterize(
    {'n_class': 11, 'compute_accuracy': True},
    {'n_class': 11, 'compute_accuracy': False},
)
class TestPixelwiseSigmoidClassifier(unittest.TestCase):

    def setUp(self):
        model = DummyCNN(self.n_class)
        self.link = PixelwiseSigmoidClassifier(
            model, self.n_class, self.compute_accuracy)
        self.x = np.random.rand(1, 3, 32, 32).astype(np.float32)
        self.t = np.random.randint(
                    self.n_class, size=(1, 11, 32, 32)).astype(np.int32)
        ssloss._segmentation_accuracies = MagicMock()

    def _check_call(self):
        xp = self.link.xp
        loss = self.link(xp.asarray(self.x), xp.asarray(self.t))
        self.assertIsInstance(loss, chainer.Variable)
        self.assertIsInstance(loss.data, self.link.xp.ndarray)
        self.assertEqual(loss.shape, ())
        
        if self.compute_accuracy:
            ssloss._segmentation_accuracies.assert_called_with(
                self.link.y, self.t, self.n_class)
            print('aaa')


    @attr.gpu
    def test_call_gpu(self):
        self.link.to_gpu()
        self._check_call()

    def test_call_cpu(self):
        self._check_call()
        

@@ -320,77 +320,8 @@
 ier%0A
-from chainercv.links.loss import semantic_segmentation_loss as ssloss
 %0A%0Acl
@@ -621,24 +621,16 @@
 able(y)%0A
-        
 %0A%0A@testi
@@ -1001,20 +1001,21 @@
 and(
-1
+2
 , 3, 
-32, 32
+16, 16 
 ).as
@@ -1075,31 +1075,35 @@
             
-       
+2, size=(2,
  self.n_clas
@@ -1109,99 +1109,32 @@
 ss, 
-size=(1, 11, 32, 32)).astype(np.int32)%0A        ssloss._segmentation_accuracies = MagicMock(
+16, 16)).astype(np.int32
 )%0A%0A 
@@ -1412,164 +1412,848 @@
 ())%0A
+%0A
     
-    %0A        if self.compute_accuracy:%0A            ssloss._segmentation_accuracies.assert_called_with(%0A                self.link.y, self.t, self.n_class
+@attr.gpu%0A    def test_call_gpu(self):%0A        self.link.to_gpu()%0A        self._check_call()%0A%0A    def test_call_cpu(self):%0A        self._check_call()%0A%0A%0A@testing.parameterize(%0A    %7B'n_class': 11, 'compute_accuracy': True%7D,%0A    %7B'n_class': 11, 'compute_accuracy': False%7D,%0A)%0Aclass TestPixelwiseSoftmaxClassifier(unittest.TestCase):%0A%0A    def setUp(self):%0A        model = DummyCNN(self.n_class)%0A        self.link = PixelwiseSoftmaxClassifier(%0A            model, self.n_class, self.compute_accuracy)%0A        self.x = np.random.rand(2, 3, 16, 16).astype(np.float32)%0A        self.t = np.random.randint(%0A                    self.n_class, size=(2, 16, 16)).astype(np.int32)%0A%0A    def _check_call(self):%0A        xp = self.link.xp%0A        loss = self.link(xp.asarray(self.x), xp.asarray(self.t))%0A        self.assertIsInstance(loss, chainer.Variable
 )%0A  
@@ -2262,25 +2262,103 @@
     
- 
-   print('aaa')%0A
+self.assertIsInstance(loss.data, self.link.xp.ndarray)%0A        self.assertEqual(loss.shape, ())
 %0A%0A  
@@ -2513,13 +2513,4 @@
 l()%0A
-        %0A

Add a test