benchmark.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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", "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. if model2 then
  78. model2_output = reconstruct.image(model2, input)
  79. end
  80. else
  81. model1_output = reconstruct.scale(model1, 2.0, input)
  82. if model2 then
  83. model2_output = reconstruct.scale(model2, 2.0, input)
  84. end
  85. end
  86. if opt.color == "y" then
  87. model1_mse = model1_mse + YMSE(ground_truth, model1_output)
  88. model1_psnr = model1_psnr + YPSNR(ground_truth, model1_output)
  89. if model2 then
  90. model2_mse = model2_mse + YMSE(ground_truth, model2_output)
  91. model2_psnr = model2_psnr + YPSNR(ground_truth, model2_output)
  92. end
  93. elseif opt.color == "rgb" then
  94. model1_mse = model1_mse + MSE(ground_truth, model1_output)
  95. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output)
  96. if model2 then
  97. model2_mse = model2_mse + MSE(ground_truth, model2_output)
  98. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output)
  99. end
  100. else
  101. error("Unknown color: " .. opt.color)
  102. end
  103. if model2 then
  104. io.stdout:write(
  105. string.format("%d/%d; model1_mse=%f, model2_mse=%f, model1_psnr=%f, model2_psnr=%f \r",
  106. i, #x,
  107. model1_mse / i, model2_mse / i,
  108. model1_psnr / i, model2_psnr / i
  109. ))
  110. else
  111. io.stdout:write(
  112. string.format("%d/%d; model1_mse=%f, model1_psnr=%f \r",
  113. i, #x,
  114. model1_mse / i, model1_psnr / i
  115. ))
  116. end
  117. io.stdout:flush()
  118. end
  119. io.stdout:write("\n")
  120. end
  121. local function load_data(test_dir)
  122. local test_x = {}
  123. local files = dir.getfiles(test_dir, "*.*")
  124. for i = 1, #files do
  125. table.insert(test_x, iproc.crop_mod4(image_loader.load_byte(files[i])))
  126. xlua.progress(i, #files)
  127. end
  128. return test_x
  129. end
  130. function load_model(filename)
  131. return torch.load(filename, "ascii")
  132. end
  133. print(opt)
  134. if opt.method == "scale" then
  135. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  136. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  137. local s1, model1 = pcall(load_model, f1)
  138. local s2, model2 = pcall(load_model, f2)
  139. if not s1 then
  140. error("Load error: " .. f1)
  141. end
  142. if not s2 then
  143. model2 = nil
  144. end
  145. local test_x = load_data(opt.dir)
  146. benchmark(opt, test_x, transform_scale, model1, model2)
  147. elseif opt.method == "noise" then
  148. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  149. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  150. local s1, model1 = pcall(load_model, f1)
  151. local s2, model2 = pcall(load_model, f2)
  152. if not s1 then
  153. error("Load error: " .. f1)
  154. end
  155. if not s2 then
  156. model2 = nil
  157. end
  158. local test_x = load_data(opt.dir)
  159. benchmark(opt, test_x, transform_jpeg, model1, model2)
  160. end