Getting Error: NotADirectoryError: [WinError 267] The directory name is invalid. File and folder both are valid

am getting the following error
Getting Error: NotADirectoryError: [WinError 267] The directory name is invalid. File and folder both are valid
I am using the following code:

data_dir = os.getcwd()
folder_name = “train”
image_folders = os.path.join(data_dir, folder_name)
transform = transforms.Compose([transforms.Resize((512,512)), transforms.ToTensor()])
images = []
for file in os.listdir(image_folders):
#print(“1–>”+file)
images.append(ImageFolder(os.path.join(image_folders, file), transform=transform))
datasets = torch.utils.data.ConcatDataset(images)

Please help here.

Hy @manoj_kumar, In some cases os.getcwd() behave very strange. Please run a quick check and print the output of os.getcwd(). Check if its printing the right directory.

Hi @Usama_Hasan, I checked that also and found it is giving the correct path to the file name. I am not sure where I making mistake.

Refer the screenshot

So Basically the ImageFolder is raising this error, because you’re providing it with a file path and It requires a Directory/Folder to work on. The path printed in the terminal block is pointing towards a .jpg file

Hi @Usama_Hasan
I have tried with only folder name which has all images but still, I am getting the error.

RuntimeError: Found 0 files in subfolders of: D:\MS_Program\DR\Code\train
Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp
while train folder has all the images which I want read by the program.

Sample: code:

transform = transforms.Compose([transforms.Resize((512,512)), transforms.ToTensor()])
images = []

images.append(ImageFolder(image_folders), transform=transform)

datasets = torch.utils.data.ConcatDataset(images)

@manoj_kumar , Image folder looks for image/examples in a predefined way/hierarchy. Please look at the documentation. It takes a root folder, which contains sub folders, with class name and looks for images inside these subfolders
https://pytorch.org/docs/stable/torchvision/datasets.html#imagefolder.
The pattren is like this.

root/class_name/example.png