问题描述
我在两个文件夹中有约1400张图像,其中文件夹名称是图像的标签.我遍历每个文件夹,并将其与标签一起添加到数组中.后来我将其传递给我的神经网络模型.
I have ~1400 images in two folder where folder name is the label for image. I am iterating over each folder and appending it in an array along with label. Later i am passing it to my neural network model.
for folder in files:
sdir = dir + "\\"
sdir = sdir + folder
print("inside loop " +sdir)
count = 0
for image in os.listdir(sdir):
img = cv2.imread(image)
count = count + 1
if count <= 450:
x_train.append(img)
y_train.append(folder)
else:
x_test.append(img)
y_test.append(folder)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))
Traceback (most recent call last):
File "testing.py", line 43, in <module>
model.fit(x_train,y_train, epochs=5)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 776, in fit
shuffle=shuffle)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2266, in _standardize_user_data
'array or a list of arrays. You passed: x=' + str(x))
ValueError: Please provide as model inputs either a single array or a list of arrays.
推荐答案
我认为您的问题是由于 files
可能是字符串列表,因此 folder
(您将其作为标签传递到网络中)是一个字符串,而网络将需要整数标签.
I imagine that your issue is due to the fact that files
is probably a list of strings and therefore that folder
(which you are passing as a label to your network) is a string, whereas the network will want integer labels.
因此,根据您拥有的标签数量(两个,如果我理解正确的话),您可以定义一个词典
Therefore, depending on how many labels you have (two, if I understand correctly), you could define a dictionary
labels_to_ints = dict(zip(files,[0,1]))
将字符串映射到整数标签,然后使用
to map your strings to integer labels, and then instead of y_train.append(folder)
, use
y_train.append(labels_to_ints[folder])
(以及相应的 y_test
).
当然,还有其他方法可以执行此映射.
Of course, there are other ways to perform this mapping.
这篇关于在python中标记图像以进行分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!