【问题标题】:How to create ROC , FAR , FRR in CNN Keras model?如何在 CNN Keras 模型中创建 ROC、FAR、FRR?
【发布时间】:2018-06-28 21:51:24
【问题描述】:

我正在做我的最后一个项目,使用 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)))

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

【问题讨论】:

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


    【解决方案1】:

    当然!有一个代码 sn-p 发布 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
    

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 2020-05-31
      • 1970-01-01
      • 2021-03-12
      • 2020-08-15
      • 2012-08-25
      • 2017-12-10
      • 2021-04-17
      • 2021-06-12
      相关资源
      最近更新 更多