Противодействие ошибке загрузки модели. Как я могу исправить эту ошибку?Python

Программы на Python
Anonymous
Противодействие ошибке загрузки модели. Как я могу исправить эту ошибку?

Сообщение Anonymous »

Я пытаюсь создать программу обнаружения камня, ножниц, бумаги. Когда я пытаюсь загрузить сохраненную модель из локального каталога с помощью функции load_model из tf.keras.models, я сталкиваюсь с этой ошибкой. Как я могу изменить этот код, чтобы использовать предварительно сохраненную модель. пожалуйста, помогите мне, я программист начального уровня
Error loading model: Layer "dense" expects 1 input(s), but it received 2 input tensors. Inputs received: [, ]

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image

# Mapping of class indices to labels

CLASS_MAP = {
0: 'rock',
1: 'paper',
2: 'scissor'
}

# Load the model once

try:
model = tf.keras.models.load_model('D:\\practice_CV22\\new_mobilenet_model.h5')
except Exception as e:
print(f"Error loading model: {str(e)}")
exit()

def detection_fn(image_batch):
predictions = model.predict_on_batch(image_batch).flatten()
class_id = np.argmax(predictions)
label = CLASS_MAP\[class_id\]
confidence = predictions\[class_id\]
return label, confidence

def preprocess_frame(frame, img_size=224):
frame = cv2.resize(frame, (img_size, img_size))
frame = image.img_to_array(frame)
frame = np.expand_dims(frame, axis=0)
frame = frame / 255.0
return frame

# Initialize webcam

cap = cv2.VideoCapture(0)

if not cap.isOpened():
print("Error: Could not open webcam.")
exit()

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

# Preprocess the frame
processed_frame = preprocess_frame(frame)

# Detect objects
label, confidence = detection_fn(processed_frame)

# Draw the label on the frame
cv2.putText(frame, f"{label}: {confidence:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

# Display the frame
cv2.imshow('Webcam Object Detection', frame)

# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture object and close all OpenCV windows

cap.release()
cv2.destroyAllWindows()


Подробнее здесь: https://stackoverflow.com/questions/787 ... this-error

Вернуться в «Python»