MCPcopy Create free account
hub / github.com/FreeformRobotics/OTS / TrainDataset

Class TrainDataset

dataset.py:70–203  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68
69
70class TrainDataset(BaseDataset):
71 def __init__(self, root_dataset, odgt, opt, batch_per_gpu=1, **kwargs):
72 super(TrainDataset, self).__init__(odgt, opt, **kwargs)
73 self.root_dataset = root_dataset
74 # down sampling rate of segm labe
75 self.segm_downsampling_rate = opt.segm_downsampling_rate
76 self.batch_per_gpu = batch_per_gpu
77
78 # classify images into two classes: 1. h > w and 2. h <= w
79 self.batch_record_list = [[], []]
80
81 # override dataset length when trainig with batch_per_gpu > 1
82 self.cur_idx = 0
83 self.if_shuffled = False
84
85 def _get_sub_batch(self):
86 while True:
87 # get a sample record
88 this_sample = self.list_sample[self.cur_idx]
89 if this_sample['height'] > this_sample['width']:
90 self.batch_record_list[0].append(this_sample) # h > w, go to 1st class
91 else:
92 self.batch_record_list[1].append(this_sample) # h <= w, go to 2nd class
93
94 # update current sample pointer
95 self.cur_idx += 1
96 if self.cur_idx >= self.num_sample:
97 self.cur_idx = 0
98 np.random.shuffle(self.list_sample)
99
100 if len(self.batch_record_list[0]) == self.batch_per_gpu:
101 batch_records = self.batch_record_list[0]
102 self.batch_record_list[0] = []
103 break
104 elif len(self.batch_record_list[1]) == self.batch_per_gpu:
105 batch_records = self.batch_record_list[1]
106 self.batch_record_list[1] = []
107 break
108 return batch_records
109
110 def __getitem__(self, index):
111 # NOTE: random shuffle for the first time. shuffle in __init__ is useless
112 if not self.if_shuffled:
113 np.random.seed(index)
114 np.random.shuffle(self.list_sample)
115 self.if_shuffled = True
116
117 # get sub-batch candidates
118 batch_records = self._get_sub_batch()
119
120 # resize all images' short edges to the chosen size
121 if isinstance(self.imgSizes, list) or isinstance(self.imgSizes, tuple):
122 this_short_size = np.random.choice(self.imgSizes)
123 else:
124 this_short_size = self.imgSizes
125
126 # calculate the BATCH's height and width
127 # since we concat more than one samples, the batch's h and w shall be larger than EACH sample

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected