Skip to content

Commit

Permalink
changed CMakeLists
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Zhu committed Aug 17, 2015
1 parent ed2aaa6 commit 1b1750e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ foreach(target ctc normalizer)
set_target_properties(${target}
PROPERTIES PREFIX ""
SUFFIX ".so"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../${target}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/../"
)
target_link_libraries(${target} TH luaT luajit)
endforeach(target)
Expand Down
1 change: 0 additions & 1 deletion ctc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ function ctc.getCTCCost(outputTable, target)

fvs = ctc.getForwardVariable(outputTable, alignedTable, target)


-- calculate backwardVariable

bvs = ctc.getBackwardVariable(outputTable, alignedTable, target)
Expand Down
33 changes: 29 additions & 4 deletions loader.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'image'
require 'codec'
require 'normalizer'
utf8 = require 'utf8'

Loader = {
Expand Down Expand Up @@ -27,11 +29,34 @@ function Loader:new(o)
return o
end

function Loader.__getNormalizedImage(src)
local im = image.load(src, 1)

if im:dim() == 3 then
im = im[1]
end

output = torch.Tensor()

w = im:size()[2]
h = im:size()[1]

ones = torch.ones(h, w)

im = ones - im

normalizer.normalize(im, output)

return output
end

function Loader:load(file)
self.samples = {}
local f = assert(io.open(file, "r"))
for line in f:lines() do
local src = line
local im = Loader.__getNormalizedImage(src)

local gt = src:gsub(".png", ".gt.txt")
local cf = assert(io.open(gt, "r"))
local gt = cf:read("*line")
Expand All @@ -45,7 +70,7 @@ function Loader:load(file)

end

table.insert(self.samples, {src = src, gt = gt})
table.insert(self.samples, {src = src, gt = gt, img = im})
end
f:close()

Expand All @@ -56,7 +81,7 @@ function Loader:load(file)
self.codec_obj = nil
self.weights = nil

return self.samples
-- return self.samples
end

function Loader:pick()
Expand All @@ -74,8 +99,8 @@ function Loader:pickWithWeight()

self.p = torch.zeros(#self.samples)
local i = 0
self.p:apply(function()
i = i + 1
self.p:apply(function()
i = i + 1
return torch.normal(1.0 / self.weights[i], 1.0 / self.weights[i] / 3.0)
end)
end
Expand Down
35 changes: 27 additions & 8 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ require 'rnn'
require 'image'
require 'optim'

require 'loader'
require 'ctc_log'
require 'utils/decoder'

mnist = require 'mnist'

DROPOUT_RATE = 0.4

local input_size = 28
local input_size = 64
local hidden_size = 100
local class_num = 10

Expand All @@ -36,12 +37,27 @@ state = {
momentum = 0.5
}

loader = Loader()
loader:load("1.txt")
codec = loader:codec()

local sample = loader:pick()
local im = sample.img
local target = codec:encode(sample.gt)

raw = image.load(sample.src, 1)

print(raw[1])

print(im)

--[[
for i = 1, 100000 do
local no = torch.random() % 100 + 1
local sample = mnist.traindataset()[no]
local im = sample.x:double():t()
local target = torch.Tensor{sample.y + 1}

local sample = loader:pick()
local im = sample.img
local target = codec:encode(sample.gt)
print(im)
local feval = function(params)
net:forget()
Expand All @@ -51,8 +67,8 @@ for i = 1, 100000 do
loss, grad = ctc.getCTCCostAndGrad(outputTable, target)
if i % 20 == 0 then
print(target[1] - 1)
print(decoder.decodeTable(outputTable))
print(sample.gt)
print(decoder.best_path_decode(outputTable))
print(loss)
end
Expand All @@ -67,3 +83,6 @@ for i = 1, 100000 do
optim.sgd(feval, params, state)
end
]]
6 changes: 3 additions & 3 deletions normalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ static double bilinear(double * in, int w, int h, double x, double y) {
xt = xt > w - 1 ? w - 1 : xt;
yt = yt > h - 1 ? h - 1 : yt;

// printf("(%d, %d)\n", xi, yi);
printf("(%d, %d)\n", xi, yi);

double p00 = in[yi * w + xi];
double p01 = in[yt * w + xi];
double p10 = in[yi * w + xt];
double p11 = in[yt * w + xt];

return p00 * (1.0 - xf) * (1.0 - yf) + p10 * xf * (1.0 - yf) + p01 * (1.0 - xf) * yf + p11 * xf * yf;
p00 * (1.0 - xf) * (1.0 - yf) + p10 * xf * (1.0 - yf) + p01 * (1.0 - xf) * yf + p11 * xf * yf;

}

Expand Down Expand Up @@ -174,7 +174,7 @@ static void normalize

THDoubleTensor_resize2d(out, target_height, target_width);

printf("scale = %.4f\n", scale);
// printf("scale = %.4f\n", scale);

double * outData = THDoubleTensor_data(out);

Expand Down
2 changes: 1 addition & 1 deletion normalizer.lua → test_normalizer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'image'
require 'normalizer/normalizer'
require 'normalizer'

im = image.load("bq01_006.png", 1)

Expand Down
2 changes: 1 addition & 1 deletion utils/decoder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function decoder.best_path_decode(outputTable, codec)
local max_val, max = torch.max(outputTable[i], 1)
max = max[1]

if max = class_num then
if max == class_num then
if last_max ~= -1 and last_max_class ~= nil then
table.insert(result, last_max_class)
last_max = -1
Expand Down

0 comments on commit 1b1750e

Please sign in to comment.