class ImageDataset(Dataset): def init(self, imgdir, transform=None): """ Args: imgdir…
- User
class ImageDataset(Dataset): def init(self, img_dir, transform=None): """ Args: img_dir (string): Directory with all the images. transform (callable, optional): Optional transform to be applied on an image. """ self.img_dir = img_dir self.image_names = os.listdir(img_dir) self.transform = transform
codedef __len__(self): return len(self.image_names) def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() # Get image path img_name = self.image_names[idx] img_path = os.path.join(self.img_dir, img_name) # Load image image = Image.open(img_path).convert('RGB') # Convert to RGB # Apply transformations if any if self.transform: image = self.transform(image) return imagemake it only load files, not folder
- Assistant
To modify the
ImageDatasetclass so that it only loads files from a specific directory and ignores any subdirectories, you can filter the list of files to include only valid image files. Additionally, you should also ensure that the dataset initializes by verifying that the given directory exists and is indeed a directory.