本文介绍了无法将Google Colab中制作的.h5文件加载到Jupyter Notebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了面罩检测和警报系统的代码,但与此同时却遇到了错误.我在Google Collaboratory中训练了该模型,并在Jupyter Notebook中运行了以下代码.代码如下:

I tried a code for Face mask detect and alert system and I am getting an error regarding the same. I trained the model in Google Collaboratory and ran the following code in Jupyter Notebook. The code is as follows:

# Import necessary libraries
from keras.models import load_model
import cv2
import numpy as np
import tkinter
from tkinter import messagebox
import smtplib

# Initialize Tkinter
root = tkinter.Tk()
root.withdraw()

#Load trained deep learning model
model = load_model('face_mask_detection_alert_system.h5')

#Classifier to detect face
face_det_classifier=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Capture Video
vid_source=cv2.VideoCapture(0)

# Dictionaries containing details of Wearing Mask and Color of rectangle around face. If wearing mask
then color would be
# green and if not wearing mask then color of rectangle around face would be red
text_dict={0:'Mask ON',1:'No Mask'}
rect_color_dict={0:(0,255,0),1:(0,0,255)}

SUBJECT = "Subject"
TEXT = "One Visitor violated Face Mask Policy. See in the camera to recognize user. A Person has been
detected without a face mask in the Hotel Lobby Area 9. Please Alert the authorities."


# While Loop to continuously detect camera feed
    while(True):

        ret, img = vid_source.read()
        grayscale_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        faces = face_det_classifier.detectMultiScale(grayscale_img,1.3,5)

        for (x,y,w,h) in faces:

            face_img = grayscale_img[y:y+w,x:x+w]
            resized_img = cv2.resize(face_img,(56,56))
            normalized_img = resized_img/255.0
            reshaped_img = np.reshape(normalized_img,(1,56,56,1))
            result=model.predict(reshaped_img)

            label=np.argmax(result,axis=1)[0]

            cv2.rectangle(img,(x,y),(x+w,y+h),rect_color_dict[label],2)
            cv2.rectangle(img,(x,y-40),(x+w,y),rect_color_dict[label],-1)
            cv2.putText(img, text_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(0,0,0),2)

            # If label = 1 then it means wearing No Mask and 0 means wearing Mask
            if (label == 1):
                # Throw a Warning Message to tell user to wear a mask if not wearing one. This will
                stay
                #open and No Access will be given He/She wears the mask
                messagebox.showwarning("Warning","Access Denied. Please wear a Face Mask")

                # Send an email to the administrator if access denied/user not wearing face mask
                message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
                mail = smtplib.SMTP('smtp.gmail.com', 587)
                mail.ehlo()
                mail.starttls()
                mail.login('[email protected]','[email protected]')
                mail.sendmail('[email protected]','[email protected]',message)
                mail.close
            else:
                pass
                break

    cv2.imshow('LIVE Video Feed',img)
    key=cv2.waitKey(1)

    if(key==27):
        break

cv2.destroyAllWindows()
source.release()

错误:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\IZZY\Desktop\Dataset\facemaskalert_2.py", line 14, in <module>
    model = load_model('face_mask_detection_alert_system.h5')
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\saving.py", line 492, in load_wrapper
    return load_function(*args, **kwargs)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\saving.py", line 584, in load_model
    model = _deserialize_model(h5dict, custom_objects, compile)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\saving.py", line 274, in _deserialize_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\saving.py", line 627, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\layers\__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\sequential.py", line 301, in from_config
    custom_objects=custom_objects)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\layers\__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\utils\generic_utils.py", line 149, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\engine\base_layer.py", line 1179, in from_config
    return cls(**config)
  File "C:\Users\IZZY\AppData\Local\Programs\Python\Python37\lib\site-
  packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  TypeError: __init__() got an unexpected keyword argument 'ragged'

源代码: https://theaiuniversity.com/courses/face -mask-detection-alert-system/

尝试过的方法,但没有帮助:在Keras中出现了意外的关键字参数"ragged"

Methods Tried but did not help : Unexpected keyword argument 'ragged' in Keras

-h5文件 https://drive.google.com/file/d/10oHrqtrYoD2Hx0olLnnfK0qYSQLlrt3 view?usp = sharing .

推荐答案

问题似乎与此.

这里公认的答案是,导出的模型可能来自tf.keras,而不是直接来自keras:

As the accepted answer here mentions that the exported model may be from tf.keras and not keras directly :

我的建议:

1.您应该尝试将tf.keras用于所有keras导入

1.You should try to use tf.keras for all keras imports

  1. 而且正如穆罕默德在答案中提到的那样,在load_model中使用compile=False.

在colab和本地环境中检查tf和keras的版本.两个版本必须相同.

Check the version of tf and keras in colab and local environment.Both versions need to be same.

来自 Keras github 问题.

这篇关于无法将Google Colab中制作的.h5文件加载到Jupyter Notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:10