示例#1
0
def resnet_v1(inputs,
              blocks,
              num_classes=None,
              is_training=True,
              global_pool=True,
              include_root_block=True,
              reuse=None,
              scope=None,
              normalize_inside=True):
    """Removes output_stride, use pre-defined rate

    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.
    """
    if normalize_inside:
        # if no normalization is used outside, use detectron style normalization
        inputs = _detectron_img_preprocess(inputs)

    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(
                [conv2d, bottleneck, stack_blocks_dense, max_pool2d],
                outputs_collections=end_points_collection):
            with arg_scope([batch_norm], is_training=is_training):
                net = inputs
                if include_root_block:
                    # net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1')
                    net = conv2d(net, 64, 7, 2, scope='conv1')
                    net = max_pool2d(net, 3, 2, scope='pool1')
                net = stack_blocks_dense(net, blocks)
                if global_pool:
                    # Global average pooling.
                    net = math_ops.reduce_mean(net, [1, 2], name='pool5', keepdims=True)
                    net = utils.collect_named_outputs(end_points_collection,
                                                      sc.name+'/gap', net)
                if num_classes is not None:
                    net = conv2d(
                        net,
                        num_classes, 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:
                    end_points['predictions'] = layers_lib.softmax(
                        net, scope='predictions')
                return net, end_points
示例#2
0
    def build_graph(self):
        logit = self.resnet(self._images)
        cross_entropy_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit, labels=self._labels)
        cross_entropy_loss = tf.reduce_mean(cross_entropy_loss, name='cross_entropy_loss')

        # cal l2loss
        l2_costs = []
        for var in tf.trainable_variables():
            if var.op.name.find(r'weight') > 0:
                l2_costs.append(tf.nn.l2_loss(var))
                tf.summary.histogram(var.op.name, var)

#        [l2_costs.append(tf.nn.l2_loss(var)) for var in tf.trainable_variables() if var.op.name.find(r'weight') > 0]

        self.l2loss = self.model_conf.WEIGHT_DECAY_RATE*tf.add_n(l2_costs)
        self.loss = cross_entropy_loss + self.model_conf.WEIGHT_DECAY_RATE*tf.add_n(l2_costs)
        self.prediction = layers.softmax(logit)
        _labels = tf.arg_max(self.prediction, 1)
        self.acc = tf.reduce_mean(tf.to_float(tf.equal(_labels, self._labels)))

        if self._is_training:
            self.build_train_op()

        tf.summary.scalar('loss', self.loss)
        tf.summary.scalar('l2loss', self.l2loss)
        tf.summary.scalar('accuracy', self.acc)
        tf.summary.image('image', self._images, max_outputs=10)
示例#3
0
  def _network_template(self, state):
    """Builds the convolutional network used to compute the agent's Q-values.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
    weights_initializer = slim.variance_scaling_initializer(
        factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

    net = tf.cast(state, tf.float32)
    net = tf.div(net, 255.)
    net = slim.conv2d(
        net, 32, [8, 8], stride=4, weights_initializer=weights_initializer)
    net = slim.conv2d(
        net, 64, [4, 4], stride=2, weights_initializer=weights_initializer)
    net = slim.conv2d(
        net, 64, [3, 3], stride=1, weights_initializer=weights_initializer)
    net = slim.flatten(net)
    net = noisy_dqn_agent.fully_connected(
        net, 512, scope='fully_connected')
    net = noisy_dqn_agent.fully_connected(
        net,
        self.num_actions * self._num_atoms,
        activation_fn=None,
        scope='fully_connected_1')

    logits = tf.reshape(net, [-1, self.num_actions, self._num_atoms])
    probabilities = contrib_layers.softmax(logits)
    q_values = tf.reduce_sum(self._support * probabilities, axis=2)
    return self._get_network_type()(q_values, logits, probabilities)
    def __init__(self, feature_num, class_num, is_training, step=0.001):
        self.weight_decay = 1e-3
        self.bn_params = {
            # Decay for the moving averages.
            'decay': 0.999,
            'center': True,
            'scale': True,
            # epsilon to prevent 0s in variance.
            'epsilon': 0.001,
            # None to force the updates during train_op
            'updates_collections': None,
            'is_training': is_training
        }

        self.feature_num = feature_num
        self.class_num = class_num

        self.X = tf.placeholder(tf.float32, [None, feature_num])
        self.y_ = tf.placeholder(tf.float32, [None, class_num])

        with tf.contrib.framework.arg_scope(
            [layers.convolution2d],
                kernel_size=3,
                stride=1,
                padding='SAME',
                activation_fn=tf.nn.relu,
                normalizer_fn=layers.batch_norm,
                normalizer_params=self.bn_params,
                weights_initializer=layers.variance_scaling_initializer(),
                weights_regularizer=layers.l2_regularizer(self.weight_decay)):
            self.X = tf.reshape(self.X, [-1, 28, 28, 1])

            net = layers.convolution2d(self.X, num_outputs=16, scope='conv1')
            net = layers.max_pool2d(net, kernel_size=2, scope='pool1')
            net = layers.relu(net, num_outputs=16)

            net = layers.convolution2d(net, num_outputs=32, scope='conv2')
            net = layers.max_pool2d(net, kernel_size=2, scope='pool2')
            net = layers.relu(net, num_outputs=32)

            net = layers.flatten(net, [-1, 7 * 7 * 32])
            net = layers.fully_connected(net,
                                         num_outputs=512,
                                         activation_fn=tf.nn.relu,
                                         scope='fc1')
            net = layers.relu(net, num_outputs=512)

            net = layers.fully_connected(net,
                                         num_outputs=self.class_num,
                                         scope='fc2')
            self.y = layers.softmax(net, scope='softmax')

        self.loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(net, self.y_))
        self.optimizer = tf.train.AdamOptimizer(step).minimize(self.loss)

        pred = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.y_, 1))
        self.acc = tf.reduce_mean(tf.cast(pred, tf.float32))

        self.sess = tf.Session()
示例#5
0
    def __init__(self, params):
        """

        :param params: dictionary with fields:
            "N_HIDDEN": number of hidden states
            "N_BINS": number of bins on input/ output
            "WINDOW_LENGTH": number of time-steps in training window
        """
        tf.reset_default_graph()

        self.session = tf.Session()

        self.inputs = tf.placeholder(tf.float32,
                                     (None, None, params['N_BINS']))

        self.cell = tf.contrib.rnn.LSTMCell(params['N_HIDDEN'],
                                            state_is_tuple=True)
        self.batch_size = tf.shape(self.inputs)[1]

        self.h_init = tf.Variable(tf.zeros([1, params['N_HIDDEN']]),
                                  trainable=True)
        self.h_init_til = tf.tile(self.h_init, [self.batch_size, 1])

        self.c_init = tf.Variable(tf.zeros([1, params['N_HIDDEN']]),
                                  trainable=True)
        self.c_init_til = tf.tile(self.c_init, [self.batch_size, 1])

        self.initial_state = LSTMStateTuple(self.c_init_til, self.h_init_til)

        self.rnn_outputs, self.rnn_states = \
            tf.nn.dynamic_rnn(self.cell,
                              self.inputs,
                              initial_state=self.initial_state,
                              time_major=True)

        self.intermediate_projection = layers.fully_connected(
            self.rnn_outputs[params["WINDOW_LENGTH"] - 2, :, :],
            num_outputs=params['N_HIDDEN'])
        self.final_features = layers.linear(self.intermediate_projection,
                                            num_outputs=params["N_BINS"])

        self.predicted_outputs = layers.softmax(self.final_features)

        with tf.variable_scope("train"):
            # get element-wise error
            self.outputs = \
                tf.placeholder(tf.float32, (None, params['N_BINS']))

            self.all_errors = losses.categorical_crossentropy(
                self.outputs, self.predicted_outputs)

            # measure error
            self.error = tf.reduce_mean(self.all_errors)

            self.train_fn = \
                tf.train.AdamOptimizer(learning_rate=params['LEARNING_RATE']) \
                    .minimize(self.error)
示例#6
0
def softmax_prediction(X, opt, is_reuse=None):
    #X shape: batchsize L emb 1
    biasInit = bias_init
    pred_H = layers.conv2d(X,
                           num_outputs=opt.n_words,
                           kernel_size=[1, opt.embed_size],
                           biases_initializer=biasInit,
                           activation_fn=tf.nn.relu,
                           padding='VALID',
                           scope='pred',
                           reuse=is_reuse)  # batch L 1 V
    pred_prob = layers.softmax(pred_H, scope='pred')  # batch L 1 V
    pred_prob = tf.squeeze(pred_prob)  # batch L V
    return pred_prob
示例#7
0
def model(inpt, num_actions, scope, reuse=False):
    """This model takes as input an observation and returns values of all actions."""
    import tensorflow as tf  # need to keep imports here for serialization to work
    import tensorflow.contrib.layers as layers
    with tf.variable_scope(scope, reuse=reuse):
        out = inpt
        out = layers.fully_connected(out,
                                     num_outputs=64,
                                     activation_fn=tf.nn.tanh)
        out = layers.fully_connected(out,
                                     num_outputs=num_actions,
                                     activation_fn=tf.nn.tanh)
        out = layers.softmax(out)
        return out
示例#8
0
    def build_forward(self, x_in):

        with tf.variable_scope(self.name):

            # input layer
            layer = x_in

            # hidden layers
            for _ in range(self.depth):
                layer = layers.relu(layer, self.width)

            # logits and output
            self.logits = layers.linear(layer, self.n_classes)
            self.output = tf.reshape(layers.softmax(self.logits)[:, 1], shape=(-1, 1))

        self.tf_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)
def visualize_landmark_predictions(landmark_predictions,
                                   image_size,
                                   use_softmax=True):
    grid_size = landmark_predictions.shape[1]
    assert grid_size == landmark_predictions.shape[2]
    # Row = [[0, 1, 2, 3...], [0, 1, 2, 3...]...]. Column = [[0, 0, ..], [1, 1,...]...]
    row, column = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
    dtype = landmark_predictions.dtype
    row = tf.cast(row, dtype) / tf.cast(grid_size, dtype)
    column = tf.cast(column, dtype) / tf.cast(grid_size, dtype)
    rc = tf.stack((row, column), axis=-1)
    rc_reshaped = tf.expand_dims(tf.expand_dims(rc, 0), 3)
    rc_reshaped = tf_repeat(rc_reshaped, [
        landmark_predictions.shape[0], 1, 1, landmark_predictions.shape[3], 1
    ])
    if True:
        landmark_confidence_reshaped = tf.transpose(
            landmark_predictions[..., 2], (0, 3, 1, 2))
        landmark_confidence_reshaped = tf.reshape(
            landmark_confidence_reshaped,
            tf.TensorShape([
                landmark_confidence_reshaped.shape[0],
                landmark_confidence_reshaped.shape[1],
                landmark_confidence_reshaped.shape[2] *
                landmark_confidence_reshaped.shape[3],
            ]))
        c = layers.softmax(landmark_confidence_reshaped)
    else:
        c = tf.sigmoid(landmark_predictions[..., 2])
    c = tf.transpose(c, (0, 2, 1))
    # c = tf.reshape(c, tf.TensorShape(
    #   [c.shape[0], grid_size, grid_size, c.shape[2]]))  # [batch size, height, width, num_landmarks]
    xy = rc_reshaped + landmark_predictions[..., 0:2] / int(
        grid_size)  # [batch size, height, width, num_landmarks, 2]
    xy_reshaped = tf.reshape(
        xy,
        tf.TensorShape([
            xy.shape[0], xy.shape[1] * xy.shape[2] * xy.shape[3] * xy.shape[4]
        ]))
    xy_one_hot = xy_to_one_hot(xy_reshaped, image_size)
    xy_one_hot_weighed = xy_one_hot * tf.expand_dims(
        tf.expand_dims(layers.flatten(c), 1), 2)
    xy_one_hot_weighed = tf.reduce_sum(xy_one_hot_weighed,
                                       axis=-1,
                                       keepdims=True)
    return xy_one_hot_weighed
示例#10
0
def rainbow_network(num_actions, num_atoms, support, network_type, state):
    """The convolutional network used to compute agent's Q-value distributions.

    Args:
      num_actions: int, number of actions.
      num_atoms: int, the number of buckets of the value function distribution.
      support: tf.linspace, the support of the Q-value distribution.
      network_type: namedtuple, collection of expected values to return.
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
    weights_initializer = contrib_slim.variance_scaling_initializer(
        factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

    net = tf.cast(state, tf.float32)
    net = tf.div(net, 255.)
    net = contrib_slim.conv2d(net,
                              32, [8, 8],
                              stride=4,
                              weights_initializer=weights_initializer)
    net = contrib_slim.conv2d(net,
                              64, [4, 4],
                              stride=2,
                              weights_initializer=weights_initializer)
    net = contrib_slim.conv2d(net,
                              64, [3, 3],
                              stride=1,
                              weights_initializer=weights_initializer)
    net = contrib_slim.flatten(net)
    net = contrib_slim.fully_connected(net,
                                       512,
                                       weights_initializer=weights_initializer)
    net = contrib_slim.fully_connected(net,
                                       num_actions * num_atoms,
                                       activation_fn=None,
                                       weights_initializer=weights_initializer)

    logits = tf.reshape(net, [-1, num_actions, num_atoms])
    probabilities = contrib_layers.softmax(logits)
    q_values = tf.reduce_sum(support * probabilities, axis=2)
    return network_type(q_values, logits, probabilities)
示例#11
0
    def make_forward_pass(self, input_layer):

        # build the graph in the scope
        with tf.variable_scope(self.name):

            # input layer
            layer = input_layer

            # hidden layers
            for _ in range(int(self.hps['depth'])):
                layer = layers.relu(layer, int(self.hps['n_units']))

            # output layer
            self.logits = layers.linear(layer, self.hps['n_classes'])

            # and an extra layer for getting the predictions directly
            self.proba = tf.reshape(layers.softmax(self.logits)[:, 1],
                                    shape=(-1, 1))

        self.tf_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                         scope=self.name)
