Beispiel #1
0
  def testDeConv2DSame(self):
    with self.test_session():
      strides = [1, 2, 2, 1]

      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 12, 8, 2]

      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]

      x = constant_op.constant(1.0, shape=x_shape, name="x",
                               dtype=types.float32)
      f = constant_op.constant(1.0, shape=f_shape, name="filter",
                               dtype=types.float32)
      output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
      value = output.eval()

      for n in xrange(x_shape[0]):
        for k in xrange(f_shape[2]):
          for w in xrange(y_shape[2]):
            for h in xrange(y_shape[1]):
              target = 3.0
              # We add a case for locations divisible by the stride.
              h_in = h % strides[1] == 0 and h > 0 and h < y_shape[1] - 1
              w_in = w % strides[2] == 0 and w > 0 and w < y_shape[2] - 1
              if h_in and w_in:
                target += 9.0
              elif h_in or w_in:
                target += 3.0
              self.assertAllClose(target, value[n, h, w, k])
Beispiel #2
0
  def testDeConv2DSame(self):
    with self.test_session():
      strides = [1, 2, 2, 1]

      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 12, 8, 2]

      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]

      x = constant_op.constant(1.0, shape=x_shape, name="x",
                               dtype=types.float32)
      f = constant_op.constant(1.0, shape=f_shape, name="filter",
                               dtype=types.float32)
      output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
      value = output.eval()

      for n in xrange(x_shape[0]):
        for k in xrange(f_shape[2]):
          for w in xrange(y_shape[2]):
            for h in xrange(y_shape[1]):
              target = 3.0
              # We add a case for locations divisible by the stride.
              h_in = h % strides[1] == 0 and h > 0 and h < y_shape[1] - 1
              w_in = w % strides[2] == 0 and w > 0 and w < y_shape[2] - 1
              if h_in and w_in:
                target += 9.0
              elif h_in or w_in:
                target += 3.0
              self.assertAllClose(target, value[n, h, w, k])
Beispiel #3
0
    def testDeConv2DSingleStride(self):
        with self.test_session():
            strides = [1, 1, 1, 1]

            # Input, output: [batch, height, width, depth]
            x_shape = [2, 6, 4, 3]
            y_shape = [2, 6, 4, 2]

            # Filter: [kernel_height, kernel_width, output_depth, input_depth]
            f_shape = [3, 3, 2, 3]

            x = constant_op.constant(1.0, shape=x_shape, name="x", dtype=dtypes.float32)
            f = constant_op.constant(1.0, shape=f_shape, name="filter", dtype=dtypes.float32)
            output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
            value = output.eval()

            # We count the number of cells being added at the locations in the output.
            # At the center, #cells=kernel_height * kernel_width
            # At the corners, #cells=ceil(kernel_height/2) * ceil(kernel_width/2)
            # At the borders, #cells=ceil(kernel_height/2)*kernel_width or
            #                        kernel_height * ceil(kernel_width/2)

            for n in xrange(x_shape[0]):
                for k in xrange(f_shape[2]):
                    for w in xrange(y_shape[2]):
                        for h in xrange(y_shape[1]):
                            target = 4 * 3.0
                            h_in = h > 0 and h < y_shape[1] - 1
                            w_in = w > 0 and w < y_shape[2] - 1
                            if h_in and w_in:
                                target += 5 * 3.0
                            elif h_in or w_in:
                                target += 2 * 3.0
                            self.assertAllClose(target, value[n, h, w, k])
Beispiel #4
0
    def testDeConv2DValid(self):
        with self.test_session():
            strides = [1, 2, 2, 1]

            # Input, output: [batch, height, width, depth]
            x_shape = [2, 6, 4, 3]
            y_shape = [2, 13, 9, 2]

            # Filter: [kernel_height, kernel_width, output_depth, input_depth]
            f_shape = [3, 3, 2, 3]

            x = constant_op.constant(1.0,
                                     shape=x_shape,
                                     name="x",
                                     dtype=types.float32)
            f = constant_op.constant(1.0,
                                     shape=f_shape,
                                     name="filter",
                                     dtype=types.float32)
            output = nn.deconv2d(x,
                                 f,
                                 y_shape,
                                 strides=strides,
                                 padding="VALID")
            value = output.eval()

            cache_values = np.zeros(y_shape, dtype=np.float32)

            # The amount of padding added
            pad = 1

            for n in xrange(x_shape[0]):
                for k in xrange(f_shape[2]):
                    for w in xrange(pad, y_shape[2] - pad):
                        for h in xrange(pad, y_shape[1] - pad):
                            target = 3.0
                            # We add a case for locations divisible by the stride.
                            h_in = h % strides[
                                1] == 0 and h > pad and h < y_shape[1] - 1 - pad
                            w_in = w % strides[
                                2] == 0 and w > pad and w < y_shape[2] - 1 - pad
                            if h_in and w_in:
                                target += 9.0
                            elif h_in or w_in:
                                target += 3.0
                            cache_values[n, h, w, k] = target

                    # copy values in the border
                    cache_values[n, :, 0, k] = cache_values[n, :, 1, k]
                    cache_values[n, :, -1, k] = cache_values[n, :, -2, k]
                    cache_values[n, 0, :, k] = cache_values[n, 1, :, k]
                    cache_values[n, -1, :, k] = cache_values[n, -2, :, k]

        self.assertAllClose(cache_values, value)
