首页 > 编程问答 >如何用代码预测由 4 个图片文件夹组成的数组。我需要

如何用代码预测由 4 个图片文件夹组成的数组。我需要

时间:2024-06-04 15:44:29浏览次数:10  
标签:prediction

我无法正确显示包含 4 个图像文件夹的数组的预测结果

from PIL import Image

导入 cv2

导入时间

defload_and_preprocess_image(img_path,target_size=(112,112)):

try:

    img = Image.open(img_path)

    img = img.resize(target_size)

    img_array = np.array(img)

    img_array = img_array / 255.0 # 归一化为 [0, 1] 范围

    返回 img_array

except Exception as e:

    print(f"Error loading image {img_path}: {e}")

    返回 None

def load_images_from_folders(folders, target_size):

images = []

folder_images = []

for folder in folders:

    if not os.listdir(folder):

      raise ValueError(f"Folder {folder} is empty.")

    for img_name in os.listdir(folder):

        img_path = os.path.join(folder, img_name)

        if img_name.lower().endswith(( '.jpg', )):

            img_array = load_and_preprocess_image(img_path, target_size)

            如果 img_array 不是 None:

                folder_images.append(img_array)

    images.append(np.concatenate(folder_images, axis=0))

返回图像

def main(Model_Enhancer, folders, target_size=(112, 112)):

images = load_images_from_folders(folders, target_size)

num_images = min(len(images[0]), len(images[1]), len(images[2]), len(images[3]))

Input_Sample1 = tf.keras.Input(shape=(112, 112, 3), name='Input_Sample1')

Input_Sample2 = tf.keras.Input(shape=(112, 112, 3), name='Input_Sample2')

Input_Sample3 = tf.keras.Input(shape=(112, 112, 3), name='Input_Sample3')

Input_Sample4 = tf.keras.Input(shape=(112, 112, 3), name='Input_Sample4')

Input_Sample1 = np.concatenate(images[0][:num_images], axis=0)

Input_Sample1 = Input_Sample1.reshape((-1, 112, 112, 3))

Input_Sample2 = np.concatenate(images[1][:num_images], axis=0)

Input_Sample2 = Input_Sample2.reshape((-1, 112, 112, 3))

Input_Sample3 = np.concatenate(images[2][:num_images], axis=0)

Input_Sample3 = Input_Sample3.reshape((-1, 112, 112, 3))

Input_Sample4 = np.concatenate(images[2][:num_images], axis=0)

Input_Sample4 = Input_Sample4.reshape((-1, 112, 112, 3))







预测 = Model_Enhancer.predict([Input_Sample1,Input_Sample2,Input_Sample3,Input_Sample4])



for i, prediction in enumerate(predictions):

    output_file = os.path.join('/content/drive/MyDrive/Dataset', f'output_{[i]}.jpg')

    normalized_prediction = (prediction - prediction.min()) / (prediction.max() - prediction.min())

    plt.imshow(normalized_prediction)

    plt.title(f"Prediction:{np.argmax(prediction)}")

    plt.axis('off')

    plt.show()

    prediction_img = Image.fromarray((normalized_prediction * 255).astype(np.uint8))

    prediction_img.save(output_file)

    print(f'Output image saved as {output_file}')

if

if name == " main ":

input_shape = (112, 112, 3)

输出 =merge(Input_Sample1,Input_Sample2,Input_Sample3,Input_Sample4)

Model_Enhancer = tf.keras.Model(inputs=[Input_Sample1,Input_Sample2,Input_Sample3,Input_Sample4], outputs=Output)

文件夹 = [

   "/content/drive/MyDrive/Dataset/input_wb_train"、

   "/content/drive/MyDrive/Dataset/input_gc_train"、

   "/content/drive/MyDrive/Dataset/input_ce_train"、

   "/content/drive/MyDrive/Dataset/input_train";

]  # 将其更改为图像文件夹路径

main(Model_Enhancer, folders,target_size=(112, 112))

用这段代码帮助我,或者给我正确的方法来获得正确的图像预测

我期待的是图像的正确预测,尽管如此,显示的图像仍然不是我想要的图像


The issue lies in how you're handling the input data and predictions. You're feeding concatenated images into the model, which isn't how multi-input models are designed to work. Each input should receive a separate image. Additionally, you're trying to directly display the prediction , which is likely the output of your model (possibly a classification vector or feature map), not a processed image.

Here's a revised approach to address these issues:

import os
import tensorflow as tf
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def load_and_preprocess_image(img_path, target_size=(112, 112)):
    """Loads and preprocesses an image."""
    try:
        img = Image.open(img_path).convert('RGB')  # Ensure RGB format
        img = img.resize(target_size)
        img_array = np.array(img) / 255.0  # Normalize
        return img_array
    except Exception as e:
        print(f"Error loading image {img_path}: {e}")
        return None

