【问题标题】:CSV File Dataset Augmentation using Keras使用 Keras 增强 CSV 文件数据集
【发布时间】:2020-01-09 13:01:12
【问题描述】:

我正在Kaggle 开展一个已经实施的项目,该项目与图像分类有关。我总共有 6 个类要预测,分别是 Angry、Happy、Sad 等。我已经实现了一个 CNN 模型,我目前只使用 4 个类(图像数量最多的类),但我的模型过度拟合,我的验证准确度最高可达 53%,因此我尝试了几件事,但似乎并没有提高我的准确度。现在我看到人们提到了一种叫做 Data Augmentation 的东西,我想试一试,因为它似乎有可能提高准确性。但是,我遇到了一个我无法弄清楚的错误。

数据集分布:

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] 访问一张图像。

P.s 根据此数据集,您是否会推荐任何其他方法来提高我的准确性。有关我的数据集的更多问题,如果您不理解此部分代码中的某些内容,请告诉我。

【问题讨论】:

    标签: python machine-learning keras neural-network data-augmentation


    【解决方案1】:

    您在 df_training 而不是 x_train 上使用 data_generator。

    关于如何避免过拟合的更多想法: Tensorflow 有一个官方教程,其中有一些很好的建议: https://www.tensorflow.org/tutorials/keras/overfit_and_underfit

    【讨论】:

    • 我在 df_training 上使用它,data_generator.fit(df_training),如上所示,然后我将 df_training 分配给 x_train,所以 x_train 已经是 df_training
    • 您正在使用返回新数据帧的 numpy .drop() 函数。原始数据框仍然存在,并由 df_training 引用
    • 尝试在x_train之后打印(df_training.shape)来比较两者
    • 我的笔记本电脑离我不近,我回家后会做的,谢谢先生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多