在tensorflow上将字母标签转换为数字的问题
Problem with converting alphabet letter labels to digits on tensor flow
我正在使用 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)
所以此时标签 y 是一个看起来有点像那个的列表(字面意思是对应于每个图像代表的实际字母的列表):
['C', 'M', 'L', 'B' ... , 'S', 'P', 'K', 'F', 'G', 'E 'O', 'N', 'C', 'B']
这是我正在训练的模型:
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_impl.UnimplementedError: Cast string to float is not supported
[[node sparse_categorical_crossentropy/Cast (defined at train_cnn_normal_img_size.py:84) ]] [Op:__inference_train_function_867]
我知道问题是我的标签是字符串而不是数字,所以我尝试将它们转换为这样的数字:
# 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)
但这显示了这个错误:
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 25 which is outside the valid range of [0, 10). Label values: 23 1 20 23 8 20 15 9 9 5 15 18 21 25 16 25 1 19 5 6 24 6 7 25 21 20 6 14 22 4 14 18 25 13 25 20 8 1 2 13 4 6 19 20 4 6 5 13 12 9 22 17 12 11 9 21 2 25 17 6 18 17 22 9
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at train_cnn_normal_img_size.py:84) ]] [Op:__inference_train_function_867]
有人知道我是否遗漏了什么吗?我不明白为什么我的标签只能有 0 到 9 的值。如果我像现在这样有 25 类 会怎样?我是否错过了从字符串到数字转换的重要步骤,或者我应该使用不同的损失函数?
如果你想要 25 个 classes,你的最后一层应该有 25 个节点。所以你的模型最后应该有这个:
model.add(Dense(25, activation='softmax'))
不是这个:
model.add(Dense(10, activation='softmax'))
您的代码将只接受 0-9 中的 10 class。
我正在使用 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)
所以此时标签 y 是一个看起来有点像那个的列表(字面意思是对应于每个图像代表的实际字母的列表):
['C', 'M', 'L', 'B' ... , 'S', 'P', 'K', 'F', 'G', 'E 'O', 'N', 'C', 'B']
这是我正在训练的模型:
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_impl.UnimplementedError: Cast string to float is not supported [[node sparse_categorical_crossentropy/Cast (defined at train_cnn_normal_img_size.py:84) ]] [Op:__inference_train_function_867]
我知道问题是我的标签是字符串而不是数字,所以我尝试将它们转换为这样的数字:
# 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)
但这显示了这个错误:
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 25 which is outside the valid range of [0, 10). Label values: 23 1 20 23 8 20 15 9 9 5 15 18 21 25 16 25 1 19 5 6 24 6 7 25 21 20 6 14 22 4 14 18 25 13 25 20 8 1 2 13 4 6 19 20 4 6 5 13 12 9 22 17 12 11 9 21 2 25 17 6 18 17 22 9 [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at train_cnn_normal_img_size.py:84) ]] [Op:__inference_train_function_867]
有人知道我是否遗漏了什么吗?我不明白为什么我的标签只能有 0 到 9 的值。如果我像现在这样有 25 类 会怎样?我是否错过了从字符串到数字转换的重要步骤,或者我应该使用不同的损失函数?
如果你想要 25 个 classes,你的最后一层应该有 25 个节点。所以你的模型最后应该有这个:
model.add(Dense(25, activation='softmax'))
不是这个:
model.add(Dense(10, activation='softmax'))
您的代码将只接受 0-9 中的 10 class。