示例#12
0
    def __init__(self, lr, s_size, a_size, h_size):
        #These lines established the feed-forward part of the network. The agent takes a state and produces an action.
        self.inputs = tf.placeholder(shape=[None, s_size], dtype=tf.float32)
        self.dropout_ratio = tf.placeholder(shape=(), dtype=tf.float32)
        hidden = layers.fully_connected(self.inputs, h_size)
        hidden = layers.dropout(hidden, self.dropout_ratio)
        hidden2 = layers.fully_connected(hidden, int(h_size / 2))
        hidden2 = layers.dropout(hidden2, self.dropout_ratio)

        output1, output2 = tf.split(hidden2, 2, 1)
        self.advantage_weights = tf.Variable(
            tf.random_normal([int(h_size / 4), a_size]))
        self.value_weights = tf.Variable(tf.random_normal([int(h_size / 4),
                                                           1]))

        self.advantage = tf.matmul(output1, self.advantage_weights)
        self.value = tf.matmul(output2, self.value_weights)

        #Then combine them together to get our final Q-values.
        self.q_values = self.value + tf.subtract(
            self.advantage,
            tf.reduce_mean(self.advantage, reduction_indices=1,
                           keep_dims=True))
        self.q_dist = layers.softmax(self.q_values)
        self.predict = tf.argmax(self.q_values, 1)

        #Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
        self.targetQ = tf.placeholder(shape=[None], dtype=tf.float32)
        self.actions = tf.placeholder(shape=[None], dtype=tf.int32)
        self.actions_onehot = tf.one_hot(self.actions,
                                         a_size,
                                         dtype=tf.float32)

        self.Q = tf.reduce_sum(tf.multiply(self.q_values, self.actions_onehot),
                               axis=1)

        self.error = tf.reduce_mean(tf.square(self.targetQ - self.Q))
        self.trainer = tf.train.AdamOptimizer(learning_rate=0.0001)
        self.updateModel = self.trainer.minimize(self.error)
