r/learnpython • u/Momo_ONLINE • Oct 12 '26
enumarting issue for images and masks
Hi!
Preface I am self taught and learning as I go.
I have been working on a U-Net semantic segmentation model, which was working fine yesterday and gave good(ish) outputs on unprocessed data and now no longer wants to read the files correctly?
I spent the day processing some new masks in fiji and changed the path of the mask variable and when I went to run my code and sanity checks, for both my images and masks, the first chunk of tif files I created a missing/ not read, when they are in file. (I even checked with fiji if it was an order of files but that was fine)
I'm a bit stumped because it was working fine yesterday, as all I have changed is the original file paths.
from simple_unet_model import simple_unet_model
from keras.utils import normalize
import os
import cv2
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
image_directory = r'E:\Uni\Dissertation\IB\test output\images\IB unprocessed/'
mask_directory = r'E:\Uni\Dissertation\IB\test output\masks\IB_BC_24_117_G4_thresh mask/'
SIZE = 256
image_dataset = []
mask_dataset = []
images = os.listdir(image_directory)
for i, image_name in enumerate(images): #Remember enumerate method adds a counter and returns the enumerate object
if (image_name.split('.')[1] == 'tif'):
#print(image_directory+image_name)
image = cv2.imread(image_directory+image_name, 0)
image = Image.fromarray(image)
image = image.resize((SIZE, SIZE))
image_dataset.append(np.array(image))
masks = os.listdir(mask_directory)
for i, image_name in enumerate(masks):
if (image_name.split('.')[1] == 'tif'):
image = cv2.imread(mask_directory+image_name, 0)
image = Image.fromarray(image)
image = image.resize((SIZE, SIZE))
mask_dataset.append(np.array(image))
And when I run a sanity check, I end up getting a mismatch between my image and ground truth/mask
In the table of elements, the correct number of elements is listed, however, 0 should be linked to mask_0_00.tiff (the same for the image data), but jumps to mask_0_00.tiff
I even changed the path back to the unprocessed masks for the data set and encountered the same problem...
Any ideas? Thank you :,)
1
u/Momo_ONLINE Oct 12 '26
Hi!, I think I've found a solution in the form of.. using onedrive instead of my external usb? thats the only thing I've found to make the list read correctly. I don't want to rely on onedrive, so if anyone has any ideas I'd be greatful!
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 12 '26
I'm not sure if I really understand your explanation of what the actual problem is, but I'd try making sure the files are sorted.
1
u/AmdilShoat82 Oct 13 '26
the `.split('.')[1] == 'tif'` check will fail on files ending in `.tiff` (two f's). looks like some of your masks might be .tiff and some .tif, so theyre getting silently skipped. print out what `os.listdir` actually returns and compare
1
u/zanfar Oct 13 '26
Revert to yesterday's commit...
0
u/[deleted] Oct 13 '26
[deleted] — view deleted comment