예제 #1
0
def encoder(inputs, z_dim):
    # encoder ()
    # 10 x 32 x 32 x 32 x 1   -> 10 x 16 x 16 x 16 x 32
    # 10 x 16 x 16 x 16 x 32  ->  10 x 8 x 8 x 8 x 16
    # 10 x 8 x 8 x 8 x 16    ->  10 x 4 x 4 x 4 x 8
    # 10 x 4 x 4 x 4 x 8  -> z dim
    net = lays.conv3d(inputs,
                      32, [4, 4, 4],
                      stride=2,
                      padding='SAME',
                      trainable=True)  #[16,16,16,32]
    net = lays.batch_norm(net, decay=0.999)
    net = lays.conv3d(net,
                      16, [4, 4, 4],
                      stride=2,
                      padding='SAME',
                      trainable=True)  #[8,8,8,16]
    net = lays.batch_norm(net, decay=0.999)
    net = lays.conv3d(net,
                      8, [4, 4, 4],
                      stride=2,
                      padding='SAME',
                      trainable=True)  #[4,4,4,8]
    net = lays.batch_norm(net, decay=0.999)

    net = lays.flatten(net)
    net = tf.layers.dense(net, units=z_dim, activation=tf.nn.relu)

    #net = tf.layers.dense(net, 4, activation=tf.nn.relu)
    #net = tf.reshape(net, [512])
    print("this is net", net)
    print("this is shape", tf.shape(net))

    return net
예제 #2
0
    def get_q_values_op(self, state, scope, reuse=False):
        """
        Returns Q values for all actions

        Args:
            state: (tf tensor) 
                shape = (batch_size, img height, img width, nchannels)
            scope: (string) scope name, that specifies if target network or not
            reuse: (bool) reuse of variables in the scope

        Returns:
            out: (tf tensor) of shape = (batch_size, num_actions)
        """
        
        num_actions = self.env.action_space.n
        out = state

        # compress the student network
        size1, size2, size3, size4 = (16, 16, 16, 128) if self.student else (32, 64, 64, 512)

        with tf.variable_scope(scope, reuse=reuse):
            conv1 = layers.conv3d(inputs=out, num_outputs=size1, kernel_size=[8,8], stride=4) #20
            conv2 = layers.conv3d(inputs=conv1, num_outputs=size2, kernel_size=[4,4], stride=2) #10
            conv3 = layers.conv3d(inputs=conv2, num_outputs=size3, kernel_size=[3,3], stride=1) #10
            hidden = layers.fully_connected(layers.flatten(conv3), size4)
            out = layers.fully_connected(hidden, num_actions, activation_fn=None)

        return out
예제 #3
0
    def _3d_dnn_level_60(self, inputs):
        with tf.variable_scope('Volume60', reuse=self._reuse):
            conv = layers.conv3d(inputs, 64, 3, scope='conv1', **self._reg)
            conv = layers.conv3d(conv,
                                 64,
                                 3,
                                 activation_fn=None,
                                 scope='conv2',
                                 **self._reg)
            shortcut = layers.conv3d(inputs,
                                     64,
                                     1,
                                     activation_fn=None,
                                     scope='shortcut1',
                                     **self._reg)
            res1 = tf.nn.relu(conv + shortcut)

            conv = layers.conv3d(res1, 64, 3, scope='conv3', **self._reg)
            conv = layers.conv3d(conv,
                                 64,
                                 3,
                                 activation_fn=None,
                                 scope='conv4',
                                 **self._reg)
            res2 = tf.nn.relu(conv + res1)

            conv = layers.conv3d(res2,
                                 64,
                                 3,
                                 rate=2,
                                 scope='conv5',
                                 **self._reg)
            conv = layers.conv3d(conv,
                                 64,
                                 3,
                                 activation_fn=None,
                                 rate=2,
                                 scope='conv6',
                                 **self._reg)
            res3 = tf.nn.relu(conv + res2)

            conv = layers.conv3d(res3,
                                 64,
                                 3,
                                 rate=2,
                                 scope='conv7',
                                 **self._reg)
            conv = layers.conv3d(conv,
                                 64,
                                 3,
                                 activation_fn=None,
                                 rate=2,
                                 scope='conv8',
                                 **self._reg)
            res4 = tf.nn.relu(conv + res3)

            concat = tf.concat([res1, res2, res3, res4],
                               axis=-1,
                               name='concat')
            return tf.nn.relu(concat)
