reconstruct.lua 10 KB

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