Exemple #1
0
    def __call__(self,
                 current_model,
                 data_pool=None,
                 excluded_indexes=None,
                 loss_function=None,
                 n_classes=None,
                 n_samples=10,
                 batch_size=32):

        # check if data pool is empty or not.
        if data_pool is None and self.data_pool is None:
            raise AssertionError("data pool is empty!")

        data_pool_ = data_pool if data_pool is not None else self.data_pool

        if excluded_indexes is not None:
            excluded_indexes_ = excluded_indexes
        elif self.excluded_indexes is not None:
            excluded_indexes_ = self.excluded_indexes
        else:
            excluded_indexes_ = []

        n_data_pool = len(list(data_pool))
        candidates = [
            i for i in range(0, n_data_pool) if i not in excluded_indexes_
        ]

        x = tf.convert_to_tensor(data_pool_[candidates])

        preds = current_model(x)
        sorted_preds = tf.sort(preds, axis=1, direction="DESCENDING")

        # compute margin
        margin = sorted_preds[:, 0] - sorted_preds[:, 1]

        indexes = heapq.nsmallest(n_samples, range(len(candidates)),
                                  margin.__getitem__)

        samples = data_pool_[indexes]

        return indexes, samples
        def forward(Y, U, PI, mode='train', remove_diag=True):
            if mode == 'train':
                U = tf.gather(U, indices=np.where(labeledIndexes)[0], axis=0)
                Y = tf.gather(Y, indices=np.where(labeledIndexes)[0], axis=0)
                #F = tf.gather(F,indices=np.where(labeledIndexes)[0],axis=0)

                PI = tf.gather(PI, indices=np.where(labeledIndexes)[0], axis=0)

            pi_Y = spd_matmul(to_sp_diag(tf.abs(PI)), Y)

            alpha = get_alpha(MU)
            """
                Maybe apply custom convolution to LAMBDA, otherwise just fit LGC's alpha using the corresponding filter 1/(1-alpha + alpha*lambda)
            """
            if not self.custom_conv:
                lambda_tilde = tf.math.reciprocal(1 - alpha + alpha * LAMBDA)
            else:
                #lambda_tilde = tf.math.reciprocal(1-alpha + alpha*LAMBDA)
                _lambda = (LAMBDA -
                           tf.reduce_mean(LAMBDA)) / tf.math.reduce_std(LAMBDA)
                lambda_tilde = tf.clip_by_value(
                    2 * tf.nn.sigmoid(
                        tf.reshape(model(_lambda[None, :, None]), (-1, ))), 0,
                    1)
                lambda_tilde = tf.sort(lambda_tilde, direction='DESCENDING')
            lambda_tilde = tf.reshape(divide_by_row(lambda_tilde[None, :]),
                                      (-1, ))

            _self_infl = mult_each_row_by(
                tf.square(U), by=lambda_tilde
            )  #Square each element of U, then dot product of each row with lambda_tilde
            _self_infl = tf.reduce_sum(_self_infl, axis=1)

            _P_op = U @ (mult_each_col_by(
                (tf.transpose(U) @ pi_Y), by=lambda_tilde))
            if not remove_diag:
                _diag_P_op = tf.zeros_like(
                    mult_each_col_by(pi_Y, by=_self_infl))
            else:
                _diag_P_op = mult_each_col_by(pi_Y, by=_self_infl)
            return divide_by_row(_P_op - _diag_P_op), lambda_tilde, pi_Y
Exemple #3
0
    def build_decoder(self):
        """
        Builds the decoder network.
        The decoder network is the generative model mapping:
            
            [z,cond] to xhat

        """
        # Input layer
        z_samp = tfkl.Input((self.nlatent, ), name='z')
        cond = tfkl.Input((self.ncond, ), name='cond')
        z_cond = tfkl.Concatenate(name='z_cond')([z_samp, cond])

        # Hidden layers
        layer_names = []
        h = z_cond
        for i in range(len(self.nunits_enc)):
            h = tfkl.Dense(self.nunits_enc[i], activation='sigmoid',\
                           bias_initializer=None, name='FC%d' % i)(h)
            layer_names.append('FC%d' % i)

        # Add the output mean with optional sorting
        x_mu = tfkl.Dense(self.ndat, name='x_mu',\
                          bias_initializer=None)(h)
        if self.sort_out:
            x_mu = tf.sort(x_mu, direction='DESCENDING', axis=-1)

        # Add the output variance.
        x_log_var = tfkl.Dense(self.ndat, name='x_log_var')(h)
        x_log_var = tf.maximum(x_log_var, np.log(self.out_var_min))

        # Build the decoder
        self.decoder = tfkm.Model([z_samp, cond], [x_mu, x_log_var])

        # Set the initialization
        set_initialization(self.decoder, layer_names,\
                           self.init_kernel_stddev, self.init_bias_stddev)

        # Build the decoder with sampling
        x_samp = self.reparm(x_mu, x_log_var)
        self.sampler = tfkm.Model([z_samp, cond], x_samp)
