some-noon-7401
03/07/2023, 8:06 AMdef __getitem__(self, index):
with S3() as s3:
byte_data = s3.get(self.data.iloc[index]['url']).blob
if byte_data is None:
return self.__getitem__((index + 1) % len(self.data))
img = Image.open(BytesIO(byte_data)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
label = self.target_transform(self.data.iloc[index]['label'])
else:
label = self.data.iloc[index]['label']
return img, label
Is there anything which I might have missed out in the code which is slowing the loading process?some-noon-7401
03/07/2023, 8:07 AMdef __init__(self,
root,
split='train',
transform=None,
target_transform=None,
batch_size=64):
self.root = root
self.transform = transform
self.target_transform = target_transform
self.split = split
self.batch_size = batch_size
self.data = pd.read_csv(self.root)
self.target_transform = target_transform
self.data = pd.read_csv(self.root).drop(columns=['geometry'])
self.data['count'] = self.data.groupby('url')['url'].transform('count')
self.data = self.data.drop_duplicates(subset=['url'])
self.data['label'] = self.data['count'].apply(
lambda x: 0 if x == 0 else 1 if x >= 1 and x < 5 else 2)
self.data = self.data.drop(columns=['count'])
if split == 'train':
self.data = self.data[self.data['split'] == 'train']
elif split == 'val':
self.data = self.data[self.data['split'] == 'val']victorious-lawyer-58417
03/07/2023, 5:40 PMs3.get_many is likely to be faster than retrieving them sequentially with s3.get.
It's possible that a batch of 64 images is too small in the IO point of view, although it might make sense for the model. If you really want to optimize your data loader, you can use larger IO batches, e.g. 256 images, to make get_many go faster, and then feed them as 4*64 batches to your modelvictorious-lawyer-58417
03/07/2023, 5:42 PMfrom metaflow import profile
with profile('load image'):
...your code here...
to get more accurate timingssome-noon-7401
03/07/2023, 7:56 PMget_many inside my getitem? Like how can I specify multiple keys?
I didn't understand your second answer regarding profile, could you please redirect me to any related documentation?some-noon-7401
03/07/2023, 8:30 PMget_many to read all keys inside the folder of S3 bucket, I am getting the following error when I am trying to access the item using its index.
with S3() as s3:
self.images = s3.get_many(self.data['url'].tolist())
Modified get item:
def __getitem__(self, index):
byte_data = self.images[index].blob
if byte_data is None:
return self.__getitem__((index + 1) % len(self.data))
img = Image.open(BytesIO(byte_data)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
label = self.target_transform(self.data.iloc[index]['label'])
else:
label = self.data.iloc[index]['label']
return img, labelsome-noon-7401
03/07/2023, 8:32 PMsome-noon-7401
03/07/2023, 9:19 PM