Knn精度为0.0,SVC精度为0.0

2024-10-01 11:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个128d的嵌入向量阵列上训练KNN和SVC进行人脸识别,但是训练后KNN和SVC的准确率都是0.0。你知道吗

奇怪的是,如果我和大约5个人一起训练,accuray显示0.6~0.7,但是在这里我和大约5000人一起训练,它返回0.0的精确度。你知道吗

import numpy as np 
import cv2
import json
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

from keras.models import Sequential, Model
from keras.layers import *
from keras.optimizers import *
import tensorflow as tf 
from keras import backend as K 
import h5py 
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from sklearn.metrics import accuracy_score

import time
import pickle


def convnet_model_():
    initial_inceptionv2_model = InceptionResNetV2(weights=None, include_top = False, input_shape = (160, 160, 1))
    x = initial_inceptionv2_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(4096, activation = 'relu')(x)
    x = Dropout(0.6)(x)
    x = Dense(4096, activation = 'relu')(x)
    x = Dropout(0.6)(x)
    x = Lambda(lambda x: K.l2_normalize(x, axis = 1))(x)
    convnet_model = Model(inputs=initial_inceptionv2_model.input, outputs = x)
    return convnet_model

def deep_rank_model():
    convnet_model = convnet_model_()

    first_input = Input(shape = (160, 160, 1))
    first_conv = Conv2D(96, kernel_size = (8, 8), strides = (16, 16), padding = 'same')(first_input)
    first_max = MaxPool2D(pool_size = (3, 3), strides = (2, 2), padding = 'same')(first_conv)
    first_max = Flatten()(first_max)
    first_max = Lambda(lambda x: K.l2_normalize(x, axis =1))(first_max)

    second_input = Input(shape = (160, 160, 1))
    second_conv = Conv2D(96, kernel_size = (8, 8), strides = (32, 32), padding = 'same')(second_input)
    second_max = MaxPool2D(pool_size = (7, 7), strides = (4, 4), padding = 'same')(second_conv)
    second_max = Flatten()(second_max)
    second_max = Lambda(lambda x: K.l2_normalize(x, axis = 1))(second_max)

    merge_one = concatenate([first_max, second_max])
    merge_two = concatenate([merge_one, convnet_model.output])
    emb = Dense(4096)(merge_two)
    emb = Dense(128)(emb)
    l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis = 1))(emb)

    final_model = Model(inputs = [first_input, second_input, convnet_model.input], outputs = l2_norm_final)
    return final_model

print("Model: ")
deep_rank_model = deep_rank_model()


from keras.models import load_model
print("Loading pre-trained weight")
deep_rank_model.load_weights("/home/fr/models/model.hdf5")

from tqdm import tqdm

print("Loading data... ")
with open('/home/fr/jsons/X_train_triplet_lfw160.pkl', 'rb') as f:
    X = pickle.load(f)
    X = np.array(X)
    X = np.expand_dims(X, axis=3)
with open('/home/fr/jsons/y_train_triplet_lfw160.pkl', 'rb') as f:
    y = pickle.load(f)
    y = np.array(y)
with open('/home/fr/jsons/name_map_lfw160.pkl', 'rb') as f:
    name_map = pickle.load(f)
with open('/home/fr/jsons/emb128.json', 'r') as f:
    embs128 = json.load(f)
    embs128 = np.array(embs128)

from sklearn.preprocessing import LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC

print("X: ", np.shape(X))
print("y: ", np.shape(y))
print("name_map: ", np.shape(name_map))
print("embs128: ", np.shape(embs128))

encoder = LabelEncoder()
encoder.fit(y)

total = np.arange(X.shape[0])
print("Total: ", total)
X = embs128[total]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, shuffle = False) #random_state = 42, shuffle = True)

knn = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
svc = LinearSVC()

print("Knn training... ")
knn.fit(X_train, y_train)
svc.fit(X_train, y_train)
acc_knn = accuracy_score(y_test, knn.predict(X_test))
acc_svc = accuracy_score(y_test, svc.predict(X_test))

print(f'KNN accuracy = {acc_knn}, SVM accuracy = {acc_svc}')

有谁在这方面有经验,你能给我一些建议吗?我现在有麻烦了。你知道吗


Tags: fromtestimportinputmodelasnptrain