示例#1
0
def image_features(images):
    with tf.variable_scope("Image_Feats"):
        #TODO add visual features
        with slim.arg_scope(
                inception_v3.inception_v3_arg_scope(weight_decay=0.0005)):
            with slim.arg_scope([slim.conv2d], trainable=True):
                # incp_feats, end_points = inception_v3.inception_v3_base(images, final_endpoint='Mixed_6e', scope='InceptionV3')
                incp_feats, end_points = inception_v3.inception_v3_base(
                    images,
                    final_endpoint='MaxPool_5a_3x3',
                    scope='InceptionV3')
                # incp_feats, end_points = inception_v3.inception_v3_base(images, final_endpoint='Mixed_5d', scope='InceptionV3')

        # print(end_points)
        # vnet = slim.conv2d(images, 128, [3, 3])
        # vnet = slim.max_pool2d(vnet, [2, 2], stride = 2)
        # vnet = slim.conv2d(vnet, 256, [3, 3])
        # vnet = slim.max_pool2d(vnet, [2, 2], stride=2)
        # vnet = slim.conv2d(vnet, 256, [3, 3])
        # vnet = slim.max_pool2d(vnet, [2, 2], stride=2)
        # vis_feats = vnet

        # image_pooled = slim.max_pool2d(incp_feats, [4, 4], [2, 2])  # Bx7x7x192

        incp_feats_pooled = slim.conv2d(
            incp_feats,
            20, [1, 1],
            weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
            weights_regularizer=slim.l1_regularizer(0.00004))
        image_pooled = slim.max_pool2d(incp_feats_pooled, [4, 4],
                                       [2, 2])  # Bx7x7x192
        # ipdb.set_trace()

        vis_feats = image_pooled
    return vis_feats
示例#2
0
    def pre_convolution(self, image1, image2, image3, name):
        with tf.variable_scope(name):
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    # normalizer_fn=self.normalizer,
                    # normalizer_params=self.norm_params1,
                    weights_initializer=initializers.xavier_initializer(
                        uniform=True),
                    weights_regularizer=slim.l1_regularizer(1e-4)):
                image1 = slim.conv2d(image1, 64, [3, 3], 1, scope='conv1_1')
                image1 = slim.conv2d(image1, 64, [3, 3], 1, scope='conv1_2')
                image2 = slim.conv2d(image2, 64, [3, 3], 1, scope='conv2_1')
                image2 = slim.conv2d(image2, 64, [3, 3], 1, scope='conv2_2')
                image3 = slim.conv2d(image3, 64, [3, 3], 1, scope='conv3_1')
                image3 = slim.conv2d(image3, 64, [3, 3], 1, scope='conv3_2')

                image_1_2 = tf.concat([image1, image2], axis=3)
                image_1_2 = slim.conv2d(image_1_2,
                                        64, [3, 3],
                                        1,
                                        scope='conv_1_2')

                image_2_3 = tf.concat([image2, image3], axis=3)
                image_2_3 = slim.conv2d(image_2_3,
                                        64, [3, 3],
                                        1,
                                        scope='conv_2_3')

                image_1_2_3 = tf.concat([image_1_2, image_2_3], axis=3)
                image_1_2_3 = slim.conv2d(image_1_2_3,
                                          64, [3, 3],
                                          1,
                                          scope='conv_1_2_3')
                return image_1_2_3
示例#3
0
    def res_block(self, input, name):
        with tf.variable_scope(name):
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    # normalizer_fn=self.normalizer,
                    # normalizer_params=self.norm_params1,
                    weights_initializer=initializers.xavier_initializer(
                        uniform=True),
                    weights_regularizer=slim.l1_regularizer(1e-4)):
                # print('......................................')
                split1 = input
                split1_1 = input
                conv3_1 = slim.conv2d(split1, 48, [3, 3], 1, scope='conv_3_1')
                conv3_2 = slim.conv2d(conv3_1, 48, [3, 3], 1, scope='conv3_2')
                slice1_1, slice1_2 = tf.split(conv3_2, [16, 32], axis=3)
                conv3_3 = slim.conv2d(slice1_2, 48, [3, 3], scope='conv3_3')
                conv3_4 = slim.conv2d(conv3_3, 64, [3, 3], scope='conv3_4')
                slice2_1, slice2_2 = tf.split(conv3_4, [16, 48], axis=3)
                conv3_5 = slim.conv2d(slice2_2, 48, [3, 3], 1, scope='conv3_5')
                conv3_6 = slim.conv2d(conv3_5, 96, [3, 3], 1, scope='conv3_6')

                concat1 = tf.concat([split1_1, slice1_1, slice2_1], axis=3)

                sum1 = concat1 + conv3_6
                down1 = slim.conv2d(sum1, 64, [1, 1], 1, scope='down1')
                return down1
