|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +""" |
| 8 | +Trainer Datasets Example |
| 9 | +======================== |
| 10 | +
|
| 11 | +This is the datasets used for the training example. It's using stock Pytorch |
| 12 | +Lightning + Classy Vision libraries. |
| 13 | +""" |
| 14 | + |
| 15 | +import os.path |
| 16 | +import tarfile |
| 17 | +from typing import Optional, Callable |
| 18 | + |
| 19 | +import fsspec |
| 20 | +import pytorch_lightning as pl |
| 21 | +from classy_vision.dataset.classy_dataset import ClassyDataset |
| 22 | +from torch.utils.data import DataLoader |
| 23 | +from torchvision import datasets, transforms |
| 24 | + |
| 25 | +# %% |
| 26 | +# This uses classy vision to define a dataset that we will then later use in our |
| 27 | +# Pytorch Lightning data module. |
| 28 | + |
| 29 | + |
| 30 | +class TinyImageNetDataset(ClassyDataset): |
| 31 | + """ |
| 32 | + TinyImageNetDataset is a ClassyDataset for the tiny imagenet dataset. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, data_path: str, transform: Callable[[object], object]) -> None: |
| 36 | + batchsize_per_replica = 16 |
| 37 | + shuffle = False |
| 38 | + num_samples = 1000 |
| 39 | + dataset = datasets.ImageFolder(data_path) |
| 40 | + super().__init__( |
| 41 | + # pyre-fixme[6] |
| 42 | + dataset, |
| 43 | + batchsize_per_replica, |
| 44 | + shuffle, |
| 45 | + transform, |
| 46 | + num_samples, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +# %% |
| 51 | +# For easy of use, we define a lightning data module so we can reuse it across |
| 52 | +# our trainer and other components that need to load data. |
| 53 | + |
| 54 | +# pyre-fixme[13]: Attribute `test_ds` is never initialized. |
| 55 | +# pyre-fixme[13]: Attribute `train_ds` is never initialized. |
| 56 | +# pyre-fixme[13]: Attribute `val_ds` is never initialized. |
| 57 | +class TinyImageNetDataModule(pl.LightningDataModule): |
| 58 | + """ |
| 59 | + TinyImageNetDataModule is a pytorch LightningDataModule for the tiny |
| 60 | + imagenet dataset. |
| 61 | + """ |
| 62 | + |
| 63 | + train_ds: TinyImageNetDataset |
| 64 | + val_ds: TinyImageNetDataset |
| 65 | + test_ds: TinyImageNetDataset |
| 66 | + |
| 67 | + def __init__(self, data_dir: str, batch_size: int = 16) -> None: |
| 68 | + super().__init__() |
| 69 | + self.data_dir = data_dir |
| 70 | + self.batch_size = batch_size |
| 71 | + |
| 72 | + def setup(self, stage: Optional[str] = None) -> None: |
| 73 | + # Setup data loader and transforms |
| 74 | + img_transform = transforms.Compose( |
| 75 | + [ |
| 76 | + transforms.Grayscale(), |
| 77 | + transforms.ToTensor(), |
| 78 | + ] |
| 79 | + ) |
| 80 | + self.train_ds = TinyImageNetDataset( |
| 81 | + data_path=os.path.join(self.data_dir, "train"), |
| 82 | + transform=lambda x: (img_transform(x[0]), x[1]), |
| 83 | + ) |
| 84 | + self.val_ds = TinyImageNetDataset( |
| 85 | + data_path=os.path.join(self.data_dir, "val"), |
| 86 | + transform=lambda x: (img_transform(x[0]), x[1]), |
| 87 | + ) |
| 88 | + self.test_ds = TinyImageNetDataset( |
| 89 | + data_path=os.path.join(self.data_dir, "test"), |
| 90 | + transform=lambda x: (img_transform(x[0]), x[1]), |
| 91 | + ) |
| 92 | + |
| 93 | + def train_dataloader(self) -> DataLoader: |
| 94 | + # pyre-fixme[6] |
| 95 | + return DataLoader(self.train_ds, batch_size=self.batch_size) |
| 96 | + |
| 97 | + def val_dataloader(self) -> DataLoader: |
| 98 | + # pyre-fixme[6]: |
| 99 | + return DataLoader(self.val_ds, batch_size=self.batch_size) |
| 100 | + |
| 101 | + def test_dataloader(self) -> DataLoader: |
| 102 | + # pyre-fixme[6] |
| 103 | + return DataLoader(self.test_ds, batch_size=self.batch_size) |
| 104 | + |
| 105 | + def teardown(self, stage: Optional[str] = None) -> None: |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +# %% |
| 110 | +# To pass data between the different components we use fsspec which allows us to |
| 111 | +# read/write to cloud or local file storage. |
| 112 | + |
| 113 | + |
| 114 | +def download_data(remote_path: str, tmpdir: str) -> str: |
| 115 | + """ |
| 116 | + download_data downloads the training data from the specified remote path via |
| 117 | + fsspec and places it in the tmpdir unextracted. |
| 118 | + """ |
| 119 | + tar_path = os.path.join(tmpdir, "data.tar.gz") |
| 120 | + print(f"downloading dataset from {remote_path} to {tar_path}...") |
| 121 | + fs, _, rpaths = fsspec.get_fs_token_paths(remote_path) |
| 122 | + assert len(rpaths) == 1, "must have single path" |
| 123 | + fs.get(rpaths[0], tar_path) |
| 124 | + |
| 125 | + data_path = os.path.join(tmpdir, "data") |
| 126 | + print(f"extracting {tar_path} to {data_path}...") |
| 127 | + with tarfile.open(tar_path, mode="r") as f: |
| 128 | + f.extractall(data_path) |
| 129 | + |
| 130 | + return data_path |
0 commit comments