Skip to content

Commit 52bde39

Browse files
Niels RoggeNiels Rogge
authored andcommitted
Improve README
1 parent 24a85cc commit 52bde39

File tree

2 files changed

+79
-46
lines changed

2 files changed

+79
-46
lines changed

examples/pytorch/image-classification/README.md

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
-->
1616

17-
# Image classification examples
17+
# Image classification example
18+
19+
This directory contains a script, `run_image_classification.py`, that showcases how to fine-tune any model supported by the [`AutoModelForImageClassification` API](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoModelForImageClassification) (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 PyTorch. It can be used to fine-tune models on both well-known datasets (like [CIFAR-10](https://huggingface.co/datasets/cifar10), [Fashion MNIST](https://huggingface.co/datasets/fashion_mnist), ...) as well as on your own custom data.
1820

19-
The following examples showcase how to fine-tune a `ViT` for image-classification using PyTorch.
2021

2122
## Using datasets from 🤗 `datasets`
2223

23-
Here we show how to fine-tune a `ViT` on the [beans](https://huggingface.co/datasets/beans) dataset.
24+
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.
2425

2526
👀 See the results here: [nateraw/vit-base-beans](https://huggingface.co/nateraw/vit-base-beans).
2627

@@ -46,36 +47,21 @@ python run_image_classification.py \
4647
--seed 1337
4748
```
4849

49-
Here we show how to fine-tune a `ViT` on the [cats_vs_dogs](https://huggingface.co/datasets/cats_vs_dogs) dataset.
50+
To fine-tune another model, simply provide the `--model_name_or_path` argument. To train on another dataset, simply set the `--dataset_name` argument.
5051

5152
👀 See the results here: [nateraw/vit-base-cats-vs-dogs](https://huggingface.co/nateraw/vit-base-cats-vs-dogs).
5253

53-
```bash
54-
python run_image_classification.py \
55-
--dataset_name cats_vs_dogs \
56-
--output_dir ./cats_vs_dogs_outputs/ \
57-
--remove_unused_columns False \
58-
--do_train \
59-
--do_eval \
60-
--push_to_hub \
61-
--push_to_hub_model_id vit-base-cats-vs-dogs \
62-
--fp16 True \
63-
--learning_rate 2e-4 \
64-
--num_train_epochs 5 \
65-
--per_device_train_batch_size 32 \
66-
--per_device_eval_batch_size 32 \
67-
--logging_strategy steps \
68-
--logging_steps 10 \
69-
--evaluation_strategy epoch \
70-
--save_strategy epoch \
71-
--load_best_model_at_end True \
72-
--save_total_limit 3 \
73-
--seed 1337
74-
```
75-
7654
## Using your own data
7755

78-
To use your own dataset, the training script expects the following directory structure:
56+
To use your own dataset, there are 2 ways:
57+
- you can either provide your own folders as `--train_dir` and/or `--validation_dir` arguments
58+
- you can upload your dataset to the hub (possibly as a private repo, if you prefer so), and simply pass the `--dataset_name` argument.
59+
60+
Below, we explain both in more detail.
61+
62+
### Provide them as folders
63+
64+
If you provide your own folders with images, the script expects the following directory structure:
7965

8066
```bash
8167
root/dog/xxx.png
@@ -87,22 +73,55 @@ root/cat/nsdf3.png
8773
root/cat/[...]/asd932_.png
8874
```
8975

90-
Once you've prepared your dataset, you can can run the script like this:
76+
In other words, you need to organize your images in subfolders, based on their class. You can then run the script like this:
9177

9278
```bash
9379
python run_image_classification.py \
94-
--dataset_name nateraw/image-folder \
9580
--train_dir <path-to-train-root> \
9681
--output_dir ./outputs/ \
9782
--remove_unused_columns False \
9883
--do_train \
9984
--do_eval
10085
```
10186

102-
### 💡 The above will split the train dir into training and evaluation sets
87+
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 a 🤗 Dataset object.
88+
89+
#### 💡 The above will split the train dir into training and evaluation sets
10390
- To control the split amount, use the `--train_val_split` flag.
10491
- To provide your own validation split in its own directory, you can pass the `--validation_dir <path-to-val-root>` flag.
10592

93+
### Upload your data to the hub, as a (possibly private) repo
94+
95+
It's very easy (and convenient) to upload your image dataset to the hub using the [`ImageFolder`](https://huggingface.co/docs/datasets/v2.0.0/en/image_process#imagefolder) feature available in 🤗 Datasets. Simply do the following:
96+
97+
```python
98+
from datasets import load_dataset
99+
100+
# example 1: local folder
101+
dataset = load_dataset("imagefolder", data_dir="path_to_your_folder")
102+
103+
# example 2: local files (suppoted formats are tar, gzip, zip, xz, rar, zstd)
104+
dataset = load_dataset("imagefolder", data_files="path_to_zip_file")
105+
106+
# example 3: remote files (suppoted formats are tar, gzip, zip, xz, rar, zstd)
107+
dataset = load_dataset("imagefolder", data_files="https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip"
108+
109+
# example 4: providing several splits
110+
dataset = load_dataset("imagefolder", data_files={"train": ["path/to/file1", "path/to/file2"], "test": ["path/to/file3", "path/to/file4"]})
111+
```
112+
113+
`ImageFolder` will create a `label` column, and the label name is based on the directory name.
114+
115+
Next, push it to the hub!
116+
117+
```python
118+
dataset.push_to_hub("name_of_your_dataset")
119+
120+
# if you want to push to a private repo, simply pass private=True:
121+
dataset.push_to_hub("name_of_your_dataset", private=True)
122+
```
123+
124+
and that's it! You can now simply train your model simply by setting the `--dataset_name` argument to the name of your dataset on the hub.
106125

107126
## Sharing your model on 🤗 Hub
108127

@@ -116,13 +135,21 @@ $ git config --global user.email "[email protected]"
116135
$ git config --global user.name "Your Name"
117136
```
118137

119-
2. Log in with your HuggingFace account credentials using `huggingface-cli`
138+
2. Log in with your HuggingFace account credentials using `huggingface-cli`:
120139

121140
```bash
122141
$ huggingface-cli login
123142
# ...follow the prompts
124143
```
125144

145+
or, in case you're running in a notebook:
146+
147+
```python
148+
from huggingface_hub import notebook_login
149+
150+
notebook_login()
151+
```
152+
126153
3. When running the script, pass the following arguments:
127154

128155
```bash

examples/pytorch/image-classification/run_image_classification.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,11 @@ def pil_loader(path: str):
7272
class DataTrainingArguments:
7373
"""
7474
Arguments pertaining to what data we are going to input our model for training and eval.
75-
Using `HfArgumentParser` we can turn this class
76-
into argparse arguments to be able to specify them on
77-
the command line.
75+
Using `HfArgumentParser` we can turn this class into argparse arguments to be able to specify
76+
them on the command line.
7877
"""
7978

80-
dataset_name: Optional[str] = field(
81-
default="nateraw/image-folder", metadata={"help": "Name of a dataset from the datasets package"}
82-
)
79+
dataset_name: Optional[str] = field(metadata={"help": "Name of a dataset from the hub."})
8380
dataset_config_name: Optional[str] = field(
8481
default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
8582
)
@@ -104,6 +101,11 @@ class DataTrainingArguments:
104101
)
105102

106103
def __post_init__(self):
104+
if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None):
105+
raise ValueError(
106+
"You must specify either a dataset name from the hub or a train and/or validation directory."
107+
)
108+
107109
data_files = dict()
108110
if self.train_dir is not None:
109111
data_files["train"] = self.train_dir
@@ -201,14 +203,18 @@ def main():
201203
)
202204

203205
# Initialize our dataset and prepare it for the 'image-classification' task.
204-
ds = load_dataset(
205-
data_args.dataset_name,
206-
data_args.dataset_config_name,
207-
data_files=data_args.data_files,
208-
cache_dir=model_args.cache_dir,
209-
task="image-classification",
210-
use_auth_token=True if model_args.use_auth_token else None,
211-
)
206+
if data_args.dataset_name is not None:
207+
ds = load_dataset(
208+
data_args.dataset_name,
209+
data_args.dataset_config_name,
210+
cache_dir=model_args.cache_dir,
211+
task="image-classification",
212+
use_auth_token=True if model_args.use_auth_token else None,
213+
)
214+
else:
215+
ds = load_dataset(
216+
"imagefolder", data_files=data_args.data_files, cache_dir=model_args.cache_dir, task="image-classification"
217+
)
212218

213219
# If we don't have a validation split, split off a percentage of train as validation.
214220
data_args.train_val_split = None if "validation" in ds.keys() else data_args.train_val_split

0 commit comments

Comments
 (0)