[PYTHON] I want to see the file name from DataLoader

Introduction

Pytorch's DataLoader It's nice to be very easy to use. I personally thought that it would be easier to analyze if I could see the file name that failed to be classified when I had a classification problem, so I will describe how to retrieve the file name as a memorandum.

Extract Path

It's not difficult at all, but it's taken directly from the Dataloader. Create dataloaders by referring to TRANSFER LEARNING FOR COMPUTER VISION STRUCT [1] of PyTorch.

IMAGE_SIZE=224
BATCH_SIZE=20
TRAIN = 'train'
VAL = 'val'
DATA_DIR = 'H:\\dataset/predata/' # select your dataset directory
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

data_transforms = {
    TRAIN: transforms.Compose([
        transforms.Resize(IMAGE_SIZE),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    VAL: transforms.Compose([
        transforms.Resize(IMAGE_SIZE),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

image_datasets = {x: datasets.ImageFolder(os.path.join(DATA_DIR, x), data_transforms[x]) for x in [TRAIN, VAL]}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], 
                                              batch_size=BATCH_SIZE, shuffle=True, num_workers=4) for x in [TRAIN, VAL]}
dataset_sizes = {x: len(image_datasets[x]) for x in [TRAIN, VAL]}
class_names = image_datasets[TRAIN].classes

Extract the data path from the created dataloaders.

from enum import Enum
class Dataset(Enum):
    FILE_PATH = 0
    LABEL = 1

# full dataset
for j in range(dataset_sizes[VAL]):
    # abs path
    print(dataloaders[VAL].dataset.imgs[j][Dataset.FILE_PATH.value])
    # file name only
    print(os.path.basename(dataloaders[VAL].dataset.imgs[j][Dataset.FILE_PATH.value])) 

# there is one dataset
print(dataloaders[VAL].dataset.imgs[0][Dataset.FILE_PATH.value])

Finally

After all, it is painful because you have to look directly at the classified data if there is no file name. So why not search by file name match?

Recommended Posts

I want to see the file name from DataLoader
I want to get the file name, line number, and function name in Python 3.4
I want to calculate the allowable downtime from the operating rate
I want to pin Spyder to the taskbar
I want to get the name of the function / method being executed
I want to output to the console coolly
I want to automatically find high-quality parts from the videos I shot
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to use jar from python
I want to display the progress bar
I want to make the second line the column name in pandas
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to send a signal only from the sub thread to the main thread
The file name was bad in Python and I was addicted to import
I want to connect to PostgreSQL from various languages
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to handle the rhyme part7 (BOW)
I want to make fits from my head
I want to use ceres solver from python
Recursively search the specified directory to see the file
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to log file I / O on Linux
I want to make C ++ code from Python code!
I want to customize the appearance of zabbix
I want to write to a file with Python
I want to use the activation function Mish
I want to display the progress in Python!
The file edited with vim was readonly but I want to save it
I want to get the path of the directory where the running file is stored.
[LINE Messaging API] I want to send a message from the program to everyone's LINE
[Ansible] I want to call my own function from the template module (macro)
Handle CSV that contains the element you want to parse in the file name
I want to make a music player and file music at the same time
I want to add silence to the beginning of a wav file for 1 second
I want to see a list of WebDAV files in the Requests module
I made a tool to generate Markdown from the exported Scrapbox JSON file
[Python] I tried to get the type name as a string from the type function
I want to detect images of cats from Instagram
I tried to detect the iris from the camera image
I want to grep the execution result of strace
I want to scroll the Django shift table, but ...
I tried to touch the CSV file with Python
Even in JavaScript, I want to see Python `range ()`!
I wanted to use the Python library from MATLAB
I want to randomly sample a file in Python
I want to inherit to the back with python dataclass
I want to fully understand the basics of Bokeh
[Python3] I want to generate harassment names from Japanese!
I want to write in Python! (3) Utilize the mock
I want to handle the rhyme part6 (organize once)
I want to automate ssh using the expect command!
I want to publish the product at the lowest cost
I read the Chainer reference (updated from time to time)
I want to use the R dataset in python
I want to handle the rhyme part8 (finished once)
I want to increase the security of ssh connections