Beispiel #5
0
 def testGradient(self):
   x_shape = [2, 6, 4, 3]
   f_shape = [3, 3, 2, 3]
   y_shape = [2, 12, 8, 2]
   strides = [1, 2, 2, 1]
   np.random.seed(1)  # Make it reproducible.
   x_val = np.random.random_sample(x_shape).astype(np.float64)
   f_val = np.random.random_sample(f_shape).astype(np.float64)
   with self.test_session():
     x = constant_op.constant(x_val, name="x", dtype=types.float32)
     f = constant_op.constant(f_val, name="f", dtype=types.float32)
     output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
     err = gc.ComputeGradientError([x, f], [x_shape, f_shape], output, y_shape)
   print "DeConv gradient err = %g " % err
   err_tolerance = 0.0005
   self.assertLess(err, err_tolerance)
Beispiel #6
0
 def testGradient(self):
   x_shape = [2, 6, 4, 3]
   f_shape = [3, 3, 2, 3]
   y_shape = [2, 12, 8, 2]
   strides = [1, 2, 2, 1]
   np.random.seed(1)  # Make it reproducible.
   x_val = np.random.random_sample(x_shape).astype(np.float64)
   f_val = np.random.random_sample(f_shape).astype(np.float64)
   with self.test_session():
     x = constant_op.constant(x_val, name="x", dtype=types.float32)
     f = constant_op.constant(f_val, name="f", dtype=types.float32)
     output = nn.deconv2d(x, f, y_shape, strides=strides, padding="SAME")
     err = gc.ComputeGradientError([x, f], [x_shape, f_shape], output, y_shape)
   print("DeConv gradient err = %g " % err)
   err_tolerance = 0.0005
   self.assertLess(err, err_tolerance)
Beispiel #7
0
  def testDeConv2DValid(self):
    with self.test_session():
      strides = [1, 2, 2, 1]

      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 13, 9, 2]

      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]

      x = constant_op.constant(1.0, shape=x_shape, name="x",
                               dtype=types.float32)
      f = constant_op.constant(1.0, shape=f_shape, name="filter",
                               dtype=types.float32)
      output = nn.deconv2d(x, f, y_shape, strides=strides, padding="VALID")
      value = output.eval()

      cache_values = np.zeros(y_shape, dtype=np.float32)

      # The amount of padding added
      pad = 1

      for n in xrange(x_shape[0]):
        for k in xrange(f_shape[2]):
          for w in xrange(pad, y_shape[2] - pad):
            for h in xrange(pad, y_shape[1] - pad):
              target = 3.0
              # We add a case for locations divisible by the stride.
              h_in = h % strides[
                  1] == 0 and h > pad and h < y_shape[1] - 1 - pad
              w_in = w % strides[
                  2] == 0 and w > pad and w < y_shape[2] - 1 - pad
              if h_in and w_in:
                target += 9.0
              elif h_in or w_in:
                target += 3.0
              cache_values[n, h, w, k] = target

          # copy values in the border
          cache_values[n, :, 0, k] = cache_values[n, :, 1, k]
          cache_values[n, :, -1, k] = cache_values[n, :, -2, k]
          cache_values[n, 0, :, k] = cache_values[n, 1, :, k]
          cache_values[n, -1, :, k] = cache_values[n, -2, :, k]

    self.assertAllClose(cache_values, value)
Beispiel #8
0
    def testDeConv2DSingleStride(self):
        with self.test_session():
            strides = [1, 1, 1, 1]

            # Input, output: [batch, height, width, depth]
            x_shape = [2, 6, 4, 3]
            y_shape = [2, 6, 4, 2]

            # Filter: [kernel_height, kernel_width, output_depth, input_depth]
            f_shape = [3, 3, 2, 3]

            x = constant_op.constant(1.0,
                                     shape=x_shape,
                                     name="x",
                                     dtype=types.float32)
            f = constant_op.constant(1.0,
                                     shape=f_shape,
                                     name="filter",
                                     dtype=types.float32)
            output = nn.deconv2d(x,
                                 f,
                                 y_shape,
                                 strides=strides,
                                 padding="SAME")
            value = output.eval()

            # We count the number of cells being added at the locations in the output.
            # At the center, #cells=kernel_height * kernel_width
            # At the corners, #cells=ceil(kernel_height/2) * ceil(kernel_width/2)
            # At the borders, #cells=ceil(kernel_height/2)*kernel_width or
            #                        kernel_height * ceil(kernel_width/2)

            for n in xrange(x_shape[0]):
                for k in xrange(f_shape[2]):
                    for w in xrange(y_shape[2]):
                        for h in xrange(y_shape[1]):
                            target = 4 * 3.0
                            h_in = h > 0 and h < y_shape[1] - 1
                            w_in = w > 0 and w < y_shape[2] - 1
                            if h_in and w_in:
                                target += 5 * 3.0
                            elif h_in or w_in:
                                target += 2 * 3.0
                            self.assertAllClose(target, value[n, h, w, k])