PyTorch训练中Dataset多线程加载数据,而不是在DataLoader

self.batches = self._get_len_batches(self.percentage)

def _get_len_batches(self, percentage):

“”"

Description:

  • you could control how many batches you want to use for training or validating

indices sort, so that could keep the batches got in order from originla batches

Parameters:

  • percentage: float, range [0, 1]

Return

  • numpy array of the new bags

“”"

batch_num = int(len(self.batches) * percentage)

indices = random.sample(list(range(len(self.batches))), batch_num)

indices.sort()

new_batches = np.array(self.batches, dtype=‘object’)[indices]

return new_batches

def _create_batches(self, ):

if self.shuffle:

random.shuffle(self.images)

batches = []

ranges = list(range(0, len(self.images), self.batch_num))

for i in ranges[:-1]:

batch = self.images[i:i + self.batch_num]

batches.append(batch)

== Drop last ===============================================

last_batch = self.images[ranges[-1]:]

if len(last_batch) == self.batch_num:

batches.append(last_batch)

elif self.drop_last:

pass

else:

batches.append(last_batch)

return batches

def getitem(self, index):

batch = self.batches[index]

== Stack all images, become a 4 dimensional tensor ===============

if self.multi_load:

batch_images = self._multi_loader(batch)

else:

batch_images = []

for image in batch:

img = self._load_transform(image)

batch_images.append(img)

batch_images_tensor = torch.stack(batch_images, dim=0)

return batch_images_tensor

def _load_transform(self, tile):

img = self.loader(tile)

if self.transform is not None:

image_np = np.array(img)

augmented = self.transform(image=image_np)

img = augmented[‘image’]

return img

def _multi_loader(self, tiles):

images = []

executor = ThreadPoolExecutor(max_workers=self.num_workers)

results = executor.map(self._load_transform, tiles)

executor.shutdown()

for result in results:

images.append(result)

return images

def len

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值