reconstruct.lua 10 KB

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