Repository: mosaicml/composer

Issue:
Bug in ImagenetDatasetHparams with validation set
composer/datasets/imagenet.py
line 171
transforms.Resize(self.resize_size),

Should check if self.resize_size>0.
The easiest fix would be to do the same as in line 238.

otherwise is going to raise error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 530, in __next__
    data = self._next_data()
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 1224, in _next_data
    return self._process_data(data)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 1250, in _process_data
    data.reraise()
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/_utils.py", line 457, in reraise
    raise exception
ValueError: Caught ValueError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 49, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torchvision/datasets/folder.py", line 232, in __getitem__
    sample = self.transform(sample)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torchvision/transforms/transforms.py", line 95, in __call__
    img = t(img)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torchvision/transforms/transforms.py", line 349, in forward
    return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torchvision/transforms/functional.py", line 436, in resize
    return F_pil.resize(img, size=size, interpolation=pil_interpolation, max_size=max_size)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/torchvision/transforms/functional_pil.py", line 258, in resize
    return img.resize((new_w, new_h), interpolation)
  File "/opt/conda/envs/spartns/lib/python3.10/site-packages/PIL/Image.py", line 2062, in resize
    return self._new(self.im.resize(size, resample, box))
ValueError: height and width must be > 0



Discussion:
Thanks @ccffccffcc for reporting this, we will add that check in a upcoming PR

Resolving patch:
diff --git a/composer/datasets/ade20k_hparams.py b/composer/datasets/ade20k_hparams.py
--- a/composer/datasets/ade20k_hparams.py
+++ b/composer/datasets/ade20k_hparams.py
@@ -68,6 +68,9 @@ def validate(self):
         if self.max_resize_scale < self.min_resize_scale:
             raise ValueError('max_resize_scale cannot be less than min_resize_scale')
 
+        if self.final_size is not None and self.final_size <= 0:
+            raise ValueError('self.final_size cannot be zero or negative.')
+
     def initialize_object(self, batch_size, dataloader_hparams) -> DataSpec:
         self.validate()
 
diff --git a/composer/datasets/imagenet_hparams.py b/composer/datasets/imagenet_hparams.py
--- a/composer/datasets/imagenet_hparams.py
+++ b/composer/datasets/imagenet_hparams.py
@@ -170,10 +170,9 @@ def initialize_object(self, batch_size: int, dataloader_hparams: DataLoaderHpara
             if self.is_train:
                 # include fixed-size resize before RandomResizedCrop in training only
                 # if requested (by specifying a size > 0)
-                train_resize_size = self.resize_size
                 train_transforms: List[torch.nn.Module] = []
-                if train_resize_size > 0:
-                    train_transforms.append(transforms.Resize(train_resize_size))
+                if self.resize_size > 0:
+                    train_transforms.append(transforms.Resize(self.resize_size))
                 # always include RandomResizedCrop and RandomHorizontalFlip
                 train_transforms += [
                     transforms.RandomResizedCrop(self.crop_size, scale=(0.08, 1.0), ratio=(0.75, 4.0 / 3.0)),
@@ -182,10 +181,11 @@ def initialize_object(self, batch_size: int, dataloader_hparams: DataLoaderHpara
                 transformation = transforms.Compose(train_transforms)
                 split = 'train'
             else:
-                transformation = transforms.Compose([
-                    transforms.Resize(self.resize_size),
-                    transforms.CenterCrop(self.crop_size),
-                ])
+                val_transforms: List[torch.nn.Module] = []
+                if self.resize_size > 0:
+                    val_transforms.append(transforms.Resize(self.resize_size))
+                val_transforms.append(transforms.CenterCrop(self.crop_size))
+                transformation = transforms.Compose(val_transforms)
                 split = 'val'
 
             device_transform_fn = NormalizationFn(mean=IMAGENET_CHANNEL_MEAN, std=IMAGENET_CHANNEL_STD)


Test patch: