【发布时间】: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