0it 0000 itsTraceback most recent call last File mntdisk1lhcodeCuPLclassify_imagenetpy line 66 in module zeroshot_weights_base = zeroshot_classifierall_imagesidx_to_label all_imagesidx_to_text ima
This error occurs because the zeroshot_weights list is empty when trying to stack the tensors using torch.stack function.
To fix this error, you need to make sure that the zeroshot_weights list is not empty before calling torch.stack. You can add a check to verify if the list is empty and handle it accordingly.
Here's an example of how you can modify the code to handle the empty list case:
def zeroshot_classifier(idx_to_label, idx_to_text, templates):
zeroshot_weights = []
for label_idx in range(len(idx_to_label)):
label = idx_to_label[label_idx]
text = idx_to_text[label_idx]
if text in templates:
zeroshot_weights.append(templates[text])
else:
# Handle the case when the template is not available
# You can either skip this label or assign a default value
# For example, you can assign a tensor of zeros
zeroshot_weights.append(torch.zeros_like(templates['default_template']))
# Check if the zeroshot_weights list is empty
if not zeroshot_weights:
raise ValueError("No zeroshot weights found")
# Stack the tensors using torch.stack
zeroshot_weights = torch.stack(zeroshot_weights, dim=1).cuda()
return zeroshot_weights
Make sure to adjust the code according to your specific requirements and available templates.
原文地址: http://www.cveoy.top/t/topic/jaUC 著作权归作者所有。请勿转载和采集!