reconstruct.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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):float():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):float():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. local yuv = image.rgb2yuv(iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2))
  89. local y = reconstruct_y(model, yuv[1], offset, block_size)
  90. y[torch.lt(y, 0)] = 0
  91. y[torch.gt(y, 1)] = 1
  92. yuv[1]:copy(y)
  93. local output = image.yuv2rgb(iproc.crop(yuv,
  94. pad_w1, pad_h1,
  95. yuv:size(3) - pad_w2, yuv:size(2) - pad_h2))
  96. output[torch.lt(output, 0)] = 0
  97. output[torch.gt(output, 1)] = 1
  98. collectgarbage()
  99. return output
  100. end
  101. function reconstruct.scale_y(model, scale, x, offset, block_size)
  102. block_size = block_size or 128
  103. local x_lanczos = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Lanczos")
  104. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
  105. local output_size = block_size - offset * 2
  106. local h_blocks = math.floor(x:size(2) / output_size) +
  107. ((x:size(2) % output_size == 0 and 0) or 1)
  108. local w_blocks = math.floor(x:size(3) / output_size) +
  109. ((x:size(3) % output_size == 0 and 0) or 1)
  110. local h = offset + h_blocks * output_size + offset
  111. local w = offset + w_blocks * output_size + offset
  112. local pad_h1 = offset
  113. local pad_w1 = offset
  114. local pad_h2 = (h - offset) - x:size(2)
  115. local pad_w2 = (w - offset) - x:size(3)
  116. local yuv_nn = image.rgb2yuv(iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2))
  117. local yuv_lanczos = image.rgb2yuv(iproc.padding(x_lanczos, pad_w1, pad_w2, pad_h1, pad_h2))
  118. local y = reconstruct_y(model, yuv_nn[1], offset, block_size)
  119. y[torch.lt(y, 0)] = 0
  120. y[torch.gt(y, 1)] = 1
  121. yuv_lanczos[1]:copy(y)
  122. local output = image.yuv2rgb(iproc.crop(yuv_lanczos,
  123. pad_w1, pad_h1,
  124. yuv_lanczos:size(3) - pad_w2, yuv_lanczos:size(2) - pad_h2))
  125. output[torch.lt(output, 0)] = 0
  126. output[torch.gt(output, 1)] = 1
  127. collectgarbage()
  128. return output
  129. end
  130. function reconstruct.image_rgb(model, x, offset, block_size)
  131. block_size = block_size or 128
  132. local output_size = block_size - offset * 2
  133. local h_blocks = math.floor(x:size(2) / output_size) +
  134. ((x:size(2) % output_size == 0 and 0) or 1)
  135. local w_blocks = math.floor(x:size(3) / output_size) +
  136. ((x:size(3) % output_size == 0 and 0) or 1)
  137. local h = offset + h_blocks * output_size + offset
  138. local w = offset + w_blocks * output_size + offset
  139. local pad_h1 = offset
  140. local pad_w1 = offset
  141. local pad_h2 = (h - offset) - x:size(2)
  142. local pad_w2 = (w - offset) - x:size(3)
  143. local input = iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2)
  144. local y = reconstruct_rgb(model, input, offset, block_size)
  145. local output = iproc.crop(y,
  146. pad_w1, pad_h1,
  147. y:size(3) - pad_w2, y:size(2) - pad_h2)
  148. collectgarbage()
  149. output[torch.lt(output, 0)] = 0
  150. output[torch.gt(output, 1)] = 1
  151. return output
  152. end
  153. function reconstruct.scale_rgb(model, scale, x, offset, block_size)
  154. block_size = block_size or 128
  155. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
  156. local output_size = block_size - offset * 2
  157. local h_blocks = math.floor(x:size(2) / output_size) +
  158. ((x:size(2) % output_size == 0 and 0) or 1)
  159. local w_blocks = math.floor(x:size(3) / output_size) +
  160. ((x:size(3) % output_size == 0 and 0) or 1)
  161. local h = offset + h_blocks * output_size + offset
  162. local w = offset + w_blocks * output_size + offset
  163. local pad_h1 = offset
  164. local pad_w1 = offset
  165. local pad_h2 = (h - offset) - x:size(2)
  166. local pad_w2 = (w - offset) - x:size(3)
  167. local input = iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2)
  168. local y = reconstruct_rgb(model, input, offset, block_size)
  169. local output = iproc.crop(y,
  170. pad_w1, pad_h1,
  171. y:size(3) - pad_w2, y:size(2) - pad_h2)
  172. output[torch.lt(output, 0)] = 0
  173. output[torch.gt(output, 1)] = 1
  174. collectgarbage()
  175. return output
  176. end
  177. function reconstruct.image(model, x, block_size)
  178. local i2rgb = false
  179. if x:size(1) == 1 then
  180. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  181. new_x[1]:copy(x)
  182. new_x[2]:copy(x)
  183. new_x[3]:copy(x)
  184. x = new_x
  185. i2rgb = true
  186. end
  187. if reconstruct.is_rgb(model) then
  188. x = reconstruct.image_rgb(model, x,
  189. reconstruct.offset_size(model), block_size)
  190. else
  191. x = reconstruct.image_y(model, x,
  192. reconstruct.offset_size(model), block_size)
  193. end
  194. if i2rgb then
  195. x = image.rgb2y(x)
  196. end
  197. return x
  198. end
  199. function reconstruct.scale(model, scale, x, block_size)
  200. local i2rgb = false
  201. if x:size(1) == 1 then
  202. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  203. new_x[1]:copy(x)
  204. new_x[2]:copy(x)
  205. new_x[3]:copy(x)
  206. x = new_x
  207. i2rgb = true
  208. end
  209. if reconstruct.is_rgb(model) then
  210. x = reconstruct.scale_rgb(model, scale, x,
  211. reconstruct.offset_size(model), block_size)
  212. else
  213. x = reconstruct.scale_y(model, scale, x,
  214. reconstruct.offset_size(model), block_size)
  215. end
  216. if i2rgb then
  217. x = image.rgb2y(x)
  218. end
  219. return x
  220. end
  221. local function tta(f, model, x, block_size)
  222. local average = nil
  223. local offset = reconstruct.offset_size(model)
  224. for i = 1, 4 do
  225. local flip_f, iflip_f
  226. if i == 1 then
  227. flip_f = function (a) return a end
  228. iflip_f = function (a) return a end
  229. elseif i == 2 then
  230. flip_f = image.vflip
  231. iflip_f = image.vflip
  232. elseif i == 3 then
  233. flip_f = image.hflip
  234. iflip_f = image.hflip
  235. elseif i == 4 then
  236. flip_f = function (a) return image.hflip(image.vflip(a)) end
  237. iflip_f = function (a) return image.vflip(image.hflip(a)) end
  238. end
  239. for j = 1, 2 do
  240. local tr_f, itr_f
  241. if j == 1 then
  242. tr_f = function (a) return a end
  243. itr_f = function (a) return a end
  244. elseif j == 2 then
  245. tr_f = function(a) return a:transpose(2, 3):contiguous() end
  246. itr_f = function(a) return a:transpose(2, 3):contiguous() end
  247. end
  248. local out = itr_f(iflip_f(f(model, flip_f(tr_f(x)),
  249. offset, block_size)))
  250. if not average then
  251. average = out
  252. else
  253. average:add(out)
  254. end
  255. end
  256. end
  257. return average:div(8.0)
  258. end
  259. function reconstruct.image_tta(model, x, block_size)
  260. if reconstruct.is_rgb(model) then
  261. return tta(reconstruct.image_rgb, model, x, block_size)
  262. else
  263. return tta(reconstruct.image_y, model, x, block_size)
  264. end
  265. end
  266. function reconstruct.scale_tta(model, scale, x, block_size)
  267. if reconstruct.is_rgb(model) then
  268. local f = function (model, x, offset, block_size)
  269. return reconstruct.scale_rgb(model, scale, x, offset, block_size)
  270. end
  271. return tta(f, model, x, block_size)
  272. else
  273. local f = function (model, x, offset, block_size)
  274. return reconstruct.scale_y(model, scale, x, offset, block_size)
  275. end
  276. return tta(f, model, x, block_size)
  277. end
  278. end
  279. return reconstruct