SavedModel文件不存在



我正试图在谷歌colab中执行此代码,用于面罩检测项目,但我对savedmodel有问题。使用的代码和错误如下。培训结束了,但是我不明白为什么没有找到保存的模型(我的朋友使用了相同的代码,她没有发现模型有任何问题),有人可以帮助我吗?我错过什么了吗?

import tensorflow
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
batch_size=32
# Create a callback that saves the model's weights
cp_callback = tensorflow.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1,
save_freq=10*batch_size)
import keras
epochs = 50
save_freq=10*batch_size
model.compile(loss = 'categorical_crossentropy',
optimizer = 'Adam',
metrics = ['accuracy'])
fitted_model = model.fit(
train_X,
train_y,
epochs = epochs,
validation_split=0.30,
callbacks=[cp_callback],
verbose=1)

import tensorflow as tf
import numpy as np
import cv2
import face_recognition
from google.colab.patches import cv2_imshow
model=tf.keras.models.load_model('mask_detection.model')

text_dict={0:'Mask ON',1:'No Mask'}
rect_color_dict={0:(0,255,0),1:(0,0,255)}
image=cv2.imread("/content/drive/MyDrive/148.jpg")
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces =face_recognition.face_locations(image,model='hog')
type = isinstance(faces, tuple)
if type == False:
for face in faces:
face_location = np.array(face)
x = face_location[3]
y = face_location[0]
w = face_location[1] - face_location[3]
h = face_location[2] - face_location[0]
im = image[y:y + w, x:x + w]

grayscale_image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
resized_image = cv2.resize(grayscale_image, (200, 200))
imags = []
imags.append(resized_image)
imags = np.array(imags) / 255.0
imags = np.reshape(imags, (imags.shape[0], 200, 200, 1))
predictions = model.predict(imags)
predictions = np.argmax(predictions)
label = predictions
cv2.rectangle(image, (x, y), (x + w, y + h), rect_color_dict[label], 2)
cv2.rectangle(image, (x, y - 40), (x + w, y), rect_color_dict[label], -1)
cv2.putText(image, text_dict[label], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
else:
print('no faces detected')
cv2_imshow(image)

错误:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-21-17164d330c43> in <module>()
6 from google.colab.patches import cv2_imshow
7 
----> 8 model=tf.keras.models.load_model('mask_detection.model')
9 
10 
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/saved_model/loader_impl.py in parse_saved_model(export_dir)
119         "SavedModel file does not exist at: %s%s{%s|%s}" %
120         (export_dir, os.path.sep, constants.SAVED_MODEL_FILENAME_PBTXT,
--> 121          constants.SAVED_MODEL_FILENAME_PB))
122 
123 
OSError: SavedModel file does not exist at: mask_detection.model/{saved_model.pbtxt|saved_model.pb}

为了造福社会

看起来您正在保存名称为"training_1/cp.ckpt"的模型。并试图加载一个名为"mask_detection.model"的模型;更好的再检查一下。(译自n1colas.m)

请确保在加载模型文件之前添加了模型文件的完整路径和正确的模型文件扩展名。

相关内容

  • 没有找到相关文章

最新更新