示例#4
0
def _build_regularizer(regularizer):

    if regularizer["type"] == 'l1_regularizer':
        return slim.l1_regularizer(scale=float(regularizer["weigth"]))

    if regularizer["type"] == 'l2_regularizer':
        return slim.l2_regularizer(scale=float(regularizer["weigth"]))
    def model(self, inputs):
        with tf.variable_scope("model"):
            outputs = slim.stack(
                inputs,
                slim.fully_connected, [200, 50],
                activation_fn=tf.sigmoid,
                weights_initializer=tf.truncated_normal_initializer(stddev=2),
                weights_regularizer=slim.l1_regularizer(1e-8))

            outputs = slim.fully_connected(outputs, 10, activation_fn=None)
        return outputs
示例#6
0
def fully_connected(input, c_outputs, name):
    output = slim.fully_connected(input,
                                  c_outputs,
                                  weights_regularizer=slim.l1_regularizer(0.00001),
                                  weights_initializer=slim.variance_scaling_initializer(),
                                  normalizer_fn=None,
                                  activation_fn=None,
                                  scope=name)
    if PRINT_LAYER_LOG:
        print(name, output.get_shape())
    return output
示例#7
0
def inference_network(x, latent_dim, hidden_size, layers, trainornot):
    """Construct an inference network parametrizing a Gaussian.

    Args:
      x: A batch of MCI data subjects.
      latent_dim: The latent dimensionality.
      hidden_size: The size of the neural net hidden layers.

    Returns:
      mu: Mean parameters for the variational family Normal
      sigma: Standard deviation parameters for the variational family Normal
    """
    if layers == True:
        with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu):
            net = slim.flatten(x)
            net = slim.fully_connected(net, hidden_size, trainable=trainornot)
            net = slim.fully_connected(net, hidden_size, trainable=trainornot)
            gaussian_params = slim.fully_connected(net,
                                                   latent_dim * 2,
                                                   activation_fn=None,
                                                   trainable=trainornot)

            # The mean parameter is unconstrained
            mu = gaussian_params[:, :latent_dim]
            # The standard deviation must be positive. Parametrize with a softplus and
            # add a small epsilon for numerical stability
            sigma = 1e-6 + tf.nn.softplus(gaussian_params[:, latent_dim:])

    elif layers == False:

        with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu):
            net = slim.flatten(x)
            gaussian_params = slim.fully_connected(
                net,
                latent_dim * 2,
                activation_fn=None,
                trainable=trainornot,
                weights_regularizer=slim.l1_regularizer(FLAGS.param))

            # The mean parameter is unconstrained
            mu = gaussian_params[:, :latent_dim]
            # The standard deviation must be positive. Parametrize with a softplus and
            # add a small epsilon for numerical stability
            sigma = 1e-6 + tf.nn.softplus(gaussian_params[:, latent_dim:])
    else:
        raise ("Only True and False in the layer parameter")

    return mu, sigma
