Diff of /code/scan.lua [000000] .. [b758a2]

Switch to unified view

a b/code/scan.lua
1
function scan_old(img, net, windowWidth, windowHeight, N)
2
    img = img:double()
3
4
    local imageHeight = img:size(2)
5
    local imageWidth = img:size(3)
6
7
    local map = torch.FloatTensor(imageHeight, imageWidth):fill(0)
8
    local window = torch.Tensor(windowHeight, windowWidth)
9
    if cuda then
10
        window = window:cuda()
11
    end
12
    numWinRows = math.ceil((imageHeight-windowHeight+1)/N)
13
    for i=1,numWinRows do   
14
        window = torch.DoubleTensor(numWinRows, 3, windowHeight, windowWidth)
15
        for j=1,numWinRows do
16
            window[{{j}, {}, {}, {}}] = image.crop(img, N*(j-1), N*(i-1), N*(j-1)+windowWidth, N*(i-1)+windowHeight)
17
        end
18
        --normalize
19
        mean = {}
20
        stdv  = {}
21
        for i=1,3 do
22
            mean[i] = window[{{}, {i}, {}, {}  }]:mean()
23
            window[{{}, {i}, {}, {}  }]:add(-mean[i])
24
25
            stdv[i] = window[{{}, {i}, {}, {}  }]:std()
26
            window[{{}, {i}, {}, {}  }]:div(stdv[i])
27
        end
28
        if cuda then
29
            window = window:cuda()
30
        end
31
        local out = net:forward(window)
32
        for j=1,numWinRows do
33
            r1 = (i-1)*N+1+(windowHeight-1)/2
34
            r2 = i*N+(windowHeight-1)/2
35
            c1 = (j-1)*N+1+(windowWidth-1)/2
36
            c2 = j*N+(windowWidth-1)/2
37
            map[{{r1,r2},{c1,c2}}] = math.exp(out[j][1])
38
            print(i,j,out[j][1])
39
        end
40
    end
41
    return map
42
end
43
44
function scan(img, net)
45
    dofile("padarray.lua")
46
    pady = 50
47
    padx = 50
48
    img = padarray(img,{0,pady,padx},'mirror')
49
50
    --normalize
51
    mean = {}
52
    stdv  = {}
53
    for i=1,3 do
54
        mean[i] = img[{{i}, {}, {}  }]:mean()
55
        img[{{i}, {}, {}  }]:add(-mean[i])
56
57
        stdv[i] = img[{{i}, {}, {}  }]:std()
58
        img[{{i}, {}, {}  }]:div(stdv[i])
59
    end
60
    local out = net:forward(img)
61
62
    map = torch.exp(out[{{1},{},{}}])
63
    map = torch.squeeze(map)
64
65
    --pad with zeros to match the size to the input size
66
    pady = (img:size(2) - out:size(2))/2
67
    padx = (img:size(3) - out:size(3))/2
68
    --map = padarray(map,{pady, padx}, 'zero')
69
70
    return map
71
end