Ejemplo n.º 1
0
def cal_iou(bboxes_a, bboxes_b):
    '''calculate iou betwwen two groups of bboxes
    args:
        bboxes_a
            a numpy array of bbox coords (with boundary coords)
        bboxes_b
            a numpy array of bbox coords (with boundary coords)
        the two array should have the same shape (None, 4)
    returns:
        a numpy array of shape (None, 1)
    '''

    max_min = tf.min(bboxes_a[:, 2:], bboxes_b[:, 2:])
    min_max = tf.max(bboxes_a[:, :2], bboxes_b[:, :2])

    mul = (max_min - min_max)
    mul = mul * (mul > 0)
    inter = mul[:, 0] * mul[:, 1]

    union = (bboxes_a[:, 2] - bboxes_a[:, 0]) * (bboxes_a[:, 3] - bboxes_a[:, 1]) + \
            (bboxes_b[:, 2] - bboxes_b[:, 0]) * (bboxes_b[:, 3] - bboxes_b[:, 1]) - inter

    iou = inter / union
    
    return iou
Ejemplo n.º 2
0
def softmax(x):
    """
    Compute the softmax function in tensorflow.

    You might find the tensorflow functions tf.exp, tf.reduce_max,
    tf.reduce_sum, tf.expand_dims useful. (Many solutions are possible, so you may
    not need to use all of these functions). Recall also that many common
    tensorflow operations are sugared (e.g. x * y does a tensor multiplication
    if x and y are both tensors). Make sure to implement the numerical stability
    fixes as in the previous homework!

    Args:
        x:   tf.Tensor with shape (n_samples, n_features). Note feature vectors are
                  represented by row-vectors. (For simplicity, no need to handle 1-d
                  input as in the previous homework)
    Returns:
        out: tf.Tensor with shape (n_sample, n_features). You need to construct this
                  tensor in this problem.
    """
    orig_shape = x.shape
    if len(x.shape) > 1:
        # Matrix
        ### YOUR CODE HERE
        x = tf.exp(x - tf.min(x, axis=1, keepdims=True))
        x = x / tf.reduce_max(x, axis=1)
        ### END YOUR CODE
    else:
        # Vector
        ### YOUR CODE HERE
        x = tf.exp(x - np.min(x))
        x = x / np.sum(x)
        ### END YOUR CODE

    assert x.shape == orig_shape
    return x
Ejemplo n.º 3
0
	def _tonemap(self, image, mode='NONE', **kwargs):
		mode = mode.upper()
		if mode=='NONE':
			image_sdr = image
		elif mode=='CLIP_NEGATIVE':
			image_sdr = tf.maximum(image, 0)
		elif mode=='SATURATE':
			image_sdr = tf.clip_by_value(image, 0, 1)
		elif mode=='NORMALIZE':
			min = tf.min(image)
			if min<0: image -= min
			max = tf.max(image)
			if max>0: image_sdr = image/max
		return image_sdr
Ejemplo n.º 4
0
    def build_graph(self):
        with tf.variable_scope('ppo_core'):
            self.state = tf.placeholder(tf.float32, shape=[None, None, self.state_size])

            n_hid = 128

            # Init https://arxiv.org/pdf/1901.03611.pdf
            fan_in = 4
            fan_out = 128
            w_mean = 0
            w_stddev = 2 / fan_out
            W1 = tf.get_variable(
                "W1",
                [fan_in, n_hid],
                tf.float32,
                tf.random_normal_initializer(w_mean, w_stddev)
            )
            b1 = tf.get_variable("b1", [fan_out], tf.float32, tf.constant_initializer(0.))
            # import pdb; pdb.set_trace()
            a1 = tf.matmul(self.state, W1) + b1

            fan_in = 128
            fan_out = 128
            w_mean = 0
            w_stddev = 2 / fan_out
            W2 = tf.get_variable(
                "W2",
                [n_hid, n_hid],
                tf.float32,
                tf.random_normal_initializer(w_mean, w_stddev)
            )
            b2 = tf.get_variable("b2", [fan_out], tf.float32, tf.constant_initializer(0.))
            a2 = tf.matmul(a1, W2) + b2

        with tf.variable_scope('ppo_policy_head'):
            fan_in = 128
            fan_out = 2
            w_mean = 0
            w_stddev = 2 / fan_out
            W_act = tf.get_variable(
                "W_act",
                [n_hid, fan_out],
                tf.float32,
                tf.random_normal_initializer(w_mean, w_stddev)
            )
            b_act = tf.get_variable("b_act", [fan_out], tf.float32, tf.constant_initializer(0.))
            a_act = tf.matmul(a2, W_act) + b_act
            self.policy = tf.nn.softmax(a_act, axis=1)
            self.act_pred = tf.argmax(self.policy, axis=1)

        with tf.variable_scope('ppo_value_f_head'):
            fan_in = 128
            fan_out = 1
            w_mean = 0
            w_stddev = 2 / fan_out
            W_val = tf.get_variable(
                "W_val",
                [n_hid, fan_out],
                tf.float32,
                tf.random_normal_initializer(w_mean, w_stddev)
            )
            b_val = tf.get_variable("b_val", [fan_out], tf.float32, tf.constant_initializer(0.))
            self.val_pred = tf.matmul(a2, W_val) + b_val

        with tf.variable_scope('train'):
            self.ex_rewards = tf.placeholder(tf.float32, shape=[None, None, 1])
            self.policy_old = tf.placeholder(tf.float32, shape=[None, None, 2])
            self.advantages = tf.placeholder(tf.float32, shape=[None, None, 1])

            loss_vf = 1 / 2 * tf.reduce_mean(tf.square(self.val_pred - self.ex_rewards))

            r_t = self.policy[self.act_pred] / self.policy_old[self.act_pred]
            clipped_rt = tf.clip(r_t, 1 - self.epsilon, 1 + self.espilon)
            loss_clip = tf.reduce_mean(tf.min(r_t * self.advantages, clipped_rt * self.advantages))

            loss = loss_clip - self.alpha_1 * loss_vf
Ejemplo n.º 5
0
def gt(a, b):
    return -tf.min(a - b, 0)
Ejemplo n.º 6
0
def prelu(inputs, is_training, scope):
    with tf.variable_scope(scope):
        a = tf.Variable(0.25 * tf.ones([inputs.shape[-1]]), name="a")
        return tf.max(0, inputs) + tf.multiply(a, tf.min(0, inputs))
Ejemplo n.º 7
0
 def min(self, axis: Optional[int]=None) -> 'ITensor':
     return Tensor(native=tf.min(self.native, axis=axis))