예제 #4
0
 def _3d_dnn(self, inputs):
     with tf.variable_scope('DownSample3D', reuse=self._reuse):
         feat_3d = layers.conv3d(inputs,
                                 64,
                                 2,
                                 stride=2,
                                 activation_fn=None,
                                 **self._reg)
     with tf.variable_scope('ExtraReception', reuse=self._reuse):
         feat_3d = layers.conv3d(feat_3d, 128, 3, **self._reg)
     feat_3d = self._3d_dnn_level_30(feat_3d)
     return self._3d_rdnn_level_60(feat_3d)
예제 #5
0
def resid_unit(inputs,
               depth,
               depth_bottleneck,
               stride,
               rate=1,
               outputs_collections=None,
               scope=None):
    """Residual unit with BN after convolutions.
  This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for
  its definition. 
  When putting together two consecutive ResNet blocks that use this unit, one
  should use stride = 2 in the last unit of the first block.
  Args:
    inputs: A tensor of size [batch, height, width, channels].
    depth: The depth of the ResNet unit output.
    depth_bottleneck: The depth of the bottleneck layers.
    stride: The ResNet unit's stride. Determines the amount of downsampling of
      the units output compared to its input.
    rate: An integer, rate for atrous convolution.
    outputs_collections: Collection to add the ResNet unit output.
    scope: Optional variable_scope.
  Returns:
    The ResNet unit's output.
  """
    with variable_scope.variable_scope(scope, 'resid_v1', [inputs]) as sc:
        # print (inputs.shape)
        depth_in = utils.last_dimension(inputs.get_shape(), min_rank=5)
        if depth == depth_in:
            shortcut = resnet_3d_utils.subsample(inputs, stride, 'shortcut')
        else:
            shortcut = layers.conv3d(inputs,
                                     depth, [1, 1, 1],
                                     stride=stride,
                                     activation_fn=None,
                                     scope='shortcut')

        residual = resnet_3d_utils.conv3d_same(inputs,
                                               depth_bottleneck,
                                               3,
                                               stride=1,
                                               scope='conv1')
        residual = layers.conv3d(residual,
                                 depth_bottleneck,
                                 3,
                                 stride,
                                 scope='conv2')

        output = nn_ops.relu(shortcut + residual)

        return utils.collect_named_outputs(outputs_collections, sc.name,
                                           output)
예제 #6
0
def conv3d(input,
           filters=16,
           kernel_size=3,
           padding="same",
           stride=1,
           activation_fn=relu,
           reuse=None,
           name=None,
           data_format='NDHWC',
           weights_initializer=None,
           biases_initializer=tf.zeros_initializer(),
           weights_regularizer=None,
           biases_regularizer=None,
           normalizer_fn=None,
           normalizer_params=None):

    return contrib_layers.conv3d(input,
                                 num_outputs=filters,
                                 kernel_size=kernel_size,
                                 padding=padding,
                                 stride=stride,
                                 scope=name,
                                 activation_fn=activation_fn,
                                 reuse=reuse,
                                 weights_initializer=weights_initializer,
                                 data_format=data_format,
                                 biases_initializer=biases_initializer,
                                 weights_regularizer=weights_regularizer,
                                 biases_regularizer=biases_regularizer,
                                 normalizer_fn=normalizer_fn,
                                 normalizer_params=normalizer_params)
예제 #7
0
 def _3d_dnn_level_60(self, inputs):
     with tf.variable_scope('Volume60', reuse=self._reuse):
         conv = layers.conv3d(inputs, 64, 3, scope='conv1', **self._reg)
         conv = layers.conv3d(conv,
                              64,
                              3,
                              activation_fn=None,
                              scope='conv2',
                              **self._reg)
         shortcut = layers.conv3d(inputs,
                                  64,
                                  1,
                                  activation_fn=None,
                                  scope='shortcut',
                                  **self._reg)
         res = tf.nn.relu(conv + shortcut)
         return layers.max_pool3d(res, 2, scope='pool')
