benchmark.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  2. package.path = path.join(path.dirname(__FILE__), "..", "lib", "?.lua;") .. package.path
  3. require 'xlua'
  4. require 'pl'
  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", 'model1 directory')
  16. cmd:option("-model2_dir", "./models/anime_style_art_rgb", 'model2 directory')
  17. cmd:option("-method", "scale", '(scale|noise)')
  18. cmd:option("-filter", "Box", "downscaling filter (Box|Jinc)")
  19. cmd:option("-color", "rgb", '(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. local opt = cmd:parse(arg)
  25. torch.setdefaulttensortype('torch.FloatTensor')
  26. if cudnn then
  27. cudnn.fastest = true
  28. cudnn.benchmark = false
  29. end
  30. local function MSE(x1, x2)
  31. return (x1 - x2):pow(2):mean()
  32. end
  33. local function YMSE(x1, x2)
  34. local x1_2 = image.rgb2y(x1)
  35. local x2_2 = image.rgb2y(x2)
  36. return (x1_2 - x2_2):pow(2):mean()
  37. end
  38. local function PSNR(x1, x2)
  39. local mse = MSE(x1, x2)
  40. return 10 * math.log10(1.0 / mse)
  41. end
  42. local function YPSNR(x1, x2)
  43. local mse = YMSE(x1, x2)
  44. return 10 * math.log10(1.0 / mse)
  45. end
  46. local function transform_jpeg(x, opt)
  47. for i = 1, opt.jpeg_times do
  48. jpeg = gm.Image(x, "RGB", "DHW")
  49. jpeg:format("jpeg")
  50. jpeg:samplingFactors({1.0, 1.0, 1.0})
  51. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  52. jpeg:fromBlob(blob, len)
  53. x = jpeg:toTensor("byte", "RGB", "DHW")
  54. end
  55. return x
  56. end
  57. local function transform_scale(x, opt)
  58. return iproc.scale(x,
  59. x:size(3) * 0.5,
  60. x:size(2) * 0.5,
  61. opt.filter)
  62. end
  63. local function benchmark(opt, x, input_func, model1, model2)
  64. local model1_mse = 0
  65. local model2_mse = 0
  66. local model1_psnr = 0
  67. local model2_psnr = 0
  68. for i = 1, #x do
  69. local ground_truth = x[i]
  70. local input, model1_output, model2_output
  71. input = input_func(ground_truth, opt)
  72. input = input:float():div(255)
  73. ground_truth = ground_truth:float():div(255)
  74. t = sys.clock()
  75. if input:size(3) == ground_truth:size(3) then
  76. model1_output = reconstruct.image(model1, input)
  77. model2_output = reconstruct.image(model2, input)
  78. else
  79. model1_output = reconstruct.scale(model1, 2.0, input)
  80. model2_output = reconstruct.scale(model2, 2.0, input)
  81. end
  82. if opt.color == "y" then
  83. model1_mse = model1_mse + YMSE(ground_truth, model1_output)
  84. model1_psnr = model1_psnr + YPSNR(ground_truth, model1_output)
  85. model2_mse = model2_mse + YMSE(ground_truth, model2_output)
  86. model2_psnr = model2_psnr + YPSNR(ground_truth, model2_output)
  87. elseif opt.color == "rgb" then
  88. model1_mse = model1_mse + MSE(ground_truth, model1_output)
  89. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output)
  90. model2_mse = model2_mse + MSE(ground_truth, model2_output)
  91. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output)
  92. else
  93. error("Unknown color: " .. opt.color)
  94. end
  95. io.stdout:write(
  96. string.format("%d/%d; model1_mse=%f, model2_mse=%f, model1_psnr=%f, model2_psnr=%f \r",
  97. i, #x,
  98. model1_mse / i, model2_mse / i,
  99. model1_psnr / i, model2_psnr / i
  100. )
  101. )
  102. io.stdout:flush()
  103. end
  104. io.stdout:write("\n")
  105. end
  106. local function load_data(test_dir)
  107. local test_x = {}
  108. local files = dir.getfiles(test_dir, "*.*")
  109. for i = 1, #files do
  110. table.insert(test_x, iproc.crop_mod4(image_loader.load_byte(files[i])))
  111. xlua.progress(i, #files)
  112. end
  113. return test_x
  114. end
  115. print(opt)
  116. if opt.method == "scale" then
  117. local model1 = torch.load(path.join(opt.model1_dir, "scale2.0x_model.t7"), "ascii")
  118. local model2 = torch.load(path.join(opt.model2_dir, "scale2.0x_model.t7"), "ascii")
  119. local test_x = load_data(opt.dir)
  120. benchmark(opt, test_x, transform_scale, model1, model2)
  121. elseif opt.method == "noise" then
  122. local model1 = torch.load(path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level)), "ascii")
  123. local model2 = torch.load(path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level)), "ascii")
  124. local test_x = load_data(opt.dir)
  125. benchmark(opt, test_x, transform_jpeg, model1, model2)
  126. end