我正在尝试计算预训练DL模型的混淆矩阵,代码如下:(请不要介意训练和验证样本的数量。
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras import applications
from keras import backend as K
K.set_image_dim_ordering('tf')
from sklearn.metrics import classification_report,confusion_matrix
import itertools
from keras.optimizers import SGD
# Plotting the confusion matrix
def plot_confusion_matrix(cm, classes,
normalize=False, #if true all values in confusion matrix is between 0 and 1
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
img_width, img_height = 64,64
top_model_weights_path = 'model.h5'
train_data_dir = 'data4/train'
validation_data_dir = 'data4/validation'
nb_train_samples = 20
nb_validation_samples = 20
epochs = 50
batch_size = 10
def save_bottlebeck_features():
datagen = ImageDataGenerator(rescale=1. / 255)
model = applications.VGG16(include_top=False, weights='imagenet', input_shape=(64, 64, 3))
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
bottleneck_features_train = model.predict_generator(
generator, nb_train_samples // batch_size)
np.save(open('bottleneck_features_train', 'wb'),bottleneck_features_train)
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
bottleneck_features_validation = model.predict_generator(
generator, nb_validation_samples // batch_size)
np.save(open('bottleneck_features_validation', 'wb'),bottleneck_features_validation)
def train_top_model():
train_data = np.load(open('bottleneck_features_train', 'rb'))
train_labels = np.array([0] * (nb_train_samples // 2) + [1] * (nb_train_samples // 2))
validation_data = np.load(open('bottleneck_features_validation', 'rb'))
validation_labels = np.array([0] * (nb_validation_samples // 2) + [1] * (nb_validation_samples // 2))
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=1e-2, decay=0.00271, momentum=0.9, nesterov=False)
model.compile(optimizer=sgd,
loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save_weights(top_model_weights_path)
Y_pred = model.predict(validation_data)
print(Y_pred)
y_pred = np.argmax(Y_pred, axis=1)
print(y_pred)
target_names = ['class 0(bt)', 'class 1(cm)']
print(classification_report(np.argmax(validation_labels,axis=1), y_pred,target_names=target_names))
print(confusion_matrix(np.argmax(validation_labels,axis=1), y_pred))
cnf_matrix = (confusion_matrix(np.argmax(validation_labels,axis=1), y_pred))
np.set_printoptions(precision=2)
plt.figure()
# Plot non-normalized confusion matrix
plot_confusion_matrix(cnf_matrix, classes=target_names,
title='Confusion matrix')
plt.show()
save_bottlebeck_features()
train_top_model()
当我运行此脚本时,我得到了轴的值错误,错误的完整描述是:
File "<ipython-input-4-f618e2626bbb>", line 1, in <module>
runfile('C:/Users/rajaramans2/codes/untitled4.py', wdir='C:/Users/rajaramans2/codes')
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/rajaramans2/codes/untitled4.py", line 116, in <module>
train_top_model()
File "C:/Users/rajaramans2/codes/untitled4.py", line 106, in train_top_model
print(classification_report(np.argmax(validation_labels,axis=1), y_pred,target_names=target_names))
File "C:\Program Files\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 963, in argmax
return _wrapfunc(a, 'argmax', axis=axis, out=out)
File "C:\Program Files\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
ValueError: axis(=1) out of bounds
我真诚地希望在分类报告和混淆矩阵中声明轴有问题。非常感谢您的帮助