Skip to content

Commit

Permalink
Improved memory efficiency
Browse files Browse the repository at this point in the history
Add “speed” and “memory” options.
  • Loading branch information
chuanli11 committed Jan 28, 2016
1 parent de9ba31 commit 14e076b
Show file tree
Hide file tree
Showing 10 changed files with 1,229 additions and 1,541 deletions.
1 change: 0 additions & 1 deletion data/result/freesyn/Readme.txt

This file was deleted.

1 change: 0 additions & 1 deletion data/result/trans/Readme.txt

This file was deleted.

14 changes: 11 additions & 3 deletions mylib/content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function ContentLoss:updateOutput(input)
if input:nElement() == self.target:nElement() then
self.loss = self.crit:forward(input, self.target) * self.strength
else
print(input:size())
print(self.target:size())
print('WARNING: Skipping content loss')
-- print(input:size())
-- print(self.target:size())
-- print('WARNING: Skipping content loss')
end
self.output = input
return self.output
Expand All @@ -31,4 +31,12 @@ function ContentLoss:updateGradInput(input, gradOutput)
self.gradInput:mul(self.strength)
self.gradInput:add(gradOutput)
return self.gradInput
end

function ContentLoss:update(other)
self.strength = other.strength
self.target = other.target
self.normalize = other.normalize
self.loss = other.loss
self.crit = other.crit
end
151 changes: 151 additions & 0 deletions mylib/helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
function computeMRF(input, size, stride, gpu)
local coord_x, coord_y = computegrid(input:size()[3], input:size()[2], size, stride)
local dim_1 = input:size()[1] * size * size
local dim_2 = coord_y:nElement()
local dim_3 = coord_x:nElement()
local t_feature_mrf = torch.Tensor(dim_2 * dim_3, input:size()[1], size, size)

if gpu >= 0 then
t_feature_mrf = t_feature_mrf:cuda()
end
local count = 1
for i_row = 1, dim_2 do
for i_col = 1, dim_3 do
t_feature_mrf[count] = input[{{1, input:size()[1]}, {coord_y[i_row], coord_y[i_row] + size - 1}, {coord_x[i_col], coord_x[i_col] + size - 1}}]
count = count + 1
end
end
local feature_mrf = t_feature_mrf:resize(dim_2 * dim_3, dim_1)

return t_feature_mrf, feature_mrf, coord_x, coord_y
end


function computeMRFnoTensor(input, size, stride, gpu)
local coord_x, coord_y = computegrid(input:size()[3], input:size()[2], size, stride)
local dim_1 = input:size()[1] * size * size
local dim_2 = coord_y:nElement()
local dim_3 = coord_x:nElement()
local t_feature_mrf = torch.Tensor(dim_2 * dim_3, input:size()[1], size, size)

if gpu >= 0 then
t_feature_mrf = t_feature_mrf:cuda()
end
local count = 1
for i_row = 1, dim_2 do
for i_col = 1, dim_3 do
t_feature_mrf[count] = input[{{1, input:size()[1]}, {coord_y[i_row], coord_y[i_row] + size - 1}, {coord_x[i_col], coord_x[i_col] + size - 1}}]
count = count + 1
end
end
local feature_mrf = t_feature_mrf:resize(dim_2 * dim_3, dim_1)

t_feature_mrf = nil
collectgarbage()
return feature_mrf, coord_x, coord_y
end


function drill_computeMRFfull(input, size, stride, gpu)
local coord_x, coord_y = computegrid(input:size()[3], input:size()[2], size, stride, 1)
local dim = torch.Tensor(2)
return coord_x, coord_y
end


function sampleMRFAndTensorfromLocation2(coord_x, coord_y, input, size, gpu)
local t_feature_mrf = torch.Tensor(coord_x:nElement(), input:size()[1], size, size)
for i_patch = 1, coord_x:nElement() do
t_feature_mrf[i_patch] = input[{{1, input:size()[1]}, {coord_y[i_patch], coord_y[i_patch] + size - 1}, {coord_x[i_patch], coord_x[i_patch] + size - 1}}]
end
local feature_mrf = t_feature_mrf:reshape(coord_x:nElement(), input:size()[1] * size * size)
return t_feature_mrf, feature_mrf
end


function computeBB(width, height, alpha)
local min_x, min_y, max_x, max_y
local x1 = 1
local y1 = 1
local x2 = width
local y2 = 1
local x3 = width
local y3 = height
local x4 = 1
local y4 = height
local x0 = width / 2
local y0 = height / 2

x1r = x0+(x1-x0)*math.cos(alpha)+(y1-y0)*math.sin(alpha)
y1r = y0-(x1-x0)*math.sin(alpha)+(y1-y0)*math.cos(alpha)

