【问题标题】:Iterate through images in 2 folders遍历 2 个文件夹中的图像
【发布时间】:2021-08-05 13:32:11
【问题描述】:

我有 2 个文件夹:1 个文件夹包含训练图像,其他文件夹具有相应的蒙版。

如何确保图像及其对应的标签被用于训练我的算法。

这是加载火车图像的代码:

train_images = []

for directory_path in glob.glob(r"D:\Train"):
    for img_path in glob.glob(os.path.join(directory_path, "*.jpg")):
        img = cv2.imread(img_path, 0)       
        img = cv2.resize(img, (SIZE_Y, SIZE_X))
        train_images.append(img)

这是加载蒙版图像的代码:

train_masks = [] 
for directory_path in glob.glob(r"D:\Mask"):
    for mask_path in glob.glob(os.path.join(directory_path, "*.tiff")):
        mask = cv2.imread(mask_path, 0)       
        mask = cv2.resize(mask, (SIZE_Y, SIZE_X), interpolation = cv2.INTER_NEAREST)  
        train_masks.append(mask)

将它们拆分为 X_train、X_test、y_train、y_test

如果我进行完整性检查以查看图像是否与它的掩码匹配,则该对是随机的。 代码:

import random
import numpy as np
image_number = random.randint(0, len(X_train))
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.imshow(np.reshape(X_train[image_number], (256, 256)), cmap='gray')
plt.subplot(122)
plt.imshow(np.reshape(y_train[image_number], (256, 256)), cmap='gray')
plt.show()'''

我该如何解决这个问题?

【问题讨论】:

  • 首先:您应该为两者使用相似的名称。第二:您应该对名称进行排序以确保它们的顺序相同。系统不必按字母顺序给出文件名。

标签: python numpy tensorflow deep-learning image-segmentation


【解决方案1】:

系统不必按字母顺序提供文件名。
您应该对它们进行排序以确保您的顺序正确

 for img_path in sorted(glob.glob(os.path.join(directory_path, "*.jpg"))):

 for mask_path in sorted(glob.glob(os.path.join(directory_path, "*.tiff"))):

当然,jpgtiff 需要相似的文件名才能获得相同的顺序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2017-04-03
    • 2023-03-03
    • 2017-02-08
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多