示例#8
0
def seismic_features(seismic):
    with tf.variable_scope("Seismic_Feats"):
        # seismic = tf.squeeze(seismic,[3])
        # snet = tflearn.conv_1d(seismic, 16, 10, activation='relu', weights_init='normal',)
        # # snet = tflearn.max_pool_1d(snet, 2)
        # snet = tflearn.conv_1d(snet, 16, 10, activation='relu', weights_init='normal')
        # snet = tflearn.max_pool_1d(snet, 2)
        # seismic_feats = snet

        snet = slim.conv2d(
            seismic,
            32, [10, 1],
            weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
            weights_regularizer=slim.l2_regularizer(0.0001))
        snet = slim.conv2d(
            snet,
            64, [10, 1],
            weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
            weights_regularizer=slim.l2_regularizer(0.0001))
        snet = slim.max_pool2d(snet, [10, 1], stride=[5, 1])

        snet = slim.conv2d(
            snet,
            64, [5, 1],
            weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
            weights_regularizer=slim.l2_regularizer(0.0001))
        snet = slim.conv2d(
            snet,
            64, [5, 1],
            weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
            weights_regularizer=slim.l2_regularizer(0.0001))
        snet = slim.max_pool2d(snet, [10, 1], stride=[5, 1])

        if POOLED:
            seismic_feats_n = slim.conv2d(
                snet,
                20, [1, 1],
                weights_initializer=tf.truncated_normal_initializer(
                    stddev=0.1),
                weights_regularizer=slim.l1_regularizer(0.0004))
            s_pooled = slim.max_pool2d(seismic_feats_n, [16, 1], [8, 1])
            seismic_feats = s_pooled
        else:
            seismic_feats = snet
        # seismic_feats = snet
    return seismic_feats
示例#9
0
 def _batch_norm(inputs,
                 decay=0.999,
                 center=True,
                 scale=False,
                 epsilon=0.001,
                 activation_fn=None,
                 param_initializers=None,
                 param_regularizers=None,
                 updates_collections=tf.GraphKeys.UPDATE_OPS,
                 is_training=True,
                 reuse=None,
                 variables_collections=None,
                 outputs_collections=None,
                 trainable=True,
                 batch_weights=None,
                 fused=None,
                 data_format='NHWC',
                 zero_debias_moving_mean=False,
                 scope=None,
                 renorm=False,
                 renorm_clipping=None,
                 renorm_decay=0.99,
                 adjustment=None):
     print("_batch_norm:center:", center)
     print("_batch_norm:scale :", scale)
     bn_with_scale_false = slim.batch_norm(
         inputs, decay, False, False, epsilon, activation_fn,
         param_initializers, param_regularizers, updates_collections,
         is_training, reuse, variables_collections, outputs_collections,
         trainable, batch_weights, fused, data_format,
         zero_debias_moving_mean, scope, renorm, renorm_clipping,
         renorm_decay, adjustment)
     with tf.variable_scope('XBatchNorm') as scbn:
         gamma = slim.model_variable('gamma',
                                     shape=[inputs.shape[-1]],
                                     initializer=tf.ones_initializer(),
                                     regularizer=slim.l1_regularizer(
                                         0.0001))  #slim.l1_regularizer!!!
         beta = slim.model_variable('beta',
                                    shape=[inputs.shape[-1]],
                                    initializer=tf.zeros_initializer())
     bn = tf.multiply(bn_with_scale_false, pruning.apply_mask(gamma, scbn))
     bn = tf.add(bn, beta)
     return bn
示例#10
0
 def Encode(self, input_img):
     # I will use conv layer to imporove performance
     with tf.variable_scope('encoder', reuse=tf.AUTO_REUSE) as scope:
         conv_l = conv2d(input_img,
                         32, [3, 3],
                         2,
                         weights_regularizer=slim.l1_l2_regularizer(.001),
                         scope='conv_1')
         conv_l = conv2d(conv_l,
                         64, [3, 3],
                         2,
                         weights_regularizer=slim.l1_l2_regularizer(.005),
                         scope='conv_2')
         conv_l = conv2d(conv_l,
                         128, [3, 3],
                         4,
                         weights_regularizer=slim.l1_regularizer(.005),
                         scope=scope)
     return conv_l
