Spaces:
Sleeping
Sleeping
File size: 7,649 Bytes
33d4721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# Image Classification & Regression
Image classification is a form of supervised learning where a model is trained to identify
and categorize objects within images. AutoTrain simplifies the process, enabling you to
train a state-of-the-art image classification model by simply uploading labeled example
images.
Image regression/scoring is a form of supervised learning where a model is trained to predict a
score or value for an image. AutoTrain simplifies the process, enabling you to train a
state-of-the-art image scoring model by simply uploading labeled example images.
## Preparing your data
To ensure your image classification model trains effectively, follow these guidelines for preparing your data:
### Organizing Images For Image Classification
Prepare a zip file containing your categorized images. Each category should have its own
subfolder named after the class it represents. For example, to differentiate between
'cats' and 'dogs', your zip file structure should resemble the following:
```
cats_and_dogs.zip
βββ cats
β βββ cat.1.jpg
β βββ cat.2.jpg
β βββ cat.3.jpg
β βββ ...
βββ dogs
βββ dog.1.jpg
βββ dog.2.jpg
βββ dog.3.jpg
βββ ...
```
You can also use a dataset from the Hugging Face Hub. Example dataset from Hugging Face Hub: [truepositive/hotdog_nothotdog](https://huggingface.co/datasets/truepositive/hotdog_nothotdog).
### Organizing Images for Image Regression/Scoring
Prepare a zip file containing your images and metadata.jsonl.
```
Archive.zip
βββ 0001.png
βββ 0002.png
βββ 0003.png
βββ .
βββ .
βββ .
βββ metadata.jsonl
```
Example for `metadata.jsonl`:
```
{"file_name": "0001.png", "target": 0.5}
{"file_name": "0002.png", "target": 0.7}
{"file_name": "0003.png", "target": 0.3}
```
Please note that metadata.jsonl should contain the `file_name` and the `target` value for each image.
You can also use a dataset from the Hugging Face Hub. Example dataset from Hugging Face Hub: [abhishek/img-quality-full](https://huggingface.co/datasets/abhishek/img-quality-full).
### Image Requirements
- Format: Ensure all images are in JPEG, JPG, or PNG format.
- Quantity: Include at least 5 images per class to provide the model with sufficient examples for learning.
- Exclusivity: The zip file should exclusively contain folders named after the classes,
and these folders should only contain relevant images. No additional files or nested
folders should be included.
** Additional Tips**
- Uniformity: While not required, having images of similar sizes and resolutions can help improve model performance.
- Variability: Include a variety of images for each class to encompass the range of
appearances and contexts the model might encounter in real-world scenarios.
Some points to keep in mind:
- The zip file should contain multiple folders (the classes), each folder should contain images of a single class.
- The name of the folder should be the name of the class.
- The images must be jpeg, jpg or png.
- There should be at least 5 images per class.
- There must not be any other files in the zip file.
- There must not be any other folders inside the zip folder.
When train.zip is decompressed, it creates two folders: cats and dogs. these are the two categories for classification. The images for both categories are in their respective folders. You can have as many categories as you want.
## Column Mapping
For image classification, if you are using a `zip` dataset format, the column mapping should be default and should not be changed.
```yaml
data:
.
.
.
column_mapping:
image_column: image
target_column: label
```
For image regression, the column mapping must be as follows:
```yaml
data:
.
.
.
column_mapping:
image_column: image
target_column: target
```
For image regression, `metadata.jsonl` should contain the `file_name` and the `target` value for each image.
If you are using a dataset from the Hugging Face Hub, you should set appropriate column mappings based on the dataset.
## Training
### Local Training
To train the model locally, create a configuration file (config.yaml) with the following content:
```yaml
task: image_classification
base_model: google/vit-base-patch16-224
project_name: autotrain-cats-vs-dogs-finetuned
log: tensorboard
backend: local
data:
path: cats_vs_dogs
train_split: train
valid_split: null
column_mapping:
image_column: image
target_column: label
params:
epochs: 2
batch_size: 4
lr: 2e-5
optimizer: adamw_torch
scheduler: linear
gradient_accumulation: 1
mixed_precision: fp16
hub:
username: ${HF_USERNAME}
token: ${HF_TOKEN}
push_to_hub: true
```
Here, we are using `cats_and_dogs` dataset from Hugging Face Hub. The model is trained for 2 epochs with a batch size of 4 and a learning rate of `2e-5`. We are using the `adamw_torch` optimizer and the `linear` scheduler. We are also using mixed precision training with a gradient accumulation of 1.
In order to use a local dataset, you can change the `data` section to:
```yaml
data:
path: data/
train_split: train # this folder inside data/ will be used for training, it contains the images in subfolders.
valid_split: valid # this folder inside data/ will be used for validation, it contains the images in subfolders. can also be null.
column_mapping:
image_column: image
target_column: label
```
Similarly, for image regression, you can use the following configuration file:
```yaml
task: image_regression
base_model: microsoft/resnet-50
project_name: autotrain-img-quality-resnet50
log: tensorboard
backend: local
data:
path: abhishek/img-quality-full
train_split: train
valid_split: null
column_mapping:
image_column: image
target_column: target
params:
epochs: 10
batch_size: 8
lr: 2e-3
optimizer: adamw_torch
scheduler: cosine
gradient_accumulation: 1
mixed_precision: fp16
hub:
username: ${HF_USERNAME}
token: ${HF_TOKEN}
push_to_hub: true
```
To train the model, run the following command:
```bash
$ autotrain --config config.yaml
```
This will start the training process and save the model to the Hugging Face Hub after training is complete. In case you dont want to save the model to the hub, you can set `push_to_hub` to `false` in the configuration file.
### Training on Hugging Face Spaces
To train the model on Hugging Face Spaces, create a training space as described in `Quickstart` section.
An example UI for training an image scoring model on Hugging Face Spaces is shown below:

In this example, we are training an image scoring model using the `microsoft/resnet-50` model on the `abhishek/img-quality-full` dataset.
We are training the model for 3 epochs with a batch size of 8 and a learning rate of `5e-5`.
We are using the `adamw_torch` optimizer and the `linear` scheduler.
We are also using mixed precision training with a gradient accumulation of 1.
Note how the column mapping has now been changed and `target` points to `quality_mos` column in the dataset.
To train the model, click on the `Start Training` button. This will start the training process and save the model to the Hugging Face Hub after training is complete.
## Parameters
### Image Classification Parameters
[[autodoc]] trainers.image_classification.params.ImageClassificationParams
### Image Regression Parameters
[[autodoc]] trainers.image_regression.params.ImageRegressionParams |