benchmark.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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|Lanczos)")
  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 baseline_scale(x, filter)
  58. return iproc.scale(x,
  59. x:size(3) * 2.0,
  60. x:size(2) * 2.0,
  61. filter)
  62. end
  63. local function transform_scale(x, opt)
  64. return iproc.scale(x,
  65. x:size(3) * 0.5,
  66. x:size(2) * 0.5,
  67. opt.filter)
  68. end
  69. local function benchmark(opt, x, input_func, model1, model2)
  70. local model1_mse = 0
  71. local model2_mse = 0
  72. local baseline_mse = 0
  73. local model1_psnr = 0
  74. local model2_psnr = 0
  75. local baseline_psnr = 0
  76. for i = 1, #x do
  77. local ground_truth = x[i]
  78. local input, model1_output, model2_output, baseline_output
  79. input = input_func(ground_truth, opt)
  80. t = sys.clock()
  81. if input:size(3) == ground_truth:size(3) then
  82. model1_output = reconstruct.image(model1, input)
  83. if model2 then
  84. model2_output = reconstruct.image(model2, input)
  85. end
  86. else
  87. model1_output = reconstruct.scale(model1, 2.0, input)
  88. if model2 then
  89. model2_output = reconstruct.scale(model2, 2.0, input)
  90. end
  91. baseline_output = baseline_scale(input, opt.filter)
  92. end
  93. if opt.color == "y" then
  94. model1_mse = model1_mse + YMSE(ground_truth, model1_output)
  95. model1_psnr = model1_psnr + YPSNR(ground_truth, model1_output)
  96. if model2 then
  97. model2_mse = model2_mse + YMSE(ground_truth, model2_output)
  98. model2_psnr = model2_psnr + YPSNR(ground_truth, model2_output)
  99. end
  100. if baseline_output then
  101. baseline_mse = baseline_mse + YMSE(ground_truth, baseline_output)
  102. baseline_psnr = baseline_psnr + YPSNR(ground_truth, baseline_output)
  103. end
  104. elseif opt.color == "rgb" then
  105. model1_mse = model1_mse + MSE(ground_truth, model1_output)
  106. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output)
  107. if model2 then
  108. model2_mse = model2_mse + MSE(ground_truth, model2_output)
  109. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output)
  110. end
  111. if baseline_output then
  112. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output)
  113. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output)
  114. end
  115. else
  116. error("Unknown color: " .. opt.color)
  117. end
  118. if model2 then
  119. if baseline_output then
  120. io.stdout:write(
  121. string.format("%d/%d; baseline_mse=%f, model1_mse=%f, model2_mse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  122. i, #x,
  123. baseline_mse / i,
  124. model1_mse / i, model2_mse / i,
  125. baseline_psnr / i,
  126. model1_psnr / i, model2_psnr / i
  127. ))
  128. else
  129. io.stdout:write(
  130. string.format("%d/%d; model1_mse=%f, model2_mse=%f, model1_psnr=%f, model2_psnr=%f \r",
  131. i, #x,
  132. model1_mse / i, model2_mse / i,
  133. model1_psnr / i, model2_psnr / i
  134. ))
  135. end
  136. else
  137. if baseline_output then
  138. io.stdout:write(
  139. string.format("%d/%d; baseline_mse=%f, model1_mse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  140. i, #x,
  141. baseline_mse / i, model1_mse / i,
  142. baseline_psnr / i, model1_psnr / i
  143. ))
  144. else
  145. io.stdout:write(
  146. string.format("%d/%d; model1_mse=%f, model1_psnr=%f \r",
  147. i, #x,
  148. model1_mse / i, model1_psnr / i
  149. ))
  150. end
  151. end
  152. io.stdout:flush()
  153. end
  154. io.stdout:write("\n")
  155. end
  156. local function load_data(test_dir)
  157. local test_x = {}
  158. local files = dir.getfiles(test_dir, "*.*")
  159. for i = 1, #files do
  160. table.insert(test_x, iproc.crop_mod4(image_loader.load_float(files[i])))
  161. xlua.progress(i, #files)
  162. end
  163. return test_x
  164. end
  165. function load_model(filename)
  166. return torch.load(filename, "ascii")
  167. end
  168. print(opt)
  169. if opt.method == "scale" then
  170. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  171. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  172. local s1, model1 = pcall(load_model, f1)
  173. local s2, model2 = pcall(load_model, f2)
  174. if not s1 then
  175. error("Load error: " .. f1)
  176. end
  177. if not s2 then
  178. model2 = nil
  179. end
  180. local test_x = load_data(opt.dir)
  181. benchmark(opt, test_x, transform_scale, model1, model2)
  182. elseif opt.method == "noise" then
  183. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  184. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  185. local s1, model1 = pcall(load_model, f1)
  186. local s2, model2 = pcall(load_model, f2)
  187. if not s1 then
  188. error("Load error: " .. f1)
  189. end
  190. if not s2 then
  191. model2 = nil
  192. end
  193. local test_x = load_data(opt.dir)
  194. benchmark(opt, test_x, transform_jpeg, model1, model2)
  195. end