notebooks icon indicating copy to clipboard operation
notebooks copied to clipboard

Error in fine-tuning using image_classification.ipynb

Open shenw000 opened this issue 1 year ago • 1 comments

When I run the image_classification.ipynb to fine tung the model, it generates the following error after ran train_ds.set_transform(preprocess_train) val_ds.set_transform(preprocess_val) train_ds[0]:


TypeError Traceback (most recent call last) /tmp/ipykernel_26181/576563551.py in ----> 1 train_ds[0]

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/arrow_dataset.py in getitem(self, key) 2656 def getitem(self, key): # noqa: F811 2657 """Can be used to index columns (by string names) or rows (by integer index or iterable of indices or bools).""" -> 2658 return self._getitem(key) 2659 2660 def getitems(self, keys: List) -> List:

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/arrow_dataset.py in _getitem(self, key, **kwargs) 2641 formatter = get_formatter(format_type, features=self.features, **format_kwargs) 2642 pa_subtable = query_table(self._data, key, indices=self._indices if self._indices is not None else None) -> 2643 formatted_output = format_table( 2644 pa_subtable, key, formatter=formatter, format_columns=format_columns, output_all_columns=output_all_columns 2645 )

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/formatting/formatting.py in format_table(table, key, formatter, format_columns, output_all_columns) 632 python_formatter = PythonFormatter(features=None) 633 if format_columns is None: --> 634 return formatter(pa_table, query_type=query_type) 635 elif query_type == "column": 636 if key in format_columns:

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/formatting/formatting.py in call(self, pa_table, query_type) 404 def call(self, pa_table: pa.Table, query_type: str) -> Union[RowFormat, ColumnFormat, BatchFormat]: 405 if query_type == "row": --> 406 return self.format_row(pa_table) 407 elif query_type == "column": 408 return self.format_column(pa_table)

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/formatting/formatting.py in format_row(self, pa_table) 488 489 def format_row(self, pa_table: pa.Table) -> dict: --> 490 formatted_batch = self.format_batch(pa_table) 491 try: 492 return _unnest(formatted_batch)

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/datasets/formatting/formatting.py in format_batch(self, pa_table) 518 batch = self.python_arrow_extractor().extract_batch(pa_table) 519 batch = self.python_features_decoder.decode_batch(batch) --> 520 return self.transform(batch) 521 522

/tmp/ipykernel_26181/936200964.py in preprocess_train(example_batch) 39 def preprocess_train(example_batch): 40 """Apply train_transforms across a batch.""" ---> 41 example_batch["pixel_values"] = [ 42 train_transforms(image.convert("RGB")) for image in example_batch["image"] 43 ]

/tmp/ipykernel_26181/936200964.py in (.0) 40 """Apply train_transforms across a batch.""" 41 example_batch["pixel_values"] = [ ---> 42 train_transforms(image.convert("RGB")) for image in example_batch["image"] 43 ] 44 return example_batch

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torchvision/transforms/transforms.py in call(self, img) 59 def call(self, img): 60 for t in self.transforms: ---> 61 img = t(img) 62 return img 63

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1101 or _global_forward_hooks or _global_forward_pre_hooks): -> 1102 return forward_call(*input, **kwargs) 1103 # Do not call functions when jit is used 1104 full_backward_hooks, non_full_backward_hooks = [], []

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torchvision/transforms/transforms.py in forward(self, img) 917 """ 918 i, j, h, w = self.get_params(img, self.scale, self.ratio) --> 919 return F.resized_crop(img, i, j, h, w, self.size, self.interpolation) 920 921 def repr(self):

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torchvision/transforms/functional.py in resized_crop(img, top, left, height, width, size, interpolation) 564 """ 565 img = crop(img, top, left, height, width) --> 566 img = resize(img, size, interpolation) 567 return img 568

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torchvision/transforms/functional.py in resize(img, size, interpolation, max_size, antialias) 417 ) 418 pil_interpolation = pil_modes_mapping[interpolation] --> 419 return F_pil.resize(img, size=size, interpolation=pil_interpolation, max_size=max_size) 420 421 return F_t.resize(img, size=size, interpolation=interpolation.value, max_size=max_size, antialias=antialias)

~/workspace/anaconda2/envs/vit-10/lib/python3.9/site-packages/torchvision/transforms/functional_pil.py in resize(img, size, interpolation, max_size) 233 raise TypeError('img should be PIL Image. Got {}'.format(type(img))) 234 if not (isinstance(size, int) or (isinstance(size, Sequence) and len(size) in (1, 2))): --> 235 raise TypeError('Got inappropriate size arg: {}'.format(size)) 236 237 if isinstance(size, Sequence) and len(size) == 1:

TypeError: Got inappropriate size arg: {'height': 224, 'width': 224}

Can anyone suggest what the problem is? Thank you!

shenw000 avatar Mar 05 '23 23:03 shenw000

@shenw000 ,There is an issue; you need to modify a small thing in the preprocess_train and preprocess_val functions. change for image in example_batch["image"] to for image in example_batch["img"]

lilyaYAHIAOUI avatar Apr 25 '23 11:04 lilyaYAHIAOUI