def calc_precision(qry_num, ref_num, reference_labels_idx, DistMatrix, labels,
                   topK):
    precision = tf.constant(0, dtype=tf.float32)
    for qry_i in range(qry_num):
        qry_label = labels[qry_i]
        precision_one = tf.constant(0, dtype=tf.float32)
        buf = DistMatrix[qry_i]
        buf = tf.sort(buf, direction='DESCENDING')

        assert topK < ref_num, "ERROR:topk >= ref_num"

        threshold = buf[topK - 1]
        for k in range(len(reference_labels_idx[qry_label])):
            ref_idx = reference_labels_idx[qry_label][k]
            if DistMatrix[qry_i][ref_idx] > threshold:
                precision_one += 1
        if precision_one > topK:
            precision_one = topK
        precision += precision_one / topK
    precision = precision / qry_num
    return precision
Exemple #5
0
    def call(self, y_true, norm_logits):
        pick_cond = tf.cast(y_true, dtype=tf.bool)
        y_pred_vals = norm_logits[pick_cond]
        theta = tf.acos(y_pred_vals)
        med_pos = tf.shape(norm_logits)[0] // 2 - 1
        theta_med = tf.sort(theta)[med_pos]

        B_avg = tf.where(pick_cond, tf.zeros_like(norm_logits),
                         tf.exp(self.scale * norm_logits))
        B_avg = tf.reduce_mean(tf.reduce_sum(B_avg, axis=1))
        self.scale.assign(
            tf.math.log(B_avg) /
            tf.cos(tf.minimum(self.theta_med_max, theta_med)))
        tf.print(", scale =", self.scale, ", theta_med =", theta_med, end="")

        arcface_logits = norm_logits * self.scale
        return tf.keras.losses.categorical_crossentropy(
            y_true,
            arcface_logits,
            from_logits=self.from_logits,
            label_smoothing=self.label_smoothing)
Exemple #6
0
def nucleus_sampling(logits, p=0.9):
    sorted_logits = tf.sort(logits, direction='DESCENDING')
    sorted_indices = tf.argsort(logits, direction='DESCENDING')
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits))
    t_sorted_indices_to_remove = cumulative_probs > p
    ''' Shift the indices to the right to keep also the first token above the threshold '''
    indices = tf.range(1, tf.shape(logits)[0], 1)
    sorted_indices_to_remove = tf.scatter_nd(tf.expand_dims(indices, 1),
                                             t_sorted_indices_to_remove[:-1],
                                             logits.shape)
    # indices_to_remove = tf.boolean_mask(sorted_indices, sorted_indices_to_remove)
    # t = tf.ones(tf.shape(indices_to_remove)[0], dtype=tf.bool)
    # to_remove = tf.scatter_nd(indices_to_remove, t, logits.shape)
    logits = tf.where(sorted_indices_to_remove,
                      tf.ones_like(logits, dtype=logits.dtype) * -1e10, logits)
    logits = tf.reshape(logits, (h_parms.batch_size, -1))
    sample = tf.random.categorical(logits,
                                   num_samples=1,
                                   dtype=tf.int32,
                                   seed=1)
    return sample
Exemple #7
0
    def make_reg_label(label, patch_pos, patch_size):
        '''
        Return absolute distance from the middle of the patch
        to the closest threshhold between labels
        '''

        label = tf.sort(label, direction='DESCENDING')
        reg_label_=[]
        threshold_=[]

        for idx in range(config['body_identification_n_classes'] - 1):
            threshold_.append( (label[idx] + label[idx + 1]) / 2)

        threshold= tf.stack(threshold_,axis=0)

        for idx in range(patch_pos.shape[0]):
            temp = tf.abs(threshold - patch_pos[idx][2] - patch_size[2] / 2)
            reg_label_.append([tf.reduce_min(temp)])

        reg_label = tf.stack(reg_label_, axis=0)
        return reg_label
