如何提高二维形状识别模型的性能?

问题描述 投票:0回答:1

所以这是一个2D形状识别问题。

搜索以下形状类别进行分类:

圆形、半圆形、四分之一圆、三角形、正方形、长方形、 梯形、五边形、六边形、七边形、八边形、星形、十字形

我使用 PIL Draw 生成了自己的图像。然后我将它们旋转 360 度,每 3 度就是另一个图像(这样做是为了获得一组不同的图像)。以下是一些图片:

四分之一圆

十字

长方形。

然后我将它们分为三组,训练(0.7)测试(0.2)验证(0.1)。

我在拟合模型时使用训练和测试数据并使用验证数据进行验证。

这是我的模型结构:

这是 25 个 epoch 后的结果:

Epoch 25/25
51/51 - 2s - loss: 0.2517 - accuracy: 0.9010 - val_loss: 0.1253 - val_accuracy: 0.9688 - 2s/epoch - 30ms/step

这是我的验证码:

test_generator = test_datagen.flow_from_directory(
    test_data_dir, target_size=img_size, batch_size=batch_size, class_mode='categorical', color_mode='grayscale', shuffle=True)

from sklearn.metrics import classification_report

# Generate predictions for the test set
predictions = loaded_model.predict(test_generator)

但是当我使用验证数据验证模型时,我得到的结果是:

当我用我拥有的真实图像测试它们时(要预测的最终图像)。它的表现根本不好。

它预测该图像是一个三角形:

我觉得这应该是一个简单的任务,但它的预测能力非常糟糕。接下来我可以尝试什么?

python tensorflow keras classification shapes
1个回答
0
投票

根据您的训练数据,您的验证准确度几乎为 97%,因此当您评估测试集时,您应该会得到接近该结果的结果。 首先,在测试生成器中设置 shuffle=False 。 下面是进行预测、获取准确性、混淆矩阵和分类报告的代码

from sklearn.metrics import confusion_matrix, classification_report
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')

def predictor(model,test_gen):
    classes=list(test_gen.class_indices.keys())
    class_count=len(classes)
    preds=model.predict(test_gen, verbose=1)
    errors=0
    pred_indices=[]
    test_count =len(preds)    
    for i, p in enumerate (preds):
        pred_index=np.argmax(p)
        pred_indices.append(pred_index)
        true_index= test_gen.labels[i]    
        if  pred_index != true_index:        
            errors +=1 
    accuracy = (test_count-errors)*100/test_count
    ytrue=np.array(test_gen.labels, dtype='int')
    ypred=np.array(pred_indices, dtype='int')    
    msg=f'There were {errors} errors in {test_count} tests for an accuracy of {accuracy:6.2f} '
    print (msg)
    cm = confusion_matrix(ytrue, ypred )
    # plot the confusion matrix
    plt.figure(figsize=(20, 20))
 sns.heatmap(cm, annot=True, vmin=0, fmt='g', cmap='Blues', cbar=False)       
    plt.xticks(np.arange(class_count)+.5, classes, rotation=90)
    plt.yticks(np.arange(class_count)+.5, classes, rotation=0)
    plt.xlabel("Predicted")
    plt.ylabel("Actual")
    plt.title("Confusion Matrix")
    plt.show()
    clr = classification_report(ytrue, ypred, target_names=classes, digits= 4) # create classification report
    print("Classification Report:\n----------------------\n", clr)
    return 
predictor(loaded_model,test_generator)

© www.soinside.com 2019 - 2024. All rights reserved.