'DataLoader' object is not subscriptable>

ChanYalcn: <p>I followed some tutorails and read repositories,then built a simple GAN model.<br>
I have jpeg files in “mountain_dataset” folder<br>
when I want to train it, it gives me an error,I searched possible ways to fix but I couldn’t.</p>
<pre><code class="lang-auto">def load_dataset(data_path, transform):
    train_dataset = torchvision.datasets.ImageFolder(
        root=data_path,
        transform=transform
    )
    
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=32,
        num_workers=0,
        shuffle=True
    )
    return train_loader
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
])
dataset = load_dataset(data_path="mountain_dataset", transform=transform)
opt_disc = optim.Adam(disc.parameters(), lr=lr)
opt_gen = optim.Adam(gen.parameters(), lr=lr)
criterion = nn.BCELoss()
step = 0

for epoch in range(num_epochs):
    for batch_idx, (real, _) in enumerate(dataset ):
        real = real.view(-1, 784).to(device)
        batch_size = real.shape[0]

        ### Train Discriminator: max log(D(real)) + log(1 - D(G(z)))
        noise = torch.randn(batch_size, z_dim).to(device)
        fake = gen(noise)
        disc_real = disc(real).view(-1)
        lossD_real = criterion(disc_real, torch.ones_like(disc_real))

        disc_fake = disc(fake).view(-1)
        lossD_fake = criterion(disc_fake, torch.zeros_like(disc_fake))
        lossD = (lossD_real + lossD_fake) / 2

        disc.zero_grad()
        lossD.backward(retain_graph=True)
        opt_disc.step()

        ### Train Generator maximize log(D(G(z)))
        output = disc(fake).view(-1)
        lossG = criterion(output, torch.ones_like(output))
        gen.zero_grad()
        lossG.backward()
        opt_gen.step()

        if batch_idx == 0:
            print(
                f"Epoch: [{epoch+1}/{num_epochs}] Loss D: {lossD:.4f}, Loss G: {lossG:.4f}"
            )

            with torch.no_grad():
                fake = gen(fixed_noise).reshape(-1, 1, 28, 28)
                data = real.reshape(-1, 1, 28, 28)
                img_grid_fake = torchvision.utils.make_grid(fake, normalize=True)
                img_grid_real = torchvision.utils.make_grid(data, normalize=True)

                step += 1
</code></pre>
<p>I get this error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[13], line 1
----&gt; 1 dataset = load_dataset(data_path="mountain_dataset", transform=transform)
      2 opt_disc = optim.Adam(disc.parameters(), lr=lr)
      3 opt_gen = optim.Adam(gen.parameters(), lr=lr)

Cell In[5], line 2, in load_dataset(data_path, transform)
      1 def load_dataset(data_path, transform):
----&gt; 2     train_dataset = torchvision.datasets.ImageFolder(
      3         root=data_path,
      4         transform=transform
      5     )
      7     train_loader = torch.utils.data.DataLoader(
      8         train_dataset,
      9         batch_size=32,
     10         num_workers=0,
     11         shuffle=True
     12     )
     13     return train_loader

File ~\pytorch\MLvenv\Lib\site-packages\torchvision\datasets\folder.py:309, in ImageFolder.__init__(self, root, transform, target_transform, loader, is_valid_file)
    301 def __init__(
    302     self,
    303     root: str,
   (...)
    307     is_valid_file: Optional[Callable[[str], bool]] = None,
    308 ):
--&gt; 309     super().__init__(
    310         root,
    311         loader,
    312         IMG_EXTENSIONS if is_valid_file is None else None,
    313         transform=transform,
    314         target_transform=target_transform,
    315         is_valid_file=is_valid_file,
    316     )
    317     self.imgs = self.samples

File ~\pytorch\MLvenv\Lib\site-packages\torchvision\datasets\folder.py:144, in DatasetFolder.__init__(self, root, loader, extensions, transform, target_transform, is_valid_file)
    134 def __init__(
    135     self,
    136     root: str,
   (...)
    141     is_valid_file: Optional[Callable[[str], bool]] = None,
    142 ) -&gt; None:
    143     super().__init__(root, transform=transform, target_transform=target_transform)
--&gt; 144     classes, class_to_idx = self.find_classes(self.root)
    145     samples = self.make_dataset(self.root, class_to_idx, extensions, is_valid_file)
    147     self.loader = loader

File ~\pytorch\MLvenv\Lib\site-packages\torchvision\datasets\folder.py:218, in DatasetFolder.find_classes(self, directory)
    191 def find_classes(self, directory: str) -&gt; Tuple[List[str], Dict[str, int]]:
    192     """Find the class folders in a dataset structured as follows::
    193 
    194         directory/
   (...)
    216         (Tuple[List[str], Dict[str, int]]): List of all classes and dictionary mapping each class to an index.
    217     """
--&gt; 218     return find_classes(directory)

File ~\OneDrive\Masaüstü\pytorch\MLvenv\Lib\site-packages\torchvision\datasets\folder.py:42, in find_classes(directory)
     40 classes = sorted(entry.name for entry in os.scandir(directory) if entry.is_dir())
     41 if not classes:
---&gt; 42     raise FileNotFoundError(f"Couldn't find any class folder in {directory}.")
     44 class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
     45 return classes, class_to_idx

FileNotFoundError: Couldn't find any class folder in mountain_dataset.```</code></pre>

marksaroufim: <p>I believe the bug is here</p>
<pre><code class="lang-auto"># load_dataset() returns a data loader
dataset = load_dataset(data_path="mountain_dataset", transform=transform)

# Given a data loader creating another data loader seems wrong
loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
</code></pre>