Exemple #8
0
def mutual_information_gap_batch_estimate(labels, samples, encoding_dist,
                                          *encoding_parameters):
    """Estimates mutual information gap (MIG)
    1/K sum_{k=1}^K 1/H(v_k) (I(z_j[k]; v_k) - max_{j !=j[k]} I(z_j;v_k))

    Args:
        labels: Factor values for samples ∊ ℝ (N, D)
        samples: z ∼ q(z|x) ∊ ℝ (N, D)
        encoding_dist: q(z|x)
        *encoding_parameters: list of parameters ∊ ℝ [N]

    Returns:
        (tf.Tensor) ∊ ℝ []
    """
    # I_n(z_j; v_k) = (H(z_j) - H(z_j | v_k)) / H(v_k) ∊ ℝ [D, K]
    nmi = normalized_mutual_information(labels, samples, encoding_dist,
                                        *encoding_parameters)
    nmi = tf.sort(nmi, axis=0, direction="DESCENDING")
    # ∊ ℝ [K]
    mig = nmi[0, :] - nmi[1, :]
    return tf.reduce_mean(mig)
def flat_perc(logits, p):
    sps = tf.sort(tf.nn.softmax(logits, axis=1),
                  direction='DESCENDING',
                  axis=1)
    indices = tf.argsort(logits, direction='DESCENDING', axis=1)
    tail_ids = tf.cast(
        sps.shape[1].value -
        tf.argmax(tf.cast(tf.greater(tf.reverse(sps, axis=[1]), p), tf.int8),
                  axis=1), tf.int32)

    logit_inds = tf.stack([tf.range(0, logits.shape[0].value), tail_ids],
                          axis=1)
    tail_min_vals = tf.expand_dims(tf.gather_nd(logits, logit_inds), 1)

    return tf.where(
        logits <
        tail_min_vals,  # if it is worse than this lower bound. I can do this for my ones too! does it for each batch simultaneously.
        tf.ones_like(logits, dtype=logits.dtype) * -1e10,
        logits,
    )
    '''while_condition = lambda i, logits_to_return: tf.less(i, logits.shape[0].value)
Exemple #10
0
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1),
                                 axis=-1)
    indices = tf.stack(
        [
            tf.range(0, batch),
            # number of indices to include
            tf.maximum(
                tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32),
                              axis=-1) - 1, 0),
        ],
        axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits < min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    )
Exemple #11
0
def compare_with_tensorflow(device_type, in_shape, axis, direction, data_type):
    assert device_type in ["gpu", "cpu"]
    assert data_type in ["float32", "double", "int8", "int32", "int64"]
    flow.clear_default_session()
    func_config = flow.FunctionConfig()
    func_config.default_logical_view(flow.scope.mirrored_view())
    func_config.default_data_type(flow.float)

    @flow.global_function(function_config=func_config)
    def SortJob(input: oft.ListNumpy.Placeholder(
        tuple([dim + 10 for dim in in_shape]),
        dtype=type_name_to_flow_type[data_type],
    )):
        with flow.scope.placement(device_type, "0:0"):
            return flow.sort(input, axis, direction)

    input = (np.random.random(in_shape) * 100).astype(
        type_name_to_np_type[data_type])
    of_out = SortJob([input]).get().numpy_list()[0]
    tf_out = tf.sort(input, axis, direction)
    assert np.array_equal(of_out, tf_out.numpy())
 def predict(mode):
     PREDS = []
     CONFS = []
     NUM_SELECT = 10
     batch_size = 320
     for batch_id, X in enumerate(batch_generator(mode,batch_size=batch_size,loop=False)):
         x = X[0]
         print("Predicting {} - Batch {}".format(mode,batch_id))
         pred = model.predict_on_batch(x)
         if batch_id == 0:
             print(pred)
         PREDS.append(tf.argsort(pred,axis=-1)[:,-NUM_SELECT:])
         CONFS.append(tf.sort(pred,axis=-1)[:,-NUM_SELECT:])
         
     PREDS = np.concatenate(PREDS,axis=0)
     CONFS = np.concatenate(CONFS,axis=0)
     PREDS = np.concatenate([PREDS,CONFS],axis=1)
     cols = ['pred_{}'.format(k) for k in range(NUM_SELECT)] + \
      ['conf_{}'.format(k) for k in range(NUM_SELECT)] 
     fname = os.path.join(DATA_DIR,'dom_pred_{}.csv'.format(mode))
     pd.DataFrame(PREDS,index=range(PREDS.shape[0]),columns=cols).to_csv(fname)
Exemple #13
0
    def fit(self, X, y):
        X = tf.convert_to_tensor(X, tf.float32)
        y = tf.convert_to_tensor(y, tf.int32)
        classes = tf.sort(tf.unique(y).y)

        if self.n_components is None:
            self.n_components = classes.shape[0] - 1

        means = []
        for i in classes:
            Xg = X[y == i]
            means.append(tf.reduce_mean(Xg, axis=0))
        self.means = tf.stack(means, axis=0)  # [cls, d]

        eigvals, eigvecs = linear_discriminative_eigvals(y,
                                                         X,
                                                         self.lambda_val,
                                                         ret_vecs=True)
        eigvecs = tf.reverse(eigvecs, axis=[1])  # [d, cls]
        eigvecs = eigvecs / tf.linalg.norm(eigvecs, axis=0,
                                           keepdims=True)  # [d, cls]
        self.scaling = eigvecs.numpy()
        self.coef = tf.matmul(tf.matmul(self.means, eigvecs),
                              tf.transpose(eigvecs, (1, 0)))  # [cls, d]
        self.intercept = -0.5 * tf.linalg.diag_part(
            tf.matmul(self.means, tf.transpose(self.coef, (1, 0))))  # [cls]
        self.coef = self.coef.numpy()
        self.intercept = self.intercept.numpy()

        eigvals = eigvals.numpy()
        if self.verbose:
            top_k_evals = eigvals[-self.n_components + 1:]
            self.logger(
                "\nLDA-Eigenvalues:",
                np.array_str(top_k_evals, precision=2, suppress_small=True))
            self.logger(
                "Eigenvalues Ratio min/max: %.3f, Mean: %.3f" %
                (top_k_evals.min() / top_k_evals.max(), top_k_evals.mean()))

        return eigvals
def labelstr2onehot(labelstr, class_list):
    """One-hot label encoding
    
    labelstr: label, encoded as 'id1#...#idN', where idn is an int
    class_list: list of classes used as the reference for the one hot encoding    
    """

    # parse string
    labels = tf.cond(tf.equal(tf.strings.length(labelstr), 0),
                     true_fn=lambda: tf.constant([], dtype=tf.int32),
                     false_fn=lambda: tf.strings.to_number(
                         tf.strings.split(labelstr, '#'), out_type=tf.int32))

    # sort class_list and get indices of labels in class_list
    class_list = tf.sort(class_list)
    labels = tf.where(tf.equal(tf.expand_dims(labels, axis=1), class_list))[:,
                                                                            1]

    return tf.cond(tf.equal(tf.size(labels), 0),
                   true_fn=lambda: tf.zeros(tf.size(class_list)),
                   false_fn=lambda: tf.reduce_max(
                       tf.one_hot(labels, tf.size(class_list)), 0))
def topp_topk(logits, p, k):
    sorted_logits = tf.sort(logits, direction='DESCENDING')
    sorted_indices = tf.argsort(logits, direction='DESCENDING')
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits))
    t_sorted_indices_to_remove = cumulative_probs > p
    ''' Shift the indices to the right to keep also the first token above the threshold '''
    indices = tf.range(1, tf.shape(logits)[0], 1)
    sorted_indices_to_remove = tf.scatter_nd(tf.expand_dims(indices, 1),
                                             t_sorted_indices_to_remove[:-1],
                                             logits.shape)
    logits = tf.where(sorted_indices_to_remove,
                      tf.ones_like(logits, dtype=logits.dtype) * -1e10, logits)
    values, _ = tf.nn.top_k(logits, k=k)
    min_value = tf.reduce_min(values)
    logits = tf.where(logits < min_value,
                      tf.ones_like(logits, dtype=logits.dtype) * -1e10, logits)
    logits = tf.reshape(logits, (h_parms.validation_batch_size, -1))
    sample = tf.random.categorical(logits,
                                   num_samples=1,
                                   dtype=tf.int32,
                                   seed=1)
    return sample
Exemple #16
0
def _tag_filter(features_dict, tags, which_tags=None):
    ''' Removes unwanted tids from the dataset based on given tags (use with tf.data.Dataset.filter).
    
    Parameters
    ----------
    features_dict: dict
        Dict of features (as provided by .filter).

    tags: list or list-like
        List containing tag idxs (as int) to be "allowed" in the output dataset.

    which_tags: int
        If not None, specifies the database to filter on (when multiple databases are provided).
    '''

    tags = tf.dtypes.cast(tags, dtype=tf.int64)

    if which_tags is None:
        dict_key = 'tags'
    else:
        assert isinstance(which_tags, int), 'which_tags must be an integer'
        dict_key = 'tags_' + str(which_tags)

    num_tags = tf.cast(tf.shape(features_dict[dict_key]), tf.int64)
    feature_tags = tf.math.equal(
        tf.unstack(features_dict[dict_key]), tf.constant(1, dtype=tf.int64)
    )  # bool tensor where True/False correspond to has/doesn't have tag
    idxs = tf.subtract(tf.reshape(tf.sort(tags), [-1, 1]),
                       tf.constant(1, dtype=tf.int64))
    vals = tf.constant(1, dtype=tf.int64, shape=tags.get_shape())
    tags_mask = tf.SparseTensor(indices=idxs,
                                values=vals,
                                dense_shape=num_tags)
    tags_mask = tf.sparse.to_dense(tags_mask)
    tags_mask = tf.dtypes.cast(tags_mask, tf.bool)

    return tf.math.reduce_any(
        feature_tags & tags_mask
    )  # returns True if and only if at least one feature tag is in the desired 'tags' list
def split_latents(x, minibatch_size, hy_ncut=1):
    # x: [b, dim]
    b = minibatch_size
    dim = x.get_shape().as_list()[1]
    split_idx = tf.random.uniform(shape=[b, hy_ncut],
                                  maxval=dim + 1,
                                  dtype=tf.int32)
    split_idx = tf.sort(split_idx, axis=-1)
    idx_range = tf.tile(tf.range(dim)[tf.newaxis, :], [b, 1])
    masks = []
    mask_last = tf.zeros([b, dim], dtype=tf.float32)
    for i in range(hy_ncut):
        mask_tmp = tf.cast(idx_range < split_idx[:, i:i + 1], tf.float32)
        masks.append(mask_tmp - mask_last)
        masks_last = mask_tmp
    mask_tmp = tf.cast(idx_range < split_idx[:, -1:], tf.float32)
    masks.append(1. - mask_tmp)
    x_split_ls = [x * mask for mask in masks]
    # mask_1 = tf.cast(idx_range < split_idx[:, tf.newaxis], tf.float32)
    # mask_2 = 1. - mask_1
    # return x * mask_1, x * mask_2
    return x_split_ls
Exemple #18
0
def ohem_single(score, gt_text, training_mask):
    pos_num = int(tf.math.reduce_sum(tf.cast(
        gt_text > 0.5, dtype=tf.float32))) - int(
            tf.math.reduce_sum(
                tf.cast(
                    (gt_text > 0.5) &
                    (training_mask <= 0.5), dtype=tf.float32)))

    if pos_num == 0:
        # selected_mask = gt_text.copy() * 0 # may be not good
        selected_mask = training_mask
        selected_mask = tf.cast(
            tf.reshape(selected_mask,
                       [selected_mask.shape[0], selected_mask.shape[1], 1]),
            dtype=tf.float32)
        return selected_mask

    neg_num = int(tf.math.reduce_sum(tf.cast(gt_text <= 0.5,
                                             dtype=tf.float32)))
    neg_num = int(min(pos_num * 3, neg_num))

    if neg_num == 0:
        selected_mask = training_mask
        selected_mask = tf.cast(
            tf.reshape(selected_mask,
                       [selected_mask.shape[0], selected_mask.shape[1], 1]),
            dtype=tf.float32)
        return selected_mask

    neg_score = score[gt_text <= 0.5]
    neg_score_sorted = tf.sort(-neg_score)
    threshold = -neg_score_sorted[neg_num - 1]

    selected_mask = ((score >= threshold) |
                     (gt_text > 0.5)) & (training_mask > 0.5)
    selected_mask = tf.cast(tf.reshape(
        selected_mask, [selected_mask.shape[0], selected_mask.shape[1], 1]),
                            dtype=tf.float32)
    return selected_mask
Exemple #19
0
def gaussian_mixture_wasserstein_loss(x, pi, mu, var, order):
    # Sort input data for computing alignment with Gaussian mixture.
    x = tf.sort(x)
    nx = x.shape[0]

    # Calculate variances of the distributions.
    prec = 1. / var

    # Split Gaussian mixture distribution into N parts to compute transportation cost.
    # It also computes the split point between right-facing transporation and left-facing transportation
    # to compute 1-Wasserstein integral properly.
    ratio = tf.cast(tf.linspace(1. / nx, 1 - 1. / nx, nx - 1), tf.float64)
    partition = gaussian_mixture_cdfinv(ratio, pi, mu, var)
    partition_left = tf.concat([[-1e+10], partition], axis=0)
    partition_right = tf.concat([partition, [1e+10]], axis=0)
    partition_mid = tf.minimum(tf.maximum(partition_left, x), partition_right)

    # Change of variables, for later integrals
    integral_left = (partition_left[:, None] - mu[None, :]) * tf.sqrt(.5 * prec)[None, :]
    integral_mid = (partition_mid[:, None] - mu[None, :]) * tf.sqrt(.5 * prec)[None, :]
    integral_right = (partition_right[:, None] - mu[None, :]) * tf.sqrt(.5 * prec)[None, :]

    if (order == 1):
        loss_left = (x[:, None] - mu[None, :]) * integrate_emx2(integral_left, integral_mid)
        loss_left -= 1. / tf.sqrt(.5 * prec[None, :]) * integrate_xemx2(integral_left, integral_mid)
        loss_left *= tf.cast(1. / tf.sqrt(np.pi), tf.float64)
        loss_right = 1. / tf.sqrt(.5 * prec[None, :]) * integrate_xemx2(integral_mid, integral_right)
        loss_right -= (x[:, None] - mu[None, :]) * integrate_emx2(integral_mid, integral_right)
        loss_right *= tf.cast(1. / tf.sqrt(np.pi), tf.float64)
        return pi[None, :] * (loss_left + loss_right)
    elif (order == 2):
        diff = x[:, None] - mu[None, :]
        loss = (diff * diff) * integrate_emx2(integral_left, integral_right)
        loss -= 2 * diff / tf.sqrt(.5 * prec[None, :]) * integrate_xemx2(integral_left, integral_right)
        loss += 1. / (.5 * prec[None, :]) * integrate_x2emx2(integral_left, integral_right)
        loss *= tf.cast(1. / tf.sqrt(np.pi), tf.float64)
        return pi[None, :] * loss
    else:
        assert False
        def compute_conf():
            batch = self.mini_buffer.get_random_batch(num_exps)
            [states, meta_actions, next_states] = batch
            [predicted_alphas,
             predicted_thetas] = self.completion(states, meta_actions)
            [actual_alphas,
             actual_thetas] = self.inv_completion(states, meta_actions,
                                                  next_states)
            for i in range(num_exps):
                batch[i].append([predicted_alphas, predicted_thetas])
                batch[i].append([actual_alphas, actual_thetas])
                batch[i].append(tf.norm(states[i] - state, ord=2))
            sorted_batch = tf.sort(batch)  # sorts according to last axis
            weight_base = (num_exps + num_exps**2) / 2
            for i in range(num_exps):
                [predicted_alpha, predicted_theta] = sorted_batch[i][-3]
                [actual_alpha, actual_theta] = sorted_batch[i][-2]
                conf += (tf.square(predicted_alpha - actual_alpha) +
                         tf.square(predicted_theta -
                                   actual_theta)) / weight_base * i

            return conf
Exemple #21
0
def balanced_sample_weight(labels, maxlen):
    # Sort
    labels = tf.sort(labels, axis=-1, direction='DESCENDING')

    # Reduce each label combination to unique sequence
    def reduce_concat(input, maxlen):
        maxlen = input.shape[-1] if maxlen is None else maxlen
        dec = 10**tf.range(maxlen - 1, -1, -1)
        return tf.reduce_sum(input * dec, axis=-1)

    labels = reduce_concat(labels, maxlen)
    # Identify unique label combinations, idx, and counts
    y, idx, count = tf.unique_with_counts(labels)
    # Calculate class weights
    total_count = tf.size(labels)
    label_count = tf.size(y)
    calc_weight = lambda x: tf.divide(tf.divide(total_count, x),
                                      tf.cast(label_count, tf.float64))
    class_weights = tf.map_fn(fn=calc_weight, elems=count, dtype=tf.float64)
    # Gather sample weights
    sample_weights = tf.gather(class_weights, idx)
    return tf.cast(sample_weights, tf.float32)
Exemple #22
0
    def __get_dlr_target(self, logits, y_input, y_target):
        """ Private function
        Get targeted version of DLR loss

        Args:
            logit: (tf_tensor) Logits
            y_input: (tf_tensor) Input label
            y_target: (tf_tensor) Input targeted label

        Returns:
            loss: (tf_tensor) Targeted DLR loss
        """

        x = logits
        x_sort = tf.sort(x, axis=1)
        y_onehot = tf.one_hot(y_input, self.num_classes)
        y_target_onehot = tf.one_hot(y_target, self.num_classes)
        loss = -(tf.reduce_sum(x * y_onehot, axis=1) - tf.reduce_sum(
            x * y_target_onehot, axis=1)) / (x_sort[:, -1] - .5 * x_sort[:, -3]
                                             - .5 * x_sort[:, -4] + 1e-12)

        return loss
    def get_move(self, state):
        """Runs all playouts sequentially and returns the most visited action.
        state: the current game state

        Return: the selected action
        """
        x = tf.placeholder(tf.int32, [None])
        y = tf.sort(x, direction='DESCENDING', axis=0)

        with tf.Session() as sess:
            for n in range(self._n_playout):
                # to prevent killed by ITP
                if state._ef_for_eight > 0:
                    a = sess.run(y,
                                 feed_dict={
                                     x: np.random.randint(100, size=(100000))
                                 }).shape

                state_copy = copy.deepcopy(state)
                self._playout(state_copy)
        return max(self._root._children.items(),
                   key=lambda act_node: act_node[1]._n_visits)[0]
Exemple #24
0
    def call(self, x, mask=None):

        avg_atten = self.calc_avg_atten(self.atten, 12)
        attended_by = self.atten_col(avg_atten)
        attended_by = attended_by[:, 1:]
        indices = tf.cast(tf.math.top_k(attended_by,
                                        k=self.index - 1,
                                        sorted=True).indices,
                          dtype=tf.int32)
        indices = tf.expand_dims(tf.add(indices,
                                        tf.constant([1], dtype=tf.int32)),
                                 axis=-1)
        CLS_SEP = tf.broadcast_to(
            tf.constant([0], dtype=tf.int32),
            shape=[tf.shape(x, out_type=tf.dtypes.int32)[0], 1])
        indices_CLS_SEP = tf.concat(
            [indices, tf.expand_dims(CLS_SEP, axis=-1)], axis=1)
        indices_CLS_SEP = tf.sort(indices_CLS_SEP,
                                  axis=1,
                                  direction='ASCENDING')
        extract_layer = tf.gather_nd(x, indices_CLS_SEP, batch_dims=1)
        return extract_layer
Exemple #25
0
def hard_negative_mining(loss, anchors_tag, negatives_per_positive,
                         min_negatives_per_image):
    """
    困难负样本挖掘
    :param loss: [num_anchors]
    :param anchors_tag: [num_anchors] 1:正样本,-1:负样本,0: ignore
    :param negatives_per_positive:
    :param min_negatives_per_image:
    :return:
    """
    positive_loss = tf.gather_nd(loss, tf.where(tf.equal(anchors_tag, 1.)))
    negative_loss = tf.gather_nd(loss, tf.where(tf.equal(anchors_tag, -1.)))

    num_positives = tf.size(positive_loss)
    num_negatives = tf.maximum(num_positives * negatives_per_positive,
                               min_negatives_per_image)

    negative_loss = tf.sort(negative_loss, axis=0, direction='DESCENDING')
    negative_loss = negative_loss[:num_negatives]

    total_loss = tf.concat([positive_loss, negative_loss], axis=0)
    return [tf.reduce_sum(total_loss), tf.cast(num_positives, tf.float32)]
Exemple #26
0
 def call(self,x,training=True):
     predictions = []
     args_predictions = []
     for feature_map in x:
         b,h,w,c = feature_map.shape
         feature_map = tf.reshape(feature_map,(b,-1,c)) 
         # normalize the feature maps to make the distance 
         #feature_map,_ = tf.linalg.normalize(feature_map,axis=-1)
         # http://www.robots.ox.ac.uk/~albanie/notes/Euclidean_distance_trick.pdf
         G = tf.einsum('bik, bjk->bij', feature_map, feature_map)
         D = tf.reshape(tf.linalg.diag_part(G),(b,-1,1))+ tf.transpose(tf.reshape(tf.linalg.diag_part(G),(b,-1,1)),perm=(0,2,1)) - 2*G
         D =  tf.keras.activations.relu(D)  # It is possible  to get negative values in the matrix due to a lack of floating point precision
         D = D+self.epsilon #any zero values in the distance matrix will produce infinite gradients
         norms = tf.sqrt(D)
         prediction = tf.sort(norms,axis=-1)
         arg_prediction = tf.argsort(norms,axis=-1)[:,:,1]
         prediction = tf.where((prediction[:,:,1] / prediction[:,:,2] < self.Tl), 2/(1+tf.exp(prediction[:,:,1])),2/(1+self.l*tf.exp(prediction[:,:,1])))
         prediction = tf.reshape(prediction,(b,h,w,1))
         predictions.append(prediction)
         arg_prediction = tf.reshape(arg_prediction,(b,h,w,1))
         args_predictions.append(arg_prediction)
     return predictions,args_predictions
Exemple #27
0
 def sample(self, time, outputs, state, name=None):
     """sample for SampleEmbeddingHelper."""
     del time, state  # unused by sample_fn
     # Outputs are logits, we sample instead of argmax (greedy).
     if not isinstance(outputs, ops.Tensor):
         raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                         type(outputs))
     logits_sort = tf.sort(outputs, direction='DESCENDING')
     probs_sort = tf.nn.softmax(logits_sort)
     probs_sort_sum = tf.cumsum(probs_sort, axis=1, exclusive=True)
     logits_sort_masked = tf.where(
         probs_sort_sum < self._top_p, logits_sort,
         tf.ones_like(outputs, dtype=outputs.dtype) * 1e10)
     min_logits = tf.reduce_min(logits_sort_masked, axis=1, keep_dims=True)
     sample_logits = tf.where(
         outputs < min_logits,
         tf.ones_like(outputs, dtype=outputs.dtype) * -1e10, outputs)
     sample_ids = tf.multinomial(sample_logits,
                                 num_samples=1,
                                 output_dtype=tf.int32)
     sample_ids = tf.squeeze(sample_ids, axis=1)
     return sample_ids
Exemple #28
0
    def Compute_LeftPoint(self, c1, UU, LL, n):
        #print('<<<<<<<<<<<<')
        c1_sort = tf.sort(c1, direction='ASCENDING')
        c1_index = tf.argsort(c1, direction='ASCENDING')
        #print('c1_index,UU,LL',c1_index,UU,LL)
        UU_sort = tf.gather(UU, c1_index)
        #print('*****************',UU,c1_index)
        LL_sort = tf.gather(LL, c1_index)
        l_out = 0
        s = 0
        s1 = 0
        b2 = c1_sort
        #print('<<<<<<<<<<<<')
        s = tf.reduce_sum(tf.multiply(b2, LL_sort))
        s1 = tf.reduce_sum(LL_sort) + 0.0000001
        l_out = s / s1
        for i in range(n):
            s += b2[i] * (UU_sort[i] - LL_sort[i])
            s1 += UU_sort[i] - LL_sort[i]
            l_out = tf.minimum(l_out, s / s1)

        return l_out
Exemple #29
0
 def greedy_update_margins(self, y_true, y_pred):
     # instabilities during learning
     if self.perc is None:
         return
     margin_other, y_true = self.preprocess_margins(y_true, y_pred)
     new_margins = []
     for cl in range(self.depth):
         selected = tf.boolean_mask(margin_other, y_true == cl)
         batch_size = tf.cast(tf.shape(selected), dtype=tf.float32)
         idx = tf.cast(self.perc * batch_size, dtype=tf.int64)
         emp_perc = tf.gather(tf.sort(selected), idx)
         emp_std = tf.math.reduce_std(selected)
         emp_mean = tf.reduce_mean(selected)
         th_perc = emp_std * (2**0.5) * tf.math.erfinv(2 * self.perc / 100 -
                                                       1) + emp_mean
         perc = emp_perc * (
             1 - self.gaussian_prior) + th_perc * self.gaussian_prior
         new_margins.append(perc)
     grad = tf.stack(new_margins, axis=1)
     update = (1 - self.lr) * self.margins + self.lr * grad
     update = tf.maximum(update, self.min_margin)
     self.margins.assign(update)
    def calc_ignore_mask(self, true_obj, true_box, pred_box):
        # YOLOv3:
        # "If the bounding box prior is not the best but does overlap a ground
        # truth object by more than some threshold we ignore the prediction,
        # following [17]. We use the threshold of .5."
        # calculate the iou for each pair of pred bbox and true bbox, then find the best among them

        # (None, 13, 13, 3, 4)
        true_box_shape = tf.shape(true_box)
        # (None, 13, 13, 3, 4)
        pred_box_shape = tf.shape(pred_box)
        # (None, 507, 4)
        true_box = tf.reshape(true_box, [true_box_shape[0], -1, 4])
        # sort true_box to have non-zero boxes rank first
        true_box = tf.sort(true_box, axis=1, direction="DESCENDING")
        # (None, 100, 4)
        # only use maximum 100 boxes per groundtruth to calcualte IOU, otherwise
        # GPU emory comsumption would explode for a matrix like (16, 52*52*3, 52*52*3, 4)
        true_box = true_box[:, 0:100, :]
        # (None, 507, 4)
        pred_box = tf.reshape(pred_box, [pred_box_shape[0], -1, 4])

        # https://github.com/dmlc/gluon-cv/blob/06bb7ec2044cdf3f433721be9362ab84b02c5a90/gluoncv/model_zoo/yolo/yolo_target.py#L198
        # (None, 507, 507)
        iou = broadcast_iou(pred_box, true_box)
        # (None, 507)
        best_iou = tf.reduce_max(iou, axis=-1)
        # (None, 13, 13, 3)
        best_iou = tf.reshape(best_iou, [
            pred_box_shape[0], pred_box_shape[1], pred_box_shape[2],
            pred_box_shape[3]
        ])
        # ignore_mask = 1 => don't ignore
        # ignore_mask = 0 => should ignore
        ignore_mask = tf.cast(best_iou < self.ignore_thresh, tf.float32)
        # (None, 13, 13, 3, 1)
        ignore_mask = tf.expand_dims(ignore_mask, axis=-1)
        return ignore_mask