Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image classification script, no trainer #16727

Merged

Conversation

NielsRogge
Copy link
Contributor

@NielsRogge NielsRogge commented Apr 12, 2022

What does this PR do?

This PR adds an example script for image classification that leverages Accelerate instead of the HuggingFace Trainer.

To do:

  • verify local train_dir and validation_dir
  • update README
  • add log fixes (Tensorboard)

Both can be updated after #16585 is merged.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 12, 2022

The documentation is not available anymore as the PR was closed or merged.

@NielsRogge NielsRogge force-pushed the add_image_classification_no_trainer branch from 4fec3dc to a5e8caf Compare April 19, 2022 07:07
@NielsRogge NielsRogge force-pushed the add_image_classification_no_trainer branch from a5e8caf to d066098 Compare April 19, 2022 07:19
@NielsRogge NielsRogge requested a review from sgugger April 19, 2022 09:35
@NielsRogge
Copy link
Contributor Author

NielsRogge commented Apr 19, 2022

@sgugger not sure why, but the test for the script fails:

WARNING  datasets.builder:builder.py:388 Using custom data configuration huggingface--image-classification-test-sample-b7448dc7ae37f2cf
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:388 ***** Running training *****
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:389   Num examples = 8
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:390   Num Epochs = 3
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:391   Instantaneous batch size per device = 2
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:392   Total train batch size (w. parallel, distributed & accumulation) = 2
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:393   Gradient Accumulation steps = 1
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:394   Total optimization steps = 12
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:471 epoch 0: {'accuracy': 0.0}
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:471 epoch 1: {'accuracy': 0.0}
INFO     run_image_classification_no_trainer:run_image_classification_no_trainer.py:471 epoch 2: {'accuracy': 0.0}

Weirdly, it passes locally for me.

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice new addition!
Tested locally and had the test pass one time out of 4, so maybe it's a seed issue? Setting a seed made it pass several times in a row. The learning rate seems extremely high for a Transformers model (but maybe ViT exepects that) so I'd say the test is a bit unstable without setting the seed.

@NielsRogge
Copy link
Contributor Author

I'm getting issues when only passing id2label and label2id to the config, but not the num_labels:

if size_average is not None or reduce is not None:
            reduction = _Reduction.legacy_get_string(size_average, reduce)
>       return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
E       IndexError: Target 6 is out of bounds.

@sgugger
Copy link
Collaborator

sgugger commented Apr 19, 2022

Oh ok, shouldn't be the case. Let's put back the num_labels for now and I'll have a look later at why it failed to update properly.

@NielsRogge NielsRogge merged commit b96e82c into huggingface:main Apr 19, 2022
elusenji pushed a commit to elusenji/transformers that referenced this pull request Jun 12, 2022
* Add first draft

* Improve README and run fixup

* Make script aligned with other scripts, improve README

* Improve script and add test

* Remove print statement

* Apply suggestions from code review

* Add num_labels to make test pass

* Improve README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants