0

我使用 Bi-LSTM 制作掩码检测程序。我想获得程序的准确性,但出现了这个错误:分类指标无法处理二进制和未知目标的混合。有人能帮我吗?这是我的代码:

i=0
if not path.exists("Withoutmask"):
    os.mkdir('Withoutmask')
with_mask = np.load('/content/drive/MyDrive/Colab Notebooks/Dataset_AI/with_mask.npy', allow_pickle=True)
without_mask = np.load('/content/drive/MyDrive/Colab Notebooks/Dataset_AI/without_mask.npy', allow_pickle=True)

with_mask = with_mask.reshape(with_mask.shape[0], 50 * 50 * 3)
without_mask = without_mask.reshape(without_mask.shape[0], 50 * 50 * 3)

X = np.r_[with_mask, without_mask]

labels = np.zeros(X.shape[0])
labels[with_mask.shape[0]:] = 1.0
names = {0: 'Mask', 1: 'No Mask'}
color = {0: (255, 255, 0), 1: (0, 255, 255)}

x_train, x_test, y_train, y_test = train_test_split(X, labels, test_size=0.25)

pca = PCA(n_components=3)
X_train = pca.fit_transform(x_train)

x_train, x_test, y_train, y_test = train_test_split(X, labels, test_size=0.25)

x_train = numpy.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
x_test = numpy.reshape(x_test, (x_test.shape[0], 7500, x_test.shape[1]))

model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True), input_shape=(1,7500)))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, batch_size=1, verbose=2)
model.summary()

start=time.time()
y_pred = model.predict(x_test)
y_pred = (y_pred>0.5).astype(int)
end=time.time()

print('Accuration : '+str(accuracy_score(y_test,y_pred)*100)+'%')
print('Time : '+str(end-start))
f, ax = plt.subplots(figsize=(8,7))
sn.set(font_scale=1.8)
sn.heatmap(confusion_matrix(y_test,y_pred), annot=True, fmt=".0f", cmap='coolwarm',xticklabels=['with mask','without mask'], 
           yticklabels=['with mask','without mask'], cbar=False)
plt.show()
4

0 回答 0