python—如何将图像数据集拆分为测试集和训练集
发布时间:2022-06-04 21:40:26 332
相关标签: # node.js
所以我有一些代码循环遍历目录以查找图像,将它们转换为灰度值的 numPy 数组,并将它们分配给 pandas 数据框:
count = 0
self.path = directory
for d in os.listdir(directory):
self.classes += d
self.classDictionary[d] = count
count+=1
count = 0
self.dataframe = pandas.DataFrame(columns=["class", "image"])
for d in os.listdir(directory):
folder = d
d = self.path + r"\\" + d
for image in os.listdir(d):
image = d + r"\\" + image
img = Image.open(image).convert('L')
data = numpy.asarray(img)
self.dataframe.at[count, "image"] = data
self.dataframe.at[count, "class"] = self.classDictionary[folder]
count+=1
我使用以下代码将数据帧拆分为图像和类号:
self.X = self.dataframe.drop("class", axis=1)
self.y = self.dataframe[["class"]]
我想将数据分成训练集和测试集,用于卷积神经网络:
x_train,x_test,y_train,y_test = train_test_split(self.X, self.y, test_size=split, random_state=random)
x_train = tensorflow.convert_to_tensor(x_train, dtype=tensorflow.int64)
x_test = tensorflow.convert_to_tensor(x_test, dtype=tensorflow.int64)
y_train = tensorflow.convert_to_tensor(y_train, dtype=tensorflow.int64)
y_test = tensorflow.convert_to_tensor(y_test, dtype=tensorflow.int64)
self.model.compile(optimizer='adam',
loss=SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
self.model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
test_loss, test_acc = self.model.evaluate(x_test, y_test, verbose=2)
self.score = test_acc
问题是我遇到了以下错误:
ValueError: setting an array element with a sequence.
在这一行:
x_train = tensorflow.convert_to_tensor(x_train, dtype=tensorflow.int64)
通过对SO和internet周围的一些研究,我发现此错误来自x_train
但是,这些数组中的每一个都是灰度像素值的简单numpy数组:
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
我不知道这里发生了什么事。有人能帮我理解为什么会发生这个错误吗?
这是完整的.py文件:https://github.com/elliot1234567/CNN
谢谢
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报