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
Image classification datasets are used to train a model to classify an entire image. There are a wide variety of applications enabled by these datasets such as identifying endangered wildlife species or screening for disease in medical images. This guide will show you how to apply transformations to an image classification dataset.
4
+
5
+
Before you start, make sure you have up-to-date versions of `albumentations` and `cv2` installed:
6
+
7
+
```bash
8
+
pip install -U albumentations opencv-python
9
+
```
10
+
11
+
This guide uses the [Beans](https://huggingface.co/datasets/beans) dataset for identifying the type of bean plant disease based on an image of its leaf.
12
+
13
+
Load the dataset and take a look at an example:
14
+
15
+
```py
16
+
>>>from datasets import load_dataset
17
+
18
+
>>> dataset = load_dataset("beans")
19
+
>>> dataset["train"][10]
20
+
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x500 at 0x7F8D2F4D7A10>,
Copy file name to clipboardExpand all lines: docs/source/image_process.mdx
+8-187Lines changed: 8 additions & 187 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
This guide shows specific methods for processing image datasets. Learn how to:
4
4
5
5
- Use [`~Dataset.map`] with image dataset.
6
-
- Apply data augmentations to your dataset with [`~Dataset.set_transform`].
6
+
- Apply data augmentations to a dataset with [`~Dataset.set_transform`].
7
7
8
8
For a guide on how to process any type of dataset, take a look at the <aclass="underline decoration-sky-400 decoration-2 font-semibold"href="./process">general process guide</a>.
9
9
@@ -37,21 +37,17 @@ The cache file saves time because you don't have to execute the same transform t
37
37
38
38
Both parameter values default to 1000, which can be expensive if you are storing images. Lower these values to use less memory when you use [`~Dataset.map`].
39
39
40
-
## Data augmentation
40
+
## Apply transforms
41
41
42
-
🤗 Datasets can apply data augmentations from any library or package to your dataset.
43
-
44
-
### Image Classification
45
-
46
-
First let's see how you can transform image classification datasets. This guide will use the transforms from [torchvision](https://pytorch.org/vision/stable/transforms.html).
42
+
🤗 Datasets applies data augmentations from any library or package to your dataset. Transforms can be applied on-the-fly on batches of data with [`~Dataset.set_transform`], which consumes less disk space.
47
43
48
44
<Tip>
49
45
50
-
Feel free to use other data augmentation libraries like [Albumentations](https://albumentations.ai/docs/), [Kornia](https://kornia.readthedocs.io/en/latest/), and [imgaug](https://imgaug.readthedocs.io/en/latest/).
46
+
The following example uses [torchvision](https://pytorch.org/vision/stable/index.html), but feel free to use other data augmentation libraries like [Albumentations](https://albumentations.ai/docs/), [Kornia](https://kornia.readthedocs.io/en/latest/), and [imgaug](https://imgaug.readthedocs.io/en/latest/).
51
47
52
48
</Tip>
53
49
54
-
As an example, try to apply a [`ColorJitter`](https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.ColorJitter) transform to change the color properties of the image randomly:
50
+
For example, if you'd like to change the color properties of an image randomly:
@@ -64,191 +60,16 @@ As an example, try to apply a [`ColorJitter`](https://pytorch.org/vision/stable/
64
60
... )
65
61
```
66
62
67
-
Create a function to apply the `ColorJitter` transform to an image:
63
+
Create a function to apply the `ColorJitter` transform:
68
64
69
65
```py
70
66
>>>deftransforms(examples):
71
67
... examples["pixel_values"] = [jitter(image.convert("RGB")) for image in examples["image"]]
72
68
...return examples
73
69
```
74
70
75
-
Use the [`~Dataset.set_transform`] function to apply the transform on-the-fly which consumes less disk space. This function is useful if you only need to access the examples once:
71
+
Apply the transform with the [`~Dataset.set_transform`] function:
76
72
77
73
```py
78
74
>>> dataset.set_transform(transforms)
79
-
```
80
-
81
-
Now you can take a look at the augmented image by indexing into the `pixel_values`:
Object detection models identify something in an image, and object detection datasets are used for applications such as autonomous driving and detecting natural hazards like wildfire. This guide will show you how to apply transformations to an object detection dataset following the [tutorial](https://albumentations.ai/docs/examples/example_bboxes/) from [Albumentations](https://albumentations.ai/docs/).
99
-
100
-
To run these examples, make sure you have up-to-date versions of `albumentations` and `cv2` installed:
101
-
102
-
```
103
-
pip install -U albumentations opencv-python
104
-
```
105
-
106
-
In this example, you'll use the [`cppe-5`](https://huggingface.co/datasets/cppe-5) dataset for identifying medical personal protective equipment (PPE) in the context of the COVID-19 pandemic.
107
-
108
-
Load the dataset and take a look at an example:
109
-
110
-
```py
111
-
from datasets import load_dataset
112
-
113
-
>>> ds = load_dataset("cppe-5")
114
-
>>> example = ds['train'][0]
115
-
>>> example
116
-
{'height': 663,
117
-
'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=943x663 at 0x7FC3DC756250>,
118
-
'image_id': 15,
119
-
'objects': {'area': [3796, 1596, 152768, 81002],
120
-
'bbox': [[302.0, 109.0, 73.0, 52.0],
121
-
[810.0, 100.0, 57.0, 28.0],
122
-
[160.0, 31.0, 248.0, 616.0],
123
-
[741.0, 68.0, 202.0, 401.0]],
124
-
'category': [4, 4, 0, 0],
125
-
'id': [114, 115, 116, 117]},
126
-
'width': 943}
127
-
```
128
-
129
-
The dataset has the following fields:
130
-
131
-
-`image`: PIL.Image.Image object containing the image.
132
-
-`image_id`: The image ID.
133
-
-`height`: The image height.
134
-
-`width`: The image width.
135
-
-`objects`: A dictionary containing bounding box metadata for the objects in the image:
136
-
-`id`: The annotation id.
137
-
-`area`: The area of the bounding box.
138
-
-`bbox`: The object's bounding box (in the [coco](https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/#coco) format).
139
-
-`category`: The object's category, with possible values including `Coverall (0)`, `Face_Shield (1)`, `Gloves (2)`, `Goggles (3)` and `Mask (4)`.
140
-
141
-
You can visualize the `bboxes` on the image using some internal torch utilities. To do that, you will need to reference the [`~datasets.ClassLabel`] feature associated with the category IDs so you can look up the string labels:
With `albumentations`, you can apply transforms that will affect the image while also updating the `bboxes` accordingly. In this case, the image is resized to (480, 480), flipped horizontally, and brightened.
171
-
172
-
`albumentations` expects the image to be in BGR format, not RGB, so you'll have to convert the image before applying the transform.
Use the [`~Dataset.set_transform`] function to apply the transform on-the-fly which consumes less disk space. The randomness of data augmentation may return a different image if you access the same example twice. It is especially useful when training a model for several epochs.
233
-
234
-
```py
235
-
>>> ds['train'].set_transform(transforms)
236
-
```
237
-
238
-
You can verify the transform works by visualizing the 10th example:
239
-
240
-
```py
241
-
>>> example = ds['train'][10]
242
-
>>> to_pil_image(
243
-
... draw_bounding_boxes(
244
-
... example['image'],
245
-
... box_convert(example['bbox'], 'xywh', 'xyxy'),
246
-
...colors='red',
247
-
...labels=[categories.int2str(x) for x in example['category']]
0 commit comments