[b758a2]: / code / detectorDNN.lua

Download this file

150 lines (129 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'torch';
require 'nn';
require 'image';
cuda = false
require 'cutorch';
require 'cunn';
require 'sys';
require 'csvigo';
local dir = require 'pl.dir';
local c = os.clock()
local t = os.time()
local folder = '/home/andrew/mitosis/data/MITOS/testing/'
--local netPath1 = '/home/andrew/mitosis/data/nets/net.t7'
local netPath1 = '/home/andrew/mitosis/data/nets/dnn1_fullset_aug_20i_lr05_lrd0005_m09_mini200_aeptgl.t7'
local netPath2 = '/home/andrew/mitosis/data/nets/dnn2_fullset_aug_20i_lr05_lrd0005_m09_mini200_aeptgl.t7'
local threshold = 0.1
dofile("getImagePaths.lua")
local imagePaths = getImagePaths(folder)
if paths.dirp(folder .. 'results') == false then
paths.mkdir(folder .. 'results');
end
dofile("scan.lua")
dofile("expand.lua")
-- define circular kernel
local d = 10
local kernel = torch.FloatTensor(2*d+1,2*d+1):fill(0.001)
for i=1,2*d+1 do
for j=1,2*d+1 do
if (i-d-1)^2 + (j-d-1)^2 >= d^2 then
kernel[i][j] = 0
end
end
end
for k,imagePath in ipairs(imagePaths) do
print(k)
local c1 = os.clock()
local t1 = os.time()
--local windowWidth = 101
--local windowHeight = 101
local net1 = torch.load(netPath1)
local net2 = torch.load(netPath2)
net1 = expand(net1)
net2 = expand(net2)
if cuda then
net1 = net1:cuda()
net2 = net2:cuda()
else
net1 = net1:float()
net2 = net2:float()
end
local img = image.load(imagePath, 3, 'float')
if cuda then
img = img:cuda()
else
img = img:float()
end
-- scan sixteen versions of the image
-- four rotations, paired with their reflections, and two neural nets
--]]
--[
local maps = {}
for i=1,4 do
for j=1,2 do
local tmp = image.rotate(img, (i-1)*math.pi/2)
if j == 2 then
tmp = image.hflip(tmp)
tmp = scan(tmp, net1)
tmp = image.hflip(tmp)
else
tmp = scan(tmp, net1)
end
maps[(i-1)*2+j] = image.rotate(tmp, -(i-1)*math.pi/2)
end
end
net1 = nil
--[
for i=5,8 do
for j=1,2 do
local tmp = image.rotate(img, (i-1)*math.pi/2)
if j == 2 then
tmp = image.hflip(tmp)
tmp = scan(tmp, net2)
tmp = image.hflip(tmp)
else
tmp = scan(tmp, net2)
end
maps[(i-1)*2+j] = image.rotate(tmp, -(i-1)*math.pi/2)
end
end
net2 = nil
--]]
-- take the mean of the sixteen maps
--[
local sum = maps[1]
for i=2,#maps do
sum = sum + maps[i]
end
local map = sum/#maps
map = image.convolve(map, kernel, 'same')
map = map/torch.max(map) -- normalize
image.save('test.png',map)
--map = image.load('test.png',1,'float')
local results = {}
local m = 1
while true do
local ind = map:eq(map:max()):nonzero()
local row = ind[1][1]
local col = ind[1][2]
local val = map[row][col]
results[m] = {row, col, val}
m = m + 1
for i=-2*d,2*d do
for j=-2*d,2*d do
if i^2 + j^2 < 4*d^2 and row+i>0 and col+j>0 and row+i<=map:size(1) and col+j<=map:size(2) then
map[row+i][col+j] = 0
end
end
end
if val < threshold then
break
end
end
local outfile = paths.concat(folder, 'results', paths.basename(imagePath, paths.extname(imagePath)) .. '.csv')
csvigo.save(outfile, results)
print(os.clock()-c1)
print(os.time()-t1)
end
print(os.clock()-c)
print(os.time()-t)