如何解决给定代码的混淆矩阵数据集仅包含6个类,没有标签
数据集分为验证集和训练集。我使用移动网络模型。我得到97%的准确性。但是混淆矩阵仅显示16。我不知道如何为混淆矩阵编写代码。帮我编写混淆矩阵和分类报告的代码。
train_datagen=ImageDataGenerator(preprocessing_function=keras.applications.mobilenet.preprocess_input,validation_split=0.30)
train_generator=train_datagen.flow_from_directory('/content/gdrive/My Drive/combin',target_size=(224,224),batch_size=32,class_mode='categorical',shuffle=True,subset='training')
validation_generator = train_datagen.flow_from_directory(
'/content/gdrive/My Drive/combin',# same directory as training data
target_size=(224,batch_size=1,subset='validation') # set as validation data
from keras.applications.mobilenet import MobileNet
from keras.models import Model
from keras.layers import Dense,GlobalAveragePooling2D
# parameters for architecture
input_shape = (224,224,3)
num_classes = 6
conv_size = 32
# parameters for training
batch_size = 32
num_epochs = 20
# load MobileNet from Keras
MobileNet_model = MobileNet(include_top=False,input_shape=input_shape)
# add custom Layers
x = MobileNet_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512,activation="relu")(x)
Custom_Output = Dense(num_classes,activation='softmax')(x)
# define the input and output of the model
model = Model(inputs = MobileNet_model.input,outputs = Custom_Output)
# compile the model
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['categorical_accuracy'])
model.summary()
#Confution Matrix and Classification Report
Y_pred = model.predict_generator(validation_generator)
y_pred = np.argmax(Y_pred,axis=1)
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes,y_pred))
print('Classification Report')
target_names = ['1','2','3','4','5','6']
print(classification_report(validation_generator.classes,y_pred,target_names=target_names))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。