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

时间:2018-01-19 15:47:35

标签: python-3.x keras classification face-recognition roc

我正在研究我的最终项目,使用CNN进行人脸识别,我是这个领域的新人,正在寻找你的建议......

我已经在keras中建立CNN模型并在Faces94上进行训练,我的准确率为90.97%

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

我尝试了很多代码,但没有任何作用。你能帮帮我吗?

PFB我的代码:

    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)))

#添加另一个2D卷积层和2D最大池化层,具有64个输出通道

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

#添加另一个2D卷积层和2D最大池化层,具有128个输出通道

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))

1 个答案:

答案 0 :(得分:0)

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

# 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