python - 使用 Keras 增强 CSV 文件数据集
问题描述
我正在Kaggle中进行一个已经实施的项目,该项目与图像分类有关。我总共有 6 个类要预测,分别是 Angry、Happy、Sad 等。我已经实现了一个 CNN 模型,我目前只使用 4 个类(图像数量最多的类),但是我的模型过度拟合,我的验证准确度最高可达 53%,因此我尝试了几件事,但似乎并没有提高我的准确度。现在我看到人们提到了一种叫做数据增强的东西,并想试一试,因为它似乎有可能提高准确性。但是,我遇到了一个我无法弄清楚的错误。
数据集分布:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from matplotlib.pyplot import imread, imshow, subplots, show
def plot(data_generator):
"""
Plots 4 images generated by an object of the ImageDataGenerator class.
"""
data_generator.fit(df_training)
image_iterator = data_generator.flow(df_training)
# Plot the images given by the iterator
fig, rows = subplots(nrows=1, ncols=4, figsize=(18,18))
for row in rows:
row.imshow(image_iterator.next()[0].astype('int'))
row.axis('off')
show()
x_train = df_training.drop("emotion",axis=1)
image = x_train[1:2].values.reshape(48, 48)
x_train = x_train.values.reshape(x_train.shape[0], 48, 48,1)
x_train = x_train.astype("float32")
image = image.astype("float32")
image = x_train[1:2].reshape(48, 48)
# Creating a dataset which contains just one image.
images = image.reshape((1, image.shape[0], image.shape[1]))
imshow(images[0])
show()
print(x_train.shape)
data_generator = ImageDataGenerator(rotation_range=90)
plot(data_generator)
错误:
ValueError: 输入
.fit()
应该有 4 级。得到了形状的数组: (28709, 2305)
我已经将我的数据重新整形为一个 4d 数组,但由于某种原因,它出现在错误中,因为我的数据是 2d。这是print(x_train.shape)
=> (28709, 48, 48, 1)的形状
x_train
是数据集所在的位置,x_train[1:2] 访问一张图像。
Ps 根据此数据集,您是否会推荐任何其他方法来提高我的准确性。有关我的数据集的更多问题,如果您不理解此部分代码中的某些内容,请告诉我。
解决方案
您在 df_training 而不是 x_train 上使用 data_generator。
至于如何避免过度拟合的更多想法:Tensorflow 有一个官方教程,其中有一些很好的建议: https ://www.tensorflow.org/tutorials/keras/overfit_and_underfit
推荐阅读
- c# - 下面单向加密的解密方法
- r - 获取执行程序目录
- karate - 空手道:在可重用功能文件夹中的 js 文件未找到文件错误,并且此功能是从另一个功能调用的
- java - 如何从java arraylist中删除用户定义的对象
- react-native - 使用 panResponder 时如何在平面列表中拖放单个项目?
- reactjs - 如何在 React devtool 中查看和更新 hooks 状态值?
- apache-spark - Spark结构化流中嵌套json对象的列数据
- c# - C#如何将本地日期转换为UTC时间00:00:00和23:59:59?
- c# - 如何将这些名称“插入”到我要更新的方法/变量中?
- c# - 无法加载文件或程序集..即使它在正确的位置并且构建良好