示例#11
0
    def enhanced_Net(self,
                     image1,
                     image2,
                     image3,
                     is_train=True,
                     name='enhanced_Net'):
        with tf.variable_scope(name):
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    # normalizer_fn=self.normalizer,
                    # normalizer_params=self.norm_params1,
                    weights_initializer=initializers.xavier_initializer(
                        uniform=True),
                    weights_regularizer=slim.l1_regularizer(1e-4)):

                image1_2_3 = self.pre_convolution(image1,
                                                  image2,
                                                  image3,
                                                  name=name)

                conv2 = slim.conv2d(image1_2_3, 64, [3, 3], 1, scope='conv2')

                down1 = self.res_block(input=conv2, name='conv3')

                down2 = self.res_block(input=down1, name='conv4')

                down3 = self.res_block(input=down2, name='conv5')

                # down4 = self.res_block(input = down3,name = 'conv6')

                conv7 = slim.conv2d(down3, 64, [3, 3], 1, scope='conv7')
                conv8 = slim.conv2d(conv7,
                                    1, [3, 3],
                                    1,
                                    activation_fn=None,
                                    scope='conv8')
                if is_train:
                    conv8_out = conv8 + image2
                else:
                    conv8_out = conv8 + image2
                    conv8_out = tf.clip_by_value(conv8_out, 0, 1.0)
                return conv8_out
示例#12
0
    def __init__(self,
                 filters,
                 kernel_size,
                 strides=(1, 1),
                 padding='valid',
                 data_format='channels_last',
                 dilation_rate=(1, 1),
                 activation=None,
                 use_bias=True,
                 param_lambda=1.0,
                 sparse_th=0.01,
                 kernel_initializer=None,
                 bias_initializer=slim.init_ops.zeros_initializer(),
                 bias_regularizer=None,
                 activity_regularizer=None,
                 trainable=True,
                 name=None,
                 **kwargs):
        # L1 Regularizer
        kernel_regularizer = slim.l1_regularizer(scale=param_lambda)

        # initialize
        super(LookupAlignConvolution2d,
              self).__init__(rank=2,
                             filters=filters,
                             kernel_size=kernel_size,
                             strides=strides,
                             padding=padding,
                             data_format=data_format,
                             dilation_rate=dilation_rate,
                             activation=activation,
                             use_bias=use_bias,
                             kernel_initializer=kernel_initializer,
                             bias_initializer=bias_initializer,
                             kernel_regularizer=kernel_regularizer,
                             bias_regularizer=bias_regularizer,
                             activity_regularizer=activity_regularizer,
                             trainable=trainable,
                             name=name,
                             **kwargs)
        self.sparse_th = sparse_th
        self.kernel_pre = None
示例#13
0
def generative_network(z, hidden_size, layers, trainornot):
    """Build a generative network parametrizing the likelihood of the data

    Args:
      z: Samples of latent variables
      hidden_size: Size of the hidden state of the neural net

    Returns:
      mu: Mean parameters for the variational family Normal
      sigma: Standard deviation parameters for the variational family Normal
    """
    if layers == True:
        with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu):
            net = slim.fully_connected(z, hidden_size, trainable=trainornot)
            net = slim.fully_connected(net, hidden_size, trainable=trainornot)
            gaussian_params = slim.fully_connected(net,
                                                   input_dim * 2,
                                                   activation_fn=None,
                                                   trainable=trainornot)
            mu = gaussian_params[:, :input_dim]
            sigma = 1e-6 + tf.nn.softplus(gaussian_params[:, input_dim:])
            mu = tf.reshape(mu, [-1, 116])
            sigma = tf.reshape(sigma, [-1, 116])

    elif layers == False:
        with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu):
            gaussian_params = slim.fully_connected(
                z,
                input_dim * 2,
                activation_fn=None,
                trainable=trainornot,
                weights_regularizer=slim.l1_regularizer(FLAGS.param))

            mu = gaussian_params[:, :input_dim]
            sigma = 1e-6 + tf.nn.softplus(gaussian_params[:, input_dim:])
            mu = tf.reshape(mu, [-1, 116])
            sigma = tf.reshape(sigma, [-1, 116])
    else:
        raise ("...")

    return mu, sigma
