Debian Codegraph @

examples/docs_examples/transform_with_inds.py

raw
1 """
2 Example of defining a custom (image) transform using FFCV.
3 For tutorial, see https://docs.ffcv.io/ffcv_examples/transform_with_inds.html.
4
5 """
6 from dataclasses import replace
7 import time
8 from typing import Callable, Optional, Tuple
9 import numpy as np
10 import torchvision
11
12 from ffcv.fields import IntField, RGBImageField
13 from ffcv.fields.decoders import IntDecoder
14 from ffcv.loader import Loader, OrderOption
15 from ffcv.pipeline.compiler import Compiler
16 from ffcv.pipeline.operation import Operation, AllocationQuery
17 from ffcv.pipeline.state import State
18 from ffcv.transforms import ToTensor
19 from ffcv.writer import DatasetWriter
20
21
22 class CorruptFixedLabels(Operation):
23 def generate_code(self) -> Callable:
24 # dst will be None since we don't ask for an allocation
25 parallel_range = Compiler.get_iterator()
26 def corrupt_fixed(labs, _, inds):
27 for i in parallel_range(labs.shape[0]):
28 # Because the random seed is tied to the image index, the
29 # same images will be corrupted every epoch:
30 np.random.seed(inds[i])
31 if np.random.rand() < 0.2:
32 # They will also be corrupted to a deterministic label:
33 labs[i] = np.random.randint(low=0, high=10)
34 return labs
35
36 corrupt_fixed.is_parallel = True
37 corrupt_fixed.with_indices = True
38 return corrupt_fixed
39
40 def declare_state_and_memory(self, previous_state: State) -> Tuple[State, Optional[AllocationQuery]]:
41 # No updates to state or extra memory necessary!
42 return previous_state, None
43
44 # Step 1: Create an FFCV-compatible CIFAR-10 dataset
45 ds = torchvision.datasets.CIFAR10('/tmp', train=True, download=True)
46 writer = DatasetWriter('/tmp/cifar.beton', {
47 'image': RGBImageField(),
48 'label': IntField()
49 })
50 writer.from_indexed_dataset(ds)
51
52 # Step 2: Create data loaders
53 BATCH_SIZE = 512
54 label_pipelines = {
55 'with': [IntDecoder(), CorruptFixedLabels(), ToTensor()],
56 'without': [IntDecoder(), ToTensor()]
57 }
58
59 for name, pipeline in label_pipelines.items():
60 # Use SEQUENTIAL ordering to compare labels.
61 loader = Loader(f'/tmp/cifar.beton', batch_size=BATCH_SIZE,
62 num_workers=8, order=OrderOption.SEQUENTIAL,
63 drop_last=True, pipelines={'label': pipeline})
64
65 # First epoch includes compilation time
66 for ims, labs in loader: pass
67 start_time = time.time()
68 for ep in range(20):
69 for i, (ims, labs) in enumerate(loader):
70 if i == 0: # Inspect first batch
71 print(f'> Labels (epoch {ep:2}): {labs[:40,0].tolist()}')
72 print(f'Method: {name} | Shape: {ims.shape} | Time per epoch: {(time.time() - start_time) / 100:.5f}s')