You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/pytorch/image-classification/README.md
+59-32Lines changed: 59 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
-->
16
16
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.
18
20
19
-
The following examples showcase how to fine-tune a `ViT` for image-classification using PyTorch.
20
21
21
22
## Using datasets from 🤗 `datasets`
22
23
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.
24
25
25
26
👀 See the results here: [nateraw/vit-base-beans](https://huggingface.co/nateraw/vit-base-beans).
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.
50
51
51
52
👀 See the results here: [nateraw/vit-base-cats-vs-dogs](https://huggingface.co/nateraw/vit-base-cats-vs-dogs).
52
53
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
-
76
54
## Using your own data
77
55
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:
79
65
80
66
```bash
81
67
root/dog/xxx.png
@@ -87,22 +73,55 @@ root/cat/nsdf3.png
87
73
root/cat/[...]/asd932_.png
88
74
```
89
75
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:
91
77
92
78
```bash
93
79
python run_image_classification.py \
94
-
--dataset_name nateraw/image-folder \
95
80
--train_dir <path-to-train-root> \
96
81
--output_dir ./outputs/ \
97
82
--remove_unused_columns False \
98
83
--do_train \
99
84
--do_eval
100
85
```
101
86
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
103
90
- To control the split amount, use the `--train_val_split` flag.
104
91
- To provide your own validation split in its own directory, you can pass the `--validation_dir <path-to-val-root>` flag.
105
92
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:
0 commit comments