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

SampleGeneratorImage.py 2.1 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  1. import traceback
  2. import cv2
  3. import numpy as np
  4. from core.joblib import SubprocessGenerator, ThisThreadGenerator
  5. from samplelib import (SampleGeneratorBase, SampleLoader, SampleProcessor,
  6. SampleType)
  7. class SampleGeneratorImage(SampleGeneratorBase):
  8. def __init__ (self, samples_path, debug, batch_size, sample_process_options=SampleProcessor.Options(), output_sample_types=[], raise_on_no_data=True, **kwargs):
  9. super().__init__(debug, batch_size)
  10. self.initialized = False
  11. self.sample_process_options = sample_process_options
  12. self.output_sample_types = output_sample_types
  13. samples = SampleLoader.load (SampleType.IMAGE, samples_path)
  14. if len(samples) == 0:
  15. if raise_on_no_data:
  16. raise ValueError('No training data provided.')
  17. return
  18. self.generators = [ThisThreadGenerator ( self.batch_func, samples )] if self.debug else \
  19. [SubprocessGenerator ( self.batch_func, samples )]
  20. self.generator_counter = -1
  21. self.initialized = True
  22. def __iter__(self):
  23. return self
  24. def __next__(self):
  25. self.generator_counter += 1
  26. generator = self.generators[self.generator_counter % len(self.generators) ]
  27. return next(generator)
  28. def batch_func(self, samples):
  29. samples_len = len(samples)
  30. idxs = [ *range(samples_len) ]
  31. shuffle_idxs = []
  32. while True:
  33. batches = None
  34. for n_batch in range(self.batch_size):
  35. if len(shuffle_idxs) == 0:
  36. shuffle_idxs = idxs.copy()
  37. np.random.shuffle (shuffle_idxs)
  38. idx = shuffle_idxs.pop()
  39. sample = samples[idx]
  40. x, = SampleProcessor.process ([sample], self.sample_process_options, self.output_sample_types, self.debug)
  41. if batches is None:
  42. batches = [ [] for _ in range(len(x)) ]
  43. for i in range(len(x)):
  44. batches[i].append ( x[i] )
  45. yield [ np.array(batch) for batch in batches]
Tip!

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

Comments

Loading...