问题描述
这是我的 CNN 通过库 Keras 的代码,我想绘制一个 ROC 并认为我需要在函数中执行它,就像我对精度图和损失图所做的那样。任何人都可以帮助我解决这个问题,因为我不确定如何去做并且目前正在挣扎。 roc 曲线的代码是否需要在函数中,如果不需要,我将如何对其进行编码
def CNN(imgs,img_labels,test_imgs,test_labels,stride):
#Number of classes (2)
num_classes = len(img_labels[0])
#Size of image
img_rows,img_cols=imgs.shape[1],imgs.shape[2]
input_shape = (img_rows,img_cols,3)
#Creating the model
model = Sequential()
#First convolution layer
model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=input_shape))
#First maxpooling layer
model.add(MaxPooling2D(pool_size=(2,2)))
#Second convolution layer
model.add(Conv2D(64,(3,activation='relu'))
#Second maxpooling layer
model.add(MaxPooling2D(pool_size=(2,2)))
#Third convolution layer
model.add(Conv2D(128,activation='relu'))
#Third maxpooling layer
model.add(MaxPooling2D(pool_size=(2,2)))
#Convert the matrix to a fully connected layer
model.add(Flatten())
#Dense function to convert FCL to 128 values
model.add(Dense(128,activation='relu'))
#Final dense layer on which softmax function is performed
model.add(Dense(num_classes,activation='softmax'))
#Model parameters
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
#Evaluate the model on the test data before training your model
score = model.evaluate(test_imgs,verbose=1)
print('\nKeras CNN binary accuracy:',score[1],'\n')
#The model details
history = model.fit(imgs,shuffle = True,epochs=30,validation_data = (test_imgs,test_labels))
#Evaluate the model on the test data after training your model
score = model.evaluate(test_imgs,verbose=1)
print('\nKeras CNN binary accuracy:','\n')
#Predict the labels from test data
y_pred = model.predict(test_imgs)
Y_pred_classes = np.argmax(y_pred,axis=1)
Y_true = np.argmax(test_labels,axis=1)
#Correct labels
for i in range(len(Y_true)):
if(Y_pred_classes[i] == Y_true[i]):
print("The predicted class is : ",Y_pred_classes[i])
print("The real class is : ",Y_true[i])
break
#The confusion matrix made from the real Y values and the predicted Y values
confusion_mtx = [Y_true,Y_pred_classes]
#Summary of the model
model.summary()
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['Train','Validation'],loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['Train',loc='upper left')
plt.show()
return model,confusion_mtx
model,conf_mat = CNN(X_train,y_trainHot,X_test,y_testHot,1);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)