首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Keras模型重复输出0,没有错误。

Keras模型重复输出0,没有错误。
EN

Stack Overflow用户
提问于 2018-12-31 19:20:47
回答 1查看 39关注 0票数 0

我一直在使用带有cv2人脸检测脚本的Keras模型来进行面部识别。最近我遇到了一个问题,当模型进行预测时,它会输出0。这特别奇怪,因为标签数组中没有0。顺便说一句,我有一个名为opencvtrainer的目录,它包含另外3个目录,每个目录都包含人们面孔的图像。以下是代码:

代码语言:javascript
运行
复制
import PIL as PIL
import tensorflow as tf
import numpy as np
import cv2 as cv2
import os
# goes to opencvtrainer directory
basedir = os.path.dirname(os.path.abspath(__file__))
imagedir = os.path.join(basedir, "opencvtrainer")
ylabels = []
# if directory person: id
labelids = {
    "john_": 001,
    "erin_": 002,
    "scott_": 003,
    "colin_": 004
}
''' "glenn_": 004,
   "faith_": 005,
'''

xtrain = []
xl = []
# make general face classifier
# creates AI needing training
# goes through files in files in the opencvtrainer directory
fc = cv2.CascadeClassifier("lib/python2.7/site-package\
s/cv2/data/haarcascade_frontalface_alt2.xml")
for root, dirs, files in os.walk(imagedir):
    for file in files:
        if "png" in file:
            # path to file
            path = os.path.join(root, file)

            # whose file it is
            label = os.path.basename(root)

            # gets image
            imagep = PIL.Image.open(path)

            # convets image into greyscale then numpy array
            imagear = np.array(imagep.convert("L"), "uint8")
            imagearre = imagear
            face = fc.detectMultiScale(imagearre)

            for (x, y, w, h) in face:
                # makes roi for face
                roi = imagearre[y:y + h, x:x + w]
                roi = cv2.resize(roi, (70, 70))
                # gives that np array to xtrain
                xtrain.append(roi)
                print(roi.shape)
                # gives ylabels a num for all files it opened
                xl.append(labelids[label])


xtrain = np.array(xtrain)
ylabels = np.array(xl)
#adds AI from keras
model = tf.keras.models.Sequential()
# tells what an input should be & does crap w/ current input
model.add(tf.keras.layers.Flatten(input_shape=(70, 70)))
# adds layer
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# adds layer
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# adds layer
model.add(tf.keras.layers.Dense(1, activation=tf.nn.softmax))
# tests for accuracy
model.compile(optimizer="adam", loss="binary_crossentropy", metrics= . 
['accuracy'])
print(ylabels)
model.fit(xtrain, ylabels, epochs=3)
model.save("test11")'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-31 19:47:22

1)将最后一层中的单元数更改为4(因为您有4个不同的类):

代码语言:javascript
运行
复制
tf.keras.layers.Dense(4, activation=tf.nn.softmax)

2)开始将标签编号从零开始,而不是一个:

代码语言:javascript
运行
复制
labelids = {"john_": 0, "erin_": 1, "scott_": 2, "colin_": 3}

3)以sparse_categorical_crossentropy作为损失函数。或者,您可以对标签进行一次热编码,然后使用categorical_crossentropy作为丢失函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53990774

复制
相关文章

相似问题

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