小智 5
数据集“ .t7”是标记张量的表。例如以下lua代码:
if (not paths.filep("cifar10torchsmall.zip")) then
os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')
os.execute('unzip cifar10torchsmall.zip')
end
Readed_t7 = torch.load('cifar10-train.t7')
print(Readed_t7)
Run Code Online (Sandbox Code Playgroud)
将通过itorch返回:
{
data : ByteTensor - size: 10000x3x32x32
label : ByteTensor - size: 10000
}
Run Code Online (Sandbox Code Playgroud)
这意味着该文件包含两个ByteTensor的表,其中一个标记为“数据”,另一个标记为“标签”。
要回答您的问题,您应该首先阅读图像(例如使用torchx:https : //github.com/nicholas-leonard/torchx/blob/master/README.md),然后将其放入带有张量标签的表格中。以下代码只是为您提供帮助的草稿。它考虑以下情况:有两个类,所有图像都在同一文件夹中,并通过这些类排序。
require 'torchx';
--Read all your dataset (the chosen extension is png)
files = paths.indexdir("/Path/to/your/images/", 'png', true)
data1 = {}
for i=1,files:size() do
local img1 = image.load(files:filename(i),3)
table.insert(data1, img1)
end
--Create the table of label according to
label1 = {}
for i=1, #data1 do
if i <= number_of_images_of_the_first_class then
label1[i] = 1
else
label1[i] = 2
end
end
--Reshape the tables to Tensors
label = torch.Tensor(label1)
data = torch.Tensor(#data1,3,16,16)
for i=1, #data1 do
data[i] = data1[i]
end
--Create the table to save
Data_to_Write = { data = data, label = label }
--Save the table in the /tmp
torch.save("/tmp/Saved_Data.t7", Data_to_Write)
Run Code Online (Sandbox Code Playgroud)
应该可以编写出不太丑陋的代码,但这一步详细介绍了所有步骤,并适用于Torch 7和Jupyter 5.0.0。
希望能帮助到你。
问候