Solved by bohare in post #2 
              
              
                I found the solution thanks to the user David S. Apparently, glob.glob returns the list in arbitrary order so I put sorted in front of both images and masks, like so: 
self.img_files = sorted(glob.glob(os.path.join(folder_path,'images','*.png')))
self.mask_files = sorted(glob.glob(os.path.join(folde…