Skip to content

Commit 9e4da2e

Browse files
committed
Update use_dataset tutorial to integrate Albumentations for data augmentation
- Replaced torchvision transforms with Albumentations for image augmentation. - Renumbered sections for clarity and updated descriptions accordingly. - Emphasized key points for using Albumentations with 🤗 Datasets.
1 parent 364a597 commit 9e4da2e

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

docs/source/use_dataset.mdx

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,37 +175,19 @@ Most image models expect the image to be in the RGB mode. The Beans images are a
175175
>>> dataset = dataset.cast_column("image", Image(mode="RGB"))
176176
```
177177

178-
**3**. Now, you can apply some transforms to the image. Feel free to take a look at the [various transforms available](https://docs.pytorch.org/vision/stable/transforms.html#v2-api-reference-recommended) in torchvision and choose one you'd like to experiment with. This example applies a transform that randomly rotates the image:
179-
180-
```py
181-
>>> from torchvision.transforms import RandomRotation
182-
183-
>>> rotate = RandomRotation(degrees=(0, 90))
184-
>>> def transforms(examples):
185-
... examples["pixel_values"] = [rotate(image) for image in examples["image"]]
186-
... return examples
187-
```
188-
189-
**4**. Use the [`~Dataset.set_transform`] function to apply the transform on-the-fly. When you index into the image `pixel_values`, the transform is applied, and your image gets rotated.
190-
191-
```py
192-
>>> dataset.set_transform(transforms)
193-
>>> dataset[0]["pixel_values"]
194-
```
195-
196-
**5**. The dataset is now ready for training with your machine learning framework!
178+
**3**. Now let's apply data augmentations to your images. 🤗 Datasets works with any augmentation library, and in this example we'll use Albumentations.
197179

198180
### Using Albumentations
199181

200-
[Albumentations](https://albumentations.ai) is another popular image augmentation library that provides a [rich set of transforms](https://albumentations.ai/docs/reference/supported-targets-by-transform/) including spatial-level transforms, pixel-level transforms, and mixing-level transforms. When running on CPU, which is typical for transformers pipelines, Albumentations is [faster than torchvision](https://albumentations.ai/docs/benchmarks/image-benchmarks/).
182+
[Albumentations](https://albumentations.ai) is a popular image augmentation library that provides a [rich set of transforms](https://albumentations.ai/docs/reference/supported-targets-by-transform/) including spatial-level transforms, pixel-level transforms, and mixing-level transforms. When running on CPU, which is typical for transformers pipelines, Albumentations is [faster than torchvision](https://albumentations.ai/docs/benchmarks/image-benchmarks/).
201183

202-
**1**. Install Albumentations:
184+
Install Albumentations:
203185

204186
```bash
205187
pip install albumentations
206188
```
207189

208-
**2**. Create a typical augmentation pipeline with Albumentations:
190+
**4**. Create a typical augmentation pipeline with Albumentations:
209191

210192
```py
211193
>>> import albumentations as A
@@ -219,7 +201,7 @@ pip install albumentations
219201
... ])
220202
```
221203

222-
**3**. Since 🤗 Datasets uses PIL images but Albumentations expects OpenCV format (numpy arrays), you need to convert between formats:
204+
**5**. Since 🤗 Datasets uses PIL images but Albumentations expects OpenCV format (numpy arrays), you need to convert between formats:
223205

224206
```py
225207
>>> def albumentations_transforms(examples):
@@ -240,14 +222,14 @@ pip install albumentations
240222
... return examples
241223
```
242224

243-
**4**. Apply the transform using [`~Dataset.set_transform`]:
225+
**6**. Apply the transform using [`~Dataset.set_transform`]:
244226

245227
```py
246228
>>> dataset.set_transform(albumentations_transforms)
247229
>>> dataset[0]["pixel_values"]
248230
```
249231

250-
The key differences when using Albumentations:
232+
**Key points when using Albumentations with 🤗 Datasets:**
251233
- Convert PIL images to numpy arrays before applying transforms
252234
- Albumentations returns a dictionary with the transformed image under the "image" key
253235
- Convert the result back to PIL format after transformation

0 commit comments

Comments
 (0)