问题描述
我需要编写一个文件,其中包含我训练的卷积神经网络的数据测试结果.该数据包括语音数据收集.文件格式需要为文件名,预测",但是我很难提取文件名.我这样加载数据:
I need to write a file with the result of the data test of a Convolutional Neural Network that I trained. The data include speech data collection. The file format needs to be "file name, prediction", but I am having a hard time to extract the file name. I load the data like this:
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
TEST_DATA_PATH = ...
trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
test_dataset = torchvision.datasets.MNIST(
root=TEST_DATA_PATH,
train=False,
transform=trans,
download=True
)
test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False)
并且我正在尝试按以下方式写入文件:
and I am trying to write to the file as follows:
f = open("test_y", "w")
with torch.no_grad():
for i, (images, labels) in enumerate(test_loader, 0):
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
file = os.listdir(TEST_DATA_PATH + "/all")[i]
format = file + ", " + str(predicted.item()) + '\n'
f.write(format)
f.close()
os.listdir(TESTH_DATA_PATH + "/all")[i]
的问题在于它与test_loader
的加载文件顺序不同步.我该怎么办?
The problem with os.listdir(TESTH_DATA_PATH + "/all")[i]
is that it is not synchronized with the loaded files order of test_loader
. What can I do?
推荐答案
通常情况下 DataLoader
可以从其中提供的数据集中为您提供批次.
In general case DataLoader
is there to provide you the batches from the Dataset(s) it has inside.
在出现单标签/多标签分类问题时提到了AS @Barriel,DataLoader
没有图像文件名,只有表示图像的张量以及类/标签.
AS @Barriel mentioned in case of single/multi-label classification problems, the DataLoader
doesn't have image file name, just the tensors representing the images , and the classes / labels.
但是,DataLoader
构造函数在加载对象时可能会占用很小的空间(与数据集一起,如果需要,您可以打包目标/标签和文件名),甚至是数据框
However, DataLoader
constructor when loading objects can take small things (together with the Dataset you may pack the targets/labels and the file names if you like) , even a dataframe
这样,DataLoader
可能会以某种方式满足您的需求.
This way, the DataLoader
may somehow grab that what you need.
这篇关于如何从DataLoader获取样本的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!