示例#13
0
def acrobot_rainbow_network(num_actions, num_atoms, support, network_type,
                            state):
    """Build the deep network used to compute the agent's Q-value distributions.

  Args:
    num_actions: int, number of actions.
    num_atoms: int, the number of buckets of the value function distribution.
    support: tf.linspace, the support of the Q-value distribution.
    network_type: `namedtuple`, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
    net = _basic_discrete_domain_network(ACROBOT_MIN_VALS,
                                         ACROBOT_MAX_VALS,
                                         num_actions,
                                         state,
                                         num_atoms=num_atoms)
    logits = tf.reshape(net, [-1, num_actions, num_atoms])
    probabilities = contrib_layers.softmax(logits)
    q_values = tf.reduce_sum(support * probabilities, axis=2)
    return network_type(q_values, logits, probabilities)
示例#14
0
 def call(self, state):
     x = self.net(state)
     logits = tf.reshape(x, [-1, self.num_actions, self.num_atoms])
     probabilities = contrib_layers.softmax(logits)
     q_values = tf.reduce_sum(self.support * probabilities, axis=2)
     return atari_lib.RainbowNetworkType(q_values, logits, probabilities)
示例#15
0
    def __init__(self,
                 num_actions,
                 state_shape=[8, 8, 5],
                 convs=[[32, 4, 2], [64, 2, 1]],
                 fully_connected=[128],
                 activation_fn=tf.nn.relu,
                 optimizers=[
                     tf.train.AdamOptimizer(2.5e-4),
                     tf.train.AdamOptimizer(2.5e-4),
                     tf.train.AdamOptimizer(2.5e-4)
                 ],
                 scope="sac",
                 reuse=False):

        with tf.variable_scope(scope, reuse=reuse):

            ###################### Neural network architecture ######################

            input_shape = [None] + state_shape
            self.input_states = tf.placeholder(dtype=tf.float32,
                                               shape=input_shape)

            with tf.variable_scope("value", reuse=reuse):
                self.v_values = full_module(self.input_states, convs,
                                            fully_connected, 1, activation_fn)

            with tf.variable_scope("qfunc", reuse=reuse):
                self.q_values = full_module(self.input_states, convs,
                                            fully_connected, num_actions,
                                            activation_fn)

            with tf.variable_scope("policy", reuse=reuse):
                self.p_logits = full_module(self.input_states, convs,
                                            fully_connected, num_actions,
                                            activation_fn)
            self.p_values = layers.softmax(self.p_logits)

            ######################### Optimization procedure ########################

            # one-hot encode actions to get q-values for state-action pairs
            self.input_actions = tf.placeholder(dtype=tf.int32, shape=[None])
            actions_onehot = tf.one_hot(self.input_actions,
                                        num_actions,
                                        dtype=tf.float32)
            q_values_selected = tf.reduce_sum(tf.multiply(
                self.q_values, actions_onehot),
                                              axis=1)
            p_logits_selected = tf.reduce_sum(tf.multiply(
                self.p_logits, actions_onehot),
                                              axis=1)

            # choose best actions (according to q-values)
            self.q_argmax = tf.argmax(self.q_values, axis=1)

            # create loss function and update rule
            self.q_targets = tf.placeholder(dtype=tf.float32, shape=[None])
            self.v_targets = tf.placeholder(dtype=tf.float32, shape=[None])
            self.p_targets = tf.placeholder(dtype=tf.float32, shape=[None])

            q_loss = tf.losses.huber_loss(self.q_targets, q_values_selected)
            self.q_loss = tf.reduce_sum(q_loss)
            q_optimizer = optimizers[0]

            v_targets_reshaped = tf.reshape(self.v_targets, (-1, 1))
            v_loss = tf.losses.huber_loss(v_targets_reshaped, self.v_values)
            self.v_loss = tf.reduce_sum(v_loss)
            v_optimizer = optimizers[1]

            p_loss = tf.losses.huber_loss(self.p_targets, p_logits_selected)
            self.p_loss = tf.reduce_sum(p_loss)
            p_optimizer = optimizers[2]

            self.update_q_values = q_optimizer.minimize(self.q_loss)
            self.update_v_values = v_optimizer.minimize(self.v_loss)
            self.update_p_logits = p_optimizer.minimize(self.p_loss)
示例#16
0
    def setup(self):
        l1 = layers.conv2d(inputs=1 - self.data,
                           num_outputs=32,
                           kernel_size=[3, 3],
                           stride=[2, 2])
        l1_bis = layers.conv2d(inputs=l1,
                               num_outputs=64,
                               kernel_size=[3, 3],
                               stride=[2, 2])
        l2 = layers.conv2d(inputs=l1_bis,
                           num_outputs=64,
                           kernel_size=[3, 3],
                           stride=[2, 2])

        l3 = layers.conv2d(
            inputs=l2, num_outputs=128, kernel_size=[3, 3],
            stride=[1, 1])  # layer 3 saves our ass 3 pq pas 8 ou 16 ou plus ?
        self.rpn_cls_score = layers.conv2d(inputs=l3,
                                           num_outputs=len(ratios) *
                                           len(anchor_scales) * 2,
                                           kernel_size=[1, 1],
                                           stride=[1, 1],
                                           padding='VALID',
                                           activation_fn=None)
        self.rpn_bbox_pred = layers.conv2d(inputs=l3,
                                           num_outputs=len(ratios) *
                                           len(anchor_scales) * 4,
                                           kernel_size=[1, 1],
                                           stride=[1, 1],
                                           padding='VALID',
                                           activation_fn=None)

        self.rpn_cls_prob = tf.nn.softmax(self.rpn_cls_score)

        if self.state == "TRAIN":
            self.rpn_labels, self.rpn_bbox_targets, self.rpn_bbox_inside_weights, self.rpn_bbox_outside_weights, debug_info = tf.py_func(
                anchor_target_layer, [
                    self.rpn_cls_score, self.gt_boxes, self.im_info, self.data,
                    _feat_stride, anchor_scales, ratios
                ], [tf.int32, tf.float32, tf.float32, tf.float32, tf.float32])

            self.debug_info = debug_info

        self.rpn_rois = tf.reshape(tf.py_func(proposal_layer, [
            self.rpn_cls_prob, self.rpn_bbox_pred, self.im_info, self.state,
            _feat_stride, anchor_scales, ratios
        ], [tf.float32]), [-1, 5],
                                   name='rpn_rois')

        if self.state == "TRAIN":
            rois, self.labels, self.bbox_targets, self.bbox_inside_weights, self.bbox_outside_weights = tf.py_func(
                proposal_target_layer,
                [self.rpn_rois, self.gt_boxes, n_classes],
                [tf.float32, tf.int32, tf.float32, tf.float32, tf.float32])
            rois = tf.reshape(rois, [-1, 5], name='rois')

        l2_swapped = tf.transpose(l2, perm=[0, 3, 1, 2])
        output_shape_tf = tf.constant((7, 7))

        if self.state == "TRAIN":
            #rois_pooled = roi_pool_op.roi_pool(l2, rois,7,7,1/16.0,name='pool_5')[0]
            new_rois, = tf.py_func(adapt_rois, [rois], [tf.int32])
        else:
            #rois_pooled = roi_pool_op.roi_pool(l2, self.rpn_rois,7,7,1/16.0,name='pool_5')[0]
            # 1. Adapt rois
            new_rois, = tf.py_func(adapt_rois, [self.rpn_rois], [tf.int32])

        rois_pooled_before, argmax = roi_pooling_op_2(l2_swapped, new_rois,
                                                      output_shape_tf)
        rois_pooled_transposed = tf.transpose(rois_pooled_before,
                                              perm=[0, 2, 1, 3, 4])
        rois_pooled = tf.reshape(
            rois_pooled_transposed,
            [-1, 64, 7, 7])  # Be careful ! The final depth is 64
        # output : [batch_size, 7, 7, features_depth]

        l5 = layers.flatten(rois_pooled)
        fc6 = layers.fully_connected(l5, 128)
        fc6 = layers.dropout(fc6, is_training=self.state == "TRAIN")
        fc7 = layers.fully_connected(fc6, 128)
        fc7 = layers.dropout(fc7, is_training=self.state == "TRAIN")
        self.logits = layers.fully_connected(fc7,
                                             n_classes,
                                             activation_fn=None)
        self.cls_prob = layers.softmax(self.logits)

        self.bbox_pred = layers.fully_connected(fc7,
                                                4 * n_classes,
                                                activation_fn=None,
                                                scope='bbox_pred')
示例#17
0
def atari_network(num_actions,
                  num_atoms,
                  support,
                  network_type,
                  state,
                  representation_layer=10):
    """The convolutional network used to compute agent's Q-value distributions.

  Args:
    num_actions: int, number of actions.
    num_atoms: int, the number of buckets of the value function distribution.
    support: tf.linspace, the support of the Q-value distribution.
    network_type: namedtuple, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.
    representation_layer: int, the layer which will be used as the
      representation for computing the bisimulation distances. Defaults to
      a high value, which defaults to the penultimate layer.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
    weights_initializer = contrib_slim.variance_scaling_initializer(
        factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

    curr_layer = 1
    net = tf.cast(state, tf.float32)
    net = tf.div(net, 255.)
    representation = None
    if representation_layer <= curr_layer:
        representation = contrib_slim.flatten(net)
    net = contrib_slim.conv2d(net,
                              32, [8, 8],
                              stride=4,
                              weights_initializer=weights_initializer,
                              trainable=False)
    curr_layer += 1
    if representation is None and representation_layer <= curr_layer:
        representation = contrib_slim.flatten(net)
    net = contrib_slim.conv2d(net,
                              64, [4, 4],
                              stride=2,
                              weights_initializer=weights_initializer,
                              trainable=False)
    curr_layer += 1
    if representation is None and representation_layer <= curr_layer:
        representation = contrib_slim.flatten(net)
    net = contrib_slim.conv2d(net,
                              64, [3, 3],
                              stride=1,
                              weights_initializer=weights_initializer,
                              trainable=False)
    net = contrib_slim.flatten(net)
    curr_layer += 1
    if representation is None and representation_layer <= curr_layer:
        representation = net
    net = contrib_slim.fully_connected(net,
                                       512,
                                       weights_initializer=weights_initializer,
                                       trainable=False)
    curr_layer += 1
    if representation is None:
        representation = net
    net = contrib_slim.fully_connected(net,
                                       num_actions * num_atoms,
                                       activation_fn=None,
                                       weights_initializer=weights_initializer,
                                       trainable=False)

    logits = tf.reshape(net, [-1, num_actions, num_atoms])
    probabilities = contrib_layers.softmax(logits)
    q_values = tf.reduce_sum(support * probabilities, axis=2)
    return network_type(q_values, logits, probabilities, representation)
示例#18
0
    def __init__(self, num_bins=8):
        tf.reset_default_graph()
        self.num_bins = num_bins
        self.x = tf.placeholder(tf.float32, shape=(None, 80,80,1), name="input")
        self.y = tf.placeholder(tf.int32, shape=(None,), name="label")
        self.training = tf.placeholder(tf.bool, name="training")
        # used for calculating average of predicted distribution 
        self.classes = tf.constant(list(range(0,self.num_bins)), dtype=tf.float32, name="classes")

        with tf.name_scope("lay1"):
            filt_num    = 10
            filt_size   = (8,8)
            filt_stride = (2, 2)  # (Height, Width)
            padding     = "same"
            activation  = tf.nn.relu
            # xavier initializer by default :-)
            self.lay1 = layers.conv2d(self.x,   filt_num, filt_size, filt_stride, padding, activation_fn=activation, scope="lay1")

        with tf.name_scope("lay2"):
            filt_num    = 20
            filt_size   = (4,4)
            filt_stride = (2, 2)
            padding     = "same"
            activation  = tf.nn.relu

            self.lay2 = layers.conv2d(self.lay1,   filt_num, filt_size, filt_stride, padding, activation_fn=activation, scope="lay2")

        with tf.name_scope("lay3"):
            filt_num    = 40
            filt_size   = (2,2)
            filt_stride = (1, 1)
            padding     = "same"
            activation  = tf.nn.relu

            self.lay3 = layers.conv2d(self.lay2,   filt_num, filt_size, filt_stride, padding, activation_fn=activation, scope="lay3")
            self.lay3_flat = layers.flatten(self.lay3)

        with tf.name_scope("fc1"):
            neurons = 1600
            activation  = tf.nn.relu
            dropout_rate = 0.6

            self.fc1 = layers.fully_connected(self.lay3_flat, neurons, activation_fn=activation, scope="fc1")
            self.fc1_drop = tf.layers.dropout(self.fc1, rate=dropout_rate, training=self.training, name="drop1")

        with tf.name_scope("fc2"):
            neurons = 160
            activation  = tf.nn.relu
            dropout_rate = 0.6

            self.fc2 = layers.fully_connected(self.fc1_drop, neurons, activation_fn=activation, scope="fc2")
            self.fc2_drop = tf.layers.dropout(self.fc2, rate=dropout_rate, training=self.training, name="drop2")

        with tf.name_scope("output"):
            neurons = self.num_bins
            activation  = None

            self.logits = layers.fully_connected(self.fc2_drop, neurons, activation_fn=activation, scope="lay_logits")
            self.probs = layers.softmax(self.logits, scope="probs")

        with tf.name_scope("loss"):
            self.per_class_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y, logits=self.logits, name="class_loss")
            self.loss = tf.reduce_mean(self.per_class_loss)

        with tf.name_scope("accuracy"):
            '''
            eg: This is not code, it's more like a derivation
                just for my own reference.
            classes    = [0,1,2]  # index of steering angle bins
            probs      = [[0.1, 0.7, 0.2], [0.8, 0.2, 0.0]]
            true       = [1, 0]
            
            Expected value of the pdf output by the softmax opperation
            prediction = [[0.0, 0.7, 0.4], [0.0, 0.2, 0.0]] # classes * probs
            prediction = [1.1, 0.2] # tf.reduce_sum axis=1 
            
            abs_dif    = [0.1, 0.2]  # abs(true - prediction)
            percent_er = [0.1/3, 0.2/3] # where 3 is the number of classes
            acc        = 1 - pervent_er
            mean_acc   = tf.reduce_mean(acc)
            '''
            self.prediction = tf.reduce_sum(tf.multiply(self.probs, self.classes), axis=1)
            abs_diff   = tf.abs(self.prediction - tf.cast(self.y, tf.float32))
            percent_error = abs_diff / tf.cast(tf.shape(self.classes), tf.float32)
            self.accuracy   = 1. - percent_error
            self.mean_accuracy = tf.reduce_mean(self.accuracy)
        
        self.saver = tf.train.Saver()
    def __init__(self,
                 sequence_length,
                 num_classes,
                 model_type,
                 vocab_size,
                 fc_hidden_size,
                 embedding_size,
                 embedding_type,
                 filter_sizes,
                 num_filters,
                 l2_reg_lambda=0.0,
                 pretrained_embedding=None):

        # Placeholders for input, output, dropout_prob and training_tag
        self.input_x_front = tf.placeholder(tf.int32, [None, sequence_length],
                                            name="input_x_front")
        self.input_x_behind = tf.placeholder(tf.int32, [None, sequence_length],
                                             name="input_x_behind")
        self.input_y = tf.placeholder(tf.float32, [None, num_classes],
                                      name="input_y")
        self.dropout_keep_prob = tf.placeholder(tf.float32,
                                                name="dropout_keep_prob")
        self.is_training = tf.placeholder(tf.bool, name="is_training")

        self.global_step = tf.Variable(0, trainable=False, name="Global_Step")

        def cos_sim(input_x1, input_x2):
            norm1 = tf.square(tf.reduce_sum(tf.square(input_x1), axis=1))
            norm2 = tf.square(tf.reduce_sum(tf.square(input_x2), axis=1))
            dot_products = tf.reduce_sum(input_x1 * input_x2,
                                         axis=1,
                                         name="cos_sim")
            return dot_products / (norm1 * norm2)

        def make_attention_mat(input_x1, input_x2):
            # shape of `input_x1` and `input_x2`: [batch_size, embedding_size, sequence_length, 1]
            # input_x2 need to transpose to the [batch_size, embedding_size, 1, sequence_length]
            # shape of output: [batch_size, sequence_length, sequence_length]
            euclidean = tf.sqrt(
                tf.reduce_sum(tf.square(input_x1 -
                                        tf.matrix_transpose(input_x2)),
                              axis=1))
            return 1 / (1 + euclidean)

        def w_pool(input_x, attention, filter_size, scope):
            # input_x: [batch_size, num_filters, sequence_length + filter_size - 1, 1]
            # attention: [batch_size, sequence_length + filter_size - 1]
            if model_type in ['ABCNN2', 'ABCNN3']:
                pools = []

                # [batch_size, 1, sequence_length + filter_size - 1, 1]
                attention = tf.transpose(
                    tf.expand_dims(tf.expand_dims(attention, -1), -1),
                    [0, 2, 1, 3])

                for i in range(sequence_length):
                    # [batch_size, num_filters, filter_size, 1]
                    # reduce_sum => [batch_size, num_filters, 1, 1]
                    pools.append(
                        tf.reduce_sum(input_x[:, :, i:i + filter_size, :] *
                                      attention[:, :, i:i + filter_size, :],
                                      axis=2,
                                      keepdims=True))
                # [batch_size, num_filters, sequence_length, 1]
                w_ap = tf.concat(pools, axis=2, name="w_ap_" + scope)
            else:
                # [batch_size, num_filters, sequence_length, 1]
                w_ap = tf.nn.avg_pool(input_x,
                                      ksize=[1, 1, filter_size, 1],
                                      strides=[1, 1, 1, 1],
                                      padding="VALID",
                                      name="w_ap_" + scope)
            return w_ap

        def all_pool(input_x, filter_size, scope):
            # input_x: [batch_size, num_filters, sequence_length + filter_size -1, 1]
            all_ap = tf.nn.avg_pool(
                input_x,
                ksize=[1, 1, sequence_length + filter_size - 1, 1],
                strides=[1, 1, 1, 1],
                padding="VALID",
                name="all_pool_" + scope)
            all_ap_reshaped = tf.reshape(all_ap, [-1, num_filters])
            return all_ap_reshaped

        def cnn_layer(variable_scope, input_x1, input_x2, dims):
            """
            Args:
                variable_scope: `cnn-1` or `cnn-2`
                input_x1:
                dims: embedding_size in `cnn-1`, num_filters in `cnn-2`
            """

            with tf.name_scope(variable_scope):
                if model_type in ['ABCNN1', 'ABCNN3']:
                    # Attention
                    with tf.name_scope("attention_matrix"):
                        W_a = tf.Variable(tf.truncated_normal(
                            shape=[sequence_length, embedding_size],
                            stddev=0.1,
                            dtype=tf.float32),
                                          name="W_a")
                        # shape of `attention_matrix`: [batch_size, sequence_length, sequence_length]
                        attention_matrix = make_attention_mat(
                            self.embedded_sentence_expanded_front_trans,
                            self.embedded_sentence_expanded_behind_trans)

                        # [batch_size, sequence_length, sequence_length] * [sequence_length, embedding_size]
                        # einsum => [batch_size, sequence_length, embedding_size]
                        # matrix transpose => [batch_size, embedding_size, sequence_length]
                        # expand dims => [batch_size, embedding_size, sequence_length, 1]
                        front_attention = tf.expand_dims(
                            tf.matrix_transpose(
                                tf.einsum("ijk,kl->ijl", attention_matrix,
                                          W_a)), -1)
                        behind_attention = tf.expand_dims(
                            tf.matrix_transpose(
                                tf.einsum(
                                    "ijk,kl->ijl",
                                    tf.matrix_transpose(attention_matrix),
                                    W_a)), -1)

                        # shape of new `embedded_sentence_expanded_front`: [batch_size, sequence_length, embedding_size, 2]
                        self.embedded_sentence_expanded_front = tf.transpose(
                            tf.concat([
                                self.embedded_sentence_expanded_front_trans,
                                front_attention
                            ],
                                      axis=3),
                            perm=[0, 2, 1, 3])
                        self.embedded_sentence_expanded_behind = tf.transpose(
                            tf.concat([
                                self.embedded_sentence_expanded_behind_trans,
                                behind_attention
                            ],
                                      axis=3),
                            perm=[0, 2, 1, 3])

                for filter_size in filter_sizes:
                    with tf.name_scope("conv-filter{0}".format(filter_size)):
                        # Convolution Layer
                        if model_type in ['ABCNN1', 'ABCNN3']:
                            # The in_channels of filter_shape is 2 (two channels, origin + attention)
                            in_channels = 2
                        else:
                            in_channels = 1

                        # shape of new `embedded_sentence_expanded_front`
                        # [batch_size, sequence_length + filter_size - 1, embedding_size, 2]
                        self.embedded_sentence_expanded_front = tf.pad(
                            self.embedded_sentence_expanded_front,
                            np.array([[0,
                                       0], [filter_size - 1, filter_size - 1],
                                      [0, 0], [0, 0]]), "CONSTANT")
                        self.embedded_sentence_expanded_behind = tf.pad(
                            self.embedded_sentence_expanded_behind,
                            np.array([[0,
                                       0], [filter_size - 1, filter_size - 1],
                                      [0, 0], [0, 0]]), "CONSTANT")

                        filter_shape = [
                            filter_size, embedding_size, in_channels,
                            num_filters
                        ]
                        W = tf.Variable(tf.truncated_normal(shape=filter_shape,
                                                            stddev=0.1,
                                                            dtype=tf.float32),
                                        name="W")
                        b = tf.Variable(tf.constant(0.1,
                                                    shape=[num_filters],
                                                    dtype=tf.float32),
                                        name="b")
                        conv_front = tf.nn.conv2d(
                            self.embedded_sentence_expanded_front,
                            W,
                            strides=[1, 1, 1, 1],
                            padding="VALID",
                            name="conv_front")

                        conv_behind = tf.nn.conv2d(
                            self.embedded_sentence_expanded_behind,
                            W,
                            strides=[1, 1, 1, 1],
                            padding="VALID",
                            name="conv_behind")

                        # Batch Normalization Layer
                        conv_bn_front = tf.layers.batch_normalization(
                            tf.nn.bias_add(conv_front, b),
                            training=self.is_training)
                        conv_bn_behind = tf.layers.batch_normalization(
                            tf.nn.bias_add(conv_behind, b),
                            training=self.is_training)

                        # Apply nonlinearity
                        # [batch_size, sequence_length + filter_size - 1, 1, num_filters]
                        conv_out_front = tf.nn.relu(conv_bn_front,
                                                    name="relu_front")
                        conv_out_behind = tf.nn.relu(conv_bn_behind,
                                                     name="relu_behind")

                        # [batch_size, num_filters, sequence_length + filter_size - 1, 1]
                        conv_out_front_trans = tf.transpose(conv_out_front,
                                                            perm=[0, 3, 1, 2])
                        conv_out_behind_trans = tf.transpose(conv_out_behind,
                                                             perm=[0, 3, 1, 2])

                    front_attention_v2, behind_attention_v2 = None, None

                    if model_type in ['ABCNN2', 'ABCNN3']:
                        # [batch_size, sequence_length + filter_size - 1, sequence_length + filter_size - 1]
                        attention_matrix_v2 = make_attention_mat(
                            conv_out_front_trans, conv_out_behind_trans)

                        # [batch_size, sequence_length + filter_size - 1]
                        front_attention_v2 = tf.reduce_sum(attention_matrix_v2,
                                                           axis=2)
                        behind_attention_v2 = tf.reduce_sum(
                            attention_matrix_v2, axis=1)

                    with tf.name_scope("pool-filter{0}".format(filter_size)):
                        # shape of `front_wp`: [batch_size, num_filters, sequence_length, 1]
                        front_wp = w_pool(input_x=conv_out_front_trans,
                                          attention=front_attention_v2,
                                          filter_size=filter_size,
                                          scope="front")
                        behind_wp = w_pool(input_x=conv_out_behind_trans,
                                           attention=behind_attention_v2,
                                           filter_size=filter_size,
                                           scope="behind")

                        # shape of `front_ap`: [batch_size, num_filters]
                        front_ap = all_pool(input_x=conv_out_front_trans,
                                            filter_size=filter_size,
                                            scope="front")
                        behind_ap = all_pool(input_x=conv_out_behind_trans,
                                             filter_size=filter_size,
                                             scope="behind")

                        FI_1, BI_1 = front_wp, behind_wp
                        F0_1, B0_1 = front_ap, behind_ap

        # Embedding Layer
        with tf.device('/cpu:0'), tf.name_scope("embedding"):
            # Use random generated the word vector by default
            # Can also be obtained through our own word vectors trained by our corpus
            if pretrained_embedding is None:
                self.embedding = tf.Variable(tf.random_uniform(
                    [vocab_size, embedding_size], -1.0, 1.0, dtype=tf.float32),
                                             trainable=True,
                                             name="embedding")
            else:
                if embedding_type == 0:
                    self.embedding = tf.constant(pretrained_embedding,
                                                 dtype=tf.float32,
                                                 name="embedding")
                if embedding_type == 1:
                    self.embedding = tf.Variable(pretrained_embedding,
                                                 trainable=True,
                                                 dtype=tf.float32,
                                                 name="embedding")
            self.embedded_sentence_front = tf.nn.embedding_lookup(
                self.embedding, self.input_x_front)
            self.embedded_sentence_behind = tf.nn.embedding_lookup(
                self.embedding, self.input_x_behind)
            self.embedded_sentence_expanded_front = tf.expand_dims(
                self.embedded_sentence_front, -1)
            self.embedded_sentence_expanded_behind = tf.expand_dims(
                self.embedded_sentence_behind, -1)

        self.embedded_sentence_front_trans = tf.transpose(
            self.embedded_sentence_front, perm=[0, 2, 1])
        self.embedded_sentence_behind_trans = tf.transpose(
            self.embedded_sentence_behind, perm=[0, 2, 1])

        # [batch_size, embedding_size, sequence_length, 1]
        self.embedded_sentence_expanded_front_trans = tf.expand_dims(
            self.embedded_sentence_front_trans, -1)
        self.embedded_sentence_expanded_behind_trans = tf.expand_dims(
            self.embedded_sentence_behind_trans, -1)

        # Average-pooling Layer
        with tf.name_scope("input-all-avg_pool"):
            self.embedded_sentence_front_avg_pool = tf.nn.avg_pool(
                self.embedded_sentence_expanded_front,
                ksize=[sequence_length, 1, 1, 1],
                strides=[1, 1, 1, 1],
                padding="VALID",
                name="all_avg_pool_front")

            self.embedded_sentence_behind_avg_pool = tf.nn.avg_pool(
                self.embedded_sentence_expanded_behind,
                ksize=[sequence_length, 1, 1, 1],
                strides=[1, 1, 1, 1],
                padding="VALID",
                name="all_avg_pool_behind")
            # shape of `L0_0` and `R0_0`: [batch_size, embedding_size]
            self.F0_0 = tf.reshape(self.embedded_sentence_front_avg_pool,
                                   [-1, embedding_size])
            self.B0_0 = tf.reshape(self.embedded_sentence_behind_avg_pool,
                                   [-1, embedding_size])

        pooled_outputs_front = []
        pooled_outputs_behind = []
        sims = []

        FI_1, F0_1, BI_1, B0_1 = cnn_layer(variable_scope="CNN-1",
                                           x1=x1_expanded,
                                           x2=x2_expanded,
                                           d=d0)

        pooled_outputs_front.append(F0_1)
        pooled_outputs_behind.append(B0_1)

        # Convolution Layer

        # Combine all the pooled features
        num_filters_total = num_filters * len(filter_sizes)
        self.pool_front = tf.concat(pooled_outputs_front, 1)
        self.pool_behind = tf.concat(pooled_outputs_behind, 1)
        # self.pool_flat_combine = tf.concat([self.pool_flat_front, self.pool_flat_behind], 1)

        sims.append([
            cos_sim(self.F0_0, self.B0_0),
            cos_sim(self.pool_front, self.pool_behind)
        ])
        self.pool_features = tf.transpose(tf.stack(sims, axis=1),
                                          perm=[2, 0, 1])
        print(self.pool_features)

        self.fc_out = fully_connected(
            inputs=self.pool_features,
            num_outputs=fc_hidden_size,
            activation_fn=tf.nn.relu,
            weights_initializer=xavier_initializer(),
            weights_regularizer=l2_regularizer(scale=l2_reg_lambda),
            biases_initializer=tf.constant_initializer(0.1),
            scope="fc")

        print(self.fc_out)
        self.haha = softmax(self.fc_out)[:, 1]
        print(self.haha)

        # Fully Connected Layer
        with tf.name_scope("fc"):
            W = tf.Variable(tf.truncated_normal(
                shape=[num_filters_total * 2, fc_hidden_size],
                stddev=0.1,
                dtype=tf.float32),
                            name="W")
            b = tf.Variable(tf.constant(0.1,
                                        shape=[fc_hidden_size],
                                        dtype=tf.float32),
                            name="b")
            self.fc = tf.nn.xw_plus_b(self.pool_flat_combine, W, b)

            # Batch Normalization Layer
            self.fc_bn = tf.layers.batch_normalization(
                self.fc, training=self.is_training)

            # Apply nonlinearity
            self.fc_out = tf.nn.relu(self.fc_bn, name="relu")

        # Highway Layer
        self.highway = highway(self.fc_out,
                               self.fc_out.get_shape()[1],
                               num_layers=1,
                               bias=0,
                               scope="Highway")

        # Add dropout
        with tf.name_scope("dropout"):
            self.h_drop = tf.nn.dropout(self.highway, self.dropout_keep_prob)

        # Final scores and predictions
        with tf.name_scope("output"):
            W = tf.Variable(tf.truncated_normal(
                shape=[fc_hidden_size, num_classes],
                stddev=0.1,
                dtype=tf.float32),
                            name="W")
            b = tf.Variable(tf.constant(0.1,
                                        shape=[num_classes],
                                        dtype=tf.float32),
                            name="b")
            self.logits = tf.nn.xw_plus_b(self.h_drop, W, b, name="logits")
            self.softmax_scores = tf.nn.softmax(self.logits,
                                                name="softmax_scores")
            self.predictions = tf.argmax(self.logits, 1, name="predictions")
            self.topKPreds = tf.nn.top_k(self.softmax_scores,
                                         k=1,
                                         sorted=True,
                                         name="topKPreds")

        # Calculate mean cross-entropy loss, L2 loss
        with tf.name_scope("loss"):
            losses = tf.nn.softmax_cross_entropy_with_logits_v2(
                labels=self.input_y, logits=self.logits)
            losses = tf.reduce_mean(losses, name="softmax_losses")
            l2_losses = tf.add_n([
                tf.nn.l2_loss(tf.cast(v, tf.float32))
                for v in tf.trainable_variables()
            ],
                                 name="l2_losses") * l2_reg_lambda
            self.loss = tf.add(losses, l2_losses, name="loss")

        # Accuracy
        with tf.name_scope("accuracy"):
            correct_predictions = tf.equal(self.predictions,
                                           tf.argmax(self.input_y, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_predictions,
                                                   "float"),
                                           name="accuracy")

        # Number of correct predictions
        with tf.name_scope("num_correct"):
            correct = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
            self.num_correct = tf.reduce_sum(tf.cast(correct, "float"),
                                             name="num_correct")

        # Calculate Fp
        with tf.name_scope("fp"):
            fp = tf.metrics.false_positives(labels=tf.argmax(self.input_y, 1),
                                            predictions=self.predictions)
            self.fp = tf.reduce_sum(tf.cast(fp, "float"), name="fp")

        # Calculate Fn
        with tf.name_scope("fn"):
            fn = tf.metrics.false_negatives(labels=tf.argmax(self.input_y, 1),
                                            predictions=self.predictions)
            self.fn = tf.reduce_sum(tf.cast(fn, "float"), name="fn")

        # Calculate Recall
        with tf.name_scope("recall"):
            self.recall = self.num_correct / (self.num_correct + self.fn)

        # Calculate Precision
        with tf.name_scope("precision"):
            self.precision = self.num_correct / (self.num_correct + self.fp)

        # Calculate F1
        with tf.name_scope("F1"):
            self.F1 = (2 * self.precision * self.recall) / (self.precision +
                                                            self.recall)

        # Calculate AUC
        with tf.name_scope("AUC"):
            self.AUC = tf.metrics.auc(self.softmax_scores,
                                      self.input_y,
                                      name="AUC")
