Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

custom_data_loading.py 1.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  1. from fastai.vision.all import \
  2. DataLoaders, \
  3. delegates, \
  4. DataBlock, \
  5. ImageBlock, \
  6. PILImage, \
  7. PILImageBW, \
  8. RandomSplitter, \
  9. Path, \
  10. get_files
  11. class ImageImageDataLoaders(DataLoaders):
  12. """Basic wrapper around several `DataLoader`s with factory methods for Image to Image problems"""
  13. @classmethod
  14. @delegates(DataLoaders.from_dblock)
  15. def from_label_func(cls, path, filenames, label_func, is_test, valid_pct=0.2, seed=None, item_transforms=None,
  16. batch_transforms=None, **kwargs):
  17. """Create from list of `fnames` in `path`s with `label_func`."""
  18. datablock = DataBlock(blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
  19. get_y=label_func,
  20. item_tfms=item_transforms,
  21. batch_tfms=batch_transforms)
  22. if not is_test:
  23. datablock.splitter = RandomSplitter(valid_pct, seed=seed)
  24. res = cls.from_dblock(datablock, filenames, path=path, **kwargs)
  25. return res
  26. def get_y_fn(x):
  27. y = str(x.absolute()).replace('.jpg', '_depth.png')
  28. y = Path(y)
  29. return y
  30. def create_data(data_path, is_test=False):
  31. filenames = get_files(data_path, extensions='.jpg')
  32. if len(filenames) == 0:
  33. raise ValueError("Could not find any files in the given path")
  34. dataset = ImageImageDataLoaders.from_label_func(data_path,
  35. is_test=is_test,
  36. seed=42,
  37. bs=4, num_workers=0,
  38. filenames=filenames,
  39. label_func=get_y_fn)
  40. return dataset
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...