keras 如何在将图像馈送到ResNet50时修复此图像形状错误?

taor4pac  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(102)

我想使用连接到Raspberry Pi 4的摄像头来同时传输视频和检测物体。我已经能够使用这个设备拍照和录制视频。
然而,当我尝试在流上运行ResNet50分类器时,我面临着形状不一致的错误。
我请求您帮我找到问题可能源自的线路号。
错误和代码(相对简单)附在下面。
错误代码:
ValueError: Input 0 of layer "resnet50" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 1, 224, 224)
代码:

#!/usr/bin/python3
import time
import os
from datetime import datetime

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput
from PIL import Image

# Define the resolution for the low-resolution video stream
lsize = (320, 240)

# Load a pre-trained TensorFlow model
classifier = tf.keras.applications.resnet50.ResNet50(weights='imagenet')

# Initialize the Picamera2
picam2 = Picamera2()

# Configure the video settings
video_config = picam2.create_video_configuration(
    main={"size": (1920, 1080), "format": "RGB888"},
    lores={"size": lsize, "format": "YUV420"}
)
picam2.configure(video_config)

# Initialize the H.264 encoder for video capture
encoder = H264Encoder(2000000, repeat=True)
encoder.output = CircularOutput()
picam2.encoder = encoder

# Start the camera and the encoder
picam2.start()
picam2.start_encoder(encoder)

# Define the dimensions of the low-resolution frame
w, h = lsize

# Continuous loop to capture and process frames
while True:
    # Capture a frame from the low-resolution stream
    cur = picam2.capture_buffer("lores")

    # Convert the frame to an image and
    # preprocess it for the deep learning model
    image = Image.fromarray(cur.astype('uint8'))
    image = image.resize((224, 224))  # Resize to match the model's input size
    image = np.array(image) / 255.0  # Normalize
    image = np.expand_dims(image, axis=0)  # Add batch dimension

    # Run the deep learning model on the frame
    predictions = classifier.predict(np.expand_dims(image, axis=-1))

    # Print the model's predictions (customize as needed)
    print(
        "Model predictions class:",
        tf.keras.applications.imagenet_utils.decode_predictions(
            preds=predictions
        )
    )
    time.sleep(1)

# Stop the camera and the encoder
picam2.stop()
picam2.stop_encoder()
mec1mxoz

mec1mxoz1#

我把lsize改成了(160, 120)

video_config = picam2.create_video_configuration(
    main={"size": (224, 224), "format": "RGB888"},
    lores={"size": lsize, "format": "YUV420"}
)

得双曲正弦值.

image = Image.fromarray(cur.astype('uint8'))
    image = np.array(image)
    image = tf.keras.applications.resnet50.preprocess_input(image)

工作代码:

#!/usr/bin/python3
import time
import os
from datetime import datetime

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput
from PIL import Image

# Define the resolution for the low-resolution video stream
lsize = (160, 120)

# Load a pre-trained TensorFlow model
classifier = tf.keras.applications.resnet50.ResNet50(weights='imagenet')

# Initialize the Picamera2
picam2 = Picamera2()

# Configure the video settings
video_config = picam2.create_video_configuration(
    main={"size": (224, 224), "format": "RGB888"},
    lores={"size": lsize, "format": "YUV420"}
)
picam2.configure(video_config)

# Initialize the H.264 encoder for video capture
encoder = H264Encoder(2000000, repeat=True)
encoder.output = CircularOutput()
picam2.encoder = encoder

# Start the camera and the encoder
picam2.start()
picam2.start_encoder(encoder)

# Define the dimensions of the low-resolution frame
w, h = lsize

# Continuous loop to capture and process frames
while True:
    # Capture a frame from the low-resolution stream
    cur = picam2.capture_array()

    # Convert the frame to an image and
    # preprocess it for the deep learning model
    image = Image.fromarray(cur.astype('uint8'))
    image = np.array(image)
    image = tf.keras.applications.resnet50.preprocess_input(image)

    # Expand dimensions to create a batch of size 1
    image = image[None, ...]

    # Run the deep learning model on the frame
    predictions = classifier.predict(image)

    # Print the model's predictions (customize as needed)
    print(
        "Model predictions class:",
        tf.keras.applications.imagenet_utils.decode_predictions(
            preds=predictions
        )
    )
    time.sleep(1)

# Stop the camera and the encoder
picam2.stop()
picam2.stop_encoder()

PS:特别感谢https://stackoverflow.com/users/12317368/ro-ot

相关问题