def load_images_from_folders(folders, target_size):
    """Loads images from multiple folders."""
    all_images = []
    for folder in folders:
        if not os.listdir(folder):
            raise ValueError(f"Folder {folder} is empty.")
        folder_images = []
        for img_name in os.listdir(folder):
            if img_name.lower().endswith(('.jpg', '.jpeg', '.png')):  # Support more formats
                img_path = os.path.join(folder, img_name)
                img_array = load_and_preprocess_image(img_path, target_size)
                if img_array is not None:
                    folder_images.append(img_array)
        all_images.append(np.array(folder_images))  # Convert to NumPy array
    return all_images

def create_model(input_shape=(112, 112, 3)):
    """Creates a multi-input Keras model (example)."""
    # Define input layers, one for each image type
    input1 = tf.keras.Input(shape=input_shape, name='Input_Sample1')
    input2 = tf.keras.Input(shape=input_shape, name='Input_Sample2')
    input3 = tf.keras.Input(shape=input_shape, name='Input_Sample3')
    input4 = tf.keras.Input(shape=input_shape, name='Input_Sample4')

    # Example processing (replace with your actual architecture)
    x1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(input1)
    x2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(input2)
    x3 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(input3)
    x4 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(input4)
    merged = tf.keras.layers.Concatenate()([x1, x2, x3, x4])
    # ... Add more layers to process the merged output

    # Example output layer (adjust based on your task)
    output = tf.keras.layers.Conv2D(3, (3, 3), activation='sigmoid')(merged)  # Output an image

    model = tf.keras.Model(inputs=[input1, input2, input3, input4], outputs=output)
    return model

def main(model, folders, target_size=(112, 112)):
    """Loads images, makes predictions, and displays results."""
    images = load_images_from_folders(folders, target_size)

    # Determine the minimum number of images across all folders
    num_images = min(len(folder_images) for folder_images in images)

    # Prepare input data (one image per input)
    input_data = [folder_images[:num_images] for folder_images in images]

    # Make predictions
    predictions = model.predict(input_data)

    # Process and display predictions (assuming 'predictions' contains images)
    for i, prediction in enumerate(predictions):
        output_file = os.path.join('/content/drive/MyDrive/Dataset', f'output_{i}.jpg')

        # Denormalize if needed
        if prediction.max() <= 1.0:  # Assuming normalization to [0, 1]
            prediction = (prediction * 255).astype(np.uint8)

        plt.imshow(prediction)
        plt.title(f"Prediction: {i}")  # Adjust title as needed
        plt.axis('off')
        plt.show()

        prediction_img = Image.fromarray(prediction)
        prediction_img.save(output_file)
        print(f'Output image saved as {output_file}')

if __name__ == "__main__":
    input_shape = (112, 112, 3)
    model = create_model(input_shape)
    folders = [
        "/content/drive/MyDrive/Dataset/input_wb_train",
        "/content/drive/MyDrive/Dataset/input_gc_train",
        "/content/drive/MyDrive/Dataset/input_ce_train",
        "/content/drive/MyDrive/Dataset/input_train",
    ]
    main(model, folders, target_size=(112, 112))

Explanation of Changes:

  1. Image Loading and Preprocessing:
  2. Added support for common image formats ( .jpeg , .png ).
  3. Ensured images are loaded in RGB format using Image.open(img_path).convert('RGB') .
  4. Converted image lists to NumPy arrays in load_images_from_folders .

  5. Model Input and Architecture:

  6. Created a create_model function to define your multi-input model.
  7. The example provided uses Conv2D layers for demonstration; replace with your actual architecture.
  8. Ensured that each input of the model receives a separate image.

  9. Prediction and Display:

  10. Used model.predict(input_data) to get predictions for the batch of input images.
  11. Added denormalization of predictions (multiplying by 255) if your model outputs values in the range [0, 1].
  12. Adjusted the display logic to handle potential image outputs from your model.

Important Considerations:

  • Model Architecture: Replace the example create_model function with your actual model architecture. Make sure it's designed to handle four separate input images and produce the desired output (image or otherwise).
  • Output Processing: The code assumes your model outputs images. If it outputs something else (e.g., classification probabilities), you'll need to adjust the prediction processing and display logic accordingly.

Remember to adapt this code to your specific model and data. If you encounter further issues, please provide: - Details about your model architecture (layers, output shape). - The expected output of your model (image, classification, etc.). - Any error messages you're getting.

标签:prediction
From: 78573982

相关文章