keras 当我使用亚当斯优化器进行预测和建模时,代码中出现值错误

4ioopgfo  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(115)

以下是我们试图运行的代码,但其给出的值错误,我们正在努力给输入 Helm 和无 Helm 和预测车手

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models

# Function to parse YOLO annotation file and extract bounding box coordinates
def parse_annotation_file(annotation_file_path):
    with open(annotation_file_path, 'r') as file:
        lines = file.readlines()

    # Extract bounding box coordinates and convert to numpy array
    # Format: [class_id, x_min, y_min, x_max, y_max]
    bounding_boxes = [list(map(float, line.strip().split())) for line in lines]

    return np.array(bounding_boxes)

# Replace 'your_data_folder' with the path to the folder containing YOLO annotation files
data_folder = '/content/drive/MyDrive/M_R_H_NH/labels'

# Lists to store input and output bounding boxes
input_boxes = []
output_boxes = []

# Loop through all YOLO annotation files in the data folder
for file_name in os.listdir(data_folder):
    if file_name.lower().endswith('.txt'):
        annotation_file_path = os.path.join(data_folder, file_name)
        helmet_bbox = parse_annotation_file(annotation_file_path)

        # Assuming the helmet/no-helmet class is 0 (modify this if different)
        if len(helmet_bbox) > 0 and helmet_bbox[0, 0] == 0:
            # Extract the corresponding rider bounding box from the helmet/no-helmet bounding box
            rider_bbox = helmet_bbox.copy()
            rider_bbox[0, 0] = 1  # Assuming the rider class is 1 (modify this if different)

            # Append the bounding boxes to the input and output lists
            input_boxes.append(helmet_bbox[0, 1:])  # Extract coordinates excluding the class ID
            output_boxes.append(rider_bbox[0, 1:])  # Extract coordinates excluding the class ID

# Convert the lists of bounding boxes to numpy arrays
input_boxes = np.array(input_boxes)
output_boxes = np.array(output_boxes)

# Create the neural network model
model = models.Sequential([
    layers.Dense(16, input_shape=(4,), activation='relu'),
    layers.Dense(64, activation='relu'),
    layers.Dense(4)  # Output layer with 4 nodes for the bounding box coordinates
])

# Compile the model with the Adam optimizer
learning_rate = 0.001
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate),
              loss='mean_squared_error')

# Train the model
epochs = 50
batch_size = 32
model.fit(input_boxes, output_boxes, epochs=epochs, batch_size=batch_size)

# Save the trained model for future use
model.save('yolo_model.h5')
# `your text`

字符串
enter image description here
当我们想要对代码建模时,我们有yolo注解文件,我们想要给予 Helm ,无 Helm 和预测骑手的输入并进行建模时,会出现错误

67up9zun

67up9zun1#

首先,检查数据集目录路径,并检查您的input_boxes和output_boxes形状。
或者只是从文件中复制路径并将其粘贴到代码中。

相关问题