示例#14
0
 def Decode(self, Decoded_img):
     with tf.variable_scope('decoder', reuse=tf.AUTO_REUSE) as scope:
         conv_d = slim.conv2d_transpose(
             Decoded_img,
             64, [3, 3],
             4,
             weights_regularizer=slim.l1_l2_regularizer(.001),
             scope='dconv_1')
         conv_d = slim.conv2d_transpose(
             conv_d,
             32, [3, 3],
             2,
             weights_regularizer=slim.l1_l2_regularizer(.005),
             scope='dconv_2')
         conv_d = slim.conv2d_transpose(
             conv_d,
             3, [3, 3],
             2,
             weights_regularizer=slim.l1_regularizer(.005),
             scope=scope)
     return conv_d
示例#15
0
def BNLF(inputs, is_training=True, scope='deep_regression', n_frames=5):
    with tf.variable_scope(scope, 'deep_regression', [inputs]):
        end_points = {}
        with slim.arg_scope([slim.fully_connected],
                            activation_fn=tf.nn.leaky_relu,
                            weights_regularizer=slim.l1_regularizer(2e-6)):
            # Use frame indices later in network.
            input2 = inputs[:, -n_frames:]

            net = slim.fully_connected(inputs, 256, scope='fc1')
            end_points['fc1'] = net

            net = slim.dropout(net, 0.8, is_training=is_training)
            net = tf.concat((net, input2), axis=1)

            net = slim.fully_connected(net, 128, scope='fc2')
            end_points['fc2'] = net

            net = slim.dropout(net, 0.8, is_training=is_training)
            net = tf.concat((net, input2), axis=1)

            net = slim.fully_connected(net, 64, scope='fc3')
            end_points['fc3'] = net

            net = slim.dropout(net, 0.8, is_training=is_training)
            net = tf.concat((net, input2), axis=1)

            net = slim.fully_connected(net, 32, scope='fc4')
            end_points['fc4'] = net

            predictions = slim.fully_connected(net,
                                               1,
                                               activation_fn=None,
                                               scope='prediction',
                                               normalizer_fn=None)
            end_points['out'] = predictions

            return predictions, end_points
示例#16
0
def _build_slim_regularizer(regularizer):
    """Builds a tf-slim regularizer from config.

  Args:
    regularizer: hyperparams_pb2.Hyperparams.regularizer proto.

  Returns:
    tf-slim regularizer.

  Raises:
    ValueError: On unknown regularizer.
  """
    regularizer_oneof = regularizer.WhichOneof('regularizer_oneof')
    if regularizer_oneof == 'l1_regularizer':
        return slim.l1_regularizer(
            scale=float(regularizer.l1_regularizer.weight))
    if regularizer_oneof == 'l2_regularizer':
        return slim.l2_regularizer(
            scale=float(regularizer.l2_regularizer.weight))
    if regularizer_oneof is None:
        return None
    raise ValueError(
        'Unknown regularizer function: {}'.format(regularizer_oneof))
示例#17
0
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

    drop = args.dropout
    if drop.lower() in ('yes', 'true'):
        FLAGS.dropout = True
        print("Dropout on")
    elif drop.lower() in ('no', 'false'):
        FLAGS.dropout = False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

    # get boolean if img summary is wanted
    img_ = args.imgsummary
    if img_.lower() in ('yes', 'true'):
        FLAGS.imgsummary = True
    elif img_.lower() in ('no', 'false'):
        FLAGS.imgsummary = False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

    # turn weight decay on/off
    if args.weights_reg != 0:
        print("Weight decay with: " + str(args.weights_reg))
        FLAGS.weights_reg = slim.l1_regularizer(args.weights_reg)
    else:
        FLAGS.weights_reg = None

    print("Using architecture: " + model.__name__)
    tf.app.run()
