2
$\begingroup$

How to create anchor-positive and anchor-negative pair for feature X in a signature data set for training Siamese network?

Im have a cedar signature data set with 55 peoples signatures(classes) with 24 original image(instance) and 24 forged image(instance ) . so for person 1(class) there will 24 original signature images and 24 forged signature images(48 instance )

i need to create (Anchor-positive pair ,anchor -negative pair ) to represent the feature X((img1,img2),(img1,img9)) . feature y(1,0) 1-> similar 0-> non similar

Anchor-> original image
positive-> this also represent the original image but with minute variations
Negative-> forged image

The below code does not give the exact pair for X labels and y labels because the negative pair is not chosen randomly.

  1. positive -positive pair -> must be of image1 and image1 original instances (total - 24 pairs )
  2. i. positive -negative pair -> must be of image1 and image1 negative instances(NEED only 12) ii. positive -negative pair -> must be of image1 and any other random image instances(NEED only 12) final positive-negative pair will be 24

below line takes image1 and pair with other instances of image1 and labels it as 1 -> anchor-positive pair

z1, z2 = digit_indices[d][i], digit_indices[d][i+1]

below line takes image1 and pair with other instances of other images eg:image9 and labels it as 0 -> anchor-negative pair

inc = random.randrange(1, nb_classes)
        dn = (d + inc) % nb_classes
        z1, z2 = digit_indices[d][i], digit_indices[dn][i]

the issue there is always perfect 50/50 positive-negative pair and model is doing random choice prediction (i.e) accuracy score is 50% always

please suggest how to create a meaningful pair of positive-positive images and positive-negative images

def create_pairs(x, digit_indices, nb_classes):
"""      x:         X_train, array of array of all train samples.
    digit_indices:  List of an array, length = no of classes; each sublist consists of train sample indices
                    belonging to that particular class index/class
"""
""" Positive and negative pair creation.
    Alternates between positive and negative pairs.
"""
pairs = []
labels = []

print ('\n\n')
print ('X_train shape:  ', x.shape)
print ('Digit_indices shape:    ', np.array(digit_indices).shape)
print ('Length of digit indices:    ', len(digit_indices))
print ('No of classes:  ', nb_classes)
print ('\n\n')

n = min([len(digit_indices[d]) for d in range(nb_classes)]) - 1
for d in range(nb_classes):
    for i in range(n):
        z1, z2 = digit_indices[d][i], digit_indices[d][i+1]
        pairs += [[x[z1], x[z2]]]
        inc = random.randrange(1, nb_classes)
        dn = (d + inc) % nb_classes
        z1, z2 = digit_indices[d][i], digit_indices[dn][i]
        pairs += [[x[z1], x[z2]]]
        labels += [1, 0]    #check label based on similarity = 0 or 1
        # labels += [0,1]     #similar pairs = 0 in this case
return np.array(pairs), np.array(labels)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.