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?

huangapple go评论82阅读模式
英文:

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?

问题

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

  1. IndexError: 索引0超出0轴的大小

与此相关的代码如下:

  1. def load_data(input_size=(100,100)):
  2. images=[]
  3. images2=[]
  4. train_df,test_df=data_dictionary()
  5. for i in train_df['image_path']:
  6. img=cv2.imread(i)
  7. img=cv2.resize(img,input_size)
  8. images.append(img)
  9. y_train=np.asarray(train_df['target'])
  10. x_train=np.asarray(images)
  11. for i in test_df['image_path']:
  12. img=cv2.imread(i)
  13. img=cv2.resize(img,input_size)
  14. images2.append(img)
  15. y_test=np.asarray(test_df['target'])
  16. x_test=np.asarray(images2)
  17. return x_train,x_test,y_train,y_test
  18. x_train,x_test,y_train,y_test=load_data(input_size=(100,100))
  19. train_img=preprocess_input(x_train)
  20. test_img=preprocess_input(x_test)

我对StackOverflow还很陌生,如果我说错了什么请原谅。

  1. def data_dictionary():
  2. path_train="../input/dermnet/train/"
  3. path_test="../input/dermnet/test/"
  4. list_train=train_list_mod
  5. train_dictionary={"image_path":[],"target":[]}
  6. test_dictionary={"image_path":[],"target":[]}
  7. k=0
  8. for i in list_train:
  9. path_disease_train=path_train+i
  10. path_disease_test=path_test+i
  11. image_list_train=os.listdir(path_disease_train)
  12. for j in image_list_train:
  13. img_path_train=path_disease_train+"/"+j
  14. train_dictionary["image_path"].append(img_path_train)
  15. train_dictionary['target'].append(k)
英文:

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

  1. IndexError Traceback (most recent call last)
  2. <ipython-input-68-bb8326040b37> in <cell line: 1>()
  3. ----> 1 train_img=preprocess_input(x_train)
  4. 2 test_img=preprocess_input(x_test)
  5. 2 frames
  6. /usr/local/lib/python3.10/dist-packages/keras/applications/imagenet_utils.py in _preprocess_numpy_input(x, data_format, mode)
  7. 238 x[:, 2, :, :] /= std[2]
  8. 239 else:
  9. --> 240 x[..., 0] -= mean[0]
  10. 241 x[..., 1] -= mean[1]
  11. 242 x[..., 2] -= mean[2]
  12. IndexError: index 0 is out of bounds for axis 0 with size 0

this is the code relevant to it:

  1. def load_data(input_size=(100,100)):
  2. images=[]
  3. images2=[]
  4. train_df,test_df=data_dictionary()
  5. for i in train_df['image_path']:
  6. img=cv2.imread(i)
  7. img=cv2.resize(img,input_size)
  8. images.append(img)
  9. y_train=np.asarray(train_df['target'])
  10. x_train=np.asarray(images)
  11. for i in test_df['image_path']:
  12. img=cv2.imread(i)
  13. img=cv2.resize(img,input_size)
  14. images2.append(img)
  15. y_test=np.asarray(test_df['target'])
  16. x_test=np.asarray(images2)
  17. return x_train,x_test,y_train,y_test
  18. x_train,x_test,y_train,y_test=load_data(input_size=(100,100))
  19. train_img=preprocess_input(x_train)
  20. test_img=preprocess_input(x_test)

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

  1. def data_dictionary():
  2. path_train="../input/dermnet/train/"
  3. path_test="../input/dermnet/test/"
  4. list_train=train_list_mod#os.listdir(path_train)
  5. train_dictionary={"image_path":[],"target":[]}
  6. test_dictionary={"image_path":[],"target":[]}
  7. k=0
  8. for i in list_train:
  9. path_disease_train=path_train+i
  10. path_disease_test=path_test+i
  11. image_list_train=os.listdir(path_disease_train)
  12. for j in image_list_train:
  13. img_path_train=path_disease_train+"/"+j
  14. train_dictionary["image_path"].append(img_path_train)
  15. train_dictionary['target'].append(k)

答案1

得分: 0

这种类型的错误会在数组为空 [] 时发生。可能的问题在于 x_train 数组的为空。

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

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

英文:

Such type of error occur when your array is empty [].<br/>
Maybe problem here - emptiness of x_train array.

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

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

huangapple
  • 本文由 发表于 2023年5月31日 23:49:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定