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
Next, let's see how to apply transformations to object detection datasets. For this we'll use [Albumentations](https://albumentations.ai/docs/), following their object detection [tutorial](https://albumentations.ai/docs/examples/example_bboxes/).
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, the [`cppe-5`](https://huggingface.co/datasets/cppe-5) dataset is used, which is a dataset for identifying medical personal protective equipments (PPEs) in the context of the COVID-19 pandemic.
107
+
108
+
You can 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 present on 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. But, 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.
Using `albumentations`, we can apply transforms that will effect the Image while also updating the bounding boxes accordingly. In this case, the image is resized to (480, 480), flipped horizontally, and brightened.
171
+
172
+
Note that `albumentations` is expecting the image to be in BGR format, not RGB, so we'll have to convert our image first before applying the transform.
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:
233
+
234
+
```py
235
+
>>> ds['train'].set_transform(transforms)
236
+
```
237
+
238
+
Verify the transform is working 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