예제 #8
0
 def _3d_dnn_level_240(self, inputs):
     with tf.variable_scope('Volume240', reuse=self._reuse):
         return layers.conv3d(inputs,
                              16,
                              7,
                              stride=2,
                              scope='conv',
                              **self._reg)
예제 #9
0
def conv3d(input, filters=16, kernel_size=3, padding="same", stride=1, activation_fn=relu, reuse=None, name=None, data_format='NDHWC',
           weights_initializer=None, biases_initializer=tf.zeros_initializer(), weights_regularizer=None, biases_regularizer=None,
           normalizer_fn=None, normalizer_params=None):

    return contrib_layers.conv3d(input, num_outputs=filters, kernel_size=kernel_size, padding=padding, stride=stride, scope=name,
                                 activation_fn=activation_fn, reuse=reuse, weights_initializer=weights_initializer, data_format=data_format,
                                 biases_initializer=biases_initializer, weights_regularizer=weights_regularizer, biases_regularizer=biases_regularizer,
                                 normalizer_fn=normalizer_fn, normalizer_params = normalizer_params)
예제 #10
0
 def _prediction_reduce(self, inputs, conv_reg_dict):
     with tf.variable_scope('PredictReduction', reuse=self._reuse):
         conv = layers.conv3d(inputs,
                              128,
                              1,
                              scope='conv4_1',
                              **conv_reg_dict)
         conv = layers.conv3d(conv,
                              128,
                              1,
                              scope='conv4_2',
                              **conv_reg_dict)
         fc12 = layers.conv3d(conv,
                              12,
                              1,
                              activation_fn=None,
                              scope='fc12',
                              **conv_reg_dict)
         return fc12
예제 #11
0
def autoencoder(inputs):
    # encoder
    # 32 x 32 x 32 x 1   -> 16 x 16 x 16 x 32
    # 16 x 16 x 16 x 32  ->  8 x 8 x 8 x 16
    # 8 x 8 x 8 x 16    ->  2 x 2 x 2 x 8
    net = lays.conv3d(inputs, 32, [5, 5, 5], stride=2, padding='SAME')
    net = lays.conv3d(net, 16, [5, 5, 5], stride=2, padding='SAME')
    print(tf.shape(net))
    net = lays.conv3d(net, 8, [5, 5, 5], stride=4, padding='SAME')
    #net = lays.fully_connected(net,1)
    latent_space = net
    # decoder
    # 2 x 2 x 2 x 8   ->  8 x 8 x 8 x 16
    # 8 x 8 x 8 x 16  ->  16 x 16 x 16 x 32
    # 16 x 16 x 16 x 32  ->  32 x 32 x 32 x 1
    net = lays.conv3d_transpose(net, 16, [5, 5, 5], stride=4, padding='SAME')
    net = lays.conv3d_transpose(net, 32, [5, 5, 5], stride=2, padding='SAME')
    net = lays.conv3d_transpose(net, 1, [5, 5, 5], stride=2, padding='SAME', activation_fn=tf.nn.tanh)
    return latent_space, net
def encoder(inputs):

    # encoder
    # 32 x 32 x 32 x 1   -> 16 x 16 x 16 x 32
    # 16 x 16 x 16 x 32  ->  8 x 8 x 8 x 16
    # 8 x 8 x 8 x 16    ->  2 x 2 x 2 x 8
    print("input type and shape", type(inputs), inputs.shape)
    net = lays.batch_norm(lays.conv3d(inputs,
                                      32, [5, 5, 5],
                                      stride=2,
                                      padding='SAME',
                                      trainable=True),
                          decay=0.9)
    net = lays.batch_norm(lays.conv3d(net,
                                      16, [5, 5, 5],
                                      stride=2,
                                      padding='SAME',
                                      trainable=True),
                          decay=0.9)
    net = lays.batch_norm(lays.conv3d(net,
                                      8, [5, 5, 5],
                                      stride=4,
                                      padding='SAME',
                                      trainable=True),
                          decay=0.9)
    net = lays.batch_norm(lays.flatten(net), decay=0.9)

    z_mean = lays.fully_connected(net, z_dims)
    z_stdev = 0.5 * tf.nn.softplus(lays.fully_connected(net, z_dims))

    # Reparameterization trick for Variational Autoencoder
    samples = tf.random_normal([tf.shape(z_mean)[0], z_dims],
                               mean=0,
                               stddev=1,
                               dtype=tf.float32)
    print("rank and shape of samples", tf.rank(samples))
    guessed_z = z_mean + tf.multiply(samples, z_stdev)
    print("rank and shape of guessed z", tf.rank(guessed_z))
    l_space = guessed_z
    return z_mean, z_stdev, l_space
