MCPcopy Create free account
hub / github.com/SooLab/CGFormer / __getitem__

Method __getitem__

utils/dataset_open.py:128–176  ·  view source on GitHub ↗
(self, index)

Source from the content-addressed store, hash-verified

126 return self.length
127
128 def __getitem__(self, index):
129 # Delay loading LMDB data until after initialization: https://github.com/chainer/chainermn/issues/129
130 if self.env is None:
131 self._init_db()
132 env = self.env
133 with env.begin(write=False) as txn:
134 byteflow = txn.get(self.keys[index])
135 ref = loads_pyarrow(byteflow)
136 # img
137 ori_img = cv2.imdecode(np.frombuffer(ref['img'], np.uint8),
138 cv2.IMREAD_COLOR)
139 img = cv2.cvtColor(ori_img, cv2.COLOR_BGR2RGB)
140 img_size = img.shape[:2]
141 # mask
142 seg_id = ref['seg_id']
143 mask_dir = os.path.join(self.mask_dir, str(seg_id) + '.png')
144 # sentences
145 idx = np.random.choice(ref['num_sents'])
146 sents = ref['sents']
147 # transform
148 # mask transform
149 mask = cv2.imdecode(np.frombuffer(ref['mask'], np.uint8),
150 cv2.IMREAD_GRAYSCALE)
151 mask = mask / 255.
152 if self.mode == 'train':
153 sent = sents[idx]
154 # sentence -> vector
155 img, mask, sent = self.convert(img, mask, sent, inference=False)
156 word_vec = tokenize(sent, self.word_length, True).squeeze(0)
157 pad_mask = (word_vec != 0).float()
158 return img, word_vec, mask, pad_mask
159 elif self.mode == 'val':
160 # sentence -> vector
161 sent = sents[-1]
162 word_vec = tokenize(sent, self.word_length, True).squeeze(0)
163 pad_mask = (word_vec != 0).float()
164 img, mask, sent = self.convert(img, mask, sent, inference=False)
165 return img, word_vec, mask, pad_mask
166 else:
167 # sentence -> vector
168 word_vecs = []
169 pad_masks = []
170 for sent in sents:
171 word_vec = tokenize(sent, self.word_length, True).squeeze(0)
172 word_vecs.append(word_vec)
173 pad_mask = (word_vec != 0).float()
174 pad_masks.append(pad_mask)
175 img, mask, sent = self.convert(img, mask, sent, inference=True)
176 return ori_img, img, word_vecs, mask, pad_masks, seg_id, sents
177
178 def convert(self, img, mask, sent, inference=False):
179 img = Image.fromarray(np.uint8(img))

Callers

nothing calls this directly

Calls 4

_init_dbMethod · 0.95
convertMethod · 0.95
loads_pyarrowFunction · 0.70
tokenizeFunction · 0.70

Tested by

no test coverage detected