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.9 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
48
49
50
51
  1. import yaml
  2. from fastai.vision.all import \
  3. DataLoaders, \
  4. delegates, \
  5. DataBlock, \
  6. ImageBlock, \
  7. PILImage, \
  8. PILImageBW, \
  9. RandomSplitter, \
  10. Path, \
  11. get_files
  12. class ImageImageDataLoaders(DataLoaders):
  13. """Basic wrapper around several `DataLoader`s with factory methods for Image to Image problems"""
  14. @classmethod
  15. @delegates(DataLoaders.from_dblock)
  16. def from_label_func(cls, path, filenames, label_func, valid_pct=0.2, seed=None, item_transforms=None,
  17. batch_transforms=None, **kwargs):
  18. """Create from list of `fnames` in `path`s with `label_func`."""
  19. datablock = DataBlock(blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
  20. get_y=label_func,
  21. splitter=RandomSplitter(valid_pct, seed=seed),
  22. item_tfms=item_transforms,
  23. batch_tfms=batch_transforms)
  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):
  31. with open(r"./src/code/params.yml") as f:
  32. params = yaml.safe_load(f)
  33. filenames = get_files(data_path, extensions='.jpg')
  34. if len(filenames) == 0:
  35. raise ValueError("Could not find any files in the given path")
  36. dataset = ImageImageDataLoaders.from_label_func(data_path,
  37. seed=int(params['seed']),
  38. bs=int(params['batch_size']),
  39. num_workers=int(params['num_workers']),
  40. filenames=filenames,
  41. label_func=get_y_fn)
  42. return dataset
Tip!

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

Comments

Loading...