reconstruct.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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)
  5. if x:dim() == 2 then
  6. x = x:reshape(1, x:size(1), x:size(2))
  7. end
  8. local ch = x:size(1)
  9. local new_x = torch.Tensor(x:size(1), x:size(2) * inner_scale, x:size(3) * inner_scale):zero()
  10. local input_block_size = block_size / inner_scale
  11. local output_block_size = block_size
  12. local output_size = output_block_size - offset * 2
  13. local output_size_in_input = input_block_size - math.ceil(offset / inner_scale) * 2
  14. local input = torch.CudaTensor(1, ch, input_block_size, input_block_size)
  15. for i = 1, x:size(2), output_size_in_input do
  16. for j = 1, x:size(3), output_size_in_input do
  17. if i + input_block_size - 1 <= x:size(2) and j + input_block_size - 1 <= x:size(3) then
  18. local index = {{},
  19. {i, i + input_block_size - 1},
  20. {j, j + input_block_size - 1}}
  21. input:copy(x[index])
  22. local output = model:forward(input)
  23. output = output:view(ch, output_size, output_size)
  24. local ii = (i - 1) * inner_scale + 1
  25. local jj = (j - 1) * inner_scale + 1
  26. local output_index = {{}, { ii , ii + output_size - 1 },
  27. { jj, jj + output_size - 1}}
  28. new_x[output_index]:copy(output)
  29. end
  30. end
  31. end
  32. return new_x
  33. end
  34. local reconstruct = {}
  35. function reconstruct.is_rgb(model)
  36. if srcnn.channels(model) == 3 then
  37. -- 3ch RGB
  38. return true
  39. else
  40. -- 1ch Y
  41. return false
  42. end
  43. end
  44. function reconstruct.offset_size(model)
  45. return srcnn.offset_size(model)
  46. end
  47. function reconstruct.has_resize(model)
  48. return srcnn.scale_factor(model) > 1
  49. end
  50. function reconstruct.inner_scale(model)
  51. return srcnn.scale_factor(model)
  52. end
  53. local function padding_params(x, model, block_size)
  54. local p = {}
  55. local offset = reconstruct.offset_size(model)
  56. p.x_w = x:size(3)
  57. p.x_h = x:size(2)
  58. p.inner_scale = reconstruct.inner_scale(model)
  59. local input_offset = math.ceil(offset / p.inner_scale)
  60. local input_block_size = block_size / p.inner_scale
  61. local process_size = input_block_size - input_offset * 2
  62. local h_blocks = math.floor(p.x_h / process_size) +
  63. ((p.x_h % process_size == 0 and 0) or 1)
  64. local w_blocks = math.floor(p.x_w / process_size) +
  65. ((p.x_w % process_size == 0 and 0) or 1)
  66. local h = (h_blocks * process_size) + input_offset * 2
  67. local w = (w_blocks * process_size) + input_offset * 2
  68. p.pad_h1 = input_offset
  69. p.pad_w1 = input_offset
  70. p.pad_h2 = (h - input_offset) - p.x_h
  71. p.pad_w2 = (w - input_offset) - p.x_w
  72. return p
  73. end
  74. function reconstruct.image_y(model, x, offset, block_size)
  75. block_size = block_size or 128
  76. local p = padding_params(x, model, block_size)
  77. x = image.rgb2yuv(iproc.padding(x, p.pad_w1, p.pad_w2, p.pad_h1, p.pad_h2))
  78. local y = reconstruct_nn(model, x[1], p.inner_scale, offset, block_size)
  79. x = iproc.crop(x, p.pad_w1, p.pad_w2, p.pad_w1 + p.x_w, p.pad_w2 + p.x_h)
  80. y = iproc.crop(y, 0, 0, p.x_w, p.x_h)
  81. y[torch.lt(y, 0)] = 0
  82. y[torch.gt(y, 1)] = 1
  83. x[1]:copy(y)
  84. local output = image.yuv2rgb(x)
  85. output[torch.lt(output, 0)] = 0
  86. output[torch.gt(output, 1)] = 1
  87. x = nil
  88. y = nil
  89. collectgarbage()
  90. return output
  91. end
  92. function reconstruct.scale_y(model, scale, x, offset, block_size, upsampling_filter)
  93. upsampling_filter = upsampling_filter or "Box"
  94. block_size = block_size or 128
  95. local x_lanczos
  96. if reconstruct.has_resize(model) then
  97. x_lanczos = x:clone()
  98. else
  99. x_lanczos = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Lanczos")
  100. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, upsampling_filter)
  101. end
  102. local p = padding_params(x, model, block_size)
  103. if p.x_w * p.x_h > 2048*2048 then
  104. collectgarbage()
  105. end
  106. x = image.rgb2yuv(iproc.padding(x, p.pad_w1, p.pad_w2, p.pad_h1, p.pad_h2))
  107. x_lanczos = image.rgb2yuv(x_lanczos)
  108. local y = reconstruct_nn(model, x[1], p.inner_scale, offset, block_size)
  109. y = iproc.crop(y, 0, 0, p.x_w * p.inner_scale, p.x_h * p.inner_scale)
  110. y[torch.lt(y, 0)] = 0
  111. y[torch.gt(y, 1)] = 1
  112. x_lanczos[1]:copy(y)
  113. local output = image.yuv2rgb(x_lanczos)
  114. output[torch.lt(output, 0)] = 0
  115. output[torch.gt(output, 1)] = 1
  116. x = nil
  117. x_lanczos = nil
  118. y = nil
  119. collectgarbage()
  120. return output
  121. end
  122. function reconstruct.image_rgb(model, x, offset, block_size)
  123. block_size = block_size or 128
  124. local p = padding_params(x, model, block_size)
  125. x = iproc.padding(x, p.pad_w1, p.pad_w2, p.pad_h1, p.pad_h2)
  126. if p.x_w * p.x_h > 2048*2048 then
  127. collectgarbage()
  128. end
  129. local y = reconstruct_nn(model, x, p.inner_scale, offset, block_size)
  130. local output = iproc.crop(y, 0, 0, p.x_w, p.x_h)
  131. output[torch.lt(output, 0)] = 0
  132. output[torch.gt(output, 1)] = 1
  133. x = nil
  134. y = nil
  135. collectgarbage()
  136. return output
  137. end
  138. function reconstruct.scale_rgb(model, scale, x, offset, block_size, upsampling_filter)
  139. upsampling_filter = upsampling_filter or "Box"
  140. block_size = block_size or 128
  141. if not reconstruct.has_resize(model) then
  142. x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, upsampling_filter)
  143. end
  144. local p = padding_params(x, model, block_size)
  145. x = iproc.padding(x, p.pad_w1, p.pad_w2, p.pad_h1, p.pad_h2)
  146. if p.x_w * p.x_h > 2048*2048 then
  147. collectgarbage()
  148. end
  149. local y
  150. y = reconstruct_nn(model, x, p.inner_scale, offset, block_size)
  151. local output = iproc.crop(y, 0, 0, p.x_w * p.inner_scale, p.x_h * p.inner_scale)
  152. output[torch.lt(output, 0)] = 0
  153. output[torch.gt(output, 1)] = 1
  154. x = nil
  155. y = nil
  156. collectgarbage()
  157. return output
  158. end
  159. function reconstruct.image(model, x, block_size)
  160. local i2rgb = false
  161. if x:size(1) == 1 then
  162. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  163. new_x[1]:copy(x)
  164. new_x[2]:copy(x)
  165. new_x[3]:copy(x)
  166. x = new_x
  167. i2rgb = true
  168. end
  169. if reconstruct.is_rgb(model) then
  170. x = reconstruct.image_rgb(model, x,
  171. reconstruct.offset_size(model), block_size)
  172. else
  173. x = reconstruct.image_y(model, x,
  174. reconstruct.offset_size(model), block_size)
  175. end
  176. if i2rgb then
  177. x = image.rgb2y(x)
  178. end
  179. return x
  180. end
  181. function reconstruct.scale(model, scale, x, block_size, upsampling_filter)
  182. local i2rgb = false
  183. if x:size(1) == 1 then
  184. local new_x = torch.Tensor(3, x:size(2), x:size(3))
  185. new_x[1]:copy(x)
  186. new_x[2]:copy(x)
  187. new_x[3]:copy(x)
  188. x = new_x
  189. i2rgb = true
  190. end
  191. if reconstruct.is_rgb(model) then
  192. x = reconstruct.scale_rgb(model, scale, x,
  193. reconstruct.offset_size(model),
  194. block_size,
  195. upsampling_filter)
  196. else
  197. x = reconstruct.scale_y(model, scale, x,
  198. reconstruct.offset_size(model),
  199. block_size,
  200. upsampling_filter)
  201. end
  202. if i2rgb then
  203. x = image.rgb2y(x)
  204. end
  205. return x
  206. end
  207. local function tta(f, model, x, block_size)
  208. local average = nil
  209. local offset = reconstruct.offset_size(model)
  210. for i = 1, 4 do
  211. local flip_f, iflip_f
  212. if i == 1 then
  213. flip_f = function (a) return a end
  214. iflip_f = function (a) return a end
  215. elseif i == 2 then
  216. flip_f = image.vflip
  217. iflip_f = image.vflip
  218. elseif i == 3 then
  219. flip_f = image.hflip
  220. iflip_f = image.hflip
  221. elseif i == 4 then
  222. flip_f = function (a) return image.hflip(image.vflip(a)) end
  223. iflip_f = function (a) return image.vflip(image.hflip(a)) end
  224. end
  225. for j = 1, 2 do
  226. local tr_f, itr_f
  227. if j == 1 then
  228. tr_f = function (a) return a end
  229. itr_f = function (a) return a end
  230. elseif j == 2 then
  231. tr_f = function(a) return a:transpose(2, 3):contiguous() end
  232. itr_f = function(a) return a:transpose(2, 3):contiguous() end
  233. end
  234. local out = itr_f(iflip_f(f(model, flip_f(tr_f(x)),
  235. offset, block_size)))
  236. if not average then
  237. average = out
  238. else
  239. average:add(out)
  240. end
  241. end
  242. end
  243. return average:div(8.0)
  244. end
  245. function reconstruct.image_tta(model, x, block_size)
  246. if reconstruct.is_rgb(model) then
  247. return tta(reconstruct.image_rgb, model, x, block_size)
  248. else
  249. return tta(reconstruct.image_y, model, x, block_size)
  250. end
  251. end
  252. function reconstruct.scale_tta(model, scale, x, block_size, upsampling_filter)
  253. if reconstruct.is_rgb(model) then
  254. local f = function (model, x, offset, block_size)
  255. return reconstruct.scale_rgb(model, scale, x, offset, block_size, upsampling_filter)
  256. end
  257. return tta(f, model, x, block_size)
  258. else
  259. local f = function (model, x, offset, block_size)
  260. return reconstruct.scale_y(model, scale, x, offset, block_size, upsampling_filter)
  261. end
  262. return tta(f, model, x, block_size)
  263. end
  264. end
  265. return reconstruct