def resnet_v1(inputs,
              blocks,
              num_classes=None,
              is_training=True,
              global_pool=True,
              output_stride=None,
              include_root_block=True,
              reuse=None,
              scope=None):
    """Generator for v1 ResNet models.
  Args:
    inputs: A tensor of size [batch, height_in, width_in, channels].
    blocks: A list of length equal to the number of ResNet blocks. Each element
      is a resnet_utils.Block object describing the units in the block.
    num_classes: Number of predicted classes for classification tasks. If None
      we return the features before the logit layer.
    is_training: whether batch_norm layers are in training mode.
    global_pool: If True, we perform global average pooling before computing the
      logits. Set to True for image classification, False for dense prediction.
    output_stride: If None, then the output will be computed at the nominal
      network stride. If output_stride is not None, it specifies the requested
      ratio of input to output spatial resolution.
    include_root_block: If True, include the initial convolution followed by
      max-pooling, if False excludes it.
    reuse: whether or not the network and its variables should be reused. To be
      able to reuse 'scope' must be given.
    scope: Optional variable_scope.
  Returns:
    net: A rank-4 tensor of size [batch, height_out, width_out, channels_out].
      If global_pool is False, then height_out and width_out are reduced by a
      factor of output_stride compared to the respective height_in and width_in,
      else both height_out and width_out equal one. If num_classes is None, then
      net is the output of the last ResNet block, potentially after global
      average pooling. If num_classes is not None, net contains the pre-softmax
      activations.
    end_points: A dictionary from components of the network to the corresponding
      activation.
  Raises:
    ValueError: If the target output_stride is not valid.
  """
    with variable_scope.variable_scope(scope,
                                       'resnet_v1', [inputs],
                                       reuse=reuse) as sc:
        end_points_collection = sc.original_name_scope + '_end_points'
        with arg_scope(
            [layers.conv3d, bottleneck, resnet_3d_utils.stack_blocks_dense],
                outputs_collections=end_points_collection):
            with arg_scope([layers.batch_norm], is_training=is_training):
                net = inputs
                #        if include_root_block:
                #          if output_stride is not None:
                #            if output_stride % 4 != 0:
                #              raise ValueError('The output_stride needs to be a multiple of 4.')
                #            output_stride /= 4
                #          net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1')
                #          net = layers_lib.max_pool2d(net, [3, 3], stride=2, scope='pool1')
                net = resnet_3d_utils.stack_blocks_dense(
                    net, blocks, output_stride)
                if global_pool:
                    net = math_ops.reduce_mean(net, [1, 2, 3],
                                               name='pool5',
                                               keepdims=True)
                if num_classes is not None:
                    net = layers.conv3d(net,
                                        num_classes, [1, 1, 1],
                                        activation_fn=None,
                                        normalizer_fn=None,
                                        scope='logits')

                # Convert end_points_collection into a dictionary of end_points.
                end_points = utils.convert_collection_to_dict(
                    end_points_collection)
                if num_classes is not None and num_classes != 1:
                    end_points['predictions'] = layers_lib.softmax(
                        net, scope='predictions')
                    net = tf.squeeze(net)
                elif num_classes == 1:
                    net = tf.squeeze(net)
                    end_points['probs'] = tf.nn.sigmoid(net)

                return net, end_points
