Home > database >  I want make positive image and label pairs but I take this error IndexError: list index out of range
I want make positive image and label pairs but I take this error IndexError: list index out of range

Time:08-24

I use the face dataset for the siamese network. In this dataset, we have 1000 unique labels(labels are names of the folders), and in each folder, we have 20 images all images in this dataset are 20000.this error is because of this line:

idxB = np.random.choice(idx[label])

so I want to make positive and negative images, but when I do that, I get:

IndexError: list index out of range error.

Code is coming in below:

pair_images = []
pair_labels = []
new_labels = []

for k in labels:
    new_labels.append(int(k))

numClasses  = len(np.unique(new_labels))

new_labels = np.array(new_labels)

idx = [np.where(new_labels == i)[0] for i in range(0,numClasses)]

print (len(idx))

for i,idxA in enumerate (range(len(images))):


    # print(i)
    # Make Posetive Images
    currentImage = images[idxA]
    label = new_labels[idxA]

    idxB = np.random.choice(idx[label])
    print (idxB)
    # posImage = images[idxB]

output:

0
1
2
3
4
....
....
....
....
11713
11718
11709
11700
11700
11710
11717
11717
11707
Traceback (most recent call last):
    File "/Users/admin/Documents/Ostad/Ostad Ghasemi/Courses/Advabced           Tensorflow/Home Works/Week-4/E-1-Face Verification/Utilities.py", line 73, in <module>
  make_pairs(all_image, all_label)       
  File "/Users/admin/Documents/Ostad/Ostad Ghasemi/Courses/Advabced Tensorflow/Home      Works/Week-4/E-1-Face Verification/Utilities.py", line 37, in make_pairs
idxB = np.random.choice(idx[label])
IndexError: list index out of range

May I know how can I fix this error?

CodePudding user response:

Need to check the following:

  • check that len(images) <= len(labels) is true

  • the random choice is choosing an index (based on label) that is larger than len(idx). To prevent index out of range, there's a need to check that label < len(idx) is true before proceeding with the random choice, for example:

for i,idxA in enumerate(range(len(images))):
    # print(i)
    # Make Posetive Images
    currentImage = images[idxA]
    label = new_labels[idxA]
    if label < len(idx):    #add this check
        idxB = np.random.choice(idx[label])
        print (idxB)
        # posImage = images[idxB]
  • Related