简体   繁体   English

CNN培训多标签分类---不起作用

[英]CNN training multi label classification---not working

Try to predict texture images' labels, an image can contain two labels like['banded', 'striped'], though most of them only have one label. 尝试预测纹理图像的标签,图像可以包含两个标签,如['带状','条纹'],但大多数只有一个标签。

The output accuracy is extremely high....the first epoch can have 0.96 acc...but the prediction array are all close to 0, which is wrong, there must be at least one number is relevant closed to 1. 输出精度非常高....第一个时期可以有0.96 acc ...但是预测数组都接近于0,这是错误的,必须至少有一个数字与1相关。

Can someone help me? 有人能帮我吗? Thank you!! 谢谢!!

Here are the code 这是代码

Input image = (read by opencv)/255
Multi-labels = First LabelEncoder convert to numbers, then keras.to_categorical

Then I built a CNN model as follow 然后我建立了一个CNN模型如下

X_train, X_test, y_train, y_test = train_test_split(img_array, test_value, test_size=0.1)

model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), padding='Same', data_format='channels_last', activation='relu',
                 input_shape=(300, 300, 3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(300, init ='uniform',activation='relu'))
model.add(Dense(285, init = 'uniform',activation='sigmoid'))
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])


history = model.fit(X_train, y_train, batch_size= 24, epochs=10, validation_split=0.15)

If your model has only 2 labels, the last layer should be 如果您的模型只有2个标签,那么最后一层应该是

model.add(Dense(2, init = 'uniform',activation='sigmoid'))

However, your class imbalance can also affect the accuracy. 但是,您的班级不平衡也会影响准确性。 If your class imbalance is too high, your model will show 95%+ training, validation, and testing accuracies but the individual accuracies will still be low and the model will not work for real world data. 如果您的班级不平衡太高,您的模型将显示95%+培训,验证和测试准确性,但个人准确度仍然很低,并且该模型不适用于真实世界数据。

The detailed and class-based accuracy can be understood using : 使用以下内容可以了解详细和基于类的准确性:

from sklearn.metrics import classification_report



X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30)
X_test1, X_valid, y_test1, y_valid = train_test_split(X_test, y_test, test_size=0.30)
model.fit(X_train, y_train, batch_size=64, epochs=8, shuffle=True, validation_data=(X_test1,y_test1), callbacks=[metrics])

Y_TEST = np.argmax(y_valid, axis=1)
y_pred = model.predict_classes(X_valid)

print("#"*50,"\n",classification_report(Y_TEST, y_pred))

Please share your class distribution for further understanding. 请分享您的课程分布以便进一步了解。

Not sure why the number of neurons in Dense layer is 285. If there are 47 categories, then the output neurons of Dense layer should be 47. Also, use a kernel initializer like he_normal instead of uniform. 不确定为什么Dense层中的神经元数量是285.如果有47个类别,那么Dense层的输出神经元应该是47.此外,使用像he_normal而不是uniform的内核初始化器。 https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py

model.add(Dense(47, activation='sigmoid'))
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])

This is a multi-label classification example with 5 classes. 这是一个包含5个类的多标签分类示例。

https://github.com/suraj-deshmukh/Keras-Multi-Label-Image-Classification https://github.com/suraj-deshmukh/Keras-Multi-Label-Image-Classification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM