【问题标题】:Keras Sequential Model accuracy is bad. Model is Ignoring/neglecting a classKeras 序列模型的准确性很差。模型忽略/忽略一个类
【发布时间】:2020-08-13 07:46:07
【问题描述】:

小背景:我正在制作一个简单的石头剪刀布图像分类器程序。基本上,我希望图像分类器能够区分石头、纸张或剪刀图像。

问题:该程序在石头和纸的两个课程中效果惊人,但在给定剪刀测试图像时完全失败。我试过增加我的训练数据和其他一些东西,但没有运气。我想知道是否有人对如何抵消这一点有任何想法。

旁注:我怀疑这也与过度拟合有关。我这样说是因为该模型在训练数据上的准确率约为 92%,而在测试数据上的准确率为 55%。

import numpy as np
import os
import cv2
import random
import tensorflow as tf
from tensorflow import keras

CATEGORIES     = ['rock', 'paper', 'scissors']
IMG_SIZE       = 400  # The size of the images that your neural network will use
CLASS_SIZE     = len(CATEGORIES)
TRAIN_DIR  = "../Train/"

def loadData( directoryPath ):
    data = []
    for category in CATEGORIES:
        path = os.path.join(directoryPath, category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
                data.append([new_array, class_num])
            except Exception as e:
                pass
    return data


training_data = loadData(TRAIN_DIR)
random.shuffle(training_data)
X = [] #features
y = [] #labels

for i in range(len(training_data)):
    features = training_data[i][0]
    label    = training_data[i][1]
    X.append(features)
    y.append(label)

X = np.array(X)
y = np.array(y)
X = X/255.0

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(IMG_SIZE, IMG_SIZE)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(CLASS_SIZE)
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])


model.fit(X, y, epochs=25)


TEST_DIR  = "../Test/"
test_data = loadData( TEST_DIR )
random.shuffle(test_data)
test_images = []
test_labels = []

for i in range(len(test_data)):
    features = test_data[i][0]
    label    = test_data[i][1]
    test_images.append(features)
    test_labels.append(label)

test_images = np.array(test_images)
test_images = test_images/255.0
test_labels = np.array(test_labels)

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

# Saving the model
model_json = model.to_json()
with open("model.json", "w") as json_file :
    json_file.write(model_json)

model.save_weights("model.h5")
print("Saved model to disk")

model.save('CNN.model')

如果你想快速创建海量训练数据:https://github.com/ThomasStuart/RockPaperScissorsMachineLearning/blob/master/source/0.0-collectMassiveData.py

提前感谢任何帮助或想法:)

【问题讨论】:

    标签: tensorflow machine-learning keras sequential image-classification


    【解决方案1】:

    您可以通过添加 2 个附加层、一个 dropout 层和一个密集层来简单地测试过度拟合。还要确保在每个 epoch 之后对您的 train_data 进行洗牌,以便模型保持学习的一般性。此外,如果我没看错,您正在做一个多类分类,但在最后一层没有 softmax 激活。我会推荐你​​使用它。

    使用 drouput 和 softmax,您的模型将如下所示:

    model = keras.Sequential([
    keras.layers.Flatten(input_shape=(IMG_SIZE, IMG_SIZE)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dropout(0.4), #0.4 means 40% of the neurons will be randomly unused
    keras.layers.Dense(CLASS_SIZE, activation="softmax")
    

    ])

    最后的建议是:Cnn 在处理此类任务时通常表现更好。您可能希望切换到 CNN 网络,以获得更好的性能。

    【讨论】:

    • 感谢您的建议。看起来过度拟合不是问题。如果我添加一个 dropout 层和/或添加 actication="softmax",则每个 Epoch 的准确度保持在 33% 左右。我的印象是这是一个 CNN,但我对 ML 很陌生,所以我会看看为什么它不是。感谢您为我指明了一个好的方向。
    • 很高兴我能进一步帮助您。如果我的回答对您有帮助,请随时给它一个赞或接受它作为答案
    • 我厌倦了支持它,但不幸的是我需要更多的声誉才能这样做:/
    • 你需要更多的点赞声望,但是,你可以通过点击它下面的钩子来接受我的回答
    • 得到了你。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多