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: docs/source/image_process.mdx
+165-1Lines changed: 165 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,11 @@ Both parameter values default to 1000, which can be expensive if you are storing
39
39
40
40
## Data augmentation
41
41
42
-
🤗 Datasets can apply data augmentations from any library or package to your dataset. This guide will use the transforms from [torchvision](https://pytorch.org/vision/stable/transforms.html).
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).
43
47
44
48
<Tip>
45
49
@@ -88,3 +92,163 @@ Now you can take a look at the augmented image by indexing into the `pixel_value
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