Mobilenet v3 - Problem with decode_prediction with savedModel

41 views Asked by At

i have problem with pre-trained model Mobilenet_v3. I'm on the beginning of my learning path so sorry for mistake. I have apple image and I want preprocess this image and check it with pre-trained model. Could You help me with this? (Ofc later I will learn about adding rectangle and label object on picture) But for now I have problem with prediction of load image what is this :)

Thank You for any tips if I use correct pre-trained model or another is better, or maybe I'm going in wrong way, every information will be helpful :) Thank You!

Below I paste my code (for now)

# Import dependencies
import cv2
from keras.applications.mobilenet_v3 import preprocess_input, decode_predictions
from keras.applications import MobileNetV3Large
from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf


class ObjectRecognition:
    def __init__(self):
        self.cap = cv2.VideoCapture(0)  # Access the default webcam (usually index 0)
        self.model = tf.saved_model.load('saved_models/mobilenet_v3') # Load MobileNetV3 model with ImageNet weights
    # Function to preprocess the image
    def preprocess_cam(self, frame):
        # Configuration, resize the image to match the input expected by MobileNetV3
        frame = self.cv2_image_config(frame)
        # Preprocess the image using MobileNetV3's preprocess_input function
        processed_cam = preprocess_input(frame)
    return processed_cam

def preprocess_image(self, image_path):
    # Load the image using OpenCV
    image = cv2.imread(image_path)
    # Configuration, resize the image to match the input expected by MobileNetV3
    image = self.cv2_image_config(image)

    # Preprocess the image using MobileNetV3's preprocess_input function
    processed_image = preprocess_input(image)

    return processed_image

def cv2_image_config(self, image):
    # Convert BGR to RGB
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # Resize the image to match the input size expected by MobileNetV3 (usually 224x224)
    input_size = (224, 224)
    image = cv2.resize(image, input_size)
    return image

def capture_image(self, image_path):
    #while True:
        # Preprocess image file
    resized_image = self.preprocess_image(image_path)
    print('Print processed image: ', resized_image)
    expanded_image = np.expand_dims(resized_image, axis=0)  # Add batch dimension
    print('Print expanded image: ', expanded_image)
    processed_image = preprocess_input(expanded_image)  # Preprocess the frame
    print('Print processed image: ', processed_image)
    # Perform object detection using MobileNetV3
    #object_features = self.model(processed_image)
    #print(object_features)
    object_features = self.model.__call__(
        tf.convert_to_tensor(processed_image),
        True,
        False,# Adjust these arguments based on the model's options
        tf.constant(0.9))
    print(object_features)
    decoded_predictions = decode_predictions(object_features.numpy())
    print(decoded_predictions)
def capture_video(self):
    while True:
        ret, frame = self.cap.read()  # Capture frame-by-frame
        if not ret:
            break
        # Preprocess captured from webcam
        processed_frame = self.preprocess_cam(frame)

        # Show preprocessed capture
        cv2.imshow('Object detection', processed_frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):  # Press 'q' to exit
            break

    self.cap.release()
    cv2.destroyAllWindows()


# Replace with the correct path to your MobileNetV3 model
face_recognition = ObjectRecognition()
flag = True
if flag:
    face_recognition.capture_image(image_path='test_1.jpg')
else:
    face_recognition.capture_video()

After running code I receive error like this:

ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
  Positional arguments (4 total):
    * <tf.Tensor 'inputs:0' shape=(1, 224, 224, 3) dtype=uint8>
    * True
    * False
    * <tf.Tensor 'batch_norm_momentum:0' shape=() dtype=float32>
  Keyword arguments: {}

 Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
    * False
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 2:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
    * False
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 3:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
    * True
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 4:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')
    * True
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}
0

There are 0 answers