dataset = MyDataset(ds) dataloader = DataLoader(dataset, batchsize=16) batchidx = 0 dir…
- User
dataset = MyDataset(ds) dataloader = DataLoader(dataset, batch_size=16) batch_idx = 0 dir = 'H:/pickapic/'
from tqdm import tqdm for items in tqdm(dataloader): image1 = items['image1'].to('cuda') image2 = items['image2'].to('cuda')
label1 = items['label1'] label2 = items['label2'] uid1 = items['uid1'] uid2 = items['uid2'] with torch.no_grad(): image1 = get_features(image1) image2 = get_features(image2) tensor_to_save = torch.stack((image1, image2)) #torch.save(tensor_to_save.cpu().detach(), dir + f'batch_{batch_idx}_features.pt') data = { 'ids': [uid1, uid2], 'labels': [label1, label2], 'tensors': tensor_to_save.cpu().detach() } #print(metadata) torch.save(data, dir + f'batch_{batch_idx}.pt') batch_idx += 1based on that code, make code that takes directory and merged all data files into one but so that is has this structure:
[ N, 2, 768, 1]
N - count 2- 1/2 items 768- feature size 1 - label ( -1 or 1)
- Assistant
To merge all the data files in the specified directory into a single tensor with the desired structure [N, 2, 768, 1], you can follow these steps. The final tensor will contain concatenated features and corresponding labels: