I’m working on a project where I need to process pairs of images, resize them, and then save the processed pairs into a dataset. I have two lists of image files, fixed_image_files
and moving_image_files
, where each image in fixed_image_files
is supposed to pair with the corresponding image in moving_image_files
. The issue I’m encountering is that when saving the dataset, all the pairs end up being the resized version of the first pair instead of the correct pairs.
The Problem:
Instead of saving the correct pairs like (fixed_image_files[0], moving_image_files[0]), (fixed_image_files[1], moving_image_files[1]), etc., the dataset ends up with all pairs being the resized version of (fixed_image_files[0], moving_image_files[0]).
Expected Output:
The dataset should contain pairs where each entry in fixed_image_files matches with the corresponding entry in moving_image_files.
Actual Output:
The dataset contains repeated pairs of the first entry in both fixed_image_files and moving_image_files.
Question:
What could be causing this issue, and how can I ensure that the correct pairs of images are saved in the dataset?
Here’s a simplified example using the MNIST dataset to demonstrate the issue:
import os
import torch
import numpy as np
from torchvision import datasets, transforms
from PIL import Image
# Define the output directory
output_dir = "./mnist_pairs"
os.makedirs(output_dir, exist_ok=True)
# Simulate the fixed and moving image file lists with MNIST images
fixed_image_files = [Image.fromarray(datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()).data[i].numpy()) for i in range(10)]
moving_image_files = [Image.fromarray(datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()).data[i+10].numpy()) for i in range(10)]
def resize_image(image, target_size):
return image.resize(target_size, Image.LANCZOS)
def process_image_pair(fixed_image, moving_image, target_size):
fixed_resized = resize_image(fixed_image, target_size)
moving_resized = resize_image(moving_image, target_size)
return fixed_resized, moving_resized
# Check if dataset exists and load it
dataset_path = os.path.join(output_dir, "processed_dataset.pt")
if os.path.exists(dataset_path):
print("Loading existing dataset...")
fixed_images, moving_images = torch.load(dataset_path)
else:
fixed_images = []
moving_images = []
# Process images and save pairs
target_size = (28, 28)
for fixed_img, moving_img in zip(fixed_image_files, moving_image_files):
fixed_resized, moving_resized = process_image_pair(fixed_img, moving_img, target_size)
fixed_images.append(np.array(fixed_resized))
moving_images.append(np.array(moving_resized))
# Save the dataset
torch.save((fixed_images, moving_images), dataset_path)
print(f"Dataset saved at {dataset_path}")