- Deep Learning with PyTorch
- Vishnu Subramanian
- 245字
- 2021-06-24 19:16:26
Dataset class
Any custom dataset class, say for example, our Dogs dataset class, has to inherit from the PyTorch dataset class. The custom class has to implement two main functions, namely __len__(self) and __getitem__(self, idx). Any custom class acting as a Dataset class should look like the following code snippet:
from torch.utils.data import Dataset
class DogsAndCatsDataset(Dataset):
def __init__(self,):
pass
def __len__(self):
pass
def __getitem__(self,idx):
pass
We do any initialization, if required, inside the init method—for example, reading the index of the table and reading the filenames of the images, in our case. The __len__(self) operation is responsible for returning the maximum number of elements in our dataset. The __getitem__(self, idx) operation returns an element based on the idx every time it is called. The following code implements our DogsAndCatsDataset class:
class DogsAndCatsDataset(Dataset):
def __init__(self,root_dir,size=(224,224)):
self.files = glob(root_dir)
self.size = size
def __len__(self):
return len(self.files)
def __getitem__(self,idx):
img = np.asarray(Image.open(self.files[idx]).resize(self.size))
label = self.files[idx].split('/')[-2]
return img,label
Once the DogsAndCatsDataset class is created, we can create an object and iterate over it, which is shown in the following code:
for image,label in dogsdset:
#Apply your DL on the dataset.
Applying a deep learning algorithm on a single instance of data is not optimal. We need a batch of data, as modern GPUs are optimized for better performance when executed on a batch of data. The DataLoader class helps to create batches by abstracting a lot of complexity.
- 筆記本電腦使用、維護與故障排除實戰
- 深入理解Spring Cloud與實戰
- 電腦維護與故障排除傻瓜書(Windows 10適用)
- 電腦組裝與維修從入門到精通(第2版)
- INSTANT Wijmo Widgets How-to
- 電腦組裝、維護、維修全能一本通(全彩版)
- 數字邏輯(第3版)
- 深入淺出SSD:固態存儲核心技術、原理與實戰(第2版)
- STM32嵌入式技術應用開發全案例實踐
- Rapid BeagleBoard Prototyping with MATLAB and Simulink
- 超大流量分布式系統架構解決方案:人人都是架構師2.0
- Neural Network Programming with Java(Second Edition)
- 數字媒體專業英語(第2版)
- 單片機原理與技能訓練
- Arduino項目案例:游戲開發