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

SampleGeneratorImageTemporal.py 2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  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. '''
  8. output_sample_types = [
  9. [SampleProcessor.TypeFlags, size, (optional)random_sub_size] ,
  10. ...
  11. ]
  12. '''
  13. class SampleGeneratorImageTemporal(SampleGeneratorBase):
  14. def __init__ (self, samples_path, debug, batch_size, temporal_image_count, sample_process_options=SampleProcessor.Options(), output_sample_types=[], **kwargs):
  15. super().__init__(debug, batch_size)
  16. self.temporal_image_count = temporal_image_count
  17. self.sample_process_options = sample_process_options
  18. self.output_sample_types = output_sample_types
  19. self.samples = SampleLoader.load (SampleType.IMAGE, samples_path)
  20. self.generator_samples = [ self.samples ]
  21. self.generators = [iter_utils.ThisThreadGenerator ( self.batch_func, 0 )] if self.debug else \
  22. [iter_utils.SubprocessGenerator ( self.batch_func, 0 )]
  23. self.generator_counter = -1
  24. def __iter__(self):
  25. return self
  26. def __next__(self):
  27. self.generator_counter += 1
  28. generator = self.generators[self.generator_counter % len(self.generators) ]
  29. return next(generator)
  30. def batch_func(self, generator_id):
  31. samples = self.generator_samples[generator_id]
  32. samples_len = len(samples)
  33. if samples_len == 0:
  34. raise ValueError('No training data provided.')
  35. mult_max = 4
  36. samples_sub_len = samples_len - ( (self.temporal_image_count)*mult_max - (mult_max-1) )
  37. if samples_sub_len <= 0:
  38. raise ValueError('Not enough samples to fit temporal line.')
  39. shuffle_idxs = []
  40. while True:
  41. batches = None
  42. for n_batch in range(self.batch_size):
  43. if len(shuffle_idxs) == 0:
  44. shuffle_idxs = [ *range(samples_sub_len) ]
  45. np.random.shuffle (shuffle_idxs)
  46. idx = shuffle_idxs.pop()
  47. temporal_samples = []
  48. mult = np.random.randint(mult_max)+1
  49. for i in range( self.temporal_image_count ):
  50. sample = samples[ idx+i*mult ]
  51. try:
  52. temporal_samples += SampleProcessor.process ([sample], self.sample_process_options, self.output_sample_types, self.debug)[0]
  53. except:
  54. raise Exception ("Exception occured in sample %s. Error: %s" % (sample.filename, traceback.format_exc() ) )
  55. if batches is None:
  56. batches = [ [] for _ in range(len(temporal_samples)) ]
  57. for i in range(len(temporal_samples)):
  58. batches[i].append ( temporal_samples[i] )
  59. 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...