ValueError: Unable to infer channel dimension format
Asked Answered
M

2

5

When training model with transformers, the following error occurs and I do not know how to resolve it (my input is torch.Size([1, 3, 224, 224])) :

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_23/2337200543.py in 11 ) 12 # begin training ---> 13 results = trainer.train()

/opt/conda/lib/python3.7/site-packages/transformers/trainer.py in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs) 1635 resume_from_checkpoint=resume_from_checkpoint, 1636
trial=trial, -> 1637 ignore_keys_for_eval=ignore_keys_for_eval, 1638 ) 1639

/opt/conda/lib/python3.7/site-packages/transformers/trainer.py in _inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval) 1870 1871 step = -1 -> 1872 for step, inputs in enumerate(epoch_iterator): 1873 total_batched_samples += 1 1874
if rng_to_sync:

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in next(self) 626 # TODO(https://github.com/pytorch/pytorch/issues/76750) 627 self._reset() # type: ignore[call-arg] --> 628 data = self._next_data() 629 self._num_yielded += 1 630 if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self) 669 def _next_data(self): 670 index = self._next_index() # may raise StopIteration --> 671 data = self._dataset_fetcher.fetch(index) # may raise StopIteration 672 if self._pin_memory: 673 data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index) 56 data = self.dataset.getitems(possibly_batched_index) 57 else: ---> 58 data = [self.dataset[idx] for idx in possibly_batched_index] 59 else: 60 data = self.dataset[possibly_batched_index]

/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in (.0) 56 data = self.dataset.getitems(possibly_batched_index) 57 else: ---> 58 data = [self.dataset[idx] for idx in possibly_batched_index] 59 else: 60 data = self.dataset[possibly_batched_index]

/opt/conda/lib/python3.7/site-packages/datasets/arrow_dataset.py in getitem(self, key) 1763 """Can be used to index columns (by string names) or rows (by integer index or iterable of indices or bools).""" 1764 return self._getitem( -> 1765 key, 1766 ) 1767

/opt/conda/lib/python3.7/site-packages/datasets/arrow_dataset.py in _getitem(self, key, decoded, **kwargs) 1748 pa_subtable = query_table(self._data, key, indices=self._indices if self._indices is not None else None) 1749 formatted_output = format_table( -> 1750 pa_subtable, key, formatter=formatter, format_columns=format_columns, output_all_columns=output_all_columns
1751 ) 1752 return formatted_output

/opt/conda/lib/python3.7/site-packages/datasets/formatting/formatting.py in format_table(table, key, formatter, format_columns, output_all_columns) 530 python_formatter = PythonFormatter(features=None) 531 if format_columns is None: --> 532 return formatter(pa_table, query_type=query_type) 533 elif query_type == "column": 534 if key in format_columns:

/opt/conda/lib/python3.7/site-packages/datasets/formatting/formatting.py in call(self, pa_table, query_type) 279 def call(self, pa_table: pa.Table, query_type: str) -> Union[RowFormat, ColumnFormat, BatchFormat]: 280 if query_type == "row": --> 281 return self.format_row(pa_table) 282 elif query_type == "column": 283 return self.format_column(pa_table)

/opt/conda/lib/python3.7/site-packages/datasets/formatting/formatting.py in format_row(self, pa_table) 385 386 def format_row(self, pa_table: pa.Table) -> dict: --> 387 formatted_batch = self.format_batch(pa_table) 388 try: 389 return _unnest(formatted_batch)

/opt/conda/lib/python3.7/site-packages/datasets/formatting/formatting.py in format_batch(self, pa_table) 416 if self.decoded: 417 batch = self.python_features_decoder.decode_batch(batch) --> 418 return self.transform(batch) 419 420

/tmp/ipykernel_23/3636630232.py in preprocess(batch) 3 inputs = feature_extractor( 4 batch['image'], ----> 5 return_tensors='pt' 6 ) 7 # include the labels

/opt/conda/lib/python3.7/site-packages/transformers/image_processing_utils.py in call(self, images, **kwargs) 456 def call(self, images, **kwargs) -> BatchFeature: 457 """Preprocess an image or a batch of images.""" --> 458 return self.preprocess(images, **kwargs) 459 460 def preprocess(self, images, **kwargs) -> BatchFeature:

/opt/conda/lib/python3.7/site-packages/transformers/models/vit/image_processing_vit.py in preprocess(self, images, do_resize, size, resample, do_rescale, rescale_factor, do_normalize, image_mean, image_std, return_tensors, data_format, **kwargs) 260 261 if do_resize: --> 262 images = [self.resize(image=image, size=size_dict, resample=resample) for image in images] 263 264 if do_rescale:

/opt/conda/lib/python3.7/site-packages/transformers/models/vit/image_processing_vit.py in (.0) 260 261 if do_resize: --> 262 images = [self.resize(image=image, size=size_dict, resample=resample) for image in images] 263 264 if do_rescale:

/opt/conda/lib/python3.7/site-packages/transformers/models/vit/image_processing_vit.py in resize(self, image, size, resample, data_format, **kwargs) 125 raise ValueError(f"The size dictionary must contain the keys height and width. Got {size.keys()}") 126 return resize( --> 127 image, size=(size["height"], size["width"]), resample=resample, data_format=data_format, **kwargs 128 ) 129

/opt/conda/lib/python3.7/site-packages/transformers/image_transforms.py in resize(image, size, resample, reducing_gap, data_format, return_numpy) 288 # For all transformations, we want to keep the same data format as the input image unless otherwise specified. 289 # The resized image from PIL will always have channels last, so find the input format first. --> 290 data_format = infer_channel_dimension_format(image) if data_format is None else data_format 291 292 # To maintain backwards compatibility with the resizing done in previous image feature extractors, we use

/opt/conda/lib/python3.7/site-packages/transformers/image_utils.py in infer_channel_dimension_format(image) 163 elif image.shape[last_dim] in (1, 3): 164 return ChannelDimension.LAST --> 165 raise ValueError("Unable to infer channel dimension format") 166 167

ValueError: Unable to infer channel dimension format

Mcneill answered 30/3, 2023 at 17:3 Comment(0)
M
4

I was using .png dataset for this training, once I converted to .jpg, all went well!

Mcneill answered 12/4, 2023 at 15:51 Comment(0)
S
10

According to this discussion, you need to load your image using image = Image.open("path_to_image").convert("RGB") to fix this issue.

Savior answered 20/7, 2023 at 17:45 Comment(0)
M
4

I was using .png dataset for this training, once I converted to .jpg, all went well!

Mcneill answered 12/4, 2023 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.