Python 在张量流上将字母标签转换为数字时出现问题

Python 在张量流上将字母标签转换为数字时出现问题,python,tensorflow,keras,multiclass-classification,Python,Tensorflow,Keras,Multiclass Classification,我使用tensorflow和keras在我拥有的手语图像数据集上训练神经网络。我正在加载一些遵循以下命名约定的图像: "A_01.jpg", "A_02.jpg", "B_01.jpg" ... "Z_01.jpg" 因此,对于我加载到分类器上的图像,它们名称的第一个字母是标签。我正在读取图像并将其转换为numpy数组,然后像这样加载它们的标签: imagepaths = [a list with all the

我使用tensorflow和keras在我拥有的手语图像数据集上训练神经网络。我正在加载一些遵循以下命名约定的图像:

"A_01.jpg", "A_02.jpg", "B_01.jpg" ... "Z_01.jpg"
因此,对于我加载到分类器上的图像,它们名称的第一个字母是标签。我正在读取图像并将其转换为numpy数组,然后像这样加载它们的标签:

imagepaths = [a list with all the path to the images]
X = [] # Image data
y = [] # Labels

# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
    img = cv2.imread(path) # Reads image and returns np.array
    X.append(img)
    label = path.split("/")[0] ## gets the first letter of the image name for instance A or B 
    y.append(label)
# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
    img = cv2.imread(path) # Reads image and returns np.array
    X.append(img)
    label = path.split("/")[3].split(".")[0][0]
    letterToNumber = ord(label.lower()) - 96
    y.append(letterToNumber)
因此,在这一点上,标签y是一个列表,看起来有点像(字面上是一个对应于每个图像所代表的实际字母的列表):

这是我正在培训的模型:

from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
from tensorflow.python.client import device_lib
from keras import backend as K

# Construction of model
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(128, 128, 3))) 
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu')) 
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Configures the model for training
model.compile(optimizer='adam', # Optimization routine, which tells the computer how to adjust the parameter values to minimize the loss function.
              loss='sparse_categorical_crossentropy', # Loss function, which tells us how bad our predictions are.
              metrics=['accuracy']) # List of metrics to be evaluated by the model during training and testing.

# Trains the model for a given number of epochs (iterations on a dataset) and validates it.
model.fit(X_train, y_train, epochs=5, batch_size=64, verbose=2, validation_data=(X_test, y_test))
尽管如此,当运行它时,它还是会给我一个错误:

tensorflow.python.framework.errors\u impl.UnimplementedError:不支持将字符串强制转换为浮点 [[node sparse\u categorical\u crossentropy/Cast(在train\u cnn\u normal\u img\u size.py:84处定义)][Op:\uuuuu推理\uu train\u函数\u867] 我知道问题在于我的标签是字符串而不是数字,所以我尝试将它们转换为数字,如下所示:

imagepaths = [a list with all the path to the images]
X = [] # Image data
y = [] # Labels

# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
    img = cv2.imread(path) # Reads image and returns np.array
    X.append(img)
    label = path.split("/")[0] ## gets the first letter of the image name for instance A or B 
    y.append(label)
# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
    img = cv2.imread(path) # Reads image and returns np.array
    X.append(img)
    label = path.split("/")[3].split(".")[0][0]
    letterToNumber = ord(label.lower()) - 96
    y.append(letterToNumber)
但这让我看到了这个错误:

张量=pywrap\u tfe.tfe\u Py\u Execute(ctx.\u句柄、设备名称、操作名称、, tensorflow.python.framework.errors\u impl.InvalidArgumentError:收到的标签值为25,超出了[0,10]的有效范围。标签值:23 1 20 23 8 20 15 9 9 15 18 21 21 25 16 1 19 5 6 24 7 25 21 20 6 14 14 14 18 25 20 8 1 13 4 19 4 6 6 6 5 13 12 12 12 12 11 9 21 17 17 22 9 [[node sparse\u categorical\u CrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits(定义于train\u cnn\u normal\u img\u size.py:84)][Op:\u推理\u train\u函数\u 867]
有人知道我是否遗漏了什么?我不知道为什么我的标签只能有0到9之间的值。如果我像现在这样有25个类呢?我是否遗漏了从字符串到数字转换的重要步骤,或者我应该使用不同的丢失函数?

如果你想要25个类,你的最后一层应该有25个节点。因此,您的模型应在末尾包含以下内容:

model.add(密集型(25,activation='softmax'))

不是这个:

model.add(密集(10,activation='softmax'))


您的代码将只接受0-9之间的10个类。

如果您想要25个类,则最后一层应该有25个节点。因此,您的模型应该在末尾有以下内容:

model.add(密集型(25,activation='softmax'))

不是这个:

model.add(密集(10,activation='softmax'))


您的代码只接受0-9之间的10级。

非常感谢您的回答!非常感谢您的回答!