reconstruct.lua 10 KB

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