Permalink
Browse files

Demo

  • Loading branch information...
1 parent fa33253 commit 98f00aa98b5babbcc9fd8fcf8f5bc0349287c2e7 @MichaelMathieu committed Jun 28, 2012
@@ -4,7 +4,7 @@
delta = 1;
use_full_image = 0;
-imdir = '~/robot/depth-estimation/data/ardrone1/';
+imdir = '~/robot/depth-estimation/data/no-risk/part7/';
lst = dir([imdir 'images']);
formatSpec = '%09.0f';
@@ -29,8 +29,8 @@
end
% set optical flow parameters (see Coarse2FineTwoFrames.m for the definition of the parameters)
- alpha = 0.05;
- ratio = 0.75;
+ alpha = 0.02;
+ ratio = 0.75;
minWidth = 60;
nOuterFPIterations = 7;
nInnerFPIterations = 1;
View
@@ -169,7 +169,10 @@ function nextFrameDepth()
print("processOutput: " .. timer:time()['real'])
output = poutput.full
print("enlargeMask : " .. timer:time()['real'])
-
+ enlargeMask(mask,
+ math.ceil((geometry.wImg-poutput.y:size(2))/2),
+ math.ceil((geometry.hImg-poutput.y:size(1))/2))
+
local mask2 = torch.Tensor(geometry.hImg, geometry.wImg):zero()
mask2:narrow(
1, math.floor((geometry.hImg-mask:size(1))/2), mask:size(1)):narrow(
View
@@ -160,8 +160,109 @@ static int ExtractOutput(lua_State *L) {
return 0;
}
+static int ExtractOutputMarginalized(lua_State *L) {
+ const void* iddouble = luaT_checktypename2id(L, ID_TENSOR_STRING);
+ const void* idlong = luaT_checktypename2id(L, "torch.LongTensor" );
+ Tensor* input = (Tensor*)luaT_checkudata(L, 1, iddouble);
+ double threshold = lua_tonumber (L, 2);
+ double threshold_acc = lua_tonumber (L, 3);
+ THLongTensor* ret = (THLongTensor*)luaT_checkudata(L, 4, idlong );
+ THLongTensor* retgd = (THLongTensor*)luaT_checkudata(L, 5, idlong );
+
+ //THLongTensor_zero(ret);
+ THLongTensor_zero(retgd);
+
+ input = Tensor_(newContiguous)(input);
+ real* input_p = Tensor_(data)(input);
+ long* ret_p = THLongTensor_data (ret );
+ long* retgd_p = THLongTensor_data (retgd);
+
+ int nvalues = input->size[2];
+ int h = input->size[0];
+ int w = input->size[1];
+ long* is = input->stride;
+ long* rs = ret->stride;
+ long* rgs = retgd->stride;
+
+ int maxhighs = 4;
+ if (threshold < 0.2)
+ maxhighs = 8;
+ Tensor* highs = Tensor_(newWithSize4d)(h, w, 2, maxhighs);
+ assert(Tensor_(isContiguous)(highs));
+ real* highs_p = Tensor_(data)(highs );
+ long* hs = highs->stride;
+
+ Tensor_(zero)(highs);
+
+ int i, j, k, n;
+ real *highs_pe, *highs_pe_end, *input_pe, *input_pe_begin, *input_pe_end;
+ input_pe = input_p;
+ for (i = 0; i < h; ++i) {
+ for (j = 0; j < w; ++j) {
+ input_pe_begin = input_pe;
+ input_pe_end = input_pe + nvalues;
+ n = 0;
+ for (; input_pe != input_pe_end; ++input_pe) {
+ if (*input_pe > threshold) {
+ highs_pe = highs_p + hs[0]*i + hs[1]*j + n;
+ *highs_pe = *input_pe;
+ *(highs_pe + hs[2]) = (int)(input_pe - input_pe_begin)+1;
+ ++n;
+ if (n == maxhighs) { //TODO this might be removed
+ input_pe = input_pe_end;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ accreal acc;
+ if (maxhighs == 4) {
+ for (i = 0; i < h; ++i) {
+ for (j = 0; j < w; ++j) {
+ highs_pe = highs_p + hs[0]*i + hs[1]*j;
+ if (*highs_pe > 0) {
+ sort4<real>(highs_pe, hs[2]);
+ ret_p [rs[0]*i + rs[1]*j ] = *(highs_pe+hs[2]);
+ for(k = 1; k < maxhighs; ++k)
+ highs_pe[k] += highs_pe[k-1];
+ acc = 0;
+ for (k = 0; k < maxhighs; ++k)
+ acc += highs_pe[k];
+ if (acc >= threshold_acc)
+ retgd_p[rgs[0]*i + rgs[1]*j] = 1;
+ }
+ }
+ }
+ } else {
+ for (i = 0; i < h; ++i) {
+ for (j = 0; j < w; ++j) {
+ highs_pe = highs_p + hs[0]*i + hs[1]*j;
+ if (*highs_pe > 0) {
+ sort8<real>(highs_pe, hs[2]);
+ ret_p [rs[0]*i + rs[1]*j ] = *(highs_pe+hs[2]);
+ for(k = 1; k < maxhighs; ++k)
+ highs_pe[k] += highs_pe[k-1];
+ acc = 0;
+ for (k = 0; k < maxhighs; ++k)
+ acc += highs_pe[k];
+ if (acc >= threshold_acc)
+ retgd_p[rgs[0]*i + rgs[1]*j] = 1;
+ }
+ }
+ }
+ }
+
+ Tensor_(free)( input);
+ Tensor_(free)( highs);
+
+ return 0;
+}
+
static const struct luaL_reg extractoutput[] = {
{"extractOutput", ExtractOutput},
+ {"extractOutputMarginalized", ExtractOutputMarginalized},
{NULL, NULL}
};
View
@@ -137,7 +137,7 @@ correction.distP[5] = -0.069770
local geometry = {}
geometry.wImg = 320
-geometry.hImg = 240
+geometry.hImg = 180
geometry.maxwHR = tonumber(opt.win_size) --high res in case of multiscale
geometry.maxhHR = tonumber(opt.win_size) --high res in case of multiscale
if opt.win_size_height then
@@ -254,11 +254,11 @@ local trainData = generateDataOpticalFlow(correction, geometry, learning,
print('Generating test set...')
local testData = generateDataOpticalFlow(correction, geometry, learning,
raw_data, opt.n_test_set)
-
+print("a")
local score = score_epoch(geometry, learning, model, criterion, testData,
raw_data, opt.n_images_test_set)
saveModel(opt.output_models_dir, 'model_of_', geometry, learning, model, 0, score)
-
+print("b")
config = {learningRate = learning.rate,
weightDecay = learning.weight_decay,
momentum = 0,
View
@@ -46,7 +46,10 @@ function getFilter(geometry)
assert(not geometry.L2Pooling)
local filter = nn.Sequential()
for i = 1,#geometry.layers do
- if i == 1 or geometry.layers[i-1][4] == geometry.layers[i][1] then
+ if i == 1 then
+ filter:add(nn.SpatialConvolution(geometry.layers[i][1], geometry.layers[i][4],
+ geometry.layers[i][2], geometry.layers[i][3]))
+ elseif geometry.layers[i-1][4] == geometry.layers[i][1] then
filter:add(nn.SpatialConvolution(geometry.layers[i][1], geometry.layers[i][4],
geometry.layers[i][2], geometry.layers[i][3]))
else
@@ -180,8 +183,14 @@ function getOutputConfidences2(geometry, input)
local y = input:clone():cmul(ymul):sum(3)[{{},{},1}]
local imaxs = torch.LongTensor(input:size(1), input:size(2))
- local gds = torch.LongTensor(input:size(1), input:size(2))
- extractoutput.extractOutput(input, 0.11, 0, imaxs, gds)
+ --local gds = torch.LongTensor(input:size(1), input:size(2))
+ --extractoutput.extractOutput(input, 0.11, 0, imaxs, gds)
+ local xgds = torch.LongTensor(input:size(1), input:size(2))
+ --local ygds = torch.LongTensor(input:size(1), input:size(2))
+ local inputmarg = input:reshape(input:size(1), input:size(2), geometry.maxh, geometry.maxw):sum(4):squeeze()
+ extractoutput.extractOutput(inputmarg, 0.11, 0, imaxs, xgds)
+ --extractoutput.extractOutput(input:sum(3), 0.11, 0, imaxs, ygds)
+ gds = xgds
return y, x, gds
end
@@ -308,11 +317,130 @@ function prepareTarget(geometry, learning, targett)
end
-function postProcessImage(input, winsize)
+function postProcessImage(input, mask, winsize, method)
local output = torch.Tensor(2, input[1]:size(1), input[1]:size(2)):zero()
- local winsizeh1 = math.ceil(winsize/2)-1
- local winsizeh2 = math.floor(winsize/2)
- local win = torch.Tensor(2,winsize,winsize)
+ --local winsizeh1 = math.ceil(winsize/2)-1
+ --local winsizeh2 = math.floor(winsize/2)
+ --local win = torch.Tensor(2,winsize,winsize)
+ inline.preamble [[
+ int comp(const void* a_,const void* b_) {
+ float a = *((float*)a_), b = *((float*)b_);
+ if (a==b) {
+ return 0;
+ } else {
+ if (a < b) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+ }
+ ]]
+ local fmax = inline.load [[
+ const void* idfloat = luaT_checktypename2id(L, "torch.FloatTensor");
+ THFloatTensor* flow = (THFloatTensor*)luaT_checkudata(L, 1, idfloat);
+ THFloatTensor* mask = (THFloatTensor*)luaT_checkudata(L, 2, idfloat);
+ int k = lua_tointeger(L, 3);
+ THFloatTensor* ret = (THFloatTensor*)luaT_checkudata(L, 4, idfloat);
+
+ int h = flow->size[1];
+ int w = flow->size[2];
+ long* fs = flow->stride;
+ float* flow_p = THFloatTensor_data(flow);
+ long* ms = mask->stride;
+ float* mask_p = THFloatTensor_data(mask);
+ long* rs = ret->stride;
+ float* ret_p = THFloatTensor_data(ret);
+ int halfk = k/2;
+
+ const int TMPSIZE = 256;
+ const int ROWSIZE = 16;
+ int tmp[TMPSIZE];
+
+ int i, j, ik, jk, l;
+ for (i = 0; i < h-k; ++i) {
+ for (j = 0; j < w-k; ++j) {
+ memset(tmp, 0, TMPSIZE*sizeof(int));
+ for (ik = i; ik < i+k; ++ik) {
+ for (jk = j; jk < j+k; ++jk) {
+ if (mask_p[ik*ms[0] + jk*ms[1] ]) {
+ int vx = flow_p[fs[0] + ik*fs[1] + jk*fs[2] ];
+ int vy = flow_p[ik*fs[1] + jk*fs[2] ];
+ int v = vx+ROWSIZE*vy;
+ ++tmp[v];
+ }
+ }
+ }
+ int im = 0;
+ for (l = 0; l < TMPSIZE; ++l) {
+ if (tmp[l] > tmp[im])
+ im = l;
+ }
+ ret_p[rs[0] + (i+halfk)*rs[1] + (j+halfk)*rs[2] ] = im%%ROWSIZE;
+ ret_p[ (i+halfk)*rs[1] + (j+halfk)*rs[2] ] = im/ROWSIZE;
+ }
+ }
+ ]]
+
+ local fmed = inline.load [[
+ const void* idfloat = luaT_checktypename2id(L, "torch.FloatTensor");
+ THFloatTensor* flow = (THFloatTensor*)luaT_checkudata(L, 1, idfloat);
+ THFloatTensor* mask = (THFloatTensor*)luaT_checkudata(L, 2, idfloat);
+ int k = lua_tointeger(L, 3);
+ THFloatTensor* ret = (THFloatTensor*)luaT_checkudata(L, 4, idfloat);
+
+ int h = flow->size[1];
+ int w = flow->size[2];
+ long* fs = flow->stride;
+ float* flow_p = THFloatTensor_data(flow);
+ long* ms = mask->stride;
+ float* mask_p = THFloatTensor_data(mask);
+ long* rs = ret->stride;
+ float* ret_p = THFloatTensor_data(ret);
+ int halfk = k/2;
+
+ const int TMPSIZE = 32;
+ const int ROWSIZE = 16;
+ float tmp[TMPSIZE];
+ float tmp2[TMPSIZE];
+
+ int i, j, ik, jk, l;
+ for (i = 0; i < h-k; ++i) {
+ for (j = 0; j < w-k; ++j) {
+ memset(tmp, 0, TMPSIZE*sizeof(int));
+ memset(tmp2, 0, TMPSIZE*sizeof(int));
+ int n = 0;
+ for (ik = i; ik < i+k; ++ik) {
+ for (jk = j; jk < j+k; ++jk) {
+ if (mask_p[ik*ms[0] + jk*ms[1] ]) {
+ float vx = flow_p[fs[0] + ik*fs[1] + jk*fs[2] ];
+ float vy = flow_p[ik*fs[1] + jk*fs[2] ];
+ tmp[n] = vy;
+ tmp2[n++] = vx;
+ }
+ }
+ }
+ qsort(tmp, n, sizeof(float), comp);
+ qsort(tmp2, n, sizeof(float), comp);
+ float im = tmp[n/2];
+ float im2 = tmp2[n/2];
+ ret_p[rs[0] + (i+halfk)*rs[1] + (j+halfk)*rs[2] ] = im2;
+ ret_p[ (i+halfk)*rs[1] + (j+halfk)*rs[2] ] = im;
+ }
+ }
+ ]]
+ inline.default_preamble()
+ if method == 'max' then
+ local inputR = (input+0.5):floor()
+ m = inputR:min()
+ fmax(inputR-m, mask, winsize, output)
+ output = output+m
+ else
+ fmed(input, mask, winsize, output)
+ output = output
+ end
+
+ --[[
for i = 1+winsizeh1,output:size(2)-winsizeh2 do
for j = 1+winsizeh1,output:size(3)-winsizeh2 do
win[1] = (input[1]:sub(i-winsizeh1,i+winsizeh2, j-winsizeh1, j+winsizeh2)+0.5):floor()
@@ -336,5 +464,6 @@ function postProcessImage(input, winsize)
output[2][i][j] = win2[2][tbest]
end
end
+ --]]
return output
end
@@ -4,7 +4,7 @@ require 'groundtruth_opticalflow'
require 'sfm2'
require 'sys'
-local datapath = 'data2/no-risk/part1/'
+local datapath = 'data/no-risk/part7/'
local ext = 'jpg'
local w = 320.
local camera = 'gopro'
View
@@ -3,7 +3,7 @@ require 'opticalflow_model'
require 'opticalflow_model_io'
require 'gnuplot'
-function flow2pol(geometry, y, x)
+function flow2pol(y, x)
--y, x = onebased2centered(geometry, y, x)
local ang = math.atan2(y, x)
local norm = math.sqrt(x*x+y*y)
@@ -12,12 +12,13 @@ end
function flow2hsv(geometry, flow)
local todisplay = torch.Tensor(3, flow:size(2), flow:size(3))
+ local normer = 1.0/math.sqrt((geometry.maxh/2)*(geometry.maxh/2)+(geometry.maxw/2)*(geometry.maxw/2))
for i = 1,flow:size(2) do
for j = 1,flow:size(3) do
- local ang, norm = flow2pol(geometry, flow[1][i][j], flow[2][i][j])
+ local ang, norm = flow2pol(flow[1][i][j], flow[2][i][j])
todisplay[1][i][j] = ang/(math.pi*2.0)
todisplay[2][i][j] = 1.0
- todisplay[3][i][j] = norm/math.max(geometry.maxh/2, geometry.maxw/2)
+ todisplay[3][i][j] = norm*normer
end
end
return image.hsl2rgb(todisplay)
Oops, something went wrong.

0 comments on commit 98f00aa

Please sign in to comment.