WaqasSultani / AnomalyDetectionCVPR2018

502 stars 182 forks source link

[URGENT] help! with tensorflow implementation #62

Open nitincs opened 4 years ago

nitincs commented 4 years ago
##objective_function
def custom_objective(y_true, y_pred):
    y_true = tf.reshape(y_true, [-1])
    y_pred = tf.reshape(y_pred, [-1])
    d = y_true - y_pred
    n_seg = 32  # Because we have 32 segments per video.
    nvid = 60
    n_exp = nvid // 2
    Num_d = 32 * nvid
    sub_max = []  # sub_max represents the highest scoring instants in bags (videos).
    sub_sum_labels = tf.ones_like(y_true)
    sub_sum_l1 = []
    sub_l2 = []  

    for ii in range(0, nvid, 1):
        # For Labels
        mm = y_true[ii * n_seg:ii * n_seg + n_seg]
        sub_sum_labels = tf.concat([sub_sum_labels, [tf.math.reduce_sum(mm)]],0)  # Just to keep track of abnormal and normal vidoes

        # For Features scores
        Feat_Score = y_pred[ii * n_seg:ii * n_seg + n_seg]
        sub_max = tf.concat([sub_max, [
            tf.reduce_max(Feat_Score, 0)]], 0)  # Keep the maximum score of scores of all instances in a Bag (video)
        sub_sum_l1 = tf.concat(
            [sub_sum_l1,
             [tf.math.reduce_sum(Feat_Score)]], 0)  # Keep the sum of scores of all instances in a Bag (video)

        z1 = tf.ones_like(Feat_Score)
        z2 = tf.concat([z1, Feat_Score], 0)
        z3 = tf.concat([Feat_Score, z1], 0)
        z_22 = z2[31:]
        z_44 = z3[:33]
        z = z_22 - z_44
        z = z[1:32]
        z = tf.math.reduce_sum(tf.math.square(z))
        sub_l2 = tf.concat([sub_l2, [z]], 0)

    sub_score = sub_max  # We need this step since we have used T.ones_like
    F_labels = sub_sum_labels[Num_d:]

    sub_sum_l1 = sub_sum_l1[:n_exp]
    sub_l2 = sub_l2[Num_d:]
    sub_l2 = sub_l2[:n_exp]

    indx_abn = tf.where(tf.equal(F_labels, 32)) # checck for zero or 1

    indx_nor = tf.where(tf.equal(F_labels, 0))
    n_Nor = n_exp

    Sub_Nor = tf.gather_nd(sub_score, indx_nor)
    Sub_Abn = tf.gather_nd(sub_score, indx_abn)

    z = []
    for ii in range(0, n_Nor, 1):
        sub_z = tf.reduce_max(1 - Sub_Abn + Sub_Nor[ii], 0)
        z = tf.concat([z, [tf.math.reduce_sum(sub_z)]], 0)

    z = tf.math.reduce_mean(z) + 0.00008 * tf.math.reduce_sum(sub_sum_l1) + 0.00008 * tf.math.reduce_sum(sub_l2)  # Final Loss f

    return z

we have implemented this code of the objective function with tenorflow v.1.13, but the training is still not showing any good results like the original implementation, can anyone take a look at this, and point out some suggestions based on this

uname0x96 commented 4 years ago

@nitincs can you post your train file?