示例#18
0
    def build_pured_net(self, keep_prob, channel_dict):
        with slim.arg_scope([slim.conv2d, slim.fully_connected],
                            activation_fn=tf.nn.relu,
                            weights_regularizer=slim.l1_regularizer(0.001)):
            conv1 = slim.layers.conv2d(self.inputs,
                                       channel_dict['conv1'],
                                       3,
                                       padding="SAME",
                                       scope='conv1')

            # 2 x 2 / 1
            pool1 = slim.layers.max_pool2d(conv1,
                                           kernel_size=[2, 2],
                                           stride=[2, 2],
                                           scope='pool1')

            # 128 / 3 x 3 / 1 / 1
            conv2 = slim.layers.conv2d(pool1,
                                       channel_dict['conv2'],
                                       3,
                                       padding="SAME",
                                       scope='conv2')

            # 2 x 2 / 1
            pool2 = slim.layers.max_pool2d(conv2,
                                           kernel_size=[2, 2],
                                           stride=[2, 2],
                                           scope='pool2')

            # 256 / 3 x 3 / 1 / 1
            conv3 = slim.layers.conv2d(pool2,
                                       channel_dict['conv3'],
                                       3,
                                       padding="SAME",
                                       scope='conv3')

            # 1 x 2 / 1
            pool3 = slim.layers.max_pool2d(conv3,
                                           kernel_size=[2, 2],
                                           stride=[2, 2],
                                           padding="SAME",
                                           scope='pool3')

            flatten = slim.flatten(pool3)

            fc1 = slim.layers.fully_connected(flatten,
                                              channel_dict['fc1'],
                                              scope='fc1')

            fc2 = slim.layers.fully_connected(fc1,
                                              channel_dict['fc2'],
                                              scope='fc2')

        dp2 = slim.layers.dropout(fc2, keep_prob=keep_prob)

        logits = slim.layers.fully_connected(dp2, 10, scope='logits')

        pred = slim.layers.softmax(logits=logits, scope='pred')

        out = {
            'conv1': conv1,
            'pool1': pool1,
            'conv2': conv2,
            'pool2': pool2,
            'conv3': conv3,
            'pool3': pool3,
            'flatten': flatten,
            'fc1': fc1,
            'fc2': fc2,
            'logits': logits,
            'pred': pred
        }
        return out
def policy_fn(state_size, goal_size, model_config):
    """Construct policy network based on model_config.

    Args:
        state_size: the size of the state. e.g. 18.
        model_config:
            a list of tuples, [(shared layer), ..., (policy layer, value layer)].
            e.g. [(64,), (64,), (64, 64)]
    returns: input_state_tensor, output_action_distribution, output_state_value
    """
    input_state_tensor = tf.placeholder(shape=(None, state_size),
                                        dtype=tf.float32)
    input_goal_tensor = tf.placeholder(shape=(None, goal_size),
                                       dtype=tf.float32)
    features = (tf.concat([input_state_tensor, input_goal_tensor], axis=1), )
    with slim.arg_scope([slim.fully_connected],
                        weights_regularizer=slim.l1_regularizer(0.0000)):
        for layer_idx, layer_config in enumerate(model_config):
            with tf.variable_scope('layer_%d' % layer_idx, reuse=False):
                if layer_idx != (len(model_config) - 1):
                    if len(layer_config) == 1:
                        assert len(features
                                   ) == 1, "Merging features are not supported"
                        features = (slim.fully_connected(
                            features[0], layer_config[0]), )
                    else:
                        assert len(
                            layer_config
                        ) == 2, "Layer config can only have 1 or 2 value"
                        if len(features) == 1:
                            features = (slim.fully_connected(
                                features[0], layer_config[0]),
                                        slim.fully_connected(
                                            features[0], layer_config[1]))
                        else:
                            features = (slim.fully_connected(
                                features[0], layer_config[0]),
                                        slim.fully_connected(
                                            features[1], layer_config[1]))
                else:
                    assert len(layer_config) == 2, 'model must has two heads'
                    if len(features) == 1:
                        features = (slim.fully_connected(
                            features[0],
                            layer_config[0],
                            activation_fn=None,
                        ),
                                    slim.fully_connected(
                                        features[0],
                                        layer_config[1],
                                        activation_fn=None,
                                    ))
                    else:
                        features = (slim.fully_connected(
                            features[0],
                            layer_config[0],
                            activation_fn=None,
                        ),
                                    slim.fully_connected(
                                        features[1],
                                        layer_config[1],
                                        activation_fn=None,
                                    ))
        actor_feautre, critic_feature = features
        action_distribution = gaussion_fn(actor_feautre)
        state_value = critic_feature
        network_io = {
            'input_state_tensor': input_state_tensor,
            'input_goal_tensor': input_goal_tensor,
            'action_distribution': action_distribution,
            'state_value': state_value
        }
    return network_io
