reconstruct.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. local i2rgb = false
  174. if x:size(1) == 1 then
  175. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  176. new_x[1]:copy(x)
  177. new_x[2]:copy(x)
  178. new_x[3]:copy(x)
  179. x = new_x
  180. i2rgb = true
  181. end
  182. if reconstruct.is_rgb(model) then
  183. x = reconstruct.image_rgb(model, x,
  184. reconstruct.offset_size(model), block_size)
  185. else
  186. x = reconstruct.image_y(model, x,
  187. reconstruct.offset_size(model), block_size)
  188. end
  189. if i2rgb then
  190. x = image.rgb2y(x)
  191. end
  192. return x
  193. end
  194. function reconstruct.scale(model, scale, x, block_size)
  195. local i2rgb = false
  196. if x:size(1) == 1 then
  197. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  198. new_x[1]:copy(x)
  199. new_x[2]:copy(x)
  200. new_x[3]:copy(x)
  201. x = new_x
  202. i2rgb = true
  203. end
  204. if reconstruct.is_rgb(model) then
  205. x = reconstruct.scale_rgb(model, scale, x,
  206. reconstruct.offset_size(model),
  207. block_size)
  208. else
  209. x = reconstruct.scale_y(model, scale, x,
  210. reconstruct.offset_size(model),
  211. block_size)
  212. end
  213. if i2rgb then
  214. x = image.rgb2y(x)
  215. end
  216. return x
  217. end
  218. local function tr_f(a)
  219. return a:transpose(2, 3):contiguous()
  220. end
  221. local function itr_f(a)
  222. return a:transpose(2, 3):contiguous()
  223. end
  224. local augmented_patterns = {
  225. {
  226. forward = function (a) return a end,
  227. backward = function (a) return a end
  228. },
  229. {
  230. forward = function (a) return image.hflip(a) end,
  231. backward = function (a) return image.hflip(a) end
  232. },
  233. {
  234. forward = function (a) return image.vflip(a) end,
  235. backward = function (a) return image.vflip(a) end
  236. },
  237. {
  238. forward = function (a) return image.hflip(image.vflip(a)) end,
  239. backward = function (a) return image.vflip(image.hflip(a)) end
  240. },
  241. {
  242. forward = function (a) return tr_f(a) end,
  243. backward = function (a) return itr_f(a) end
  244. },
  245. {
  246. forward = function (a) return image.hflip(tr_f(a)) end,
  247. backward = function (a) return itr_f(image.hflip(a)) end
  248. },
  249. {
  250. forward = function (a) return image.vflip(tr_f(a)) end,
  251. backward = function (a) return itr_f(image.vflip(a)) end
  252. },
  253. {
  254. forward = function (a) return image.hflip(image.vflip(tr_f(a))) end,
  255. backward = function (a) return itr_f(image.vflip(image.hflip(a))) end
  256. }
  257. }
  258. local function get_augmented_patterns(n)
  259. if n == 1 then
  260. -- no tta
  261. return {augmented_patterns[1]}
  262. elseif n == 2 then
  263. return {augmented_patterns[1], augmented_patterns[5]}
  264. elseif n == 4 then
  265. return {augmented_patterns[1], augmented_patterns[5],
  266. augmented_patterns[2], augmented_patterns[7]}
  267. elseif n == 8 then
  268. return augmented_patterns
  269. else
  270. error("unsupported TTA level: " .. n)
  271. end
  272. end
  273. local function tta(f, n, model, x, block_size)
  274. local average = nil
  275. local offset = reconstruct.offset_size(model)
  276. local augments = get_augmented_patterns(n)
  277. for i = 1, #augments do
  278. local out = augments[i].backward(f(model, augments[i].forward(x), offset, block_size))
  279. if not average then
  280. average = out
  281. else
  282. average:add(out)
  283. end
  284. end
  285. return average:div(#augments)
  286. end
  287. function reconstruct.image_tta(model, n, x, block_size)
  288. if reconstruct.is_rgb(model) then
  289. return tta(reconstruct.image_rgb, n, model, x, block_size)
  290. else
  291. return tta(reconstruct.image_y, n, model, x, block_size)
  292. end
  293. end
  294. function reconstruct.scale_tta(model, n, scale, x, block_size)
  295. if reconstruct.is_rgb(model) then
  296. local f = function (model, x, offset, block_size)
  297. return reconstruct.scale_rgb(model, scale, x, offset, block_size)
  298. end
  299. return tta(f, n, model, x, block_size)
  300. else
  301. local f = function (model, x, offset, block_size)
  302. return reconstruct.scale_y(model, scale, x, offset, block_size)
  303. end
  304. return tta(f, n, model, x, block_size)
  305. end
  306. end
  307. return reconstruct