x2r = x0+(x2-x0)*math.cos(alpha)+(y2-y0)*math.sin(alpha)
y2r = y0-(x2-x0)*math.sin(alpha)+(y2-y0)*math.cos(alpha)

x3r = x0+(x3-x0)*math.cos(alpha)+(y3-y0)*math.sin(alpha)
y3r = y0-(x3-x0)*math.sin(alpha)+(y3-y0)*math.cos(alpha)

x4r = x0+(x4-x0)*math.cos(alpha)+(y4-y0)*math.sin(alpha)
y4r = y0-(x4-x0)*math.sin(alpha)+(y4-y0)*math.cos(alpha)

-- print(x1r .. ' ' .. y1r .. ' ' .. x2r .. ' ' .. y2r .. ' ' .. x3r .. ' ' .. y3r .. ' ' .. x4r .. ' ' .. y4r)
if alpha > 0 then
-- find intersection P of line [x1, y1]-[x4, y4] and [x1r, y1r]-[x2r, y2r]
local px1 = ((x1 * y4 - y1 * x4) * (x1r - x2r) - (x1 - x4) * (x1r * y2r - y1r * x2r)) / ((x1 - x4) * (y1r - y2r) - (y1 - y4) * (x1r - x2r))
local py1 = ((x1 * y4 - y1 * x4) * (y1r - y2r) - (y1 - y4) * (x1r * y2r - y1r * x2r)) / ((x1 - x4) * (y1r - y2r) - (y1 - y4) * (x1r - x2r))
local px2 = px1 + 1
local py2 = py1
-- print(px1 .. ' ' .. py1)
-- find the intersection Q of line [px1, py1]-[px2, py2] and [x2r, y2r]-[x3r][y3r]

local qx = ((px1 * py2 - py1 * px2) * (x2r - x3r) - (px1 - px2) * (x2r * y3r - y2r * x3r)) / ((px1 - px2) * (y2r - y3r) - (py1 - py2) * (x2r - x3r))
local qy = ((px1 * py2 - py1 * px2) * (y2r - y3r) - (py1 - py2) * (x2r * y3r - y2r * x3r)) / ((px1 - px2) * (y2r - y3r) - (py1 - py2) * (x2r - x3r))
-- print(qx .. ' ' .. qy)

min_x = width - qx
min_y = qy
max_x = qx
max_y = height - qy
else if alpha < 0 then
-- find intersection P of line [x2, y2]-[x3, y3] and [x1r, y1r]-[x2r, y2r]
local px1 = ((x2 * y3 - y2 * x3) * (x1r - x2r) - (x2 - x3) * (x1r * y2r - y1r * x2r)) / ((x2 - x3) * (y1r - y2r) - (y2 - y3) * (x1r - x2r))
local py1 = ((x2 * y3 - y1 * x3) * (y1r - y2r) - (y2 - y3) * (x1r * y2r - y1r * x2r)) / ((x2 - x3) * (y1r - y2r) - (y2 - y3) * (x1r - x2r))
local px2 = px1 - 1
local py2 = py1
-- find the intersection Q of line [px1, py1]-[px2, py2] and [x1r, y1r]-[x4r][y4r]
local qx = ((px1 * py2 - py1 * px2) * (x1r - x4r) - (px1 - px2) * (x1r * y4r - y1r * x4r)) / ((px1 - px2) * (y1r - y4r) - (py1 - py2) * (x1r - x4r))
local qy = ((px1 * py2 - py1 * px2) * (y1r - y4r) - (py1 - py2) * (x1r * y4r - y1r * x4r)) / ((px1 - px2) * (y1r - y4r) - (py1 - py2) * (x1r - x4r))
min_x = qx
min_y = qy
max_x = width - min_x
max_y = height - min_y
else
min_x = x1
min_y = y1
max_x = x2
max_y = y3
end
end

return math.floor(min_x), math.floor(min_y), math.floor(max_x), math.floor(max_y)
end

function computegrid(width, height, block_size, block_stride, flag_all)
coord_block_y = torch.range(1, height - block_size + 1, block_stride)
if flag_all == 1 then
if coord_block_y[#coord_block_y] < height - block_size + 1 then
local tail = torch.Tensor(1)
tail[1] = height - block_size + 1
coord_block_y = torch.cat(coord_block_y, tail)
end
end
coord_block_x = torch.range(1, width - block_size + 1, block_stride)
if flag_all == 1 then
if coord_block_x[#coord_block_x] < width - block_size + 1 then
local tail = torch.Tensor(1)
tail[1] = width - block_size + 1
coord_block_x = torch.cat(coord_block_x, tail)
end
end
return coord_block_x, coord_block_y
end
Loading

0 comments on commit 14e076b

Please sign in to comment.