reconstruct.lua 11 KB

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