def __call__(self, task_embedding, training=False, keep_prob=1.0):
        """

        :param task_images: images from a task
        :param training:
        :return:
        """
        with tf.variable_scope("TaskTransformer", reuse=self.reuse):
            # 11*11
            with tf.variable_scope('t_conv1'):
                te = tf.layers.conv2d(task_embedding, self.layer_sizes[0], [3, 3], strides=(1, 1),
                                                   padding='SAME')
                te = relu(te, name='relu')
                te = normalization(te, training=training, type='batch_norm')
                te = max_pool(te, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
            # 6*6
            with tf.variable_scope('t_conv2'):
                te = tf.layers.conv2d(te, self.layer_sizes[1], [3, 3], strides=(1, 1), padding='SAME')
                te = relu(te, name='relu')
                te = normalization(te, training=training, type='batch_norm')
                te = max_pool(te, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
            # 3*3
            with tf.variable_scope("t_conv3"):
                te = tf.layers.conv2d(te, self.layer_sizes[2], [3, 3], strides=(1, 1), padding='SAME')
                te = tf.reduce_mean(te, axis=[1, 2])

        self.reuse = True
        return te
    def __call__(self, image_embedding, task_context, training=False, keep_prob=1.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for.
        :param training: A flag indicating training or evaluation
        :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        print("task_context shape ", task_context.get_shape())
        with tf.variable_scope('Classifier', reuse=self.reuse):
            # 11*11
            with tf.variable_scope("meta_conv1"):
                m_conv1, m_conv1_w, m_conv1_b = self.meta_conv(image_embedding, task_context, self.layer_sizes[0], [3, 3], training=training)
                m_conv1 = relu(m_conv1, name='outputs')
                m_conv1 = max_pool(m_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                               padding='SAME')
            # 6*6
            with tf.variable_scope("meta_conv2"):
                m_conv2, m_conv2_w, m_conv2_b = self.meta_conv(m_conv1, task_context, self.layer_sizes[1], [3, 3], training=training)
                m_conv2 = tf.contrib.layers.flatten(m_conv2)
            print("m_conv2 ", m_conv2.get_shape())
            gen_weights_list = [m_conv1_w, m_conv1_b, m_conv2_w, m_conv2_b]


        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Classifier')
        # print_params(self.variables, name="Feature Extractor")
        return m_conv2, gen_weights_list
def GetSingleEngineGraphDef(dtype=dtypes.float32):
    """Create a graph containing single segment."""
    g = ops.Graph()
    with g.as_default():
        inp = array_ops.placeholder(dtype=dtype,
                                    shape=[None] + INPUT_DIMS[1:],
                                    name=INPUT_NAME)
        with g.device("/GPU:0"):
            conv_filter = constant_op.constant(
                [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
                name="weights",
                dtype=dtype)
            conv = nn.conv2d(input=inp,
                             filter=conv_filter,
                             strides=[1, 2, 2, 1],
                             padding="SAME",
                             name="conv")
            bias = constant_op.constant([4., 1.5, 2., 3., 5., 7.],
                                        name="bias",
                                        dtype=dtype)
            added = nn.bias_add(conv, bias, name="bias_add")
            relu = nn.relu(added, "relu")
            identity = array_ops.identity(relu, "identity")
            pool = nn_ops.max_pool(identity, [1, 2, 2, 1], [1, 2, 2, 1],
                                   "VALID",
                                   name="max_pool")
        array_ops.squeeze(pool, name=OUTPUT_NAME)
    return g.as_graph_def()
 def testDepthwiseMaxPoolInvalidConfigs(self):
   self._testDepthwiseMaxPoolInvalidConfig(
       [1, 2, 2, 4], [1, 2, 2, 2], [1, 1, 1, 2],
       "exactly one of pooling across depth")
   self._testDepthwiseMaxPoolInvalidConfig(
       [1, 2, 2, 4], [1, 1, 1, 2], [1, 1, 1, 1],
       "depth window to equal the depth stride")
   self._testDepthwiseMaxPoolInvalidConfig([1, 2, 2, 4], [1, 1, 1, 3],
                                           [1, 1, 1, 3], "evenly divide")
   if test.is_gpu_available():
     with self.test_session(use_gpu=True):
       t = constant_op.constant(1.0, shape=[1, 2, 2, 4])
       with self.assertRaisesOpError("for CPU devices"):
         nn_ops.max_pool(
             t, ksize=[1, 1, 1, 2], strides=[1, 1, 1, 2],
             padding="SAME").eval()
 def GetParams(self):
   """Single vgg layer test in TF-TRT conversion."""
   dtype = dtypes.float32
   input_name = "input"
   input_dims = [5, 8, 8, 2]
   output_name = "output"
   g = ops.Graph()
   with g.as_default():
     x = array_ops.placeholder(dtype=dtype, shape=input_dims, name=input_name)
     x, _, _ = nn_impl.fused_batch_norm(
         x, [1.0, 1.0], [0.0, 0.0],
         mean=[0.5, 0.5],
         variance=[1.0, 1.0],
         is_training=False)
     e = constant_op.constant(
         np.random.randn(1, 1, 2, 6), name="weights", dtype=dtype)
     conv = nn.conv2d(
         input=x, filter=e, strides=[1, 2, 2, 1], padding="SAME", name="conv")
     b = constant_op.constant(np.random.randn(6), name="bias", dtype=dtype)
     t = nn.bias_add(conv, b, name="biasAdd")
     relu = nn.relu(t, "relu")
     idty = array_ops.identity(relu, "ID")
     v = nn_ops.max_pool(
         idty, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")
     array_ops.squeeze(v, name=output_name)
   return trt_test.TfTrtIntegrationTestParams(
       gdef=g.as_graph_def(),
       input_names=[input_name],
       input_dims=[input_dims],
       output_names=[output_name],
       expected_output_dims=[(5, 2, 2, 6)])
Beispiel #6
0
 def GraphFn(self, x):
   dtype = x.dtype
   x, _, _ = nn_impl.fused_batch_norm(
       x, [1.0, 1.0], [0.0, 0.0],
       mean=[0.5, 0.5],
       variance=[1.0, 1.0],
       data_format="NCHW",
       is_training=False)
   e = constant_op.constant(
       np.random.randn(1, 1, 2, 6), name="weights", dtype=dtype)
   conv = nn.conv2d(
       input=x,
       filter=e,
       data_format="NCHW",
       strides=[1, 1, 2, 2],
       padding="SAME",
       name="conv")
   b = constant_op.constant(np.random.randn(6), name="bias", dtype=dtype)
   t = nn.bias_add(conv, b, data_format="NCHW", name="biasAdd")
   relu = nn.relu(t, "relu")
   idty = array_ops.identity(relu, "ID")
   v = nn_ops.max_pool(
       idty, [1, 1, 2, 2], [1, 1, 2, 2],
       "VALID",
       data_format="NCHW",
       name="max_pool")
   return array_ops.squeeze(v, name="output_0")
  def _testMaxPoolGradDirect(self, input_data, output_backprop,
                             expected_input_backprop, input_sizes, output_sizes,
                             window_rows, window_cols, row_stride, col_stride,
                             padding, use_gpu):
    with self.test_session(use_gpu=use_gpu) as sess:
      input_tensor = constant_op.constant(input_data, shape=input_sizes)
      output_tensor = nn_ops.max_pool(input_tensor,
                                      [1, window_rows, window_cols, 1],
                                      [1, row_stride, col_stride, 1], padding)
      output_backprop_tensor = constant_op.constant(
          output_backprop, shape=output_sizes)

      input_backprop_tensor = self._MaxPoolGrad(input_tensor, output_tensor,
                                                output_backprop_tensor,
                                                window_rows, window_cols,
                                                row_stride, col_stride, padding)

      actual_input_backprop = input_backprop_tensor.eval()
      self.assertShapeEqual(actual_input_backprop, input_backprop_tensor)
      actual_input_backprop = actual_input_backprop.flatten()
      actual_input_backprop = self._GetNdArray(actual_input_backprop)

      actual_output = output_tensor.eval().flatten()
      actual_output = self._GetNdArray(actual_output)

      self.assertAllClose(
          expected_input_backprop, actual_input_backprop, rtol=1e-6, atol=1e-6)
def GetSingleEngineGraphDef(dtype=dtypes.float32):
  """Create a graph containing single segment."""
  g = ops.Graph()
  with g.as_default():
    inp = array_ops.placeholder(
        dtype=dtype, shape=[None] + INPUT_DIMS[1:], name=INPUT_NAME)
    with g.device("/GPU:0"):
      conv_filter = constant_op.constant(
          [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
          name="weights",
          dtype=dtype)
      conv = nn.conv2d(
          input=inp,
          filter=conv_filter,
          strides=[1, 2, 2, 1],
          padding="SAME",
          name="conv")
      bias = constant_op.constant(
          [4., 1.5, 2., 3., 5., 7.], name="bias", dtype=dtype)
      added = nn.bias_add(conv, bias, name="bias_add")
      relu = nn.relu(added, "relu")
      identity = array_ops.identity(relu, "identity")
      pool = nn_ops.max_pool(
          identity, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")
    array_ops.squeeze(pool, name=OUTPUT_NAME)
  return g.as_graph_def()
 def testDirectNotUseOverlapping(self):
   for num_batches in [1, 3]:
     for row_window_size in [2, 5]:
       for col_window_size in [2, 4]:
         num_rows = row_window_size * 5
         num_cols = col_window_size * 7
         for num_channels in [1, 2]:
           input_shape = (num_batches, num_rows, num_cols, num_channels)
           with self.cached_session() as _:
             input_tensor = constant_op.constant(
                 self._GenerateUniqueRandomInputTensor(input_shape))
             window_size = [1, row_window_size, col_window_size, 1]
             stride_size = [1, row_window_size, col_window_size, 1]
             padding = "VALID"
             output_tensor = nn_ops.max_pool(input_tensor, window_size,
                                             stride_size, padding)
             output_data = self.evaluate(output_tensor)
             output_backprop = self._PRNG.randint(100, size=output_data.shape)
             input_backprop_tensor = gen_nn_ops.max_pool_grad(
                 input_tensor, output_tensor, output_backprop, window_size,
                 stride_size, padding)
             input_backprop = self.evaluate(input_backprop_tensor)
             row_seq = list(range(0, num_rows + 1, row_window_size))
             col_seq = list(range(0, num_cols + 1, col_window_size))
             fmp_input_backprop_tensor = gen_nn_ops.fractional_max_pool_grad(
                 input_tensor,
                 output_tensor,
                 output_backprop,
                 row_seq,
                 col_seq,
                 overlapping=False)
             fmp_input_backprop = self.evaluate(fmp_input_backprop_tensor)
             self.assertShapeEqual(input_backprop, fmp_input_backprop_tensor)
             self.assertAllClose(input_backprop, fmp_input_backprop)
Beispiel #10
0
    def __call__(self, image_input, training=False, dropout_rate=0.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
        :param training: A flag indicating training or evaluation
        :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        with tf.variable_scope(self.name, reuse=self.reuse):
            outputs = image_input
            with tf.variable_scope('conv_layers'):
                for idx, num_filters in enumerate(self.layer_sizes):
                    with tf.variable_scope('g_conv_{}'.format(idx)):
                        if idx == len(self.layer_sizes) - 1:
                            outputs = tf.layers.conv2d(outputs, num_filters, [2, 2], strides=(1, 1),
                                                       padding='VALID')
                        else:
                            outputs = tf.layers.conv2d(outputs, num_filters, [3, 3], strides=(1, 1),
                                                               padding='VALID')
                        outputs = leaky_relu(outputs)
                        outputs = tf.contrib.layers.batch_norm(outputs, updates_collections=None,
                                                                       decay=0.99,
                                                                       scale=True, center=True,
                                                                       is_training=training)
                        outputs = max_pool(outputs, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                                   padding='SAME')
                        outputs = tf.layers.dropout(outputs, rate=dropout_rate, training=training)

            image_embedding = tf.contrib.layers.flatten(outputs)


        self.reuse = tf.AUTO_REUSE
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name)
        return image_embedding
 def get_simple_graph_def(self):
   """Create a simple graph and return its graph_def."""
   g = ops.Graph()
   with g.as_default():
     a = aops.placeholder(
         dtype=dtypes.float32, shape=(None, 24, 24, 2), name="input")
     e = cop.constant(
         [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
         name="weights",
         dtype=dtypes.float32)
     conv = nn.conv2d(
         input=a,
         filter=e,
         strides=[1, 2, 2, 1],
         padding="SAME",
         name="conv")
     b = cop.constant(
         [4., 1.5, 2., 3., 5., 7.], name="bias", dtype=dtypes.float32)
     t = nn.bias_add(conv, b, name="biasAdd")
     relu = nn.relu(t, "relu")
     idty = aops.identity(relu, "ID")
     v = nn_ops.max_pool(
         idty, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")
     aops.squeeze(v, name="output")
   return g.as_graph_def()
 def _CompareMaxPoolingBk(self, input_shape, output_shape, ksize, strides,
                          padding):
   for dtype in np.float32, np.float16:
     # Generate numbers in a narrow range, so that there are many duplicates
     # in the input.
     tensor_input = np.random.random_integers(0, 3, input_shape).astype(dtype)
     tensor_output = np.random.rand(*output_shape).astype(dtype)
     with self.test_session(use_gpu=True):
       t = constant_op.constant(tensor_input, shape=input_shape)
       _, argmax_op = nn_ops.max_pool_with_argmax(t, ksize, strides, padding)
       argmax = argmax_op.eval()
       grad_in = constant_op.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad_with_argmax(t, grad_in, argmax,
                                                      ksize, strides, padding)
       gpu_val = out_op.eval()
       self.assertShapeEqual(gpu_val, out_op)
     with self.test_session(use_gpu=False):
       t = constant_op.constant(tensor_input, shape=input_shape)
       out_op = nn_ops.max_pool(t, ksize, strides, padding)
       orig_out = out_op.eval()
       grad_in = constant_op.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad(t, orig_out, grad_in, ksize, strides,
                                          padding)
       cpu_val = out_op.eval()
       self.assertShapeEqual(cpu_val, out_op)
     if dtype == np.float16:
       # The CPU version accumulates its gradient on fp16, so it's less
       # accurate than the GPU version that does the accumulation on fp32
       self.assertAllClose(cpu_val, gpu_val, rtol=0.01, atol=0.01)
     else:
       self.assertAllClose(cpu_val, gpu_val)
Beispiel #13
0
def get_simple_graph_def():
    """Create a simple graph and return its graph_def."""
    g = ops.Graph()
    with g.as_default():
        a = aops.placeholder(dtype=dtypes.float32,
                             shape=(None, 24, 24, 2),
                             name="input")
        e = cop.constant(
            [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
            name="weights",
            dtype=dtypes.float32)
        conv = nn.conv2d(input=a,
                         filter=e,
                         strides=[1, 2, 2, 1],
                         padding="SAME",
                         name="conv")
        b = cop.constant([4., 1.5, 2., 3., 5., 7.],
                         name="bias",
                         dtype=dtypes.float32)
        t = nn.bias_add(conv, b, name="biasAdd")
        relu = nn.relu(t, "relu")
        idty = aops.identity(relu, "ID")
        v = nn_ops.max_pool(idty, [1, 2, 2, 1], [1, 2, 2, 1],
                            "VALID",
                            name="max_pool")
        aops.squeeze(v, name="output")
    return g.as_graph_def()
Beispiel #14
0
  def test2DNumpy(self):
    x = np.ones([3, 6, 6, 5])
    ksize = 2
    strides = 2

    y1 = nn_ops.max_pool_v2(x, ksize, strides, "SAME")
    y2 = nn_ops.max_pool(x, ksize, strides, "SAME")

    self.assertAllEqual(self.evaluate(y1), self.evaluate(y2))
Beispiel #15
0
  def test2DNumpy(self):
    x = np.ones([3, 6, 6, 5])
    ksize = 2
    strides = 2

    y1 = nn_ops.max_pool_v2(x, ksize, strides, "SAME")
    y2 = nn_ops.max_pool(x, ksize, strides, "SAME")

    self.assertAllEqual(self.evaluate(y1), self.evaluate(y2))
Beispiel #16
0
    def __call__(self, image_input, training=False, dropout_rate=0.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
        :param training: A flag indicating training or evaluation
        :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        with tf.variable_scope(self.name, reuse=self.reuse):
            outputs = image_input  #image_input.shape: (32,28,28,1)
            with tf.variable_scope('conv_layers'):
                for idx, num_filters in enumerate(self.layer_sizes):
                    with tf.variable_scope('g_conv_{}'.format(idx)):
                        if idx == len(self.layer_sizes) - 1:
                            outputs = tf.layers.conv2d(outputs,
                                                       num_filters, [2, 2],
                                                       strides=(1, 1),
                                                       padding='VALID')
                        else:
                            outputs = tf.layers.conv2d(outputs,
                                                       num_filters, [3, 3],
                                                       strides=(1, 1),
                                                       padding='VALID')
                        outputs = leaky_relu(outputs)
                        outputs = tf.contrib.layers.batch_norm(
                            outputs,
                            updates_collections=None,
                            decay=0.99,
                            scale=True,
                            center=True,
                            is_training=training)
                        outputs = max_pool(outputs,
                                           ksize=[1, 2, 2, 1],
                                           strides=[1, 2, 2, 1],
                                           padding='SAME')
                        #outputs = tf.layers.dropout(outputs, rate=dropout_rate, training=training)

                        # # 全连接层1
                        # W_fc1 = weight_variable([64,1024])
                        # b_fc1 = bias_variable([1024])
                        # h_pool2_flat = tf.reshape(outputs, [-1,7*7*64])   #[n_samples,1,1,64]->>[n_samples,1*1*64]
                        # h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
                        # h_fc1_drop = tf.nn.dropout(h_fc1, dropout_rate) # 减少计算量dropout

                        # # 全连接层2
                        # W_fc2 = weight_variable([1024, 所有标签种类数])
                        # b_fc2 = bias_variable([所有标签种类数])
                        # prediction = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

            image_embedding = tf.contrib.layers.flatten(
                outputs)  #image_embedding: (32,64)

        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           scope=self.name)
        return image_embedding
 def _testDepthwiseMaxPoolInvalidConfig(self,
                                        in_size,
                                        ksize,
                                        strides,
                                        error_msg,
                                        use_gpu=False):
   with self.test_session(use_gpu=use_gpu) as sess:
     t = constant_op.constant(1.0, shape=in_size)
     with self.assertRaisesRegexp(errors_impl.UnimplementedError, error_msg):
       t = nn_ops.max_pool(
           t, ksize=ksize, strides=strides, padding="SAME").eval()
Beispiel #18
0
def _max_pool_2d(_input,
                 kh=2,
                 kw=2,
                 sh=2,
                 sw=2,
                 name="max_pool_2d",
                 padding='SAME'):
    return nn_ops.max_pool(_input,
                           ksize=[1, kh, kw, 1],
                           strides=[1, sh, sw, 1],
                           padding=padding,
                           name=name)
 def _CompareMaxPoolingFwd(self, input_shape, ksize, strides, padding):
   for dtype in np.float32, np.float16:
     tensor_input = np.random.rand(*input_shape).astype(dtype)
     with self.test_session(use_gpu=True):
       t = constant_op.constant(tensor_input, shape=input_shape)
       out_op, _ = nn_ops.max_pool_with_argmax(t, ksize, strides, padding)
       gpu_val = out_op.eval()
     with self.test_session(use_gpu=False):
       t = constant_op.constant(tensor_input, shape=input_shape)
       out_op = nn_ops.max_pool(t, ksize, strides, padding)
       cpu_val = out_op.eval()
     self.assertAllCloseAccordingToType(cpu_val, gpu_val)
Beispiel #20
0
    def backprop_pool(self, activation, relevance, ksize, strides, pooling_type, padding='SAME'):

        if pooling_type.lower() in 'avg':
            z = nn_ops.avg_pool(activation, ksize, strides, padding) + 1e-10
            s = relevance / z
            c = gen_nn_ops._avg_pool_grad(tf.shape(activation), s, ksize, strides, padding)
            return activation * c
        else:
            z = nn_ops.max_pool(activation, ksize, strides, padding) + 1e-10
            s = relevance / z
            c = gen_nn_ops._max_pool_grad(activation, z, s, ksize, strides, padding)
            return activation * c
Beispiel #21
0
def inference(x):

    with vs.variable_scope('all', use_resource=True):
        x = conv(x, 7, 2, 64)
        x = nn_ops.relu(x)
        x = max_pool(x, ksize=3, stride=2)
        x = block("b1", 64, 1, 2, x)
        x = nn_ops.max_pool(x, [1, x.shape[1], x.shape[2], 1], [1, 1, 1, 1],
                            'VALID')
        x = array_ops.reshape(x, [x.shape[0], x.shape[3]])
        x = fc("fc1", x, 1000)

    return x
Beispiel #22
0
 def testDirectUseOverlapping(self):
     for num_batches in [1, 3]:
         for row_window_size in [2, 5]:
             for col_window_size in [2, 4]:
                 num_rows = (row_window_size - 1) * 5 + 1
                 num_cols = (col_window_size - 1) * 7 + 1
                 for num_channels in [1, 2]:
                     input_shape = (num_batches, num_rows, num_cols,
                                    num_channels)
                     with self.test_session() as _:
                         input_tensor = constant_op.constant(
                             self._GenerateUniqueRandomInputTensor(
                                 input_shape))
                         window_size = [
                             1, row_window_size, col_window_size, 1
                         ]
                         stride_size = [
                             1, row_window_size - 1, col_window_size - 1, 1
                         ]
                         padding = "VALID"
                         output_tensor = nn_ops.max_pool(
                             input_tensor, window_size, stride_size,
                             padding)
                         output_data = output_tensor.eval()
                         output_backprop = self._PRNG.randint(
                             100, size=output_data.shape)
                         input_backprop_tensor = gen_nn_ops.max_pool_grad(
                             input_tensor, output_tensor, output_backprop,
                             window_size, stride_size, padding)
                         input_backprop = input_backprop_tensor.eval()
                         row_seq = list(
                             range(0, num_rows, row_window_size - 1))
                         col_seq = list(
                             range(0, num_cols, col_window_size - 1))
                         row_seq[-1] += 1
                         col_seq[-1] += 1
                         fmp_input_backprop_tensor = gen_nn_ops.fractional_max_pool_grad(
                             input_tensor,
                             output_tensor,
                             output_backprop,
                             row_seq,
                             col_seq,
                             overlapping=True)
                         fmp_input_backprop = fmp_input_backprop_tensor.eval(
                         )
                         self.assertShapeEqual(input_backprop,
                                               fmp_input_backprop_tensor)
                         self.assertAllClose(input_backprop,
                                             fmp_input_backprop)
def backprop_pool(activation,
                  relevance,
                  ksize,
                  strides,
                  pooling_type,
                  padding='VALID'):
    if pooling_type.lower() is 'avg':  # avg pooling
        z = nn_ops.avg_pool(activation, ksize, strides, padding) + 1e-10
        s = relevance / z
        c = gen_nn_ops._avg_pool_grad(tf.shape(activation), s, ksize, strides,
                                      padding)
        return activation * c
    else:  # max pooling
        z = nn_ops.max_pool(activation, ksize, strides, padding) + 1e-10
        s = relevance / z
        c = gen_nn_ops.max_pool_grad(activation, z, s, ksize, strides, padding)
        return activation * c
 def _conv_and_pool_1(self, inp):
     dtype = inp.dtype
     conv_filter = constant_op.constant(
         [[[[1., 0.5], [4., 6.], [0.5, 1.]]]], name="weights", dtype=dtype)
     conv = nn.conv2d(input=inp,
                      filter=conv_filter,
                      strides=[1, 2, 2, 1],
                      padding="SAME",
                      name="conv")
     bias = constant_op.constant([4., 1.5], name="bias", dtype=dtype)
     added = nn.bias_add(conv, bias, name="bias_add")
     relu = nn.relu(added, "relu")
     identity = array_ops.identity(relu, "identity")
     pool = nn_ops.max_pool(identity, [1, 2, 2, 1], [1, 2, 2, 1],
                            "VALID",
                            name="max_pool")
     return array_ops.squeeze(pool)
 def GetParams(self):
     """Single vgg layer in NCHW unit tests in TF-TRT."""
     dtype = dtypes.float32
     input_name = "input"
     input_dims = [5, 2, 8, 8]
     g = ops.Graph()
     with g.as_default():
         x = array_ops.placeholder(dtype=dtype,
                                   shape=input_dims,
                                   name=input_name)
         x, _, _ = nn_impl.fused_batch_norm(
             x,
             np.random.randn(2).astype(np.float32),
             np.random.randn(2).astype(np.float32),
             mean=np.random.randn(2).astype(np.float32),
             variance=np.random.randn(2).astype(np.float32),
             data_format="NCHW",
             is_training=False)
         e = constant_op.constant(np.random.randn(1, 1, 2, 6),
                                  name="weights",
                                  dtype=dtype)
         conv = nn.conv2d(input=x,
                          filter=e,
                          data_format="NCHW",
                          strides=[1, 1, 2, 2],
                          padding="SAME",
                          name="conv")
         b = constant_op.constant(np.random.randn(6),
                                  name="bias",
                                  dtype=dtype)
         t = nn.bias_add(conv, b, data_format="NCHW", name="biasAdd")
         relu = nn.relu(t, "relu")
         idty = array_ops.identity(relu, "ID")
         v = nn_ops.max_pool(idty, [1, 1, 2, 2], [1, 1, 2, 2],
                             "VALID",
                             data_format="NCHW",
                             name="max_pool")
         array_ops.squeeze(v, name="output")
     return trt_test.TfTrtIntegrationTestParams(gdef=g.as_graph_def(),
                                                input_names=[input_name],
                                                input_dims=[input_dims],
                                                num_expected_engines=1,
                                                expected_output_dims=(5, 6,
                                                                      2, 2),
                                                allclose_atol=1.e-03,
                                                allclose_rtol=1.e-03)
Beispiel #26
0
  def _get_plugin_graph_def(self):
    """Create a simple graph and return its graph_def."""
    g = ops.Graph()
    with g.as_default():
      a = array_ops.placeholder(
          dtype=dtypes.float32, shape=(None, 24, 24, 2), name="input")
      relu = nn.relu(a, "relu")
      v = nn_ops.max_pool(
          relu, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")

      # insert custom_op in the graph
      v = custom_plugin_examples.inc_op(v, inc=[16.5], name="plugin_test")

      v *= 2.0
      v = nn.relu(v)
      v = nn.relu(v)
      array_ops.squeeze(v, name="output")
    return g.as_graph_def()
Beispiel #27
0
 def GetParams(self):
     """Create a graph containing single segment."""
     # TODO(aaroey): test graph with different dtypes.
     dtype = dtypes.float32
     input_name = "input"
     input_dims = [100, 24, 24, 2]
     g = ops.Graph()
     with g.as_default():
         inp = array_ops.placeholder(dtype=dtype,
                                     shape=[None] + input_dims[1:],
                                     name=input_name)
         with g.device("/GPU:0"):
             conv_filter = constant_op.constant(
                 [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]
                  ],
                 name="weights",
                 dtype=dtype)
             conv = nn.conv2d(input=inp,
                              filter=conv_filter,
                              strides=[1, 2, 2, 1],
                              padding="SAME",
                              name="conv")
             bias = constant_op.constant([4., 1.5, 2., 3., 5., 7.],
                                         name="bias",
                                         dtype=dtype)
             added = nn.bias_add(conv, bias, name="bias_add")
             relu = nn.relu(added, "relu")
             identity = array_ops.identity(relu, "identity")
             pool = nn_ops.max_pool(identity, [1, 2, 2, 1], [1, 2, 2, 1],
                                    "VALID",
                                    name="max_pool")
         array_ops.squeeze(pool, name=self.output_name)
     return trt_test.TfTrtIntegrationTestParams(
         gdef=g.as_graph_def(),
         input_names=[input_name],
         input_dims=[input_dims],
         # TODO(aaroey): LayoutOptimizer adds additional nodes to the graph which
         # breaks the connection check, fix it.
         # - my_trt_op_0 should have ["weights", "conv", "bias", "bias_add",
         #   "relu", "identity", "max_pool"]
         expected_engines=["my_trt_op_0"],
         expected_output_dims=(100, 6, 6, 6),
         allclose_atol=1.e-03,
         allclose_rtol=1.e-03)
 def GetParams(self):
   """Single vgg layer in NCHW unit tests in TF-TRT."""
   dtype = dtypes.float32
   input_name = "input"
   input_dims = [5, 2, 8, 8]
   g = ops.Graph()
   with g.as_default():
     x = array_ops.placeholder(dtype=dtype, shape=input_dims, name=input_name)
     x, _, _ = nn_impl.fused_batch_norm(
         x,
         np.random.randn(2).astype(np.float32),
         np.random.randn(2).astype(np.float32),
         mean=np.random.randn(2).astype(np.float32),
         variance=np.random.randn(2).astype(np.float32),
         data_format="NCHW",
         is_training=False)
     e = constant_op.constant(
         np.random.randn(1, 1, 2, 6), name="weights", dtype=dtype)
     conv = nn.conv2d(
         input=x,
         filter=e,
         data_format="NCHW",
         strides=[1, 1, 2, 2],
         padding="SAME",
         name="conv")
     b = constant_op.constant(np.random.randn(6), name="bias", dtype=dtype)
     t = nn.bias_add(conv, b, data_format="NCHW", name="biasAdd")
     relu = nn.relu(t, "relu")
     idty = array_ops.identity(relu, "ID")
     v = nn_ops.max_pool(
         idty, [1, 1, 2, 2], [1, 1, 2, 2],
         "VALID",
         data_format="NCHW",
         name="max_pool")
     array_ops.squeeze(v, name="output")
   return trt_test.TfTrtIntegrationTestParams(
       gdef=g.as_graph_def(),
       input_names=[input_name],
       input_dims=[input_dims],
       num_expected_engines=1,
       expected_output_dims=(5, 6, 2, 2),
       allclose_atol=1.e-03,
       allclose_rtol=1.e-03)
Beispiel #29
0
 def GetParams(self):
   """Create a graph containing single segment."""
   # TODO(aaroey): test graph with different dtypes.
   dtype = dtypes.float32
   input_name = "input"
   input_dims = [100, 24, 24, 2]
   g = ops.Graph()
   with g.as_default():
     inp = array_ops.placeholder(
         dtype=dtype, shape=[None] + input_dims[1:], name=input_name)
     with g.device("/GPU:0"):
       conv_filter = constant_op.constant(
           [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
           name="weights",
           dtype=dtype)
       conv = nn.conv2d(
           input=inp,
           filter=conv_filter,
           strides=[1, 2, 2, 1],
           padding="SAME",
           name="conv")
       bias = constant_op.constant(
           [4., 1.5, 2., 3., 5., 7.], name="bias", dtype=dtype)
       added = nn.bias_add(conv, bias, name="bias_add")
       relu = nn.relu(added, "relu")
       identity = array_ops.identity(relu, "identity")
       pool = nn_ops.max_pool(
           identity, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")
     array_ops.squeeze(pool, name=self.output_name)
   return trt_test.TfTrtIntegrationTestParams(
       gdef=g.as_graph_def(),
       input_names=[input_name],
       input_dims=[input_dims],
       # TODO(aaroey): LayoutOptimizer adds additional nodes to the graph which
       # breaks the connection check, fix it.
       # - my_trt_op_0 should have ["weights", "conv", "bias", "bias_add",
       #   "relu", "identity", "max_pool"]
       expected_engines=["my_trt_op_0"],
       expected_output_dims=(100, 6, 6, 6),
       allclose_atol=1.e-03,
       allclose_rtol=1.e-03)
Beispiel #30
0
 def GetParams(self):
     """Create a graph containing single segment."""
     # TODO(aaroey): test graph with different dtypes.
     dtype = dtypes.float32
     input_name = "input"
     input_dims = [100, 24, 24, 2]
     output_name = "output"
     g = ops.Graph()
     with g.as_default():
         inp = array_ops.placeholder(dtype=dtype,
                                     shape=[None] + input_dims[1:],
                                     name=input_name)
         with g.device("/GPU:0"):
             conv_filter = constant_op.constant(
                 [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]
                  ],
                 name="weights",
                 dtype=dtype)
             conv = nn.conv2d(input=inp,
                              filter=conv_filter,
                              strides=[1, 2, 2, 1],
                              padding="SAME",
                              name="conv")
             bias = constant_op.constant([4., 1.5, 2., 3., 5., 7.],
                                         name="bias",
                                         dtype=dtype)
             added = nn.bias_add(conv, bias, name="bias_add")
             relu = nn.relu(added, "relu")
             identity = array_ops.identity(relu, "identity")
             pool = nn_ops.max_pool(identity, [1, 2, 2, 1], [1, 2, 2, 1],
                                    "VALID",
                                    name="max_pool")
         array_ops.squeeze(pool, name=output_name)
     return trt_test.TfTrtIntegrationTestParams(gdef=g.as_graph_def(),
                                                input_names=[input_name],
                                                input_dims=[input_dims],
                                                output_names=[output_name],
                                                expected_output_dims=[
                                                    (100, 6, 6, 6)
                                                ])
Beispiel #31
0
 def GraphFn(self, inp):
     """Create a graph containing single segment."""
     dtype = inp.dtype
     conv_filter = constant_op.constant(
         [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
         name="weights",
         dtype=dtype)
     conv = nn.conv2d(input=inp,
                      filter=conv_filter,
                      strides=[1, 2, 2, 1],
                      padding="SAME",
                      name="conv")
     bias = constant_op.constant([4., 1.5, 2., 3., 5., 7.],
                                 name="bias",
                                 dtype=dtype)
     added = nn.bias_add(conv, bias, name="bias_add")
     relu = nn.relu(added, "relu")
     identity = array_ops.identity(relu, "identity")
     pool = nn_ops.max_pool(identity, [1, 2, 2, 1], [1, 2, 2, 1],
                            "VALID",
                            name="max_pool")
     return array_ops.squeeze(pool, name="output_0")
 def testDirectUseOverlapping(self):
   for num_batches in [1, 3]:
     for row_window_size in [2, 5]:
       for col_window_size in [2, 4]:
         num_rows = (row_window_size - 1) * 5 + 1
         num_cols = (col_window_size - 1) * 7 + 1
         for num_channels in [1, 2]:
           input_shape = (num_batches, num_rows, num_cols, num_channels)
           with self.test_session() as _:
             input_tensor = constant_op.constant(
                 self._GenerateUniqueRandomInputTensor(input_shape))
             window_size = [1, row_window_size, col_window_size, 1]
             stride_size = [1, row_window_size - 1, col_window_size - 1, 1]
             padding = "VALID"
             output_tensor = nn_ops.max_pool(input_tensor, window_size,
                                             stride_size, padding)
             output_data = output_tensor.eval()
             output_backprop = self._PRNG.randint(100, size=output_data.shape)
             input_backprop_tensor = gen_nn_ops._max_pool_grad(input_tensor,
                                                               output_tensor,
                                                               output_backprop,
                                                               window_size,
                                                               stride_size,
                                                               padding)
             input_backprop = input_backprop_tensor.eval()
             row_seq = list(range(0, num_rows, row_window_size - 1))
             col_seq = list(range(0, num_cols, col_window_size - 1))
             row_seq[-1] += 1
             col_seq[-1] += 1
             fmp_input_backprop_tensor = gen_nn_ops._fractional_max_pool_grad(
                 input_tensor,
                 output_tensor,
                 output_backprop,
                 row_seq,
                 col_seq,
                 overlapping=True)
             fmp_input_backprop = fmp_input_backprop_tensor.eval()
             self.assertShapeEqual(input_backprop, fmp_input_backprop_tensor)
             self.assertAllClose(input_backprop, fmp_input_backprop)
Beispiel #33
0
 def GetParams(self):
   """Create a graph containing single segment."""
   # TODO(aaroey): test graph with different dtypes.
   dtype = dtypes.float32
   input_name = "input"
   input_dims = [100, 24, 24, 2]
   output_name = "output"
   g = ops.Graph()
   with g.as_default():
     inp = array_ops.placeholder(
         dtype=dtype, shape=[None] + input_dims[1:], name=input_name)
     with g.device("/GPU:0"):
       conv_filter = constant_op.constant(
           [[[[1., 0.5, 4., 6., 0.5, 1.], [1., 0.5, 1., 1., 0.5, 1.]]]],
           name="weights",
           dtype=dtype)
       conv = nn.conv2d(
           input=inp,
           filter=conv_filter,
           strides=[1, 2, 2, 1],
           padding="SAME",
           name="conv")
       bias = constant_op.constant([4., 1.5, 2., 3., 5., 7.],
                                   name="bias",
                                   dtype=dtype)
       added = nn.bias_add(conv, bias, name="bias_add")
       relu = nn.relu(added, "relu")
       identity = array_ops.identity(relu, "identity")
       pool = nn_ops.max_pool(
           identity, [1, 2, 2, 1], [1, 2, 2, 1], "VALID", name="max_pool")
     array_ops.squeeze(pool, name=output_name)
   return trt_test.TfTrtIntegrationTestParams(
       gdef=g.as_graph_def(),
       input_names=[input_name],
       input_dims=[input_dims],
       output_names=[output_name],
       expected_output_dims=[(100, 6, 6, 6)])
Beispiel #34
0
def test_maxpool2d():
    ''' Run tests on the Wave custom maxpool2d operator.
    '''
    tf.reset_default_graph()
    # Turn off graph-rewriting optimizations
    config = tf.ConfigProto(graph_options=tf.GraphOptions(
        optimizer_options=tf.OptimizerOptions(
            opt_level=tf.OptimizerOptions.L0)))

    iterations = 100

    for i in range(iterations):
        tf.reset_default_graph()

        # NCHW
        t_n = 1
        t_h = 64
        t_w = 64
        t_c = 1

        # window
        w_n = 1
        w_h = 2
        w_w = 2
        w_c = 1

        #strides
        s_n = 1
        s_h = 2
        s_w = 2
        s_c = 1

        # N H W C
        max_in_tmp = tf.get_variable(
            "a", [t_n, t_h, t_w, t_c],
            dtype=tf.float32,
            initializer=tf.truncated_normal_initializer(stddev=0.1))
        max_in = waveflow.wavecomp_ops_module.wave_quantize_dfx(
            max_in_tmp, [16, 14])

        t_init = tf.global_variables_initializer()

        # SAME variant
        with tf.Session('', config=config) as sess:
            t_init.run()

            z1_op = nn_ops.max_pool(max_in,
                                    ksize=[w_n, w_h, w_w, w_c],
                                    strides=[s_n, s_h, s_w, s_c],
                                    padding='SAME',
                                    data_format='NHWC')

            z2_grad = tf.gradients(z1_op, max_in)[0]

            z3_op = waveflow.wavecomp_ops_module.wave_max_pool_dfx(
                max_in,
                ksize=[w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='SAME',
                data_format='NHWC')

            z4_grad = tf.gradients(z3_op, max_in)[0]

            z1, z2, z3, z4 = sess.run([z1_op, z2_grad, z3_op, z4_grad])

            assert_str = "Failure on i: %d, mode: SAME" % (i)
            if not compare_tensor(z2, z4, assert_str):
                print("z2: shape: %s, %s" % (z2.shape, z2))
                print("z4 (np): shape: %s, %s" % (z4.shape, z4))
                print("\n\n")
                assert False

        # # Valid variant
        with tf.Session('', config=config) as sess:
            t_init.run()

            z1_op = nn_ops.max_pool(max_in,
                                    ksize=[w_n, w_h, w_w, w_c],
                                    strides=[s_n, s_h, s_w, s_c],
                                    padding='VALID',
                                    data_format='NHWC')

            z2_grad = tf.gradients(z1_op, max_in)[0]

            z3_op = waveflow.wavecomp_ops_module.wave_max_pool_dfx(
                max_in,
                ksize=[w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='VALID',
                data_format='NHWC')

            z4_grad = tf.gradients(z3_op, max_in)[0]

            z1, z2, z3, z4 = sess.run([z1_op, z2_grad, z3_op, z4_grad])

            assert_str = "Failure on i: %d, mode: VALID" % (i)
            if not compare_tensor(z2, z4, assert_str):
                print("z2: shape: %s, %s" % (z2.shape, z2))
                print("z4 (np): shape: %s, %s" % (z4.shape, z4))
                print("\n\n")
                assert False

    return True
Beispiel #35
0
def test_maxpool2d():
    ''' Run tests on the Wave custom maxpool2d operator.
    '''
    tf.reset_default_graph()
    # Turn off graph-rewriting optimizations
    config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options=tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0)))

    iterations = 100

    for i in range(iterations):
        tf.reset_default_graph()

        # NCHW
        t_n = 1
        t_h = 64
        t_w = 64
        t_c = 1

        # window
        w_n = 1
        w_h = 2
        w_w = 2
        w_c = 1

        #strides
        s_n = 1
        s_h = 2
        s_w = 2
        s_c = 1

        # N H W C
        max_in = tf.get_variable("a", [t_n, t_h, t_w, t_c], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1))

        t_init = tf.global_variables_initializer()

        # SAME variant
        with tf.Session('', config=config) as sess:
            t_init.run()

            # print("Wave Kernel:\n-------------------------------------------------")

            z_op = waveflow.wavecomp_ops_module.wave_max_pool_dfx(
                max_in,
                ksize=[w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='SAME',
                data_format='NHWC')

            # Base tensorflow. Only supports NHWC.
            z2_op = nn_ops.max_pool(
                max_in,
                ksize=[w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='SAME',
                data_format='NHWC')

            # z = z_op.eval()
            # z2 = z2_op.eval()
            z, z2 = sess.run([z_op, z2_op])

            # print("\nTF:\n-------------------------------------------------")

            assert_str = "Failure on i: %d, mode: SAME" % (i)
            if not compare_tensor(z, z2, assert_str):
                print("z: shape: %s, %s" % (z.shape, z))
                print("z (np): shape: %s, %s" % (z2.shape, z2))
                print("\n\n")
                assert False

        # Valid variant
        with tf.Session('', config=config) as sess:
            t_init.run()

            # print("Wave Kernel:\n-------------------------------------------------")

            z_op = waveflow.wavecomp_ops_module.wave_max_pool_dfx(
                max_in,
                ksize=[w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='VALID',
                data_format='NHWC')

            # Base tensorflow. Only supports NHWC.
            z2_op = nn_ops.max_pool(
                max_in,
                ksize= [w_n, w_h, w_w, w_c],
                strides=[s_n, s_h, s_w, s_c],
                padding='VALID',
                data_format='NHWC')


            z, z2 = sess.run([z_op, z2_op])
            # print("\nTF:\n-------------------------------------------------")

            assert_str = "Failure on i: %d, mode: VALID" % (i)
            if not compare_tensor(z, z2, assert_str):
                print("z: shape: %s, %s" % (z.shape, z))
                print("z (np): shape: %s, %s" % (z2.shape, z2))
                print("\n\n")
                assert False

    return True
Beispiel #36
0
    def __call__(self, image_input, training=False, keep_prob=1.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
        :param training: A flag indicating training or evaluation
        :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """

        with tf.variable_scope('g'):
            if self.reuse:
                tf.get_variable_scope().reuse_variables()

            with tf.variable_scope('conv_layers'):
                input_channel = image_input.get_shape().as_list()[-1]
                w0 = tf.get_variable(
                    'w0', [3, 3, input_channel, self.layer_sizes[0]])
                w1 = tf.get_variable(
                    'w1', [3, 3, self.layer_sizes[0], self.layer_sizes[1]])
                w2 = tf.get_variable(
                    'w2', [3, 3, self.layer_sizes[1], self.layer_sizes[2]])
                w3 = tf.get_variable(
                    'w3', [2, 2, self.layer_sizes[2], self.layer_sizes[3]])
                with tf.variable_scope('g_conv1'):
                    g_conv1_encoder = tf.nn.conv2d(image_input,
                                                   w0,
                                                   strides=[1, 1, 1, 1],
                                                   padding='VALID')
                    g_conv1_encoder = tf.nn.relu(g_conv1_encoder,
                                                 name='outputs')
                    g_conv1_encoder = tf.contrib.layers.batch_norm(
                        g_conv1_encoder, is_training=training)
                    g_conv1_encoder = max_pool(g_conv1_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv1_encoder = tf.nn.dropout(g_conv1_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv2'):
                    g_conv2_encoder = tf.nn.conv2d(g_conv1_encoder,
                                                   w1,
                                                   strides=[1, 1, 1, 1],
                                                   padding='VALID')
                    g_conv2_encoder = tf.nn.relu(g_conv2_encoder,
                                                 name='outputs')
                    g_conv2_encoder = tf.contrib.layers.batch_norm(
                        g_conv2_encoder, is_training=training, reuse=None)
                    g_conv2_encoder = max_pool(g_conv2_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv2_encoder = tf.nn.dropout(g_conv2_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv3'):
                    g_conv3_encoder = tf.nn.conv2d(g_conv2_encoder,
                                                   w2,
                                                   strides=[1, 1, 1, 1],
                                                   padding='VALID')
                    g_conv3_encoder = tf.nn.relu(g_conv3_encoder,
                                                 name='outputs')
                    g_conv3_encoder = tf.contrib.layers.batch_norm(
                        g_conv3_encoder, is_training=training, reuse=None)

                    g_conv3_encoder = max_pool(g_conv3_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv3_encoder = tf.nn.dropout(g_conv3_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv4'):
                    g_conv4_encoder = tf.nn.conv2d(g_conv3_encoder,
                                                   w3,
                                                   strides=[1, 1, 1, 1],
                                                   padding='VALID')
                    g_conv4_encoder = tf.nn.relu(g_conv4_encoder,
                                                 name='outputs')
                    g_conv4_encoder = tf.contrib.layers.batch_norm(
                        g_conv4_encoder, is_training=training, reuse=None)
                    g_conv4_encoder = max_pool(g_conv4_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv4_encoder = tf.nn.dropout(g_conv4_encoder,
                                                    keep_prob=keep_prob)

            g_conv_encoder = g_conv4_encoder
            g_conv_encoder = tf.contrib.layers.flatten(g_conv_encoder)

        #
        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           scope='g')
        return g_conv_encoder
Beispiel #37
0
def max_pool(x, ksize=3, stride=2):
    return nn_ops.max_pool(x,
                           ksize=[1, ksize, ksize, 1],
                           strides=[1, stride, stride, 1],
                           padding='SAME')
Beispiel #38
0
    def __call__(self, image_input, training=False, keep_prob=1.0):
        """
        this module use to implement relstion module
        """
        def leaky_relu(x, leak=0.2, name=''):
            return tf.maximum(x, x * leak, name=name)

        with tf.variable_scope('RelationModule', reuse=self.reuse):
            with tf.variable_scope('conv_layers'):

                with tf.variable_scope('RelationModule_conv1'):
                    # pdb.set_trace()
                    g_conv1_encoder = tf.layers.conv2d(image_input,
                                                       64, [3, 3],
                                                       strides=(1, 1),
                                                       padding='SAME')
                    g_conv1_encoder = leaky_relu(g_conv1_encoder,
                                                 name='outputs')
                    g_conv1_encoder = tf.contrib.layers.batch_norm(
                        g_conv1_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv1_encoder = max_pool(g_conv1_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv1_encoder = tf.nn.dropout(g_conv1_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('RelationModule_conv2'):
                    g_conv2_encoder = tf.layers.conv2d(g_conv1_encoder,
                                                       64, [3, 3],
                                                       strides=(1, 1),
                                                       padding='SAME')
                    g_conv2_encoder = leaky_relu(g_conv2_encoder,
                                                 name='outputs')
                    g_conv2_encoder = tf.contrib.layers.batch_norm(
                        g_conv2_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv2_encoder = max_pool(g_conv2_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv2_encoder = tf.nn.dropout(g_conv2_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('fully_connected_relu'):
                    # pdb.set_trace()
                    g_fc1_encoder = tf.contrib.layers.flatten(g_conv2_encoder)
                    g_fc1_encoder = tf.contrib.layers.fully_connected(
                        g_fc1_encoder, 8, trainable=True, scope='fc_relu')

                with tf.variable_scope('fully_connected_sigmoid'):
                    g_fc2_encoder = tf.contrib.layers.fully_connected(
                        g_fc1_encoder,
                        1,
                        activation_fn=tf.nn.sigmoid,
                        trainable=True,
                        scope='fc_sigmoid')

            g_conv_encoder = g_fc2_encoder
            # need to concatenate feature map
            # g_conv_encoder = tf.contrib.layers.flatten(g_conv_encoder)

        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           scope='RelationModule')
        return g_conv_encoder
Beispiel #39
0
def atrous_pool2d(value, ksize, rate, padding, name=None, pooling_type="MAX"):
    with ops.name_scope(name, "atrous_pool2d", [value]) as name:
        value = ops.convert_to_tensor(value, name="value")
        if rate < 1:
            raise ValueError("rate {} cannot be less than one".format(rate))

        if rate == 1:
            if pooling_type == "MAX":
                value = nn_ops.max_pool(value=value,
                                        ksize=ksize,
                                        strides=[1, 1, 1, 1],
                                        padding=padding)
                return value
            elif pooling_type == "AVG":
                value = nn_ops.avg_pool(value=value,
                                        ksize=ksize,
                                        strides=[1, 1, 1, 1],
                                        padding=padding)
                return value
            else:
                raise ValueError("Invalid pooling type")

        # We have two padding contributions. The first is used for converting "SAME"
        # to "VALID". The second is required so that the height and width of the
        # zero-padded value tensor are multiples of rate.

        # Padding required to reduce to "VALID" convolution
        if padding == "SAME":
            # Handle filters whose shape is unknown during graph creation.
            # if filters.get_shape().is_fully_defined():
            #   filter_shape = filters.get_shape().as_list()
            # else:
            #   filter_shape = array_ops.shape(filters)
            # filter_height, filter_width = filter_shape[0], filter_shape[1]
            kernel_height, kernel_width = ksize[1], ksize[2]

            # Spatial dimensions of the filters and the upsampled filters in which we
            # introduce (rate - 1) zeros between consecutive filter values.
            kernel_height_up = kernel_height + (kernel_height - 1) * (rate - 1)
            kernel_width_up = kernel_width + (kernel_width - 1) * (rate - 1)

            pad_height = kernel_height_up - 1
            pad_width = kernel_width_up - 1

            # When pad_height (pad_width) is odd, we pad more to bottom (right),
            # following the same convention as conv2d().
            pad_top = pad_height // 2
            pad_bottom = pad_height - pad_top
            pad_left = pad_width // 2
            pad_right = pad_width - pad_left
        elif padding == "VALID":
            pad_top = 0
            pad_bottom = 0
            pad_left = 0
            pad_right = 0
        else:
            raise ValueError("Invalid padding")

        # Handle input whose shape is unknown during graph creation.
        if value.get_shape().is_fully_defined():
            value_shape = value.get_shape().as_list()
        else:
            value_shape = array_ops.shape(value)

        in_height = value_shape[1] + pad_top + pad_bottom
        in_width = value_shape[2] + pad_left + pad_right

        # More padding so that rate divides the height and width of the input.
        pad_bottom_extra = (rate - in_height % rate) % rate
        pad_right_extra = (rate - in_width % rate) % rate

        # The paddings argument to space_to_batch includes both padding components.
        space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra],
                              [pad_left, pad_right + pad_right_extra]]

        value = array_ops.space_to_batch(input=value,
                                         paddings=space_to_batch_pad,
                                         block_size=rate)
        if pooling_type == "MAX":
            value = nn_ops.max_pool(value=value,
                                    ksize=ksize,
                                    strides=[1, 1, 1, 1],
                                    padding="VALID",
                                    name=name)

        elif pooling_type == "AVG":
            value = nn_ops.avg_pool(value=value,
                                    ksize=ksize,
                                    strides=[1, 1, 1, 1],
                                    padding="VALID",
                                    name=name)
        else:
            raise ValueError("Invalid pooling type")

        # The crops argument to batch_to_space is just the extra padding component.
        batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]]

        value = array_ops.batch_to_space(input=value,
                                         crops=batch_to_space_crop,
                                         block_size=rate)

        return value
    def __call__(self, support_target_images, training=False, keep_prob=1.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
        :param training: A flag indicating training or evaluation
        :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        [bs, kn, w, h, c] = support_target_images.get_shape().as_list()
        support_target_images = tf.reshape(support_target_images, shape=[bs*kn, w, h, c])
        with tf.variable_scope('extractor', reuse=self.reuse):

            with tf.variable_scope('conv_layers'):
                # 84*84
                with tf.variable_scope('g_conv1'):
                    g_conv1_encoder = tf.layers.conv2d(support_target_images, self.layer_sizes[0], [3, 3], strides=(1, 1),
                                                       padding='SAME')
                    g_conv1_encoder = tf.contrib.layers.batch_norm(g_conv1_encoder, updates_collections=None, decay=0.99,
                                                                   scale=True, center=True, is_training=training)

                    g_conv1_encoder = relu(g_conv1_encoder, name='outputs')
                    g_conv1_encoder = max_pool(g_conv1_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv1_encoder = tf.nn.dropout(g_conv1_encoder, keep_prob=keep_prob)
                # 42*42
                with tf.variable_scope('g_conv2'):
                    g_conv2_encoder = tf.layers.conv2d(g_conv1_encoder, self.layer_sizes[1], [3, 3], strides=(1, 1),
                                                       padding='SAME')
                    g_conv2_encoder = tf.contrib.layers.batch_norm(g_conv2_encoder, updates_collections=None, decay=0.99,
                                                                   scale=True, center=True, is_training=training)

                    g_conv2_encoder = relu(g_conv2_encoder, name='outputs')
                    g_conv2_encoder = max_pool(g_conv2_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')

                # 21*21
                with tf.variable_scope('g_conv3'):
                    g_conv3_encoder = tf.layers.conv2d(g_conv2_encoder, self.layer_sizes[2], [3, 3], strides=(1, 1),
                                                       padding='SAME')
                    g_conv3_encoder = tf.contrib.layers.batch_norm(g_conv3_encoder, updates_collections=None, decay=0.99,
                                                                   scale=True, center=True, is_training=training)

                    g_conv3_encoder = relu(g_conv3_encoder, name='outputs')
                    g_conv3_encoder = max_pool(g_conv3_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                # 11*11
                with tf.variable_scope('g_conv4'):
                    g_conv4_encoder = tf.layers.conv2d(g_conv3_encoder, self.layer_sizes[3], [3, 3], strides=(1, 1),
                                                       padding='SAME')
                    g_conv4_encoder = tf.contrib.layers.batch_norm(g_conv4_encoder, updates_collections=None, decay=0.99,
                                                                   scale=True, center=True, is_training=training)
                    g_conv4_encoder = relu(g_conv4_encoder, name='outputs') # ?


        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='conv_layers')
        [bskn, we, he, ce] = g_conv4_encoder.get_shape().as_list()
        embeddings = tf.reshape(g_conv4_encoder, [bs, kn, we, he, ce])

        # print_params(self.variables, name="Feature Extractor")
        return embeddings
def atrous_pool2d(value,ksize, rate, padding, name=None, pooling_type="MAX"):
  with ops.name_scope(name, "atrous_pool2d", [value]) as name:
    value = ops.convert_to_tensor(value, name="value")
    if rate < 1:
      raise ValueError("rate {} cannot be less than one".format(rate))

    if rate == 1:
      if pooling_type == "MAX":
        value = nn_ops.max_pool(value=value,
                                ksize=ksize,
                                strides=[1, 1, 1, 1],
                                padding=padding)
        return value
      elif pooling_type == "AVG":
        value = nn_ops.avg_pool(value=value,
                                ksize=ksize,
                                strides=[1, 1, 1, 1],
                                padding=padding)
        return value
      else:
        raise ValueError("Invalid pooling type")


    # We have two padding contributions. The first is used for converting "SAME"
    # to "VALID". The second is required so that the height and width of the
    # zero-padded value tensor are multiples of rate.

    # Padding required to reduce to "VALID" convolution
    if padding == "SAME":
      # Handle filters whose shape is unknown during graph creation.
      # if filters.get_shape().is_fully_defined():
      #   filter_shape = filters.get_shape().as_list()
      # else:
      #   filter_shape = array_ops.shape(filters)
      # filter_height, filter_width = filter_shape[0], filter_shape[1]
      kernel_height, kernel_width = ksize[1], ksize[2]


      # Spatial dimensions of the filters and the upsampled filters in which we
      # introduce (rate - 1) zeros between consecutive filter values.
      kernel_height_up = kernel_height + (kernel_height - 1) * (rate - 1)
      kernel_width_up = kernel_width + (kernel_width - 1) * (rate - 1)

      pad_height = kernel_height_up - 1
      pad_width = kernel_width_up - 1

      # When pad_height (pad_width) is odd, we pad more to bottom (right),
      # following the same convention as conv2d().
      pad_top = pad_height // 2
      pad_bottom = pad_height - pad_top
      pad_left = pad_width // 2
      pad_right = pad_width - pad_left
    elif padding == "VALID":
      pad_top = 0
      pad_bottom = 0
      pad_left = 0
      pad_right = 0
    else:
      raise ValueError("Invalid padding")

    # Handle input whose shape is unknown during graph creation.
    if value.get_shape().is_fully_defined():
      value_shape = value.get_shape().as_list()
    else:
      value_shape = array_ops.shape(value)

    in_height = value_shape[1] + pad_top + pad_bottom
    in_width = value_shape[2] + pad_left + pad_right

    # More padding so that rate divides the height and width of the input.
    pad_bottom_extra = (rate - in_height % rate) % rate
    pad_right_extra = (rate - in_width % rate) % rate

    # The paddings argument to space_to_batch includes both padding components.
    space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra],
                          [pad_left, pad_right + pad_right_extra]]

    value = array_ops.space_to_batch(input=value,
                                     paddings=space_to_batch_pad,
                                     block_size=rate)
    if pooling_type == "MAX":
      value = nn_ops.max_pool(value=value,
                                ksize=ksize,
                                strides=[1, 1, 1, 1],
                                padding="VALID",
                                name=name)

    elif pooling_type == "AVG":
      value = nn_ops.avg_pool(value=value,
                                ksize=ksize,
                                strides=[1, 1, 1, 1],
                                padding="VALID",
                                name=name)
    else:
      raise ValueError("Invalid pooling type")

    # The crops argument to batch_to_space is just the extra padding component.
    batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]]

    value = array_ops.batch_to_space(input=value,
                                     crops=batch_to_space_crop,
                                     block_size=rate)

    return value
Beispiel #42
0
    def __call__(self, image_input, training=False, keep_prob=1.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 320, 480, 1]
        :param training: A flag indicating training or evaluation
        :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        
        def leaky_relu(x, leak=0.2, name=''):
            return tf.maximum(x, x * leak, name=name)

        with tf.variable_scope('g', reuse=self.reuse):

            with tf.variable_scope('conv_layers'):
                with tf.variable_scope('g_conv1'):
                    self.g_conv1_encoder = tf.layers.conv2d(image_input, self.layer_sizes[0], [3, 3], strides=(1, 1),
                                                       padding='VALID')
                    self.g_conv1_encoder = leaky_relu(self.g_conv1_encoder, name='outputs')
                    self.g_conv1_encoder = tf.contrib.layers.batch_norm(self.g_conv1_encoder, updates_collections=None, decay=0.99,
                                                                   scale=True, center=True, is_training=training)
                    self.g_conv1_encoder = max_pool(self.g_conv1_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                    self.g_conv1_encoder = tf.nn.dropout(self.g_conv1_encoder, keep_prob=keep_prob)

                with tf.variable_scope('g_conv2'):
                    self.g_conv2_encoder = tf.layers.conv2d(self.g_conv1_encoder, self.layer_sizes[1], [3, 3], strides=(1, 1),
                                                       padding='VALID')
                    self.g_conv2_encoder = leaky_relu(self.g_conv2_encoder, name='outputs')
                    self.g_conv2_encoder = tf.contrib.layers.batch_norm(self.g_conv2_encoder, updates_collections=None,
                                                                   decay=0.99,
                                                                   scale=True, center=True, is_training=training)
                    self.g_conv2_encoder = max_pool(self.g_conv2_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                    self.g_conv2_encoder = tf.nn.dropout(self.g_conv2_encoder, keep_prob=keep_prob)

                with tf.variable_scope('g_conv3'):
                    self.g_conv3_encoder = tf.layers.conv2d(self.g_conv2_encoder, self.layer_sizes[2], [3, 3], strides=(2, 2),
                                                       padding='VALID')
                    self.g_conv3_encoder = leaky_relu(self.g_conv3_encoder, name='outputs')
                    self.g_conv3_encoder = tf.contrib.layers.batch_norm(self.g_conv3_encoder, updates_collections=None,
                                                                   decay=0.99,
                                                                   scale=True, center=True, is_training=training)
                    self.g_conv3_encoder = max_pool(self.g_conv3_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                    self.g_conv3_encoder = tf.nn.dropout(self.g_conv3_encoder, keep_prob=keep_prob)

                with tf.variable_scope('g_conv4'):
                    self.g_conv4_encoder = tf.layers.conv2d(self.g_conv3_encoder, self.layer_sizes[3], [3, 3], strides=(2, 2),
                                                       padding='VALID')
                    self.g_conv4_encoder = leaky_relu(self.g_conv4_encoder, name='outputs')
                    self.g_conv4_encoder = tf.contrib.layers.batch_norm(self.g_conv4_encoder, updates_collections=None,
                                                                   decay=0.99,
                                                                   scale=True, center=True, is_training=training)
                    self.g_conv4_encoder = max_pool(self.g_conv4_encoder, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                                               padding='SAME')
                    self.g_conv4_encoder = tf.nn.dropout(self.g_conv4_encoder, keep_prob=keep_prob)
            
                conv4_oneimage = tf.expand_dims(tf.maximum(self.g_conv4_encoder[0,], 0), 0) # summary_image request [batch_size, height, width, nchannels]
                conv4_max = tf.reduce_max(conv4_oneimage)
                conv4_oneimage = tf.reduce_mean(conv4_oneimage, 3, keep_dims = True) / conv4_max * 255
                tf.summary.image('conv4%d' % 0, conv4_oneimage)
                image_oneimage = tf.expand_dims(image_input[0], 0)
                tf.summary.image('image%d' % 0, image_oneimage)
                
            self.g_conv_encoder = self.g_conv4_encoder
#            g_conv_encoder = tf.contrib.layers.flatten(g_conv_encoder)

        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='g')
        
        self.layers['g_conv1'] = self.g_conv1_encoder
        self.layers['g_conv2'] = self.g_conv2_encoder
        self.layers['g_conv3'] = self.g_conv3_encoder
        self.layers['g_conv4'] = self.g_conv4_encoder
        
        return self.g_conv_encoder
Beispiel #43
0
    def __call__(self, image_input, training=False, keep_prob=1.0):
        """
        Runs the CNN producing the embeddings and the gradients.
        :param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
        :param training: A flag indicating training or evaluation
        :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied
        :return: Embeddings of size [batch_size, 64]
        """
        def leaky_relu(x, leak=0.2, name=''):
            return tf.maximum(x, x * leak, name=name)

        with tf.variable_scope('g', reuse=self.reuse):

            with tf.variable_scope('conv_layers'):
                with tf.variable_scope('g_conv1'):
                    g_conv1_encoder = tf.layers.conv2d(image_input,
                                                       self.layer_sizes[0],
                                                       [3, 3],
                                                       strides=(1, 1),
                                                       padding='VALID')
                    g_conv1_encoder = leaky_relu(g_conv1_encoder,
                                                 name='outputs')
                    g_conv1_encoder = tf.contrib.layers.batch_norm(
                        g_conv1_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv1_encoder = max_pool(g_conv1_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv1_encoder = tf.nn.dropout(g_conv1_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv2'):
                    g_conv2_encoder = tf.layers.conv2d(g_conv1_encoder,
                                                       self.layer_sizes[1],
                                                       [3, 3],
                                                       strides=(1, 1),
                                                       padding='VALID')
                    g_conv2_encoder = leaky_relu(g_conv2_encoder,
                                                 name='outputs')
                    g_conv2_encoder = tf.contrib.layers.batch_norm(
                        g_conv2_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv2_encoder = max_pool(g_conv2_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv2_encoder = tf.nn.dropout(g_conv2_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv3'):
                    g_conv3_encoder = tf.layers.conv2d(g_conv2_encoder,
                                                       self.layer_sizes[2],
                                                       [3, 3],
                                                       strides=(1, 1),
                                                       padding='VALID')
                    g_conv3_encoder = leaky_relu(g_conv3_encoder,
                                                 name='outputs')
                    g_conv3_encoder = tf.contrib.layers.batch_norm(
                        g_conv3_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv3_encoder = max_pool(g_conv3_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv3_encoder = tf.nn.dropout(g_conv3_encoder,
                                                    keep_prob=keep_prob)

                with tf.variable_scope('g_conv4'):
                    g_conv4_encoder = tf.layers.conv2d(g_conv3_encoder,
                                                       self.layer_sizes[3],
                                                       [2, 2],
                                                       strides=(1, 1),
                                                       padding='VALID')
                    g_conv4_encoder = leaky_relu(g_conv4_encoder,
                                                 name='outputs')
                    g_conv4_encoder = tf.contrib.layers.batch_norm(
                        g_conv4_encoder,
                        updates_collections=None,
                        decay=0.99,
                        scale=True,
                        center=True,
                        is_training=training)
                    g_conv4_encoder = max_pool(g_conv4_encoder,
                                               ksize=[1, 2, 2, 1],
                                               strides=[1, 2, 2, 1],
                                               padding='SAME')
                    g_conv4_encoder = tf.nn.dropout(g_conv4_encoder,
                                                    keep_prob=keep_prob)

            g_conv_encoder = g_conv4_encoder
            g_conv_encoder = tf.contrib.layers.flatten(g_conv_encoder)

        self.reuse = True
        self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           scope='g')
        return g_conv_encoder