【问题标题】:Completely Different prediction results from .h5 keras model and .json tensorflow.js model.h5 keras 模型和 .json tensorflow.js 模型的预测结果完全不同
【发布时间】:2020-10-13 07:59:19
【问题描述】:

所以,我的模型在测试图像上给出了相当准确的结果

import cv2
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode

import matplotlib.pyplot as plt
face_haar_cascade = cv2.CascadeClassifier('/content/gdrive/My Drive/New FEC Facial Expression/haarcascade_frontalface_default.xml')
from IPython.display import Image
try:
 filename = '/content/gdrive/My Drive/photo-1533227268428-f9ed0900fb3b.jpg'
 img = cv2.imread(filename)

 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

 faces = face_haar_cascade.detectMultiScale(gray, 1.3,6)
 print('faces', faces)
 for(x,y,w,h) in faces:
   cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
   roi_gray = gray[y:y+h, x:x+w]
   roi_color = img[y:y+h, x:x+w]
   plt.grid(None)
   plt.xticks([])
   plt.yticks([])
   imgplot = plt.imshow(img)
 # Show the image which was just taken.
 # display(Image(filename))
except Exception as err:
 # Errors will be thrown if the user does not have a webcam or if they do not
 # grant the page permission to access it.
 print(str(err))


import cv2
import sys

imagePath ='/content/gdrive/My Drive/photo-1533227268428-f9ed0900fb3b.jpg'
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faceCascade = cv2.CascadeClassifier('/content/gdrive/My Drive/New FEC Facial Expression/haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(
   gray,
   scaleFactor=1.3,
   minNeighbors=3,
   minSize=(30, 30)
)

print("[INFO] Found {0} Faces.".format(len(faces)))

for (x, y, w, h) in faces:
   cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
   roi_color = image[y:y + h, x:x + w]
   print("[INFO] Object found. Saving locally.")
   cv2.imwrite('/content/gdrive/My Drive/converted Images/faces.jpg', roi_color)

status = cv2.imwrite('faces_detected.jpg', image)
print("[INFO] Image faces_detected.jpg written to filesystem: ", status)
# from skimage import io
from keras.preprocessing import image
img = image.load_img('/content/gdrive/My Drive/converted Images/faces.jpg', color_mode = "grayscale", target_size=(48, 48))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x /= 255
show_img=image.load_img('/content/gdrive/My Drive/converted Images/faces.jpg', grayscale=False, target_size=(200, 200))
plt.gray()
plt.imshow(show_img)
plt.show()
if len(faces): 
 custom = model.predict(x)
 index = np.argmax(custom[0])
 emotion1 = custom[0][index]*100
 print(custom)
 print(emotion_label_to_text[index],' => ',  emotion1)
else:
 print('No Face Detected')

这给出了很好的结果,并且相同结果的输出是正确的,我插入的图像是快乐图像,opencv 用于检测面部并裁剪它,然后使用裁剪后的图像放入模型并给我效果不错,

但是 tf.js 部分我使用 tfjs 转换器将 keras 模型转换为 .json 并编写了以下代码

 const classifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2);
    try {
        const canvImg = await canvas.loadImage(
            path.join(__dirname, `images/${req.file.filename}`)
        );
        const image = await cv.imread(path.join(__dirname, `/images/${req.file.filename}`));
        const classifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2);
        const { objects, numDetections } = classifier.detectMultiScale(image.bgrToGray());
        if (!objects.length) {
            return next({
                msg: 'No face detected'
            })
        } else {
            const model = await tf.loadLayersModel(
                "http://localhost:8000/models/model.json"
            );
            const obj = objects[0]
            const cnvs = canvas.createCanvas(48, 48);
            const ctx = cnvs.getContext("2d");
            ctx.drawImage(canvImg, obj.x, obj.y, obj.width, obj.height, 0, 0, cnvs.width, cnvs.height);
            var tensor = tf.browser
                .fromPixels(cnvs)
                .mean(2)
                .toFloat()
                .expandDims(-1)
                .expandDims(0, 'None')



            const prediction = await model.predict(tensor).data();
            console.log(prediction);
            var emotions = [
                "angry",
                "disgust",
                "fear",
                "happy",
                "sad",
                "surprise",
                        ];
            var index = Object.values(prediction).findIndex(
                (p) => p === Math.max(...Object.values(prediction))
            );
            res.status(200).json(emotions[index])
            fs.unlink(
                path.join(process.cwd(), "./faceDetection/images/" + req.file.filename),
                function(err, removed) {
                    if (err) console.log("file removing err");
                    else console.log("file removed");
                }
            );
        }

    } catch (e) {
        return next(e)
    }

我使用 opencv4nodejs 检测图像,使用 canvas 裁剪图像(canvas 为我裁剪面部部分提供了良好的结果)和 tf.js 用于预测,但是每次在所有这些键中,输出都会给我相同的结果在对象中,其中一个将得到 1(在这种情况下是恐惧),并为我在 keras 中测试的同一图像继续给我相同的结果。

我在张量的操作上做错了吗?

【问题讨论】:

  • 您是否尝试打印张量以查看每个预测是否不同?
  • 是的,它们对于不同的图像是不同的,但仍然给我相同的结果,或者如果不一样,那么完全随机和不准确的结果(这与我的 h5 模型给出的结果相去甚远)跨度>

标签: python node.js tensorflow keras tensorflow.js


【解决方案1】:

一个可能的原因。在 python 中,您使用x /= 255 将图像输入“标准化”为 [0,1]。你不是用 Javascript 做的。

【讨论】:

  • 让 tensor = tf.browser.fromPixels(cnvs, 1) const resized = tf.image.resizeBilinear(tensor, [48, 48]).toFloat() const offset = tf.scalar(255.0 ); const normalized = resized.sub(offset).div(offset); const batched = normalized.expandDims(0) const prediction = await model.predict(batched).data();是的,现在我使用归一化(如果这是正确的)仍然给我不好的结果
  • 还是很奇怪。你确定你需要子(偏移量)吗?这样你的张量就会是负数。
【解决方案2】:

js中的预处理与python中的不同。

在 python 中,图像通过将其除以 255 进行归一化

在 Js 中,通过计算第三轴上的平均值(mean(2))将图像转换为灰度。这是张量应该是什么:

 const tensor = tf.browser.fromPixels(cnvs)
  .div(255)
  .toFloat()
  .expandDims(0)       

【讨论】:

  • 你提供的张量没有变成灰度,你说的是 sum(2) 但是在哪里添加呢?
  • 这意味着(2)。当您计算图像的均值(2)时,它会输出灰度图像。这不是您想要的
  • 是的,我实际上想要灰度图像,因为我的模型需要 [null,48,48,1] 输入。所以我需要灰度,你也可以在 python 代码中看到它
  • 是的,我之前错过了那部分。因此,您可以保留.mean(2)。但是,有几种计算灰度的方法。如果你想复制完全相同的功能,你需要知道 plt 到底是哪一个完成的
猜你喜欢
  • 2019-07-19
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2021-01-27
  • 2021-07-31
  • 1970-01-01
  • 2019-11-07
相关资源
最近更新 更多