huggingface / notebooks

Notebooks using the Hugging Face libraries 🤗
Apache License 2.0
3.57k stars 1.5k forks source link

image_classification_albumentations.ipynb failing with Target size (torch.Size([32, 224, 224, 3])) must be the same as input size (torch.Size([32, 187])) #185

Open amitkml opened 2 years ago

amitkml commented 2 years ago

Hi,

I have used a similar dataset as image_classification_albumentations.ipynb and reused the notbook code completely but model training failing with Target size (torch.Size([32, 224, 224, 3])) must be the same as input size (torch.Size([32, 187])).

This error seems to indicate the classification head issue and is there any way I can fix this?

` Running training Num examples = 8415 Num Epochs = 10 Instantaneous batch size per device = 32 Total train batch size (w. parallel, distributed & accumulation) = 128 Gradient Accumulation steps = 4 Total optimization steps = 650

ValueError Traceback (most recent call last) in () ----> 1 trainer.train()

7 frames /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight) 2978 2979 if not (target.size() == input.size()): -> 2980 raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size())) 2981 2982 return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([32, 224, 224, 3])) must be the same as input size (torch.Size([32, 187])) `

amitkml commented 2 years ago

Stack trace details are

`

ValueError Traceback (most recent call last) in () ----> 1 trainer.train()

7 frames /usr/local/lib/python3.7/dist-packages/transformers/trainer.py in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs) 1420 tr_loss_step = self.training_step(model, inputs) 1421 else: -> 1422 tr_loss_step = self.training_step(model, inputs) 1423 1424 if (

/usr/local/lib/python3.7/dist-packages/transformers/trainer.py in training_step(self, model, inputs) 2009 2010 with self.autocast_smart_context_manager(): -> 2011 loss = self.compute_loss(model, inputs) 2012 2013 if self.args.n_gpu > 1:

/usr/local/lib/python3.7/dist-packages/transformers/trainer.py in compute_loss(self, model, inputs, return_outputs) 2041 else: 2042 labels = None -> 2043 outputs = model(**inputs) 2044 # Save past state if it exists 2045 # TODO: this needs to be fixed and made cleaner later.

/usr/local/lib/python3.7/dist-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 = [], []

/usr/local/lib/python3.7/dist-packages/transformers/models/convnext/modeling_convnext.py in forward(self, pixel_values, labels, output_hidden_states, return_dict) 424 elif self.config.problem_type == "multi_label_classification": 425 loss_fct = BCEWithLogitsLoss() --> 426 loss = loss_fct(logits, labels) 427 if not return_dict: 428 output = (logits,) + outputs[2:]

/usr/local/lib/python3.7/dist-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 = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py in forward(self, input, target) 705 self.weight, 706 pos_weight=self.pos_weight, --> 707 reduction=self.reduction) 708 709

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight) 2978 2979 if not (target.size() == input.size()): -> 2980 raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size())) 2981 2982 return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)

ValueError: Target size (torch.Size([32, 224, 224, 3])) must be the same as input size (torch.Size([32, 187])) `

NielsRogge commented 2 years ago

It seems that you want to do multi-label classification, is that right?