def bottleneck(inputs,
               depth_out,
               depth_bottleneck,
               stride,
               rate=1,
               outputs_collections=None,
               scope=None):

    with variable_scope.variable_scope(scope, 'resid_v1', [inputs]) as sc:
        # print (inputs.shape)
        depth_in = utils.last_dimension(inputs.get_shape(), min_rank=5)
        if depth_out == depth_in:
            shortcut = resnet_3d_utils.subsample(inputs, stride, 'shortcut')
        else:
            shortcut = layers.conv3d(inputs,
                                     depth_out, [1, 1, 1],
                                     stride=stride,
                                     activation_fn=None,
                                     scope='shortcut')

        residual = layers.conv3d(inputs,
                                 depth_bottleneck,
                                 1,
                                 stride=1,
                                 scope='conv1')

        residual1 = resnet_3d_utils.conv3d_same(residual,
                                                depth_bottleneck,
                                                3,
                                                stride,
                                                scope='conv2')
        #residual1 = layers.conv3d(residual1, depth_out, 1, stride=1, scope='conv3')

        with tf.variable_scope(scope, 'Amul', [inputs]):
            amul = amul_module.amul
            #
            # residual2 = inputs
            residual2 = tf.pad(residual,
                               [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]],
                               "CONSTANT")
            Amul_weight = tf.contrib.slim.model_variable('Amul_weight',
                                                         shape=[27, 27])
            residual2 = amul(residual2, Amul_weight)
            residual2 = layers.conv3d(residual2,
                                      depth_bottleneck, [3, 3, 3],
                                      padding='SAME',
                                      stride=[x * stride for x in [3, 3, 3]],
                                      scope='conv_amul')


#      #residual2 = resnet_3d_utils.conv3d_same(residual2, depth, 3, stride=3, scope='conv1_amul')
#tf.summary.histogram('Amul_weight_1',Amul_weight_1)
#
        residual_concat = tf.concat([residual1, residual2], 4)
        residual_concat = layers.conv3d(residual_concat,
                                        depth_out, [1, 1, 1],
                                        stride=1,
                                        scope='conv_concat',
                                        activation_fn=None)
        output = nn_ops.relu(shortcut + residual_concat)

        return utils.collect_named_outputs(outputs_collections, sc.name,
                                           output)
예제 #15
0
 def _3d_dnn(self, inputs):
     with tf.variable_scope('ExtraReception', reuse=self._reuse):
         feat_3d = layers.conv3d(inputs, 128, 3, **self._reg)
     feat_3d = self._3d_dnn_level_30(feat_3d)
     return self._3d_rdnn_level_60(feat_3d)
예제 #16
0
파일: networks.py 프로젝트: xuldor/MLEP
def resnet_conv3d(inputs,
                  num_filters=64,
                  upsample_fn=cyclegan_upsample,
                  kernel_size=3,
                  num_outputs=3,
                  tanh_linear_slope=0.0,
                  use_decoder=True):

    end_points = {}

    with tf.contrib.framework.arg_scope(cyclegan_arg_scope()):

        ###########
        # Encoder #
        ###########
        with tf.variable_scope('input'):
            # 7x7 input stage
            net = tf_layers.conv3d(inputs,
                                   num_filters,
                                   kernel_size=[1, 7, 7],
                                   stride=1,
                                   padding='SAME')
            end_points['encoder_0'] = net

        with tf.variable_scope('encoder'):
            with tf.contrib.framework.arg_scope([tf_layers.conv3d],
                                                kernel_size=kernel_size,
                                                stride=2,
                                                activation_fn=tf.nn.relu,
                                                padding='SAME'):
                net = tf_layers.conv3d(net, num_filters * 2)
                end_points['encoder_1'] = net
                net = tf_layers.conv3d(net, num_filters * 4)
                end_points['encoder_2'] = net

        ###################
        # Residual Blocks #
        ###################
        with tf.variable_scope('residual_blocks'):
            with tf.contrib.framework.arg_scope([tf_layers.conv3d],
                                                kernel_size=kernel_size,
                                                stride=1,
                                                activation_fn=tf.nn.relu,
                                                padding='SAME'):
                for block_id in range(6):
                    with tf.variable_scope('block_{}'.format(block_id)):
                        res_net = tf_layers.conv3d(net, num_filters * 4)
                        res_net = tf_layers.conv3d(res_net,
                                                   num_filters * 4,
                                                   activation_fn=None)
                        net += res_net

                        end_points['resnet_block_%d' % block_id] = net

        hidden_state = tf.nn.tanh(net)
        hidden_state = tf.squeeze(hidden_state, axis=1)
        end_points['hidden_state'] = hidden_state

        ###########
        # Decoder #
        ###########
        with tf.variable_scope('decoder'):
            with tf.contrib.framework.arg_scope([tf_layers.conv2d],
                                                kernel_size=kernel_size,
                                                stride=1,
                                                activation_fn=tf.nn.relu):
                with tf.variable_scope('decoder1'):
                    net = upsample_fn(hidden_state,
                                      num_outputs=num_filters * 2,
                                      stride=[2, 2])
                end_points['decoder1'] = net

                with tf.variable_scope('decoder2'):
                    net = upsample_fn(net,
                                      num_outputs=num_filters,
                                      stride=[2, 2])
                end_points['decoder2'] = net

        with tf.variable_scope('output'):
            logits = tf_layers.conv2d(net,
                                      num_outputs, [7, 7],
                                      activation_fn=None,
                                      normalizer_fn=None,
                                      padding='SAME')

            end_points['logits'] = logits
            end_points['predictions'] = tf.tanh(
                logits) + logits * tanh_linear_slope
        print('Decoder output = {}'.format(logits))

    return end_points['predictions'], end_points['hidden_state'], end_points
