2015-09-24 06:19:30 +08:00
|
|
|
local ffi = require 'ffi'
|
|
|
|
|
|
|
|
local batchNumber, nImgs = 0
|
|
|
|
|
2015-11-10 00:12:10 +08:00
|
|
|
torch.setdefaulttensortype('torch.FloatTensor')
|
|
|
|
|
2015-09-24 06:19:30 +08:00
|
|
|
function batchRepresent()
|
2015-09-24 06:46:38 +08:00
|
|
|
local loadSize = {3, opt.imgDim, opt.imgDim}
|
2016-01-11 21:25:04 +08:00
|
|
|
print(opt.data)
|
|
|
|
local cacheFile = paths.concat(opt.data, 'cache.t7')
|
|
|
|
print('cache lotation: ', cacheFile)
|
2016-01-11 21:47:34 +08:00
|
|
|
local dumpLoader
|
2016-01-11 21:25:04 +08:00
|
|
|
if paths.filep(cacheFile) then
|
|
|
|
print('Loading metadata from cache.')
|
|
|
|
print('If your dataset has changed, delete the cache file.')
|
|
|
|
dumpLoader = torch.load(cacheFile)
|
|
|
|
else
|
|
|
|
print('Creating metadata for cache.')
|
|
|
|
dumpLoader = dataLoader{
|
|
|
|
paths = {opt.data},
|
|
|
|
loadSize = loadSize,
|
|
|
|
sampleSize = loadSize,
|
|
|
|
split = 0,
|
|
|
|
verbose = true
|
|
|
|
}
|
|
|
|
torch.save(cacheFile, dumpLoader)
|
|
|
|
end
|
|
|
|
collectgarbage()
|
2015-09-24 06:19:30 +08:00
|
|
|
nImgs = dumpLoader:sizeTest()
|
|
|
|
print('nImgs: ', nImgs)
|
|
|
|
assert(nImgs > 0, "Failed to get nImgs")
|
|
|
|
|
|
|
|
batchNumber = 0
|
|
|
|
|
|
|
|
for i=1,math.ceil(nImgs/opt.batchSize) do
|
|
|
|
local indexStart = (i-1) * opt.batchSize + 1
|
|
|
|
local indexEnd = math.min(nImgs, indexStart + opt.batchSize - 1)
|
2016-03-16 23:17:15 +08:00
|
|
|
local batchSz = indexEnd-indexStart+1
|
2015-09-24 06:19:30 +08:00
|
|
|
local inputs, labels = dumpLoader:get(indexStart, indexEnd)
|
|
|
|
local paths = {}
|
2015-12-27 22:09:14 +08:00
|
|
|
for j=indexStart,indexEnd do
|
2016-01-13 06:29:34 +08:00
|
|
|
table.insert(paths,
|
|
|
|
ffi.string(dumpLoader.imagePath[dumpLoader.testIndices[j]]:data()))
|
2015-09-24 06:19:30 +08:00
|
|
|
end
|
2016-03-16 23:17:15 +08:00
|
|
|
repBatch(paths, inputs, labels, batchSz)
|
2015-09-24 06:19:30 +08:00
|
|
|
if i % 5 == 0 then
|
|
|
|
collectgarbage()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if opt.cuda then
|
|
|
|
cutorch.synchronize()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 23:17:15 +08:00
|
|
|
function repBatch(paths, inputs, labels, batchSz)
|
|
|
|
batchNumber = batchNumber + batchSz
|
2015-09-24 06:19:30 +08:00
|
|
|
|
|
|
|
if opt.cuda then
|
|
|
|
inputs = inputs:cuda()
|
|
|
|
end
|
|
|
|
local embeddings = model:forward(inputs):float()
|
2015-09-30 06:02:01 +08:00
|
|
|
if opt.cuda then
|
|
|
|
cutorch.synchronize()
|
|
|
|
end
|
2015-09-24 06:19:30 +08:00
|
|
|
|
2016-03-16 23:17:15 +08:00
|
|
|
if batchSz == 1 then
|
|
|
|
embeddings = embeddings:reshape(1, embeddings:size(1))
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1,batchSz do
|
2015-09-24 06:19:30 +08:00
|
|
|
labelsCSV:write({labels[i], paths[i]})
|
|
|
|
repsCSV:write(embeddings[i]:totable())
|
|
|
|
end
|
|
|
|
|
|
|
|
print(('Represent: %d/%d'):format(batchNumber, nImgs))
|
|
|
|
end
|