Skip to content

Instantly share code, notes, and snippets.

@vbalnt
Last active September 30, 2016 14:30
Show Gist options
  • Save vbalnt/30829bac54a2fa92acb9923f8b532657 to your computer and use it in GitHub Desktop.
Save vbalnt/30829bac54a2fa92acb9923f8b532657 to your computer and use it in GitHub Desktop.
example on how to save hpatches descriptors for hbench
require 'cutorch'
require 'xlua'
require 'trepl'
require 'cunn'
require 'cudnn'
require 'image'
require 'nn'
require 'torch'
require 'lfs'
-- hard-coded patch-image extention
ext = '.png'
-- descr name
descr_name = arg[1]
-- patch size for this descr
patch_size = arg[2]
-- folder with sequence-patches to describe (root folder)
patches_folder = arg[3]
-- load the description network
local net = torch.load('nets/'..descr_name..'.t7'):cuda()
stats = torch.load('nets/stats.liberty.t7')
-- make the dir with the results
lfs.mkdir(descr_name)
-- loop through the sequences
for seq in lfs.dir(patches_folder) do
if seq ~= "." and seq ~= ".." and seq ~= "test_set.txt" then
print("Computing descriptors for sequence: "..seq)
lfs.mkdir(descr_name.."/"..seq)
for file in paths.files(patches_folder..seq) do
if file:find(ext .. '$') then
-- hpatches image, compute the descriptors
local img = image.load(patches_folder..seq.."/"..file,1)
img:add(-stats.mi):div(stats.sigma)
-- split the patches-image to patches
patches = img:split(65,2)
for i,p in ipairs(patches) do
patches[i] = image.scale(p,patch_size,patch_size)
end
-- fix the input for the CNN
patches = torch.cat(patches,1)
N = patches:size(1)
patches = patches:view(N,1,patch_size,patch_size):cuda()
descrs = net:forward(patches)
-- save the output descriptors in csv file
file = io.open (descr_name.."/"..seq.."/"..string.gsub(file,'.png','')..".csv","w")
for i=1,descrs:size(1) do
for j=1,descrs:size(2) do
file:write(descrs[i][j])
if (j<descrs:size(2)) then
file:write(",")
end
end
file:write("\n")
end
file:close()
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment