Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the len function used in PyTorch Dataset?

I am looking to use the code from here . However, I am looking at box 5, where there is the following function;

def __len__(self):
    # Default epoch size is 10 000 samples
    return 10000

I do not see anywhere throughout this script where this function is being used. Clarification on this would be appreciated.

Also, I want to determine the number of image patches used for training this convolutional neural network. Is this len function linked to the number of patches?

like image 710
user121 Avatar asked Jan 01 '26 09:01

user121


1 Answers

This is a function of the Dataset class. The __len__() function specifies the size of the dataset. In your referenced code, in box 10, a dataset is initialized and passed to a DataLoader object:

train_set = ISPRS_dataset(train_ids, cache=CACHE)
train_loader = torch.utils.data.DataLoader(train_set,batch_size=BATCH_SIZE)

You see that in the DataLoader the dataset object is passed as well as the batch size. The DataLoader object then uses the __len__ function of the Dataset to create the batches. This happens in box 13, where it is iterated over the DataLoader.

like image 130
blckbird Avatar answered Jan 02 '26 22:01

blckbird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!