예제 #17
0
class CostCalculatorTest(parameterized.TestCase, tf.test.TestCase):
    def _batch_norm_scope(self):
        params = {
            'trainable': True,
            'normalizer_fn': layers.batch_norm,
            'normalizer_params': {
                'scale': True
            }
        }

        with arg_scope([layers.conv2d], **params) as sc:
            return sc

    def testImageIsNotZerothOutputOfOp(self):
        # Throughout the framework, we assume that the 0th output of each op is the
        # only one of interest. One exception that often happens is when the input
        # image comes from a queue or from a staging op. Then the image is one of
        # the outputs of the dequeue (or staging) op, not necessarily the 0th one.
        # Here we test that the BilinearNetworkRegularizer deals correctly with this
        # case.

        # Create an input op where the image is output number 1, not 0.
        # TODO(g1) Move this mechanism to add_concat_model_stub, possibly using
        # tf.split to produce an op where the image is not the 0th output image
        # (instead of FIFOQueue).
        image = add_concat_model_stub.image_stub()
        non_image_tensor = tf.zeros(shape=(41, ))
        queue = tf.FIFOQueue(capacity=1,
                             dtypes=(tf.float32, ) * 2,
                             shapes=(non_image_tensor.shape, image.shape))

        # Pass the image (output[1]) to the network.
        with arg_scope(self._batch_norm_scope()):
            output_op = add_concat_model_stub.build_model(queue.dequeue()[1])

        # Create OpHandler dict for test.
        op_handler_dict = collections.defaultdict(
            grouping_op_handler.GroupingOpHandler)
        op_handler_dict.update({
            'FusedBatchNormV3':
            batch_norm_source_op_handler.BatchNormSourceOpHandler(0.1),
            'Conv2D':
            output_non_passthrough_op_handler.OutputNonPassthroughOpHandler(),
            'ConcatV2':
            concat_op_handler.ConcatOpHandler(),
        })

        # Create OpRegularizerManager and NetworkRegularizer for test.
        manager = orm.OpRegularizerManager([output_op], op_handler_dict)
        calculator = cc.CostCalculator(manager,
                                       resource_function.flop_function)

        # Calculate expected FLOP cost.
        expected_alive_conv1 = sum(
            add_concat_model_stub.expected_alive()['conv1'])
        conv1_op = tf.get_default_graph().get_operation_by_name('conv1/Conv2D')
        conv1_coeff = resource_function.flop_coeff(conv1_op)
        num_channels = 3
        expected_cost = conv1_coeff * num_channels * expected_alive_conv1

        with self.session():
            tf.global_variables_initializer().run()
            # Set gamma values to replicate aliveness in add_concat_model_stub.
            name_to_var = {v.op.name: v for v in tf.global_variables()}
            gamma1 = name_to_var['conv1/BatchNorm/gamma']
            gamma1.assign([0, 1, 1, 0, 1, 0, 1]).eval()
            gamma4 = name_to_var['conv4/BatchNorm/gamma']
            gamma4.assign([0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0]).eval()

            queue.enqueue((non_image_tensor, image)).run()
            self.assertEqual(expected_cost,
                             calculator.get_cost([conv1_op]).eval())
            # for 0/1 assigments cost and reg_term are equal:
            self.assertEqual(
                expected_cost,
                calculator.get_regularization_term([conv1_op]).eval())

    @parameterized.named_parameters(
        ('_conv2d', 4, lambda x: layers.conv2d(x, 16, 3), 'Conv2D'),
        ('_convt', 4, lambda x: layers.conv2d_transpose(x, 16, 3),
         'conv2d_transpose'),
        ('_conv2s', 4, lambda x: layers.separable_conv2d(x, None, 3),
         'depthwise'),
        ('_conv3d', 5, lambda x: layers.conv3d(x, 16, 3), 'Conv3D'))
    def test_get_input_activation2(self, rank, fn, op_name):
        g = tf.get_default_graph()
        inputs = tf.zeros([6] * rank)
        with arg_scope([
                layers.conv2d, layers.conv2d_transpose,
                layers.separable_conv2d, layers.conv3d
        ],
                       scope='test_layer'):
            _ = fn(inputs)
        for op in g.get_operations():
            print(op.name)
        self.assertEqual(
            inputs,
            cc.get_input_activation(
                g.get_operation_by_name('test_layer/' + op_name)))
    def _build_net(self, inputs):
        w_init = layers.variance_scaling_initializer()
        conv1 = layers.conv3d(inputs=inputs,
                              padding="SAME",
                              num_outputs=64,
                              kernel_size=[5, 5, 5],
                              stride=2,
                              data_format="NDHWC",
                              weights_initializer=w_init,
                              activation_fn=tf.nn.relu)
        conv2 = layers.conv3d(inputs=conv1,
                              padding="SAME",
                              num_outputs=128,
                              kernel_size=[3, 3, 3],
                              stride=1,
                              data_format="NDHWC",
                              weights_initializer=w_init,
                              activation_fn=tf.nn.relu)
        conv3 = layers.conv3d(inputs=conv2,
                              padding="SAME",
                              num_outputs=256,
                              kernel_size=[3, 3, 3],
                              stride=2,
                              data_format="NDHWC",
                              weights_initializer=w_init,
                              activation_fn=None)
        res = layers.conv3d(inputs=inputs,
                            padding="VALID",
                            num_outputs=256,
                            kernel_size=[4, 2, 4],
                            stride=3,
                            data_format="NDHWC",
                            weights_initializer=w_init,
                            activation_fn=None)
        conv4_in = tf.nn.relu(conv3 + res)
        conv4 = layers.conv3d(inputs=conv4_in,
                              padding="SAME",
                              num_outputs=256,
                              kernel_size=[2, 1, 2],
                              stride=2,
                              data_format="NDHWC",
                              weights_initializer=w_init,
                              activation_fn=tf.nn.relu)
        conv5 = layers.conv3d(inputs=conv4,
                              padding="VALID",
                              num_outputs=512,
                              kernel_size=[2, 1, 2],
                              stride=1,
                              data_format="NDHWC",
                              weights_initializer=w_init,
                              activation_fn=None)
        res2 = layers.conv3d(inputs=conv4_in,
                             padding="VALID",
                             num_outputs=512,
                             kernel_size=[3, 2, 3],
                             stride=1,
                             data_format="NDHWC",
                             weights_initializer=w_init,
                             activation_fn=None)
        flat = tf.nn.relu(layers.flatten(conv5 + res2))

        h1 = layers.fully_connected(inputs=flat, num_outputs=512)
        h2 = layers.fully_connected(inputs=h1,
                                    num_outputs=512,
                                    activation_fn=None)
        h3 = tf.nn.relu(h2 + flat)

        #Recurrent network for temporal dependencies
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(512, state_is_tuple=True)
        c_init = np.zeros((1, lstm_cell.state_size.c), np.float32)
        h_init = np.zeros((1, lstm_cell.state_size.h), np.float32)
        state_init = [c_init, h_init]
        c_in = tf.placeholder(tf.float32, [1, lstm_cell.state_size.c])
        h_in = tf.placeholder(tf.float32, [1, lstm_cell.state_size.h])
        state_in = (c_in, h_in)
        rnn_in = tf.expand_dims(h3, [0])
        step_size = tf.shape(inputs)[:1]
        state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in)
        lstm_outputs, lstm_state = tf.nn.dynamic_rnn(lstm_cell,
                                                     rnn_in,
                                                     initial_state=state_in,
                                                     sequence_length=step_size,
                                                     time_major=False)
        lstm_c, lstm_h = lstm_state
        state_out = (lstm_c[:1, :], lstm_h[:1, :])
        rnn_out = tf.reshape(lstm_outputs, [-1, 512])

        policy_layer = layers.fully_connected(
            inputs=rnn_out,
            num_outputs=a_size,
            weights_initializer=normalized_columns_initializer(1. /
                                                               float(a_size)),
            biases_initializer=None,
            activation_fn=None)
        policy = tf.nn.softmax(policy_layer)
        policy_sig = tf.sigmoid(policy_layer)
        value = layers.fully_connected(
            inputs=rnn_out,
            num_outputs=1,
            weights_initializer=normalized_columns_initializer(1.0),
            biases_initializer=None,
            activation_fn=None)
        has_block = layers.fully_connected(
            inputs=rnn_out,
            num_outputs=1,
            weights_initializer=normalized_columns_initializer(1.0),
            biases_initializer=None,
            activation_fn=tf.sigmoid)
        is_built = layers.fully_connected(
            inputs=rnn_out,
            num_outputs=1,
            weights_initializer=normalized_columns_initializer(1.0),
            biases_initializer=None,
            activation_fn=tf.sigmoid)

        return policy, value, state_out, state_in, state_init, has_block, policy_sig, is_built
