reconstruct.lua 10 KB

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