benchmark.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. require 'pl'
  2. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  3. package.path = path.join(path.dirname(__FILE__), "..", "lib", "?.lua;") .. package.path
  4. require 'xlua'
  5. require 'w2nn'
  6. local iproc = require 'iproc'
  7. local reconstruct = require 'reconstruct'
  8. local image_loader = require 'image_loader'
  9. local gm = require 'graphicsmagick'
  10. local cmd = torch.CmdLine()
  11. cmd:text()
  12. cmd:text("waifu2x-benchmark")
  13. cmd:text("Options:")
  14. cmd:option("-dir", "./data/test", 'test image directory')
  15. cmd:option("-model1_dir", "./models/anime_style_art_rgb", 'model1 directory')
  16. cmd:option("-model2_dir", "", 'model2 directory (optional)')
  17. cmd:option("-method", "scale", '(scale|noise)')
  18. cmd:option("-filter", "Catrom", "downscaling filter (Box|Lanczos|Catrom(Bicubic))")
  19. cmd:option("-color", "y", '(rgb|y)')
  20. cmd:option("-noise_level", 1, 'model noise level')
  21. cmd:option("-jpeg_quality", 75, 'jpeg quality')
  22. cmd:option("-jpeg_times", 1, 'jpeg compression times')
  23. cmd:option("-jpeg_quality_down", 5, 'value of jpeg quality to decrease each times')
  24. cmd:option("-range_bug", 0, 'Reproducing the dynamic range bug that is caused by MATLAB\'s rgb2ycbcr(1|0)')
  25. cmd:option("-gamma_correction", 0, 'Resizing with colorspace correction(sRGB:gamma 2.2) (0|1)')
  26. local opt = cmd:parse(arg)
  27. torch.setdefaulttensortype('torch.FloatTensor')
  28. if cudnn then
  29. cudnn.fastest = true
  30. cudnn.benchmark = false
  31. end
  32. if opt.gamma_correction == 1 then
  33. opt.gamma_correction = true
  34. else
  35. opt.gamma_correction = false
  36. end
  37. local function rgb2y_matlab(x)
  38. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  39. x = iproc.byte2float(x)
  40. y:add(x[1] * 65.481)
  41. y:add(x[2] * 128.553)
  42. y:add(x[3] * 24.966)
  43. y:add(16.0)
  44. return y:byte():float()
  45. end
  46. local function RGBMSE(x1, x2)
  47. x1 = iproc.float2byte(x1):float()
  48. x2 = iproc.float2byte(x2):float()
  49. return (x1 - x2):pow(2):mean()
  50. end
  51. local function YMSE(x1, x2)
  52. if opt.range_bug == 1 then
  53. local x1_2 = rgb2y_matlab(x1)
  54. local x2_2 = rgb2y_matlab(x2)
  55. return (x1_2 - x2_2):pow(2):mean()
  56. else
  57. local x1_2 = image.rgb2y(x1):mul(255.0)
  58. local x2_2 = image.rgb2y(x2):mul(255.0)
  59. return (x1_2 - x2_2):pow(2):mean()
  60. end
  61. end
  62. local function MSE(x1, x2, color)
  63. if color == "y" then
  64. return YMSE(x1, x2)
  65. else
  66. return RGBMSE(x1, x2)
  67. end
  68. end
  69. local function PSNR(x1, x2, color)
  70. local mse = math.max(MSE(x1, x2, color), 1)
  71. return 10 * math.log10((255.0 * 255.0) / mse)
  72. end
  73. local function transform_jpeg(x, opt)
  74. for i = 1, opt.jpeg_times do
  75. jpeg = gm.Image(x, "RGB", "DHW")
  76. jpeg:format("jpeg")
  77. jpeg:samplingFactors({1.0, 1.0, 1.0})
  78. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  79. jpeg:fromBlob(blob, len)
  80. x = jpeg:toTensor("byte", "RGB", "DHW")
  81. end
  82. return iproc.byte2float(x)
  83. end
  84. local function baseline_scale(x, filter)
  85. return iproc.scale(x,
  86. x:size(3) * 2.0,
  87. x:size(2) * 2.0,
  88. filter)
  89. end
  90. local function transform_scale(x, opt)
  91. if opt.gamma_correction then
  92. return iproc.scale_with_gamma22(x,
  93. x:size(3) * 0.5,
  94. x:size(2) * 0.5,
  95. opt.filter)
  96. else
  97. return iproc.scale(x,
  98. x:size(3) * 0.5,
  99. x:size(2) * 0.5,
  100. opt.filter)
  101. end
  102. end
  103. local function benchmark(opt, x, input_func, model1, model2)
  104. local model1_mse = 0
  105. local model2_mse = 0
  106. local baseline_mse = 0
  107. local model1_psnr = 0
  108. local model2_psnr = 0
  109. local baseline_psnr = 0
  110. for i = 1, #x do
  111. local ground_truth = x[i]
  112. local input, model1_output, model2_output, baseline_output
  113. input = input_func(ground_truth, opt)
  114. t = sys.clock()
  115. if input:size(3) == ground_truth:size(3) then
  116. model1_output = reconstruct.image(model1, input)
  117. if model2 then
  118. model2_output = reconstruct.image(model2, input)
  119. end
  120. else
  121. model1_output = reconstruct.scale(model1, 2.0, input)
  122. if model2 then
  123. model2_output = reconstruct.scale(model2, 2.0, input)
  124. end
  125. baseline_output = baseline_scale(input, opt.filter)
  126. end
  127. model1_mse = model1_mse + MSE(ground_truth, model1_output, opt.color)
  128. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output, opt.color)
  129. if model2 then
  130. model2_mse = model2_mse + MSE(ground_truth, model2_output, opt.color)
  131. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output, opt.color)
  132. end
  133. if baseline_output then
  134. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output, opt.color)
  135. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output, opt.color)
  136. end
  137. if model2 then
  138. if baseline_output then
  139. io.stdout:write(
  140. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, model2_rmse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  141. i, #x,
  142. math.sqrt(baseline_mse / i),
  143. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  144. baseline_psnr / i,
  145. model1_psnr / i, model2_psnr / i
  146. ))
  147. else
  148. io.stdout:write(
  149. string.format("%d/%d; model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  150. i, #x,
  151. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  152. model1_psnr / i, model2_psnr / i
  153. ))
  154. end
  155. else
  156. if baseline_output then
  157. io.stdout:write(
  158. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  159. i, #x,
  160. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  161. baseline_psnr / i, model1_psnr / i
  162. ))
  163. else
  164. io.stdout:write(
  165. string.format("%d/%d; model1_rmse=%f, model1_psnr=%f \r",
  166. i, #x,
  167. math.sqrt(model1_mse / i), model1_psnr / i
  168. ))
  169. end
  170. end
  171. io.stdout:flush()
  172. end
  173. io.stdout:write("\n")
  174. end
  175. local function load_data(test_dir)
  176. local test_x = {}
  177. local files = dir.getfiles(test_dir, "*.*")
  178. for i = 1, #files do
  179. table.insert(test_x, iproc.crop_mod4(image_loader.load_float(files[i])))
  180. xlua.progress(i, #files)
  181. end
  182. return test_x
  183. end
  184. function load_model(filename)
  185. return torch.load(filename, "ascii")
  186. end
  187. print(opt)
  188. if opt.method == "scale" then
  189. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  190. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  191. local s1, model1 = pcall(load_model, f1)
  192. local s2, model2 = pcall(load_model, f2)
  193. if not s1 then
  194. error("Load error: " .. f1)
  195. end
  196. if not s2 then
  197. model2 = nil
  198. end
  199. local test_x = load_data(opt.dir)
  200. benchmark(opt, test_x, transform_scale, model1, model2)
  201. elseif opt.method == "noise" then
  202. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  203. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  204. local s1, model1 = pcall(load_model, f1)
  205. local s2, model2 = pcall(load_model, f2)
  206. if not s1 then
  207. error("Load error: " .. f1)
  208. end
  209. if not s2 then
  210. model2 = nil
  211. end
  212. local test_x = load_data(opt.dir)
  213. benchmark(opt, test_x, transform_jpeg, model1, model2)
  214. end