示例#20
0
def calculate_loss(predict, labels):
    landmark_label = labels[:, 0:136]
    pose_label = labels[:, 136:139]
    leye_cla_label = labels[:, 139]
    reye_cla_label = labels[:, 140]
    mouth_cla_label = labels[:, 141]
    big_mouth_cla_label = labels[:, 142]

    landmark_predict = predict[:, 0:136]
    pose_predict = predict[:, 136:139]
    leye_cla_predict = predict[:, 139]
    reye_cla_predict = predict[:, 140]
    mouth_cla_predict = predict[:, 141]
    big_mouth_cla_predict = predict[:, 142]

    loss = _wing_loss(landmark_predict, landmark_label)

    loss_pose = _mse(pose_predict, pose_label)

    loss=loss_pose+loss

    leye_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=leye_cla_predict,
                                                                      labels=leye_cla_label))
    reye_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=reye_cla_predict,
                                                                      labels=reye_cla_label))
    mouth_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=mouth_cla_predict,
                                                                       labels=mouth_cla_label))
    mouth_loss_big = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=big_mouth_cla_predict,
                                                                        labels=big_mouth_cla_label))
    mouth_loss=mouth_loss+mouth_loss_big
    ###make crosssentropy
    leye_cla_correct_prediction = tf.equal(
        tf.cast(tf.greater_equal(tf.nn.sigmoid(leye_cla_predict), 0.5), tf.int32),
        tf.cast(leye_cla_label, tf.int32))
    leye_cla_accuracy = tf.reduce_mean(tf.cast(leye_cla_correct_prediction, tf.float32))

    reye_cla_correct_prediction = tf.equal(
        tf.cast(tf.greater_equal(tf.nn.sigmoid(reye_cla_predict), 0.5), tf.int32),
        tf.cast(reye_cla_label, tf.int32))
    reye_cla_accuracy = tf.reduce_mean(tf.cast(reye_cla_correct_prediction, tf.float32))
    mouth_cla_correct_prediction = tf.equal(
        tf.cast(tf.greater_equal(tf.nn.sigmoid(mouth_cla_predict), 0.5), tf.int32),
        tf.cast(mouth_cla_label, tf.int32))
    mouth_cla_accuracy = tf.reduce_mean(tf.cast(mouth_cla_correct_prediction, tf.float32))


    regularization_losses = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), name='l2_loss')


    if cfg.MODEL.pruning:
        bn_l1_loss = []
        bn_reg = slim.l1_regularizer(cfg.MODEL.pruning_bn_reg)
        variables_restore = tf.get_collection(tf.GraphKeys.MODEL_VARIABLES, scope=cfg.MODEL.net_structure)
        for var in variables_restore:
            if 'beta' in var.name:
                bn_l1_loss.append(bn_reg(var))
        l1_loss = tf.add_n(bn_l1_loss, name='l1_loss')

        regularization_losses = regularization_losses + l1_loss


    return loss, leye_loss, reye_loss, mouth_loss, leye_cla_accuracy, reye_cla_accuracy, mouth_cla_accuracy, regularization_losses
