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:
two tensors [N,768] one tensor [N] with label ( -1 or 1)
- Assistant
To achieve this, you'll need to load each of the
.ptfiles in the specified directory, extract and concatenate the necessary data, and then save the combined data as described. Here's a code snippet to perform this task: - User
make it skip where one of labels is 0.5
- Assistant
To modify the code such that it skips any data where one of the labels is , you can add a condition to filter out such entries. Here’s how you can integrate this change into the existing code: