【问题标题】:List index out of range error using TensorFlow使用 TensorFlow 列出索引超出范围错误
【发布时间】:2021-07-02 02:42:39
【问题描述】:

我正在使用 TensorFlow 创建图像分类模型。我写了以下几行代码:

import pandas as pd
import os
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout 
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from  matplotlib import pyplot as plt
import matplotlib.image as mpimg
import random 
%matplotlib inline 
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models
import glob
from PIL import Image

--导入我所有的库

#newer code
dic = {}
# assuming you have .png format files else change the same into the glob statement
train_images='/Users/FOLDER/downloads/Boneage_competition/training_dataset/Resized/'

for file in glob.glob(train_images+'/*.png'):
    b_name = os.path.basename(file).split('.')[0]
    dic[b_name] = mpimg.imread(file)

dic_label_match = {}
label_file = '/Users/FOLDER/downloads/train.csv'
train_labels = pd.read_csv (r'/Users/FOLDER/downloads/train.csv')
for i in range(len(train_labels)):
    # given your first column is age and image no starts from 1
    dic_label_match[i+1] =  str(train_labels.iloc[i][0])
    # you can use the below line too
    # dic_label_match[i+1] =  str(train_labels.iloc[i][age])

# now you have dict with keys and values 
# create two lists / arrays and you can pass the same to the keram model

train_x = []
label_ = []

for val in dic:
    if val in dic and val in dic_label_match:
        train_x.append(dic[val])
        label_.append(dic_label_match[val])

-- 将每个图像附加到其对应的标签

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(12611,300,300,1)),
    tf.keras.layers.Dense(2, activation='relu'),
    tf.keras.layers.Dense(2)
])

--将模型应用于数据集

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])

-- 编译我的模型

model.fit(train_x, label_, epochs=5)

运行此代码时,我在最后一行收到一条错误消息。整个消息是:

IndexError                                Traceback (most recent call last)
<ipython-input-24-ca24364bad96> in <module>
----> 1 model.fit(train_x, label_, epochs=5)

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    706     self._check_call_args('fit')
    707 
--> 708     func = self._select_training_loop(x)
    709     return func.fit(
    710         self,

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _select_training_loop(self, inputs)
    498             self._distribution_strategy)):
    499       try:
--> 500         valid_adapter = data_adapter.select_data_adapter(inputs, None)
    501       except ValueError as data_failure_exception:
    502         valid_adapter = None

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in select_data_adapter(x, y)
    645 def select_data_adapter(x, y):
    646   """Selects a data adapter than can handle a given x and y."""
--> 647   adapter_cls = [cls for cls in ALL_ADAPTER_CLS if cls.can_handle(x, y)]
    648   if not adapter_cls:
    649     # TODO(scottzhu): This should be a less implementation-specific error.

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in <listcomp>(.0)
    645 def select_data_adapter(x, y):
    646   """Selects a data adapter than can handle a given x and y."""
--> 647   adapter_cls = [cls for cls in ALL_ADAPTER_CLS if cls.can_handle(x, y)]
    648   if not adapter_cls:
    649     # TODO(scottzhu): This should be a less implementation-specific error.

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in can_handle(x, y)
    451   @staticmethod
    452   def can_handle(x, y=None):
--> 453     handles_x = ListsOfScalarsDataAdapter._is_list_of_scalars(x)
    454     handles_y = True
    455     if y is not None:

~/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in _is_list_of_scalars(inp)
    462       return True
    463     if isinstance(inp, (list, tuple)):
--> 464       return ListsOfScalarsDataAdapter._is_list_of_scalars(inp[0])
    465     return False
    466 

IndexError: list index out of range

我尝试过调整纪元数,以及使用其他模型都无济于事。

如果您对我的代码有任何想法或任何提示,将不胜感激!

【问题讨论】:

    标签: python tensorflow keras deep-learning image-classification


    【解决方案1】:

    由于以下行,您会遇到错误

    dic_label_match[i+1] =  str(train_labels.iloc[i][0])
    

    在这里,您使用 1 而不是 0 进行索引。因此,当您执行以下行时

    label_.append(dic_label_match[val])
    

    您的label_ 的值从1, 2, .... 开始,它应该从0, 1, ... 开始。

    因此,您可以按如下方式更改该行

    label_.append(dic_label_match[val - 1])
    

    或者您可以更改以下行

    dic_label_match[i] =  str(train_labels.iloc[i][0])
    

    【讨论】:

    • 我已经更新了代码,但我能问一下这是做什么的吗?我仍然在下一个代码块中收到错误。 --- 作为旁注,我忘记在我的代码中定义我的损失 f(x),所以我在我的问题中更新了我的代码。
    • @John 因为您没有提供损失函数的详细信息,所以我认为这可能是您遇到错误的原因。但是现在你已经更新了代码,我已经相应地更新了我的答案---
    • 谢谢,我已经解决了这个问题,但不幸的是,我仍然收到错误消息。如果您有任何想法,我将不胜感激,但如果没有,那也很好。
    • 另外(我很抱歉再次发表评论)但作为参考,图像标签和 CSV 文件都从图像 #1377 开始编号。另外,我有一个测试集和相应的真实标签,但我还没有将它们放入代码中。我现在只是想训练模型。我不知道这是否有帮助,但我想我会提到它。再次感谢您迄今为止的所有帮助。
    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    相关资源
    最近更新 更多