in src/data_loader.py [0:0]
def __getitem__(self, index):
"""Returns one data pair (image and caption)."""
sample = self.dataset[self.ids[index]]
img_id = sample['id']
paths = sample['images'][0:self.maxnumims]
idx = index
labels = self.dataset[self.ids[idx]]['ingredients']
true_ingr_idxs = []
for i in range(len(labels)):
true_ingr_idxs.append(self.ingrs_vocab(labels[i]))
true_ingr_idxs = list(set(true_ingr_idxs))
if self.shuffle:
np.random.shuffle(true_ingr_idxs)
if self.include_eos:
true_ingr_idxs.append(self.ingrs_vocab('<end>'))
if len(paths) == 0:
path = None
image_input = torch.zeros((3, 224, 224))
else:
if self.split == 'train':
img_idx = np.random.randint(0, len(paths))
else:
img_idx = 0
path = paths[img_idx]
impath = os.path.join(self.root, path[0], path[1], path[2], path[3], path)
if self.use_lmdb:
try:
with self.image_file.begin(write=False) as txn:
image = txn.get(path.encode())
image = np.fromstring(image, dtype=np.uint8)
image = np.reshape(image, (256, 256, 3))
image = Image.fromarray(image.astype('uint8'), 'RGB')
except:
print("Image id not found in lmdb. Loading jpeg file...")
image = Image.open(impath).convert('RGB')
else:
image = Image.open(impath).convert('RGB')
if self.transform is not None:
image = self.transform(image)
image_input = image
return image_input, true_ingr_idxs