示例#20
0
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.

  This function generates a family of ResNet v1 models. See the resnet_v1_*()
  methods for specific model instantiations, obtained by selecting different
  block instantiations that produce ResNets of various depths.

  Training for image classification on Imagenet is usually done with [224, 224]
  inputs, resulting in [7, 7] feature maps at the output of the last ResNet
  block for the ResNets defined in [1] that have nominal stride equal to 32.
  However, for dense prediction tasks we advise that one uses inputs with
  spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In
  this case the feature maps at the ResNet output will have spatial shape
  [(height - 1) / output_stride + 1, (width - 1) / output_stride + 1]
  and corners exactly aligned with the input image corners, which greatly
  facilitates alignment of the features to the image. Using as input [225, 225]
  images results in [8, 8] feature maps at the output of the last ResNet block.

  For dense prediction tasks, the ResNet needs to run in fully-convolutional
  (FCN) mode and global_pool needs to be set to False. The ResNets in [1, 2] all
  have nominal stride equal to 32 and a good choice in FCN mode is to use
  output_stride=16 in order to increase the density of the computed features at
  small computational and memory overhead, cf. http://arxiv.org/abs/1606.00915.

  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.conv2d, bottleneck, resnet_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_utils.stack_blocks_dense(net, blocks, output_stride)
        if global_pool:
          # Global average pooling.
          net = math_ops.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)
        if num_classes is not None:
          net = layers.conv2d(
              net,
              num_classes, [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:
          end_points['predictions'] = layers_lib.softmax(
              net, scope='predictions')
        return net, end_points
示例#21
0
    def __init__(self,
                 num_actions,
                 state_shape=[8, 8, 5],
                 convs=[[32, 4, 2], [64, 2, 1]],
                 fully_connected=[128],
                 activation_fn=tf.nn.relu,
                 optimizers=[
                     tf.train.AdamOptimizer(2.5e-4),
                     tf.train.AdamOptimizer(2.5e-4),
                     tf.train.AdamOptimizer(2.5e-4)
                 ],
                 scope="sac",
                 reuse=False):

        with tf.variable_scope(scope, reuse=reuse):

            ################### Neural network architecture ###################

            input_shape = [None] + state_shape
            self.input_states = tf.placeholder(dtype=tf.float32,
                                               shape=input_shape)

            self.v_values = full_module(self.input_states, convs,
                                        fully_connected, 1, activation_fn)
            self.q_values = full_module(self.input_states, convs,
                                        fully_connected, num_actions,
                                        activation_fn)
            self.p_logits = full_module(self.input_states, convs,
                                        fully_connected, num_actions,
                                        activation_fn)
            self.p_values = layers.softmax(self.p_logits)

            ##################### Optimization procedure ######################

            # convert =actions to indices for p-logits and q-values selection
            self.input_actions = tf.placeholder(dtype=tf.int32, shape=[None])
            indices_range = tf.range(tf.shape(self.input_actions)[0])
            action_indices = tf.stack([indices_range, self.input_actions],
                                      axis=1)

            q_values_selected = tf.gather_nd(self.q_values, action_indices)
            p_logits_selected = tf.gather_nd(self.p_logits, action_indices)

            # choose best actions (according to q-values)
            self.q_argmax = tf.argmax(self.q_values, axis=1)

            # define loss function and update rule
            self.q_targets = tf.placeholder(dtype=tf.float32, shape=[None])
            self.v_targets = tf.placeholder(dtype=tf.float32, shape=[None])
            self.p_targets = tf.placeholder(dtype=tf.float32, shape=[None])

            q_loss = tf.losses.huber_loss(self.q_targets, q_values_selected)
            self.q_loss = tf.reduce_sum(q_loss)
            q_optimizer = optimizers[0]

            v_loss = tf.losses.huber_loss(self.v_targets[:, None],
                                          self.v_values)
            self.v_loss = tf.reduce_sum(v_loss)
            v_optimizer = optimizers[1]

            p_loss = tf.losses.huber_loss(self.p_targets, p_logits_selected)
            self.p_loss = tf.reduce_sum(p_loss)
            p_optimizer = optimizers[2]

            self.update_q_values = q_optimizer.minimize(self.q_loss)
            self.update_v_values = v_optimizer.minimize(self.v_loss)
            self.update_p_logits = p_optimizer.minimize(self.p_loss)
示例#22
0
    def __init__(self,
                 nqc,
                 value_encodings,
                 relation_encodings,
                 num_gpus=1,
                 encoder=None):
        """Builds a simple, fully-connected model to predict the outcome set given a query string.

    Args:
      nqc: NeuralQueryContext
      value_encodings: (bert features for values, length of value span)
      relation_encodings: (bert features for relations, length of relation span)
      num_gpus: number of gpus for distributed computation
      encoder: encoder (layers.RNN) for parameter sharing between train and dev

    Needs:
      self.input_ph: input to encoder (either one-hot or BERT layers)
      self.mask_ph: mask for the input
      self.correct_set_ph.name: target labels (if loss or accuracy is computed)
      self.prior_start: sparse matrix for string similarity features
      self.is_training: whether the model should is training (for dropout)

    Exposes:
      self.loss: objective for loss
      self.accuracy: mean accuracy metric (P_{predicted}(gold))
      self.accuracy_per_ex: detailed per example accuracy
      self.log_nql_pred_set: predicted entity set (in nql)
      self.log_decoded_relations: predicted relations (as indices)
      self.log_start_values: predicted start values (in nql)
      self.log_start_cmps: components of predicted start values (in nql)
    """
        # Encodings should have the same dimensions
        assert value_encodings[0].shape[-1] == relation_encodings[0].shape[-1]
        self.context = nqc
        self.input_ph = tf.placeholder(tf.float32,
                                       shape=(None, FLAGS.max_query_length,
                                              value_encodings[0].shape[-1]),
                                       name="oh_seq_ph")
        self.mask_ph = tf.placeholder(tf.float32,
                                      shape=(None, FLAGS.max_query_length),
                                      name="oh_mask_ph")
        self.debug = None
        layer_size = FLAGS.layer_size
        num_layers = FLAGS.num_layers
        max_properties = FLAGS.max_properties
        logits_strategy = FLAGS.logits
        dropout_rate = FLAGS.dropout

        inferred_batch_size = tf.shape(self.input_ph)[0]
        self.is_training = tf.placeholder(tf.bool, shape=[])
        value_tensor = util.reshape_to_tensor(value_encodings[0],
                                              value_encodings[1])
        relation_tensor = util.reshape_to_tensor(relation_encodings[0],
                                                 relation_encodings[1])
        # The last state of LSTM encoder is the representation of the input string
        with tf.variable_scope("model"):
            # Build all the model parts:

            #   encoder: LSTM encoder
            #   prior: string features
            #   {value, relation}_similarity: learned embedding similarty
            #   decoder: LSTM decoder
            #   value_model: map from encoder to key for attention
            #   attention: Luong (dot product) attention

            # Builds encoder - note that this is in keras
            self.encoder = self._build_encoder(encoder, layer_size, num_layers)

            # Build module to turn prior (string features) into logits
            self.prior_start = tf.sparse.placeholder(
                tf.float32,
                name="prior_start_ph",
                shape=[inferred_batch_size, value_tensor.shape[1]])

            with tf.variable_scope("prior"):
                prior = Prior()

            # Build similarity module - biaffine qAr
            with tf.variable_scope("value_similarity"):
                value_similarity = Similarity(layer_size, value_tensor,
                                              num_gpus)
            # Build relation decoder
            with tf.variable_scope("relation_decoder"):
                rel_dec_rnn_layers = [
                    contrib_rnn.LSTMBlockCell(layer_size,
                                              name=("attr_lstm_%d" % i))
                    for (i, layer_size) in enumerate([layer_size] * num_layers)
                ]
                relation_decoder_cell = tf.nn.rnn_cell.MultiRNNCell(
                    rel_dec_rnn_layers)
                tf.logging.info(
                    "relation decoder lstm has state of size: {}".format(
                        relation_decoder_cell.state_size))

            # Build similarity module - biaffine qAr
            with tf.variable_scope("relation_similarity"):
                relation_similarity = Similarity(layer_size, relation_tensor,
                                                 1)
            with tf.variable_scope("attention"):
                attention = layers.Attention()
            value_model = tf.get_variable(
                "value_transform",
                shape=[layer_size, relation_decoder_cell.output_size],
                trainable=True)

        # Initialization for logging, variables shouldn't be used elsewhere
        log_decoded_starts = []
        log_start_logits = []
        log_decoded_relations = []

        # Initialization to prepare before first iteration of loop
        prior_logits_0 = prior.compute_logits(
            tf.sparse.to_dense(self.prior_start))
        cumulative_entities = nqc.all("id_t")
        relation_decoder_out = tf.zeros([inferred_batch_size, layer_size])
        encoder_output = self.encoder(self.input_ph, mask=self.mask_ph)
        query_encoder_out = encoder_output[0]
        relation_decoder_state = encoder_output[1:]

        # Initialization for property loss, equal to log vars but separating
        value_dist = []
        relation_dist = []

        for i in range(max_properties):
            prior_logits = tf.layers.dropout(prior_logits_0,
                                             rate=dropout_rate,
                                             training=self.is_training)
            # Use the last state to determine key; more stable than last output
            query_key = tf.nn.relu(
                tf.matmul(
                    tf.expand_dims(relation_decoder_state[-1][-1], axis=1),
                    value_model))

            query_emb = tf.squeeze(attention(
                [query_key, query_encoder_out],
                mask=[None, tf.cast(self.mask_ph, tf.bool)]),
                                   axis=1)

            similarity_logits = value_similarity.compute_logits(query_emb)
            if logits_strategy == "prior":
                total_logits = prior_logits
            elif logits_strategy == "sim":
                total_logits = similarity_logits
            elif logits_strategy == "mixed":
                total_logits = prior_logits + similarity_logits
            total_dist = contrib_layers.softmax(total_logits)
            values_pred = nqc.as_nql(total_dist, "val_g")
            with tf.variable_scope("start_follow_{}".format(i)):
                start_pred = nqc.all("v_t").follow(
                    values_pred)  # find starting nodes

            # Given the previous set of attributes, where are we going?
            (relation_decoder_out,
             relation_decoder_state) = relation_decoder_cell(
                 relation_decoder_out, relation_decoder_state)
            pred_relation = tf.nn.softmax(
                relation_similarity.compute_logits(relation_decoder_out))
            if FLAGS.enforce_type:
                if i == 0:
                    is_adjust = nqc.as_tf(nqc.one(IS_A, "rel_g"))
                else:
                    is_adjust = 1 - nqc.as_tf(nqc.one(IS_A, "rel_g"))
                pred_relation = pred_relation * is_adjust
            nql_pred_relation = nqc.as_nql(pred_relation, "rel_g")
            # Conjunctive (& start.follow() & start.follow()...).
            with tf.variable_scope("relation_follow_{}".format(i)):
                current_entities = start_pred.follow(nql_pred_relation)
            cumulative_entities = cumulative_entities & current_entities

            # For property loss and regularization
            value_dist.append(total_dist)
            relation_dist.append(pred_relation)

            # Store predictions for logging
            log_decoded_starts.append(start_pred)
            log_decoded_relations.append(pred_relation)
            log_start_logits.append([prior_logits, similarity_logits])

        (loss, pred_set_tf,
         pred_set_tf_norm) = self._compute_loss(cumulative_entities)
        property_loss = self._compute_property_loss(value_dist, relation_dist)
        (accuracy_per_ex,
         accuracy) = self._compute_accuracy(cumulative_entities, pred_set_tf)
        value_loss = self._compute_distribution_regularizer(value_dist)
        relation_loss = self._compute_distribution_regularizer(relation_dist)
        self.regularization = FLAGS.time_reg * (value_loss + relation_loss)
        self.loss = loss - self.regularization
        self.property_loss = property_loss
        self.accuracy_per_ex = accuracy_per_ex
        self.accuracy = accuracy

        # Debugging/logging information
        log_decoded_relations = tf.transpose(tf.stack(log_decoded_relations),
                                             [1, 0, 2])
        tf.logging.info("decoded relations has shape: {}".format(
            log_decoded_relations.shape))
        self.log_start_values = log_decoded_starts
        self.log_start_cmps = [[
            nqc.as_nql(logits, "val_g") for logits in comp
        ] for comp in log_start_logits]
        self.log_decoded_relations = tf.nn.top_k(log_decoded_relations, k=5)
        self.log_nql_pred_set = nqc.as_nql(pred_set_tf_norm, "id_t")
    def _network_template(self, state):
        """Builds a convolutional network that outputs Q-value distributions.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
        weights_initializer = slim.variance_scaling_initializer(factor=1.0 /
                                                                np.sqrt(3.0),
                                                                mode='FAN_IN',
                                                                uniform=True)

        with tf.variable_scope('body'):
            net = tf.cast(state, tf.float32)
            net = tf.div(net, 255.)
            net = slim.conv2d(net,
                              int(32 * self.network_size_expansion), [8, 8],
                              stride=4,
                              weights_initializer=weights_initializer)
            net = slim.conv2d(net,
                              int(64 * self.network_size_expansion), [4, 4],
                              stride=2,
                              weights_initializer=weights_initializer)
            net = slim.conv2d(net,
                              int(64 * self.network_size_expansion), [3, 3],
                              stride=1,
                              weights_initializer=weights_initializer)
            net = slim.flatten(net)
            body_net = slim.fully_connected(
                net,
                int(512 * self.network_size_expansion),
                weights_initializer=weights_initializer)

        logits = []
        probabilities = []
        q_values = []
        with tf.variable_scope('head'):
            for _ in range(self.number_of_gammas):
                net = slim.fully_connected(
                    body_net,
                    self.num_actions * self._num_atoms,
                    activation_fn=None,
                    weights_initializer=weights_initializer)

                gamma_logits = tf.reshape(
                    net, [-1, self.num_actions, self._num_atoms])
                gamma_probabilities = contrib_layers.softmax(gamma_logits)
                gamma_q_values = tf.reduce_sum(self._support *
                                               gamma_probabilities,
                                               axis=2)

                # Add one for each gamma being used.
                logits.append(gamma_logits)
                probabilities.append(gamma_probabilities)
                q_values.append(gamma_q_values)

        # Estimate the hyperbolic discounted q-values.
        hyp_q_values = agent_utils.integrate_q_values(q_values,
                                                      self.integral_estimate,
                                                      self.eval_gammas,
                                                      self.number_of_gammas,
                                                      self.gammas)

        return self._get_network_type()(hyp_q_values, q_values, logits,
                                        probabilities)
示例#24
0
    def _create_model(self):

        self.input_image = tf.placeholder(tf.float32, shape=(None, None, None, self.img_input_channels), name='input_image_placeholder')
        self.gt_image = tf.placeholder(tf.int32, shape=(None, None, None, self.num_classes), name='gt_image_placeholder')
        self.gt_contours = tf.placeholder(tf.int32, shape=(None, None, None, self.num_classes), name='gt_contours_placeholder')
        self.dropout_prob = tf.placeholder(dtype=tf.float32, shape=None, name='dropout_prob_placeholder')

        self.lr = tf.placeholder(dtype=tf.float32, shape=None, name='learning_rate_placeholder')

        scale_nc = self.hps.get('scale_nc')

        with tf.variable_scope("encoder"):
            with tf.variable_scope("block_1"):
                conv1 = self._add_common_layers(conv2d(self.input_image, filters=32*scale_nc, kernel_size=3, padding='same'))
                conv2 = self._add_common_layers(conv2d(conv1, filters=32*scale_nc, kernel_size=3, padding='same'))

            with tf.variable_scope("block_2"):
                mp2 = max_pooling2d(conv2, pool_size=2, strides=2, padding='same')
                bn1 = self._add_common_layers(self._bottleneck(mp2, size=64*scale_nc))
                bn2 = self._add_common_layers(self._bottleneck(bn1, size=64*scale_nc))

            with tf.variable_scope("block_3"):
                mp3 = max_pooling2d(bn2, pool_size=2, strides=2, padding='same')
                bn3 = self._add_common_layers(self._bottleneck(mp3, size=128*scale_nc))
                bn4 = self._add_common_layers(self._bottleneck(bn3, size=128*scale_nc))

            with tf.variable_scope("block_4"):
                mp4 = max_pooling2d(bn4, pool_size=2, strides=2, padding='same')
                bn5 = self._add_common_layers(self._bottleneck(mp4, size=256*scale_nc))
                bn6 = self._add_common_layers(self._bottleneck(bn5, size=256*scale_nc))
                d1 = dropout(bn6, rate=self.dropout_prob)

            with tf.variable_scope("block_5"):
                mp5 = max_pooling2d(d1, pool_size=2, strides=2, padding='same')
                bn7 = self._add_common_layers(self._bottleneck(mp5, size=256*scale_nc))
                bn8 = self._add_common_layers(self._bottleneck(bn7, size=256*scale_nc))
                d2 = dropout(bn8, rate=self.dropout_prob)

            with tf.variable_scope("block_6"):
                mp6 = max_pooling2d(d2, pool_size=2, strides=2, padding='same')
                bn9 = self._add_common_layers(self._bottleneck(mp6, size=256*scale_nc))
                bn10 = self._add_common_layers(self._bottleneck(bn9, size=256*scale_nc))
                d3 = dropout(bn10, rate=self.dropout_prob)

        self.img_descriptor = tf.reduce_mean(d3, axis=(1, 2))

        with tf.variable_scope("decoder_seg"):
            deconvs = []
            deconvs.append(conv2d(conv2, filters=self.num_classes, kernel_size=3,
                                  padding='same'))
            deconvs.append(self._upsample(bn2, k=1))
            deconvs.append(self._upsample(bn4, k=2))
            deconvs.append(self._upsample(d1, k=3))
            deconvs.append(self._upsample(d2, k=4))
            deconvs.append(self._upsample(d3, k=5))

            concat = tf.concat(deconvs, axis=3)

            conv3 = conv2d(concat, filters=self.num_classes, kernel_size=3, padding='same')
            ac1 = self._add_common_layers(conv3)

            conv4 = conv2d(ac1, filters=self.num_classes, kernel_size=1, padding='same')
            ac2 = self._add_common_layers(conv4)

            self.preds_seg = softmax(ac2)

        with tf.variable_scope("decoder_cont"):
            deconvs = []
            deconvs.append(conv2d(conv2, filters=self.num_classes, kernel_size=3,
                                  padding='same'))
            deconvs.append(self._upsample(bn2, k=1))
            deconvs.append(self._upsample(bn4, k=2))
            deconvs.append(self._upsample(d1, k=3))
            deconvs.append(self._upsample(d2, k=4))
            deconvs.append(self._upsample(d3, k=5))

            concat = tf.concat(deconvs, axis=3)

            conv3 = conv2d(concat, filters=self.num_classes, kernel_size=3, padding='same')
            ac1 = self._add_common_layers(conv3)

            conv4 = conv2d(ac1, filters=self.num_classes, kernel_size=1, padding='same')
            ac2 = self._add_common_layers(conv4)

            self.preds_cont = softmax(ac2)

        cond1 = tf.greater_equal(self.preds_seg, self.threshold)
        cond2 = tf.less(self.preds_cont, self.threshold)

        conditions = tf.logical_and(cond1, cond2)

        self.preds = tf.where(conditions, tf.ones_like(conditions), tf.zeros_like(conditions))

        self._add_train_op()

        self.summaries = tf.summary.merge_all()
示例#25
0
def my_digit_classifier(x: (None, 28 * 28)):  # specify shape as (None, 28*28)
    y = layers.fully_connected(x, 100)
    z = layers.fully_connected(y, 10, None)
    p = layers.softmax(z)
    return x, y, z, p
# Fully connected layer 2
net = lays.fully_connected(net, num_outputs=256, activation_fn=tf.nn.tanh)
print(net.shape)

# Apply dropout to reduce overfitting
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(net, keep_prob)

# Matmul layer
W_fc2 = weight_variable([256, numClasses])
b_fc2 = bias_variable([numClasses])
matmulResult = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
print(matmulResult.shape)

# Softmax layer
net = lays.softmax(matmulResult)
print(net.shape)

# Create session and initialise variables
sess = tf.Session()

# Train and evaluate the model
cross_entropy = tf.reduce_mean(
    -tf.reduce_sum(y_ * tf.log(net), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(learningRate).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(net, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())

# Create the saver
saver = tf.train.Saver(tf.trainable_variables())