示例#21
0
Author: Joshua Powers
"""

import tensorflow as tf
import tensorflow.contrib.slim as slim

# initializers
xavier = tf.contrib.layers.xavier_initializer()
uniform = tf.random_uniform_initializer(minval=-0.003, maxval=0.003)
normal = tf.random_normal_initializer(mean=0.0, stddev=1.0)
truncnorm = tf.truncated_normal_initializer(mean=0.0, stddev=1.0)
constant = tf.constant_initializer(value=0.0)
# regularizers
l2 = slim.l2_regularizer(0.0001)
l1 = slim.l1_regularizer(0.0001)
# activation shorthands
fl32 = tf.float32
relu = tf.nn.relu
relu6 = tf.nn.relu6
elu = tf.nn.elu
tanh = tf.nn.tanh
sigmoid = tf.nn.sigmoid


class ActorNetwork(object):
    """The Actor Network of our Acotr/Critic Model"""
    def __init__(self, sess, state, action, learning_rate, tau, bound):
        self.sess = sess
        self.state_dim = len(state)
        self.action_dim = len(action)
示例#22
0
def train():
    """run training"""

    # get parameter values
    parser = ArgumentParser()
    parser = params.add_trainer_args(parser)
    param_dict = vars(parser.parse_args())
    if not os.path.exists(param_dict["train_chkpt_dir"]):
        os.makedirs(param_dict["train_chkpt_dir"])
    param_yml = os.path.join(param_dict["train_chkpt_dir"], 'train_params.yml')
    with open(param_yml, 'w') as outfile:
        yaml.dump(param_dict, outfile, default_flow_style=False)

    # load batch and make model predictions
    [param_dict, true_labels, logits, layers,
     meta_batch] = models.construct_model(param_dict, is_training=True)

    # create model summaries stats
    [
        pred_labels, correct_bool, prob_vec, entropy, true_prob_score,
        pred_prob_score
    ] = models.model_summarize(true_labels, logits,
                               param_dict['out_label_count'])

    # calculated losses
    true_labels_one_hot = tf.one_hot(true_labels,
                                     depth=param_dict['out_label_count'],
                                     on_value=1,
                                     off_value=0)
    classification_loss = tf.losses.softmax_cross_entropy(
        true_labels_one_hot, logits)
    weights = tf.trainable_variables()
    # l1
    l1_reg = slim.l1_regularizer(float(param_dict['reg_l1_scale']))
    l1_loss = slim.regularizers.apply_regularization(l1_reg,
                                                     weights_list=weights)
    # l2
    l2_reg = slim.l2_regularizer(float(param_dict['reg_l2_scale']))
    l2_loss = slim.regularizers.apply_regularization(l2_reg,
                                                     weights_list=weights)
    # KL
    global KL_SCALE
    global KL_SPARCE
    KL_SCALE = param_dict['reg_kl_scale']
    KL_SPARCE = param_dict['reg_kl_sparsity']
    print("train kl_params: " + str([KL_SCALE, KL_SPARCE]))
    kl_loss = slim.regularizers.apply_regularization(kl_regularizer,
                                                     weights_list=layers)
    total_loss = tf.losses.get_total_loss()
    tf.summary.scalar('batch_optimization/total_loss', total_loss)
    tf.summary.scalar('batch_optimization/classification_loss',
                      classification_loss)
    tf.summary.scalar('batch_optimization/kl_loss', kl_loss)
    tf.summary.scalar('batch_optimization/l1_loss', l1_loss)
    tf.summary.scalar('batch_optimization/l2_loss', l2_loss)

    # create optimizer
    if param_dict['train_optimizer_str'] == "Adam":
        optimizer = tf.train.AdamOptimizer(param_dict['train_learning_rate'])
    else:
        optimizer = tf.train.MomentumOptimizer(
            param_dict['train_learning_rate'])

    # create training op
    train_op = slim.learning.create_train_op(total_loss,
                                             optimizer=optimizer,
                                             summarize_gradients=False)

    # save training parameters
    with open(param_yml, 'w') as outfile:
        yaml.dump(param_dict, outfile, default_flow_style=False)

    # print model parameter stats
    param_stats = tf.contrib.tfprof.model_analyzer.print_model_analysis(
        tf.get_default_graph(),
        tfprof_options=tf.contrib.tfprof.model_analyzer.
        TRAINABLE_VARS_PARAMS_STAT_OPTIONS)
    print('model total_params: %d\n' % param_stats.total_parameters)

    # run training
    error = slim.learning.train(
        train_op,
        param_dict['train_chkpt_dir'],
        number_of_steps=param_dict['train_max_steps'],
        save_summaries_secs=param_dict['train_save_summ_secs'],
        save_interval_secs=param_dict['train_save_ckpt_secs'],
        session_config=tf.ConfigProto(
            gpu_options=tf.GPUOptions(allow_growth=True),
            log_device_placement=False,
            allow_soft_placement=True))
    print("train error: " + str(error))