Skip to content

Instantly share code, notes, and snippets.

@szagoruyko
Created July 15, 2016 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szagoruyko/569c2b713a2c40629ecbe75fb3c9d980 to your computer and use it in GitHub Desktop.
Save szagoruyko/569c2b713a2c40629ecbe75fb3c9d980 to your computer and use it in GitHub Desktop.
-- 2014 Sergey Zagoruyko, ENPC-UPEM, IMAGINE, Paris
-- Creates a t7 file from local image patches dataset
-- http://www.cs.ubc.ca/~mbrown/patchdata/patchdata.html
-- usage:
-- th create_dataset_file.lua *dataset_name*
-- where dataset_name is notredame, liberty or yosemite
-- which is also a folder with the same name, containing:
-- info.txt, m_50_500000_500000_0.txt and 1024x1024 bmp images
-- saves data.t7 file in the corresponding folder
require 'sys'
local cv = require 'cv'
require 'cv.imgcodecs'
if not arg[1] then
error'dataset name not provided'
end
local impath = arg[1]
-- read matches
local function readMatchesFile(filename)
local matches = {}
local nonmatches = {}
for line in io.lines(filename) do
local ar = sys.split(line, ' ')
local pair = {ar[1], ar[4]}
table.insert(ar[2] == ar[5] and matches or nonmatches, pair)
end
return {
matches = torch.LongTensor(matches),
nonmatches = torch.LongTensor(nonmatches),
}
end
local match_data = {}
for name in paths.files(impath, 'm50_') do
print(name..' done')
match_data[name] = readMatchesFile(paths.concat(impath, name))
end
-- read info file
local info_filename = paths.concat(impath, 'info.txt')
local info = {}
for line in io.lines(info_filename) do
table.insert(info, sys.split(line, ' ')[1])
end
-- read 1024x1024 bmp files from sorted list of images
-- into a big byte tensor
local image_list = {}
for k,v in paths.files(impath, 'bmp') do table.insert(image_list, k) end
table.sort(image_list)
local patches = torch.ByteTensor(256*#image_list,64,64)
local counter = 1
for k,imname in ipairs(image_list) do
xlua.progress(k, #image_list)
local im = cv.imread{paths.concat(impath, imname), cv.IMREAD_GRAYSCALE}
for i=1,1024,64 do
for j=1,1024,64 do
patches[counter]:copy(im:narrow(1,i,64):narrow(2,j,64))
counter = counter + 1
end
end
end
patches = patches:narrow(1,1,#info)
local patches_mean = patches:float():view(#info,-1):mean(2)
torch.save(impath..'/data.t7', {
patches = patches,
patches_mean = patches_mean,
info = torch.LongTensor(info),
match_data,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment