benchmark.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. local opt = cmd:parse(arg)
  26. torch.setdefaulttensortype('torch.FloatTensor')
  27. if cudnn then
  28. cudnn.fastest = true
  29. cudnn.benchmark = false
  30. end
  31. local function rgb2y_matlab(x)
  32. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  33. x = iproc.byte2float(x)
  34. y:add(x[1] * 65.481)
  35. y:add(x[2] * 128.553)
  36. y:add(x[3] * 24.966)
  37. y:add(16.0)
  38. return y:byte():float()
  39. end
  40. local function RGBMSE(x1, x2)
  41. x1 = iproc.float2byte(x1):float()
  42. x2 = iproc.float2byte(x2):float()
  43. return (x1 - x2):pow(2):mean()
  44. end
  45. local function YMSE(x1, x2)
  46. if opt.range_bug == 1 then
  47. local x1_2 = rgb2y_matlab(x1)
  48. local x2_2 = rgb2y_matlab(x2)
  49. return (x1_2 - x2_2):pow(2):mean()
  50. else
  51. local x1_2 = image.rgb2y(x1):mul(255.0)
  52. local x2_2 = image.rgb2y(x2):mul(255.0)
  53. return (x1_2 - x2_2):pow(2):mean()
  54. end
  55. end
  56. local function MSE(x1, x2, color)
  57. if color == "y" then
  58. return YMSE(x1, x2)
  59. else
  60. return RGBMSE(x1, x2)
  61. end
  62. end
  63. local function PSNR(x1, x2, color)
  64. local mse = MSE(x1, x2, color)
  65. return 10 * math.log10((255.0 * 255.0) / mse)
  66. end
  67. local function transform_jpeg(x, opt)
  68. for i = 1, opt.jpeg_times do
  69. jpeg = gm.Image(x, "RGB", "DHW")
  70. jpeg:format("jpeg")
  71. jpeg:samplingFactors({1.0, 1.0, 1.0})
  72. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  73. jpeg:fromBlob(blob, len)
  74. x = jpeg:toTensor("byte", "RGB", "DHW")
  75. end
  76. return iproc.byte2float(x)
  77. end
  78. local function baseline_scale(x, filter)
  79. return iproc.scale(x,
  80. x:size(3) * 2.0,
  81. x:size(2) * 2.0,
  82. filter)
  83. end
  84. local function transform_scale(x, opt)
  85. return iproc.scale(x,
  86. x:size(3) * 0.5,
  87. x:size(2) * 0.5,
  88. opt.filter)
  89. end
  90. local function benchmark(opt, x, input_func, model1, model2)
  91. local model1_mse = 0
  92. local model2_mse = 0
  93. local baseline_mse = 0
  94. local model1_psnr = 0
  95. local model2_psnr = 0
  96. local baseline_psnr = 0
  97. for i = 1, #x do
  98. local ground_truth = x[i]
  99. local input, model1_output, model2_output, baseline_output
  100. input = input_func(ground_truth, opt)
  101. t = sys.clock()
  102. if input:size(3) == ground_truth:size(3) then
  103. model1_output = reconstruct.image(model1, input)
  104. if model2 then
  105. model2_output = reconstruct.image(model2, input)
  106. end
  107. else
  108. model1_output = reconstruct.scale(model1, 2.0, input)
  109. if model2 then
  110. model2_output = reconstruct.scale(model2, 2.0, input)
  111. end
  112. baseline_output = baseline_scale(input, opt.filter)
  113. end
  114. model1_mse = model1_mse + MSE(ground_truth, model1_output, opt.color)
  115. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output, opt.color)
  116. if model2 then
  117. model2_mse = model2_mse + MSE(ground_truth, model2_output, opt.color)
  118. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output, opt.color)
  119. end
  120. if baseline_output then
  121. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output, opt.color)
  122. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output, opt.color)
  123. end
  124. if model2 then
  125. if baseline_output then
  126. io.stdout:write(
  127. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, model2_rmse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  128. i, #x,
  129. math.sqrt(baseline_mse / i),
  130. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  131. baseline_psnr / i,
  132. model1_psnr / i, model2_psnr / i
  133. ))
  134. else
  135. io.stdout:write(
  136. string.format("%d/%d; model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  137. i, #x,
  138. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  139. model1_psnr / i, model2_psnr / i
  140. ))
  141. end
  142. else
  143. if baseline_output then
  144. io.stdout:write(
  145. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  146. i, #x,
  147. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  148. baseline_psnr / i, model1_psnr / i
  149. ))
  150. else
  151. io.stdout:write(
  152. string.format("%d/%d; model1_rmse=%f, model1_psnr=%f \r",
  153. i, #x,
  154. math.sqrt(model1_mse / i), model1_psnr / i
  155. ))
  156. end
  157. end
  158. io.stdout:flush()
  159. end
  160. io.stdout:write("\n")
  161. end
  162. local function load_data(test_dir)
  163. local test_x = {}
  164. local files = dir.getfiles(test_dir, "*.*")
  165. for i = 1, #files do
  166. table.insert(test_x, iproc.crop_mod4(image_loader.load_float(files[i])))
  167. xlua.progress(i, #files)
  168. end
  169. return test_x
  170. end
  171. function load_model(filename)
  172. return torch.load(filename, "ascii")
  173. end
  174. print(opt)
  175. if opt.method == "scale" then
  176. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  177. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  178. local s1, model1 = pcall(load_model, f1)
  179. local s2, model2 = pcall(load_model, f2)
  180. if not s1 then
  181. error("Load error: " .. f1)
  182. end
  183. if not s2 then
  184. model2 = nil
  185. end
  186. local test_x = load_data(opt.dir)
  187. benchmark(opt, test_x, transform_scale, model1, model2)
  188. elseif opt.method == "noise" then
  189. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  190. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  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_jpeg, model1, model2)
  201. end