Issue with Incorrect Image Pairing While Saving Processed Image Dataset

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}")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật