-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Add TF image classification example script #19956
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
Merged
amyeroberts
merged 16 commits into
huggingface:main
from
amyeroberts:add-examples-tf-image-classification
Feb 1, 2023
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
28dcb60
TF image classification script
amyeroberts a6e2181
Update requirements
amyeroberts b153089
Fix up
amyeroberts 331678e
Add tests
amyeroberts 78a9cb1
Update test fetcher
amyeroberts 4daf09e
Fix directory path
amyeroberts 1366831
Adding `zero-shot-object-detection` pipeline doctest. (#20274)
Narsil 04e8e96
Add generate kwargs to `AutomaticSpeechRecognitionPipeline` (#20952)
bofenghuang 0cc9d56
Trigger CI
sgugger f8c3a11
Data collator returns np
amyeroberts a083d39
Update feature extractor -> image processor
amyeroberts de79a3f
Bug fixes - updates to reflect changes in API
amyeroberts 91535f4
Update flags to match PT & run faster
amyeroberts 8ef50e3
Update instructions - Maria's comment
amyeroberts f26c37a
Update examples/tensorflow/image-classification/README.md
amyeroberts 69cc303
Remove slow decorator
amyeroberts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <!--- | ||
| Copyright 2021 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # Image classification examples | ||
|
|
||
| This directory contains 2 scripts that showcase how to fine-tune any model supported by the [`TFAutoModelForImageClassification` API](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.TFAutoModelForImageClassification) (such as [ViT](https://huggingface.co/docs/transformers/main/en/model_doc/vit), [ConvNeXT](https://huggingface.co/docs/transformers/main/en/model_doc/convnext), [ResNet](https://huggingface.co/docs/transformers/main/en/model_doc/resnet), [Swin Transformer](https://huggingface.co/docs/transformers/main/en/model_doc/swin)...) using TensorFlow. They can be used to fine-tune models on both [datasets from the hub](#using-datasets-from-hub) as well as on [your own custom data](#using-your-own-data). | ||
|
|
||
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/image_classification_inference_widget.png" height="400" /> | ||
|
|
||
| Try out the inference widget here: https://huggingface.co/google/vit-base-patch16-224 | ||
|
|
||
| ## TensorFlow | ||
|
|
||
| Based on the script [`run_image_classification.py`](https://github.com/huggingface/transformers/blob/main/examples/tensorflow/image-classification/run_image_classification.py). | ||
|
|
||
| ### Using datasets from Hub | ||
|
|
||
| Here we show how to fine-tune a Vision Transformer (`ViT`) on the [beans](https://huggingface.co/datasets/beans) dataset, to classify the disease type of bean leaves. The following will train a model and push it to the `amyeroberts/vit-base-beans` repo. | ||
|
|
||
| ```bash | ||
| python run_image_classification.py \ | ||
| --dataset_name beans \ | ||
| --output_dir ./beans_outputs/ \ | ||
| --remove_unused_columns False \ | ||
| --do_train \ | ||
| --do_eval \ | ||
| --push_to_hub \ | ||
| --hub_model_id amyeroberts/vit-base-beans \ | ||
| --learning_rate 2e-5 \ | ||
| --num_train_epochs 5 \ | ||
| --per_device_train_batch_size 8 \ | ||
| --per_device_eval_batch_size 8 \ | ||
| --logging_strategy steps \ | ||
| --logging_steps 10 \ | ||
| --evaluation_strategy epoch \ | ||
| --save_strategy epoch \ | ||
| --load_best_model_at_end True \ | ||
| --save_total_limit 3 \ | ||
| --seed 1337 | ||
| ``` | ||
|
|
||
| 👀 See the results here: [amyeroberts/vit-base-beans](https://huggingface.co/amyeroberts/vit-base-beans). | ||
|
|
||
| Note that you can replace the model and dataset by simply setting the `model_name_or_path` and `dataset_name` arguments respectively, with any model or dataset from the [hub](https://huggingface.co/). For an overview of all possible arguments, we refer to the [docs](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments) of the `TrainingArguments`, which can be passed as flags. | ||
|
|
||
| > If your model classification head dimensions do not fit the number of labels in the dataset, you can specify `--ignore_mismatched_sizes` to adapt it. | ||
|
|
||
| ### Using your own data | ||
|
|
||
| To use your own dataset, there are 2 ways: | ||
| - you can either provide your own folders as `--train_dir` and/or `--validation_dir` arguments | ||
| - you can upload your dataset to the hub (possibly as a private repo, if you prefer so), and simply pass the `--dataset_name` argument. | ||
|
|
||
| Below, we explain both in more detail. | ||
|
|
||
| #### Provide them as folders | ||
|
|
||
| If you provide your own folders with images, the script expects the following directory structure: | ||
|
|
||
| ```bash | ||
| root/dog/xxx.png | ||
| root/dog/xxy.png | ||
| root/dog/[...]/xxz.png | ||
|
|
||
| root/cat/123.png | ||
| root/cat/nsdf3.png | ||
| root/cat/[...]/asd932_.png | ||
| ``` | ||
|
|
||
| In other words, you need to organize your images in subfolders, based on their class. You can then run the script like this: | ||
|
|
||
| ```bash | ||
| python run_image_classification.py \ | ||
| --train_dir <path-to-train-root> \ | ||
| --output_dir ./outputs/ \ | ||
| --remove_unused_columns False \ | ||
| --do_train \ | ||
| --do_eval | ||
| ``` | ||
|
|
||
| Internally, the script will use the [`ImageFolder`](https://huggingface.co/docs/datasets/v2.0.0/en/image_process#imagefolder) feature which will automatically turn the folders into 🤗 Dataset objects. | ||
|
|
||
| ##### 💡 The above will split the train dir into training and evaluation sets | ||
| - To control the split amount, use the `--train_val_split` flag. | ||
| - To provide your own validation split in its own directory, you can pass the `--validation_dir <path-to-val-root>` flag. | ||
|
|
||
| #### Upload your data to the hub, as a (possibly private) repo | ||
|
|
||
| To upload your image dataset to the hub you can use the [`ImageFolder`](https://huggingface.co/docs/datasets/v2.0.0/en/image_process#imagefolder) feature available in 🤗 Datasets. Simply do the following: | ||
|
|
||
| ```python | ||
| from datasets import load_dataset | ||
|
|
||
| # example 1: local folder | ||
| dataset = load_dataset("imagefolder", data_dir="path_to_your_folder") | ||
|
|
||
| # example 2: local files (suppoted formats are tar, gzip, zip, xz, rar, zstd) | ||
| dataset = load_dataset("imagefolder", data_files="path_to_zip_file") | ||
|
|
||
| # example 3: remote files (suppoted formats are tar, gzip, zip, xz, rar, zstd) | ||
| dataset = load_dataset("imagefolder", data_files="https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip") | ||
|
|
||
| # example 4: providing several splits | ||
| dataset = load_dataset("imagefolder", data_files={"train": ["path/to/file1", "path/to/file2"], "test": ["path/to/file3", "path/to/file4"]}) | ||
| ``` | ||
|
|
||
| `ImageFolder` will create a `label` column, and the label name is based on the directory name. | ||
|
|
||
| Next, push it to the hub! | ||
|
|
||
| ```python | ||
| # assuming you have ran the huggingface-cli login command in a terminal | ||
| dataset.push_to_hub("name_of_your_dataset") | ||
|
|
||
| # if you want to push to a private repo, simply pass private=True: | ||
| dataset.push_to_hub("name_of_your_dataset", private=True) | ||
| ``` | ||
|
|
||
| and that's it! You can now train your model by simply setting the `--dataset_name` argument to the name of your dataset on the hub (as explained in [Using datasets from the 🤗 hub](#using-datasets-from-hub)). | ||
|
|
||
| More on this can also be found in [this blog post](https://huggingface.co/blog/image-search-datasets). | ||
|
|
||
| ### Sharing your model on 🤗 Hub | ||
|
|
||
| 0. If you haven't already, [sign up](https://huggingface.co/join) for a 🤗 account | ||
|
|
||
| 1. Make sure you have `git-lfs` installed and git set up. | ||
|
|
||
| ```bash | ||
| $ apt install git-lfs | ||
| $ git config --global user.email "[email protected]" | ||
| $ git config --global user.name "Your Name" | ||
| ``` | ||
|
|
||
| 2. Log in with your HuggingFace account credentials using `huggingface-cli`: | ||
|
|
||
| ```bash | ||
| $ huggingface-cli login | ||
| # ...follow the prompts | ||
| ``` | ||
|
|
||
| 3. When running the script, pass the following arguments: | ||
|
|
||
| ```bash | ||
| python run_image_classification.py \ | ||
| --push_to_hub \ | ||
| --push_to_hub_model_id <name-your-model> \ | ||
| ... | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| datasets>=1.17.0 | ||
| evaluate | ||
| tensorflow>=2.4 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.