gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
import os import json dir = "H:/gelbooruall/embeddings/" for path in os.listdir(dir):…
- User
import os import json
dir = "H:/gelbooru_all/embeddings/" for path in os.listdir(dir): path = dir+path if(os.path.isdir(path)): result = {} #paths = torch.load(f'{path}_path.pt') tensors = torch.load(f'{path}_tensor.pt').to('cuda') with torch.no_grad(): scores = model(tensors).cpu().detach()
#print(scores.shape) #breakmake it load all tensors into one effciently
- Assistant
To efficiently load all tensors and process them together, you'll want to gather all the tensors into a list and then concatenate them into a single tensor. Here's how you can achieve this:
import os import torch dir = "H:/gelbooru_all/embeddings/" all_tensors = [] # Gather all tensors for path in os.listdir(dir): path = os.path.join(dir, path) if os.path.isdir(path): tensor = torch.load(f'{path}_tensor.pt').to('cuda') # Load each tensor all_tensors.append(tensor) # Concatenate all tensors into one if all_tensors: combined_tensor = torch.cat(all_tensors, dim=0) # Adjust dim depending on your tensor shape # Process the combined tensor with torch.no_grad(): scores = model(combined_tensor).cpu().detach() # Print tensor shape to verify print(scores.shape)Key Points:
- Using
os.path.join: This ensures that your paths are correctly formatted, especially important on different operating systems.
- Using