numpy 遇到“IndexError:在Colab上训练皮肤检测模型期间,索引0超出大小为0'的轴0的界限?

flseospp  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(132)

我在这个领域是个新手。最近我在Colab上训练一个皮肤检测模型,就出现了这个错误

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

这是与之相关的代码:

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)

我对stackoverflow很陌生所以如果我说了一些误解请原谅。

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)
cl25kdpy

cl25kdpy1#

当你的数组为空[]时,会发生这种类型的错误。
这里可能有问题-x_train数组为空。

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

IndexError:索引0超出大小为0的轴0的界限

相关问题