pythonnumpyindexingindex-error

What should I do when encountering 'IndexError: index 0 is out of bounds for axis 0 with size 0' during training of a skin detection model on Colab?


I'm a newcomer in this field. Recently i was training a skin detection model on Colab, and this error occurred


IndexError                                Traceback (most recent call last)

<ipython-input-68-bb8326040b37> in <cell line: 1>()
----> 1 train_img=preprocess_input(x_train)
      2 test_img=preprocess_input(x_test)

2 frames

/usr/local/lib/python3.10/dist-packages/keras/applications/imagenet_utils.py in _preprocess_numpy_input(x, data_format, mode)
    238                 x[:, 2, :, :] /= std[2]
    239     else:
--> 240         x[..., 0] -= mean[0]
    241         x[..., 1] -= mean[1]
    242         x[..., 2] -= mean[2]

IndexError: index 0 is out of bounds for axis 0 with size 0

this is the code relevant to it:

def load_data(input_size=(100,100)):
    images=[]
    images2=[]
    train_df,test_df=data_dictionary()
    for i in train_df['image_path']:
        img=cv2.imread(i)
        img=cv2.resize(img,input_size)
        images.append(img)
    y_train=np.asarray(train_df['target'])
    x_train=np.asarray(images)
    for i in test_df['image_path']:
        img=cv2.imread(i)
        img=cv2.resize(img,input_size)
        images2.append(img)
    y_test=np.asarray(test_df['target'])
    x_test=np.asarray(images2)
    return x_train,x_test,y_train,y_test

x_train,x_test,y_train,y_test=load_data(input_size=(100,100))

train_img=preprocess_input(x_train)
test_img=preprocess_input(x_test)

i'm pretty new to stackoverflow so forgive me if i say something misunderstanding.

def data_dictionary():
    path_train="../input/dermnet/train/"
    path_test="../input/dermnet/test/"
    list_train=train_list_mod#os.listdir(path_train)
    train_dictionary={"image_path":[],"target":[]}
    test_dictionary={"image_path":[],"target":[]}
    k=0
    for i in list_train:
        path_disease_train=path_train+i
        path_disease_test=path_test+i
        image_list_train=os.listdir(path_disease_train)
        for j in image_list_train:
            img_path_train=path_disease_train+"/"+j
            train_dictionary["image_path"].append(img_path_train)
            train_dictionary['target'].append(k) 

Solution

  • Such type of error occur when your array is empty [].
    Maybe problem here - emptiness of x_train array.

    x = np.array([])
    x[0]
    

    IndexError: index 0 is out of bounds for axis 0 with size 0