예제 #19
0
def conv3d_same(inputs, num_outputs, kernel_size, stride, rate=1, scope=None, outputs_collections=None):
  """Strided 3-D convolution with 'SAME' padding.

  When stride > 1, then we do explicit zero-padding, followed by conv2d with
  'VALID' padding.

  Note that

     net = conv2d_same(inputs, num_outputs, 3, stride=stride)

  is equivalent to

     net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=1,
     padding='SAME')
     net = subsample(net, factor=stride)

  whereas

     net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=stride,
     padding='SAME')

  is different when the input's height or width is even, which is why we add the
  current function. For more details, see ResnetUtilsTest.testConv2DSameEven().

  Args:
    inputs: A 4-D tensor of size [batch, height_in, width_in, channels].
    num_outputs: An integer, the number of output filters.
    kernel_size: An int with the kernel_size of the filters.
    stride: An integer, the output stride.
    rate: An integer, rate for atrous convolution.
    scope: Scope.

  Returns:
    output: A 4-D tensor of size [batch, height_out, width_out, channels] with
      the convolution output.
  """
  if stride == 1:
    return layers_lib.conv3d(
        inputs,
        num_outputs,
        kernel_size,
        stride=1,
        rate=rate,
        padding='SAME',
        scope=scope,
        outputs_collections=None)
  else:
    if type(kernel_size)==list:
        kernel = np.array(kernel_size)
        pad_total = kernel - 1
        pad_beg = pad_total // 2
        pad_end = pad_total - pad_beg
        inputs = array_ops.pad(inputs, [[0, 0], [pad_beg[0], pad_end[0]], [pad_beg[1], pad_end[1]], [pad_beg[2], pad_end[2]], [0, 0]])
    else:       
        kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1)
        pad_total = kernel_size_effective - 1
        pad_beg = pad_total // 2
        pad_end = pad_total - pad_beg
        inputs = array_ops.pad(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]])
    
    return layers_lib.conv3d(
        inputs,
        num_outputs,
        kernel_size,
        stride=stride,
        rate=rate,
        padding='VALID',
        scope=scope,
        outputs_collections=None)