Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JessicaDuFirst/5be1bb20af15a68223a2848473fe0dfa to your computer and use it in GitHub Desktop.
Save JessicaDuFirst/5be1bb20af15a68223a2848473fe0dfa to your computer and use it in GitHub Desktop.

Revisions

  1. @andrewjong andrewjong revised this gist Jul 4, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ class ImageFolderWithPaths(datasets.ImageFolder):
    torchvision.datasets.ImageFolder
    """

    # override the __getitem__ method. this is the method dataloader calls
    # override the __getitem__ method. this is the method that dataloader calls
    def __getitem__(self, index):
    # this is what ImageFolder normally returns
    original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
  2. @andrewjong andrewjong revised this gist Nov 5, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,8 @@ def __getitem__(self, index):
    # make a new tuple that includes original and the path
    tuple_with_path = (original_tuple + (path,))
    return tuple_with_path


    # EXAMPLE USAGE:
    # instantiate the dataset and dataloader
    data_dir = "your/data_dir/here"
    dataset = ImageFolderWithPaths(data_dir) # our custom dataset
  3. @andrewjong andrewjong revised this gist Jun 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    from torchvision import datasets

    class ImageFolderWithPaths(datasets.ImageFolder):
    """Custom dataset that includes image paths. Extends
    """Custom dataset that includes image file paths. Extends
    torchvision.datasets.ImageFolder
    """

  4. @andrewjong andrewjong revised this gist Jun 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ class ImageFolderWithPaths(datasets.ImageFolder):
    torchvision.datasets.ImageFolder
    """

    # override the __getitem__ method that dataloader calls
    # override the __getitem__ method. this is the method dataloader calls
    def __getitem__(self, index):
    # this is what ImageFolder normally returns
    original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
  5. @andrewjong andrewjong revised this gist Jun 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,4 @@ def __getitem__(self, index):
    # iterate over data
    for inputs, labels, paths in dataloader:
    # use the above variables freely
    pass
    print(inputs, labels, paths)
  6. @andrewjong andrewjong revised this gist Jun 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ def __getitem__(self, index):
    dataset = ImageFolderWithPaths(data_dir) # our custom dataset
    dataloader = torch.utils.DataLoader(dataset)

    # iterate over data like this
    # iterate over data
    for inputs, labels, paths in dataloader:
    # use the above variables freely
    pass
  7. @andrewjong andrewjong revised this gist Jun 27, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,10 @@ def __getitem__(self, index):

    # instantiate the dataset and dataloader
    data_dir = "your/data_dir/here"
    dataset = ImageFolderWithPaths(data_dir)
    dataset = ImageFolderWithPaths(data_dir) # our custom dataset
    dataloader = torch.utils.DataLoader(dataset)

    # iterate over data like this
    for inputs, labels, paths in dataloader:
    # use the above variables freely
    pass
  8. @andrewjong andrewjong created this gist Jun 27, 2018.
    26 changes: 26 additions & 0 deletions pytorch_image_folder_with_file_paths.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import torch
    from torchvision import datasets

    class ImageFolderWithPaths(datasets.ImageFolder):
    """Custom dataset that includes image paths. Extends
    torchvision.datasets.ImageFolder
    """

    # override the __getitem__ method that dataloader calls
    def __getitem__(self, index):
    # this is what ImageFolder normally returns
    original_tuple = super(ImageFolderWithPaths, self).__getitem__(index)
    # the image file path
    path = self.imgs[index][0]
    # make a new tuple that includes original and the path
    tuple_with_path = (original_tuple + (path,))
    return tuple_with_path

    # instantiate the dataset and dataloader
    data_dir = "your/data_dir/here"
    dataset = ImageFolderWithPaths(data_dir)
    dataloader = torch.utils.DataLoader(dataset)

    for inputs, labels, paths in dataloader:
    # use the above variables freely
    pass