reconstruct.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. require 'image'
  2. local iproc = require 'iproc'
  3. local function reconstruct_y(model, x, offset, block_size)
  4. if x:dim() == 2 then
  5. x = x:reshape(1, x:size(1), x:size(2))
  6. end
  7. local new_x = torch.Tensor():resizeAs(x):zero()
  8. local output_size = block_size - offset * 2
  9. local input = torch.CudaTensor(1, 1, block_size, block_size)
  10. for i = 1, x:size(2), output_size do
  11. for j = 1, x:size(3), output_size do
  12. if i + block_size - 1 <= x:size(2) and j + block_size - 1 <= x:size(3) then
  13. local index = {{},
  14. {i, i + block_size - 1},
  15. {j, j + block_size - 1}}
  16. input:copy(x[index])
  17. local output = model:forward(input):view(1, output_size, output_size)
  18. local output_index = {{},
  19. {i + offset, offset + i + output_size - 1},
  20. {offset + j, offset + j + output_size - 1}}
  21. new_x[output_index]:copy(output)
  22. end
  23. end
  24. end
  25. return new_x
  26. end
  27. local function reconstruct_rgb(model, x, offset, block_size)
  28. local new_x = torch.Tensor():resizeAs(x):zero()
  29. local output_size = block_size - offset * 2
  30. local input = torch.CudaTensor(1, 3, block_size, block_size)
  31. for i = 1, x:size(2), output_size do
  32. for j = 1, x:size(3), output_size do
  33. if i + block_size - 1 <= x:size(2) and j + block_size - 1 <= x:size(3) then
  34. local index = {{},
  35. {i, i + block_size - 1},
  36. {j, j + block_size - 1}}
  37. input:copy(x[index])
  38. local output = model:forward(input):view(3, output_size, output_size)
  39. local output_index = {{},
  40. {i + offset, offset + i + output_size - 1},
  41. {offset + j, offset + j + output_size - 1}}
  42. new_x[output_index]:copy(output)
  43. end
  44. end
  45. end
  46. return new_x
  47. end
  48. local reconstruct = {}
  49. function reconstruct.is_rgb(model)
  50. if model:get(model:size() - 1).weight:size(1) == 3 then
  51. -- 3ch RGB
  52. return true
  53. else
  54. -- 1ch Y
  55. return false
  56. end
  57. end
  58. function reconstruct.offset_size(model)
  59. local conv = model:findModules("nn.SpatialConvolutionMM")
  60. if #conv > 0 then
  61. local offset = 0
  62. for i = 1, #conv do
  63. offset = offset + (conv[i].kW - 1) / 2
  64. end
  65. return math.floor(offset)
  66. else
  67. conv = model:findModules("cudnn.SpatialConvolution")
  68. local offset = 0
  69. for i = 1, #conv do
  70. offset = offset + (conv[i].kW - 1) / 2
  71. end
  72. return math.floor(offset)
  73. end
  74. end
  75. function reconstruct.image_y(model, x, offset, block_size)
  76. block_size = block_size or 128
  77. local output_size = block_size - offset * 2
  78. local h_blocks = math.floor(x:size(2) / output_size) +
  79. ((x:size(2) % output_size == 0 and 0) or 1)
  80. local w_blocks = math.floor(x:size(3) / output_size) +
  81. ((x:size(3) % output_size == 0 and 0) or 1)
  82. local h = offset + h_blocks * output_size + offset
  83. local w = offset + w_blocks * output_size + offset
  84. local pad_h1 = offset
  85. local pad_w1 = offset
  86. local pad_h2 = (h - offset) - x:size(2)
  87. local pad_w2 = (w - offset) - x:size(3)
  88. x = image.rgb2yuv(iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2))
  89. local y = reconstruct_y(model, x[1], offset, block_size)
  90. y[torch.lt(y, 0)] = 0
  91. y[torch.gt(y, 1)] = 1
  92. x[1]:copy(y)
  93. local output = image.yuv2rgb(iproc.crop(x,
  94. pad_w1, pad_h1,
  95. x:size(3) - pad_w2, x:size(2) - pad_h2))
  96. output[torch.lt(output, 0)] = 0
  97. output[torch.gt(output, 1)] = 1
  98. x = nil
  99. y = nil
  100. collectgarbage()
  101. return output
  102. end
  103. function reconstruct.scale_y(model, scale, x, offset, block_size)
  104. block_size = block_size or 128
  105. local x_lanczos = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Lanczos")
  106. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
  107. if x:size(2) * x:size(3) > 2048*2048 then
  108. collectgarbage()
  109. end
  110. local output_size = block_size - offset * 2
  111. local h_blocks = math.floor(x:size(2) / output_size) +
  112. ((x:size(2) % output_size == 0 and 0) or 1)
  113. local w_blocks = math.floor(x:size(3) / output_size) +
  114. ((x:size(3) % output_size == 0 and 0) or 1)
  115. local h = offset + h_blocks * output_size + offset
  116. local w = offset + w_blocks * output_size + offset
  117. local pad_h1 = offset
  118. local pad_w1 = offset
  119. local pad_h2 = (h - offset) - x:size(2)
  120. local pad_w2 = (w - offset) - x:size(3)
  121. x = image.rgb2yuv(iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2))
  122. x_lanczos = image.rgb2yuv(iproc.padding(x_lanczos, pad_w1, pad_w2, pad_h1, pad_h2))
  123. local y = reconstruct_y(model, x[1], offset, block_size)
  124. y[torch.lt(y, 0)] = 0
  125. y[torch.gt(y, 1)] = 1
  126. x_lanczos[1]:copy(y)
  127. local output = image.yuv2rgb(iproc.crop(x_lanczos,
  128. pad_w1, pad_h1,
  129. x_lanczos:size(3) - pad_w2, x_lanczos:size(2) - pad_h2))
  130. output[torch.lt(output, 0)] = 0
  131. output[torch.gt(output, 1)] = 1
  132. x = nil
  133. x_lanczos = nil
  134. y = nil
  135. collectgarbage()
  136. return output
  137. end
  138. function reconstruct.image_rgb(model, x, offset, block_size)
  139. block_size = block_size or 128
  140. local output_size = block_size - offset * 2
  141. local h_blocks = math.floor(x:size(2) / output_size) +
  142. ((x:size(2) % output_size == 0 and 0) or 1)
  143. local w_blocks = math.floor(x:size(3) / output_size) +
  144. ((x:size(3) % output_size == 0 and 0) or 1)
  145. local h = offset + h_blocks * output_size + offset
  146. local w = offset + w_blocks * output_size + offset
  147. local pad_h1 = offset
  148. local pad_w1 = offset
  149. local pad_h2 = (h - offset) - x:size(2)
  150. local pad_w2 = (w - offset) - x:size(3)
  151. x = iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2)
  152. if x:size(2) * x:size(3) > 2048*2048 then
  153. collectgarbage()
  154. end
  155. local y = reconstruct_rgb(model, x, offset, block_size)
  156. local output = iproc.crop(y,
  157. pad_w1, pad_h1,
  158. y:size(3) - pad_w2, y:size(2) - pad_h2)
  159. output[torch.lt(output, 0)] = 0
  160. output[torch.gt(output, 1)] = 1
  161. x = nil
  162. y = nil
  163. collectgarbage()
  164. return output
  165. end
  166. function reconstruct.scale_rgb(model, scale, x, offset, block_size)
  167. block_size = block_size or 128
  168. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
  169. if x:size(2) * x:size(3) > 2048*2048 then
  170. collectgarbage()
  171. end
  172. local output_size = block_size - offset * 2
  173. local h_blocks = math.floor(x:size(2) / output_size) +
  174. ((x:size(2) % output_size == 0 and 0) or 1)
  175. local w_blocks = math.floor(x:size(3) / output_size) +
  176. ((x:size(3) % output_size == 0 and 0) or 1)
  177. local h = offset + h_blocks * output_size + offset
  178. local w = offset + w_blocks * output_size + offset
  179. local pad_h1 = offset
  180. local pad_w1 = offset
  181. local pad_h2 = (h - offset) - x:size(2)
  182. local pad_w2 = (w - offset) - x:size(3)
  183. x = iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2)
  184. if x:size(2) * x:size(3) > 2048*2048 then
  185. collectgarbage()
  186. end
  187. local y = reconstruct_rgb(model, x, offset, block_size)
  188. local output = iproc.crop(y,
  189. pad_w1, pad_h1,
  190. y:size(3) - pad_w2, y:size(2) - pad_h2)
  191. output[torch.lt(output, 0)] = 0
  192. output[torch.gt(output, 1)] = 1
  193. x = nil
  194. y = nil
  195. collectgarbage()
  196. return output
  197. end
  198. function reconstruct.image(model, x, block_size)
  199. local i2rgb = false
  200. if x:size(1) == 1 then
  201. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  202. new_x[1]:copy(x)
  203. new_x[2]:copy(x)
  204. new_x[3]:copy(x)
  205. x = new_x
  206. i2rgb = true
  207. end
  208. if reconstruct.is_rgb(model) then
  209. x = reconstruct.image_rgb(model, x,
  210. reconstruct.offset_size(model), block_size)
  211. else
  212. x = reconstruct.image_y(model, x,
  213. reconstruct.offset_size(model), block_size)
  214. end
  215. if i2rgb then
  216. x = image.rgb2y(x)
  217. end
  218. return x
  219. end
  220. function reconstruct.scale(model, scale, x, block_size)
  221. local i2rgb = false
  222. if x:size(1) == 1 then
  223. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  224. new_x[1]:copy(x)
  225. new_x[2]:copy(x)
  226. new_x[3]:copy(x)
  227. x = new_x
  228. i2rgb = true
  229. end
  230. if reconstruct.is_rgb(model) then
  231. x = reconstruct.scale_rgb(model, scale, x,
  232. reconstruct.offset_size(model), block_size)
  233. else
  234. x = reconstruct.scale_y(model, scale, x,
  235. reconstruct.offset_size(model), block_size)
  236. end
  237. if i2rgb then
  238. x = image.rgb2y(x)
  239. end
  240. return x
  241. end
  242. local function tta(f, model, x, block_size)
  243. local average = nil
  244. local offset = reconstruct.offset_size(model)
  245. for i = 1, 4 do
  246. local flip_f, iflip_f
  247. if i == 1 then
  248. flip_f = function (a) return a end
  249. iflip_f = function (a) return a end
  250. elseif i == 2 then
  251. flip_f = image.vflip
  252. iflip_f = image.vflip
  253. elseif i == 3 then
  254. flip_f = image.hflip
  255. iflip_f = image.hflip
  256. elseif i == 4 then
  257. flip_f = function (a) return image.hflip(image.vflip(a)) end
  258. iflip_f = function (a) return image.vflip(image.hflip(a)) end
  259. end
  260. for j = 1, 2 do
  261. local tr_f, itr_f
  262. if j == 1 then
  263. tr_f = function (a) return a end
  264. itr_f = function (a) return a end
  265. elseif j == 2 then
  266. tr_f = function(a) return a:transpose(2, 3):contiguous() end
  267. itr_f = function(a) return a:transpose(2, 3):contiguous() end
  268. end
  269. local out = itr_f(iflip_f(f(model, flip_f(tr_f(x)),
  270. offset, block_size)))
  271. if not average then
  272. average = out
  273. else
  274. average:add(out)
  275. end
  276. end
  277. end
  278. return average:div(8.0)
  279. end
  280. function reconstruct.image_tta(model, x, block_size)
  281. if reconstruct.is_rgb(model) then
  282. return tta(reconstruct.image_rgb, model, x, block_size)
  283. else
  284. return tta(reconstruct.image_y, model, x, block_size)
  285. end
  286. end
  287. function reconstruct.scale_tta(model, scale, x, block_size)
  288. if reconstruct.is_rgb(model) then
  289. local f = function (model, x, offset, block_size)
  290. return reconstruct.scale_rgb(model, scale, x, offset, block_size)
  291. end
  292. return tta(f, model, x, block_size)
  293. else
  294. local f = function (model, x, offset, block_size)
  295. return reconstruct.scale_y(model, scale, x, offset, block_size)
  296. end
  297. return tta(f, model, x, block_size)
  298. end
  299. end
  300. return reconstruct