简体   繁体   English

使用 pytorch 数据加载器转储图像数据并加载

[英]Dumping Image data and load using pytorch dataloader

I want to dump the data so that I can load it back for training my model.我想转储数据,以便可以将其加载回来以训练我的 model。

My code snipped for dumping the data:我的代码被截断以转储数据:

for batch_idx, (image, label) in enumerate(dataloader):
    image, label = image.to(device), label.to(device)
    perturbed_image = attack.perturb(image, label)
    
    #---------- Classifier ----------
    predict_A = classifier(perturbed_image)
    pred_label = torch.max(predict_A.data, 1)[1]
    
    if pred_label != label:
        adv_data.append( (perturbed_image.to("cpu"), label.to("cpu")) )

Is there any other way I can dump it correctly so as to load it in the torch.utils.data.DataLoader .有没有其他方法可以正确转储它以便将其加载到torch.utils.data.DataLoader中。

The most straight forward approach would be to use torch.save to save the actual tensors of perturbed_image and label as binary files, and then use a custom Dataset .最直接的方法是使用torch.saveperturbed_imagelabel的实际张量保存为二进制文件,然后使用自定义Dataset Note that the saved tensors are not single image/label, but rather batches of images/labels.请注意,保存的张量不是单个图像/标签,而是成批的图像/标签。 Your new custom Dataset should account for it.您的新自定义Dataset应该考虑到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM