首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CNN Keras模型中创建ROC,FAR,FRR?

如何在CNN Keras模型中创建ROC,FAR,FRR?
EN

Stack Overflow用户
提问于 2018-01-19 23:47:35
回答 1查看 1.2K关注 0票数 3

我正在做我的最后一个项目,使用CNN进行人脸识别,我是这个领域的新手,正在寻求建议。

我已经在Keras中建立了CNN模型,并在Faces94上对其进行了训练,我得到了90.97%的准确率

现在,我正在尝试绘制CRO,FAR,FRR。

我已经尝试了很多代码,但都不起作用。你能帮帮我吗?

PFB我的代码:

代码语言:javascript
复制
import keras
from keras import backend as K
import os
from keras.layers.advanced_activations import LeakyReLU
from __future__ import print_function
from keras.datasets import mnist
import matplotlib.pylab as plt
 
from importlib import reload
def set_keras_backend(backend):
    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        reload(K)
        assert K.backend() == backend

set_keras_backend("tensorflow")

DATA = joblib.load(open('Data.sav', 'rb'))
LABEL = joblib.load(open('Lable.sav', 'rb'))
    
print(DATA.shape)
print(LABEL.shape)

print(tf.__version__)

X_train, X_test, y_train, y_test = train_test_split(DATA, LABEL, test_size=0.30, random_state=45)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
print(X_train[0])

X_train = np.reshape(X_train,(X_train.shape[0],200,180,1))
X_test = np.reshape(X_test,(X_test.shape[0],200,180,1))

# convert the data from binary to float 
    
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

    
model = Sequential()
    
model.add(Conv2D(32, kernel_size=(5,5), strides=(1, 1),
                     activation='relu',
                     input_shape=([200,180,1])))
  
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2)))

# add another 2D convolutional layer and 2D max pooling layer, with 64 output channels

model.add(Conv2D(64,(5,5), activation='relu'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2)))


# add another 2D convolutional layer and 2D max pooling layer, with 128 output channels

model.add(Conv2D(128,(5,5), activation='relu'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.30))


model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(72, activation='softmax'))

# When we compile the model, we declare the loss function and the optimizer
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy'])

# Train the model
hist = model.fit(X_train, Y_train,batch_size=32,epochs=12, verbose=1, validation_data=(X_test, Y_test))


score = model.evaluate(X_test, Y_test, verbose=0)

print("%s: %.2f%%" % ('Accuracy', score[1]*100))
EN

回答 1

Stack Overflow用户

发布于 2018-01-23 10:12:47

好的!有一个发布的代码片段用于AUC计算,但您可以调整它以获得here和FAR too (+details)。为了存储计算值,您可以实现一些callback并在最后绘制它们。

代码语言:javascript
复制
# AUC for a binary classifier

def auc(y_true, y_pred):
    ptas = tf.stack([binary_PTA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)
    pfas = tf.stack([binary_PFA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)
    pfas = tf.concat([tf.ones((1,)) ,pfas],axis=0)
    binSizes = -(pfas[1:]-pfas[:-1])
    s = ptas*binSizes
    return K.sum(s, axis=0)

# PFA, prob false alert for binary classifier
def binary_PFA(y_true, y_pred, threshold=K.variable(value=0.5)):
    y_pred = K.cast(y_pred >= threshold, 'float32')
    # N = total number of negative labels
    N = K.sum(1 - y_true)
    # FP = total number of false alerts, alerts from the negative class labels
    FP = K.sum(y_pred - y_pred * y_true)
    return FP/N

# P_TA prob true alerts for binary classifier
def binary_PTA(y_true, y_pred, threshold=K.variable(value=0.5)):
    y_pred = K.cast(y_pred >= threshold, 'float32')
    # P = total number of positive labels
    P = K.sum(y_true)
    # TP = total number of correct alerts, alerts from the positive class labels
    TP = K.sum(y_pred * y_true)
    return TP/P
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48344760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档